-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
300 lines (251 loc) · 8.52 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
%{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include "loc.h"
#include "ast.h"
#include "ast-print.h"
#include "ast-dump.h"
#include "ast-visit.h"
#include "ast-dumper.h"
#include "ast-symtabctor.h"
#include "ast-semanlzer.h"
#include "ast-codegen.h"
#include "hash.h"
#include "error.h"
#define YYLTYPE LocType
#define MAX_LINE_LENG 256
extern int line_no, col_no, opt_list;
extern char buffer[MAX_LINE_LENG];
extern FILE *yyin; /* declared by lex */
extern char *yytext; /* declared by lex */
extern int yyleng;
int pass_error = 0;
extern char *output = NULL;
static Node root = NULL;
extern
#ifdef __cplusplus
"C"
#endif
int yylex(void);
static void yyerror(const char *msg);
extern int yylex_destroy(void);
%}
%locations
%token PROGRAM VAR ARRAY OF INTEGER REAL STRING FUNCTION PROCEDURE PBEGIN END IF THEN ELSE WHILE DO NOT AND OR
%token LPAREN RPAREN SEMICOLON DOT COMMA COLON LBRACE RBRACE DOTDOT ASSIGNMENT ADDOP SUBOP MULOP DIVOP LTOP GTOP EQOP GETOP LETOP NEQOP
%token IDENTIFIER REALNUMBER INTEGERNUM SCIENTIFIC LITERALSTR
%union {
int val;
char* text;
real rval;
Node node;
Cons cons;
Type type;
BinOp binop;
ProgNode prog;
FunctionNode func;
DeclNode decl;
CompStmtNode comp;
BinExprNode bine;
UnaExprNode unae;
AssignNode assn;
VarRefNode vref;
ValNode valn;
InvocationNode invo;
IfTestNode iftn;
WhileNode whil;
}
%type <prog> prog
%type <func> subprogram_declaration subprogram_head
%type <comp> compound_statement
%type <valn> num
%type <vref> variable
%type <invo> procedure_statement
%type <node> statement expression
%type <node> boolexpression simple_expression term factor
%type <cons> identifier_list declarations subprogram_declarations
%type <cons> expression_list arguments tail parameter_list statement_list
%type <type> type standard_type
%type <binop> addop mulop relop
%type <text> IDENTIFIER
%%
prog : PROGRAM IDENTIFIER LPAREN identifier_list RPAREN SEMICOLON
declarations
subprogram_declarations
compound_statement
DOT { root = (Node)newProgNode(@1, $2, $4, $7, $8, (Node)$9); }
;
identifier_list : IDENTIFIER
{ $$ = newCons(newVarNode(@1, $1), NULL); }
| IDENTIFIER COMMA identifier_list
{ $$ = newCons(newVarNode(@1, $1), $3); }
;
declarations : VAR identifier_list COLON type SEMICOLON declarations
{ $$ = newCons(set_type((Node)newDeclNode(@1, $2), $4), $6); }
| { $$ = NULL; }
;
type : standard_type { $$ = $1; }
| ARRAY LBRACE num DOTDOT num RBRACE OF type
{ $$ = newType(ARRAY_TYPE, $3, $5, $8); }
;
standard_type : INTEGER { $$ = newType(INTEGER_TYPE, NULL, NULL, NULL); }
| REAL { $$ = newType(REAL_TYPE, NULL, NULL, NULL); }
| STRING { $$ = newType(STRING_TYPE, NULL, NULL, NULL); }
;
subprogram_declarations : subprogram_declaration SEMICOLON subprogram_declarations
{ $$ = newCons($1, $3); }
| { $$ = NULL; }
;
subprogram_declaration : subprogram_head
declarations
subprogram_declarations
compound_statement
{
$1->decl_vars = $2;
$1->decl_funcs = $3;
$1->stmt = (Node)$4;
$$ = $1;
}
;
subprogram_head : FUNCTION IDENTIFIER arguments COLON standard_type SEMICOLON
{ $$ = (FunctionNode)set_type(newFunctionNode(@1, $2, $3, NULL, NULL, NULL), $5); }
| PROCEDURE IDENTIFIER arguments SEMICOLON
{ $$ = newFunctionNode(@1, $2, $3, NULL, NULL, NULL); }
;
arguments : LPAREN parameter_list RPAREN { $$ = $2; }
| { $$ = NULL; }
;
parameter_list : optional_var identifier_list COLON type
{ $$ = newCons(set_type((Node)newDeclNode(@1, $2), $4), NULL); }
| optional_var identifier_list COLON type SEMICOLON parameter_list
{ $$ = newCons(set_type((Node)newDeclNode(@1, $2), $4), $6);}
;
optional_var : VAR
|
;
compound_statement : PBEGIN statement_list END { $$ = newCompStmtNode(@1, $2); }
statement_list : statement { $$ = newCons($1, NULL); }
| statement SEMICOLON statement_list { $$ = newCons($1, $3); }
;
statement : variable ASSIGNMENT expression
{ $$ = (Node)newAssignNode(@1, (Node)$1, $3); }
| procedure_statement
{ $$ = (Node)$1; }
| compound_statement
{ $$ = (Node)$1; }
| IF expression THEN statement ELSE statement
{ $$ = (Node)newIfTestNode(@1, $2, $4, $6); }
| WHILE expression DO statement
{ $$ = (Node)newWhileNode(@1, $2, $4); }
| { $$ = NULL; }
;
variable : IDENTIFIER tail { $$ = newVarRefNode(@1, $1, $2); }
tail : LBRACE expression RBRACE tail { $$ = newCons($2, $4); }
| { $$ = NULL; }
;
procedure_statement : IDENTIFIER
{ $$ = newInvocationNode(@1, $1, NULL); }
| IDENTIFIER LPAREN expression_list RPAREN
{ $$ = newInvocationNode(@1, $1, $3); }
;
expression_list : expression
{ $$ = newCons($1, NULL); }
| expression COMMA expression_list
{ $$ = newCons($1, $3); }
;
expression : boolexpression
{ $$ = (Node)$1; }
| boolexpression AND boolexpression
{ $$ = (Node)newBinExprNode(@2, And, $1, $3); }
| boolexpression OR boolexpression
{ $$ = (Node)newBinExprNode(@2, Or, $1, $3); }
;
boolexpression : simple_expression
{ $$ = $1; }
| simple_expression relop simple_expression
{ $$ = (Node)newBinExprNode(@2, $2, $1, $3); }
;
simple_expression : term
{ $$ = $1; }
| simple_expression addop term
{ $$ = (Node)newBinExprNode(@2, $2, $1, $3); }
;
term : factor
{ $$ = $1; }
| term mulop factor
{ $$ = (Node)newBinExprNode(@2, $2, $1, $3); }
;
factor : variable
{ $$ = (Node)$1; }
| IDENTIFIER LPAREN expression_list RPAREN
{ $$ = (Node)newInvocationNode(@1, $1, $3); }
| num
{ $$ = (Node)$1; }
| LITERALSTR
{ $$ = (Node)set_type(
(Node)newValNode(@1, (Data)yylval.text),
newType(STRING_TYPE, NULL, NULL, NULL)); }
| LPAREN expression RPAREN
{ $$ = (Node)$2; }
| NOT factor
{ $$ = (Node)newUnaExprNode(@1, Not, $2); }
| SUBOP factor
{ $$ = (Node)newUnaExprNode(@1, Neg, $2); }
;
addop : ADDOP { $$ = Add; }
| SUBOP { $$ = Sub; }
;
mulop : MULOP { $$ = Mul; }
| DIVOP { $$ = Div; }
;
relop : LTOP { $$ = Lt; }
| GTOP { $$ = Gt; }
| EQOP { $$ = Eq; }
| LETOP { $$ = Let; }
| GETOP { $$ = Get; }
| NEQOP { $$ = Neq; }
;
num : INTEGERNUM { $$ = (ValNode)set_type((Node)newValNode(@1, (Data)yylval.val), newType(INTEGER_TYPE, NULL, NULL, NULL)); }
| REALNUMBER { $$ = (ValNode)set_type((Node)newValNode(@1, (Data)yylval.rval), newType(REAL_TYPE, NULL, NULL, NULL)); }
| SCIENTIFIC { $$ = (ValNode)set_type((Node)newValNode(@1, (Data)yylval.rval), newType(REAL_TYPE, NULL, NULL, NULL)); }
;
%%
void yyerror(const char *msg) {
fprintf(stderr,
"[ERROR] line %4d:%3d %s, Unmatched token: %s\n",
line_no, col_no-(int)yyleng+1, buffer, yytext);
pass_error = 1;
}
int main(int argc, char *argv[]) {
char c;
while((c=getopt(argc, argv, "o:")) != -1){
switch(c){
case 'o':
output = optarg;
break;
case '?':
fprintf(stderr, "Illegal option:-%c\n", isprint(optopt)?optopt:'#');
break;
default:
fprintf( stderr, "Usage: %s [-o output] filename\n", argv[0]), exit(0);
break;
}
}
FILE *fp = argc == 1 ? stdin : fopen(argv[optind], "r");
if(fp == NULL)
fprintf( stderr, "Open file error\n" ), exit(-1);
yyin = fp;
yyparse();
if(!pass_error && root){
//visit(root, dumper);
Hash symtab = SymbolTable(root);
if(pass_error) return 0;
SemanticCheck(root, symtab);
if(pass_error) return 0;
CodeGen(root, symtab);
}
return 0;
}