-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparser.y
More file actions
368 lines (302 loc) · 7.04 KB
/
parser.y
File metadata and controls
368 lines (302 loc) · 7.04 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
%{
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "base.h"
#include "parser.h"
#include "ast.h"
#include "symbol_table.h"
#include "typecheck_visitor.h"
#include "graphprinter_visitor.h"
extern FILE *yyin;
static void yyerror( const char *msg );
static struct AstNode *ast;
%}
%defines
%locations
%pure-parser
%error-verbose
%union {
char* lexeme;
int integer;
int boolean;
char character;
int type;
struct AstNode *astnode;
};
%token <lexeme> IDENTIFIER
%token <type> TYPE_IDENTIFIER
%token <integer> INT_LITERAL
%token <boolean> BOOL_LITERAL
%token <character> CHAR_LITERAL
%token T_WHILE T_IF T_PROGRAM T_FUNCTION T_COLON T_PROC
%token T_COMMA T_SEMICOLON T_LBRACE T_RBRACE T_LPAR T_RPAR
%token T_DO T_DOT T_ASSIGN
%nonassoc IFX
%nonassoc T_ELSE
%left <lexeme> T_BITOR
%left <lexeme> T_LOGICOR
%left <lexeme> T_BITAND
%left <lexeme> T_LOGICAND
%left <lexeme> T_EQ T_NE
%left <lexeme> T_LT T_GT T_LE T_GE
%left <lexeme> T_PLUS T_MINUS
%left <lexeme> T_STAR T_SLASH
%nonassoc UMINUS
%type <astnode> program
%type <astnode> ident
%type <astnode> program_decl
%type <astnode> var_decl_list
%type <astnode> multi_var_decl
%type <astnode> var_decl
%type <astnode> identlist
%type <astnode> single_identifier
%type <astnode> multi_identifier
%type <astnode> proc_func_list
%type <astnode> multi_proc_func_decl
%type <astnode> proc_func_decl
%type <astnode> func_decl
%type <astnode> proc_decl
%type <astnode> param_list
%type <astnode> single_param
%type <astnode> multi_param
%type <type> type
%start program
%%
program:
program_decl var_decl_list proc_func_list statement_block
{
struct AstNode *ast_node;
ast_node = ast_node_new("Program", PROGRAM, VOID,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $1); // program_decl
ast_node_add_child(ast_node, $2); // var_decl_list
ast_node_add_child(ast_node, $3); // proc_func_list
$$ = ast_node;
ast = ast_node;
//ast_node_add_child(ast_node, $3); // statement_block
}
;
var_decl_list:
/* empty */ { $$ = NULL; }
| multi_var_decl
{
struct AstNode *ast_node;
ast_node = ast_node_new("VarDeclList", VARDECL_LIST, VOID,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $1);
$$ = ast_node;
}
;
multi_var_decl:
var_decl { $$ = $1; }
| var_decl multi_var_decl
{
ast_node_add_sibling($1, $2);
$$ = $1;
}
;
var_decl:
type identlist T_SEMICOLON
{
struct AstNode *ast_node;
ast_node = ast_node_new("VarDecl", VARDECL, $1,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $2);
$$ = ast_node;
}
;
proc_func_list:
/* empty */ { $$ = NULL; }
| proc_func_decl multi_proc_func_decl
{
struct AstNode *ast_node;
ast_node = ast_node_new("ProcFuncList", PROCFUNC_LIST, VOID,
yylloc.last_line, NULL);
ast_node_add_sibling($1, $2);
ast_node_add_child(ast_node, $1);
$$ = ast_node;
}
;
multi_proc_func_decl:
/* empty */ { $$ = NULL; }
| proc_func_decl multi_proc_func_decl
{
ast_node_add_sibling($1, $2);
}
;
proc_func_decl:
proc_decl { $$ = $1; }
| func_decl { $$ = $1; }
;
func_decl:
T_FUNCTION ident T_LPAR param_list T_RPAR T_COLON type T_LBRACE var_decl_list statement_block T_RBRACE
{
Symbol *symtab;
struct AstNode *ast_node;
ast_node = ast_node_new("FuncDecl", FUNCTION, VOID,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $2); // Identifier
ast_node_add_child(ast_node, $4); // ParamList
ast_node_add_child(ast_node, $9); // VarDeclList
//ast_node_add_child(ast_node, $9); // Statements
$2->symbol->type = $7;
ast_node->symbol = symbol_new(NULL);
$$ = ast_node;
}
;
proc_decl:
T_PROC ident T_LPAR param_list T_RPAR T_LBRACE var_decl_list statement_block T_RBRACE
{
Symbol *symtab;
struct AstNode *ast_node;
ast_node = ast_node_new("ProcDecl", PROCEDURE, VOID,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $2); // Identifier
//ast_node_add_child(ast_node, $4); // ParamList
ast_node_add_child(ast_node, $7); // VarDeclList
//ast_node_add_child(ast_node, $9); // Statements
ast_node->symbol = symbol_new(NULL);
$$ = ast_node;
}
;
program_decl:
T_PROGRAM ident T_SEMICOLON
{
struct AstNode *ast_node;
ast_node = ast_node_new("ProgramDecl", PROGRAM_DECL, VOID,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $2); /* identifier */
$$ = ast_node;
}
;
param_list:
/* empty */ { $$ = NULL; }
| single_param multi_param
{
struct AstNode *ast_node;
ast_node = ast_node_new("ParamList", PARAM_LIST, VOID,
yylloc.last_line, NULL);
ast_node_add_sibling($1, $2);
ast_node_add_child(ast_node, $1);
$$ = ast_node;
}
;
multi_param:
/* empty */ { $$ = NULL; }
| T_COMMA single_param multi_param
{
ast_node_add_sibling($2, $3);
$$ = $2;
}
;
single_param:
type ident
{
struct AstNode *ast_node;
ast_node = ast_node_new("Parameter", PARAMETER, $1,
yylloc.last_line, NULL);
ast_node_add_child(ast_node, $2); // Identifier
$$ = ast_node;
}
;
identlist:
single_identifier multi_identifier
{
struct AstNode *ast_node;
ast_node = ast_node_new("IdentifierList", IDENT_LIST, VOID,
yylloc.last_line, NULL);
ast_node_add_sibling($1, $2);
ast_node_add_child(ast_node, $1);
$$ = ast_node;
}
;
multi_identifier:
/* empty */ { $$ = NULL; }
| T_COMMA single_identifier multi_identifier
{
ast_node_add_sibling($2, $3);
$$ = $2;
}
;
single_identifier:
ident { $$ = $1; }
;
ident:
IDENTIFIER
{
struct AstNode *ast_node;
ast_node = ast_node_new("Identifier", IDENTIFIER, VOID,
yylloc.last_line, NULL);
ast_node->symbol = symbol_new($1);
// ast-node->symbol->decl_linenum = yylloc.last_line;
$$ = ast_node;
}
;
statement_block:
statement_block stmt
|
/* empty */
;
type: TYPE_IDENTIFIER
;
stmt:
T_SEMICOLON { }
| expr T_SEMICOLON { }
| IDENTIFIER T_ASSIGN expr T_SEMICOLON { }
| T_WHILE T_LPAR expr T_RPAR stmt
{ }
| T_DO stmt T_WHILE T_LPAR expr T_RPAR T_SEMICOLON { }
| T_IF T_LPAR expr T_RPAR stmt %prec IFX
{ }
| T_IF T_LPAR expr T_RPAR stmt T_ELSE stmt
{ }
| T_LBRACE stmt_list T_RBRACE { }
;
stmt_list:
stmt { }
| stmt_list stmt { }
;
expr:
INT_LITERAL { }
| CHAR_LITERAL { }
| BOOL_LITERAL { }
| IDENTIFIER { }
| T_MINUS expr %prec UMINUS { }
| expr T_PLUS expr { }
| expr T_MINUS expr { }
| expr T_STAR expr { }
| expr T_SLASH expr { }
| expr T_LT expr { }
| expr T_GT expr { }
| expr T_GE expr { }
| expr T_LE expr { }
| expr T_NE expr { }
| expr T_EQ expr { }
| T_LPAR expr T_RPAR { }
;
%%
static void yyerror(const char *msg)
{
fprintf(stderr, "Error(%d): %s\n", yyget_lineno(), msg);
}
int main(int argc, char **argv)
{
if (argc > 1)
yyin = fopen(argv[1], "r");
else
yyin = stdin;
/*yylloc.first_line = yylloc.last_line = 1;
yylloc.first_column = yylloc.last_column = 0;*/
yyparse();
Visitor *visitor;
visitor = typecheck_new();
ast_node_accept(ast, visitor);
if(ast_node_check_errors(ast)) {
fprintf(stderr, "Too many errors to compile.\n");
}
visitor = graphprinter_new();
ast_node_accept(ast, visitor);
return 0;
}