-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.y
479 lines (392 loc) · 18.9 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
%{
// Prologue
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "symbol_table.h"
#include "ast.h"
#include "global.h"
#include "semantic.h"
#include "translate.h"
char *filename; // name of input file
int error_num; // number of errors
int main_flag; // flag indicating if there is a main function
int current_scope_lvl;
struct symbol_list *current_symbol_table;
extern int yylex();
extern FILE *yyin; // pointer to the input file
extern int yylineno;
extern char *line;
struct AstNode *root; // root node of the AST
struct AstNode *param_list = NULL; // pointer to the list of function parameters (to be included in the scope)
enum TYPE ret_type = -1; // function return type (to be included in the scope)
void scope_enter();
void scope_exit();
void fill_symbol_table(struct symbol_list * symlist, struct AstNode *node, enum TYPE type, enum symbol_type sym_type);
int print_symtab_flag = 0; // -s
int print_ast_flag = 0; // -t
void print_usage(); // compiler usage
%}
// Bison declarations
%define parse.error verbose
// The %union declaration specifies the entire collection of possible data types for semantic values.
%union {
//char *name_token; // for identifiers and strings
//int integer_token; // for integers
//float float_token; // for floats
char* s; // the numbers will be converted later if necessary
struct AstNode *ast;
int t;
}
// token kind name (terminal symbol)
%token <s> INT_VALUE //numerical constants (integer)
%token <s> FLOAT_VALUE //numerical constants (float)
%token <s> STRING_VALUE //strings
%token <s> ID
%token <s> SCANF PRINTF
%token CHAR INT FLOAT
%token IF ELSE FOR VOID RETURN
// Precedence and associativity ()
%left '+' '-'
%left '*' '/'
%right '='
%left AND OR NOT // NOT is a unary operator
%nonassoc MINUS
%left '>' '<' GE LE EQ NE
%left '[' ']' '(' ')' ','
// The declaration says there should be 1 shift/reduce conflicts and no reduce/reduce conflicts.
// Bison reports an error if the number of shift/reduce conflicts differs from n, or if there are any reduce/reduce conflicts.
%expect 1
%type <ast> number var var_declaration global_declaration_list global_declaration expr assignment_statement assignment
%type <ast> return_statement expr_statement func_call args format_string printf_statement if_cond
%type <ast> declarator_list initializer_list
%type <ast> func_declaration param_list param compound_statement statement_list statement
%type <ast> scanf_statement scanf_var_list iteration_statement init cond update selection_statement
%type <ast> embedded_statement single_statement
%type <t> type
//%start program
%%
// Grammar rules
program
: { scope_enter(); } global_declaration_list { root = $2; scope_exit(); }
;
global_declaration_list
: global_declaration
| global_declaration global_declaration_list { $$ = link_AstNode($1, $2); }
;
// in the global scope, only variable or function declarations can be present
global_declaration
: var_declaration
| func_declaration { check_main($1); }
;
var_declaration
: type declarator_list ';' { $$ = new_declaration_node(DECL_T, $1, $2); fill_symbol_table(current_symbol_table, $$, -1, VARIABLE); }
| type initializer_list ';' { $$ = new_declaration_node(DECL_T, $1, $2); fill_symbol_table(current_symbol_table, $$, -1, VARIABLE); }
| ID declarator_list ';' { $$ = new_error_node(ERROR_NODE_T); yyerror(error_string_format("Unknown type name: " BOLD "%s" RESET, $1 )); }
;
declarator_list
: var
| var ',' declarator_list { $$ = link_AstNode($1, $3); }
;
initializer_list
: assignment
| assignment ',' initializer_list {$$ = link_AstNode($1, $3); }
;
type
: INT { $$ = INT_T; }
| FLOAT { $$ = FLOAT_T; }
| CHAR { $$ = CHAR_T; }
| VOID { $$ = VOID_T; }
;
var
: ID { $$ = new_variable_node(VAR_T, $1, NULL); }
| ID '[' expr ']' { $$ = new_variable_node(VAR_T, $1, $3); check_array($3); }
;
func_declaration
: type ID '(' param_list ')' { param_list = $4; ret_type = $1; insert_symbol(current_symbol_table, $2, $1, FUNCTION, $4, yylineno, line); }
compound_statement { $$ = new_func_def_node(FDEF_T, $1, $2, $4, $7); check_func_return($1, $7); }
| type ID '(' ')' { ret_type = $1; insert_symbol(current_symbol_table, $2, $1, FUNCTION, NULL, yylineno, line); }
compound_statement { $$ = new_func_def_node(FDEF_T, $1, $2, NULL, $6); check_func_return($1, $6); }
| type ID '(' VOID ')' { ret_type = $1; insert_symbol(current_symbol_table, $2, $1, FUNCTION, NULL, yylineno, line); }
compound_statement { $$ = new_func_def_node(FDEF_T, $1, $2, NULL, $7); check_func_return($1, $7); }
;
compound_statement
: scope_enter statement_list scope_exit { $$ = $2; }
;
scope_enter
: '{' { scope_enter(); }
;
scope_exit
: '}' { scope_exit(); }
;
param_list
: param
| param ',' param_list { $$ = link_AstNode($1, $3); }
;
param
: type ID { $$ = new_declaration_node(DECL_T, $1, new_variable_node(VAR_T, $2, NULL)); }
| type ID '[' ']' { $$ = new_declaration_node(DECL_T, $1,new_variable_node(VAR_T, $2, new_value_node(VAL_T, INT_T, "0"))); }
;
statement_list
: statement
| statement statement_list { $$ = link_AstNode($1, $2); }
;
statement
: var_declaration
| compound_statement
| expr_statement
| selection_statement
| iteration_statement
| return_statement
| printf_statement
| scanf_statement
| assignment_statement
;
embedded_statement
: compound_statement
| single_statement
;
single_statement
: var_declaration { yyerror("embedded statement cannot be a declaration"); }
| expr_statement
| selection_statement
| iteration_statement
| return_statement
| printf_statement
| scanf_statement
| assignment_statement
;
expr_statement
: expr ';' { eval_expr_type($1); $$ = $1; }
;
selection_statement
: IF '(' if_cond ')' embedded_statement { $$ = new_if_node(IF_T, $3, $5, NULL); }
| IF '(' if_cond ')' embedded_statement ELSE embedded_statement { $$ = new_if_node(IF_T, $3, $5, $7); }
;
if_cond
: expr { check_cond(eval_expr_type($1).type); $$ = $1; }
;
iteration_statement
: FOR '(' init ';' cond ';' update ')' embedded_statement { $$ = new_for_node(FOR_T, $3, $5, $7, $9); scope_exit(); }
;
init
: assignment { scope_enter(); eval_expr_type($1); $$ = $1;}
| type assignment { scope_enter(); $$ = new_declaration_node(DECL_T, $1, $2); fill_symbol_table(current_symbol_table, $$, -1, VARIABLE); }
| /* empty */ { scope_enter(); $$ = NULL; }
;
cond
: expr { check_cond(eval_expr_type($1).type); $$ = $1; }
| /* empty */ { $$ = NULL; }
;
update
: assignment { eval_expr_type($1); $$ = $1; }
| expr { eval_expr_type($1); $$ = $1; }
| /* empty */ { $$ = NULL; }
;
return_statement
: RETURN ';' { $$ = new_return_node(RETURN_T, NULL); check_return(NULL); }
| RETURN expr ';' { $$ = new_return_node(RETURN_T, $2); check_return($2); }
;
printf_statement
: PRINTF '(' format_string ')' ';' { $$ = new_func_call_node(FCALL_T, $1, $3); check_format_string($3, NULL, PRINTF_T); }
| PRINTF '(' format_string ',' args ')' ';' { $$ = new_func_call_node(FCALL_T, $1, link_AstNode($3,$5)); check_format_string($3, $5, PRINTF_T); }
| PRINTF '(' ')' ';' { $$ = new_error_node(ERROR_NODE_T); yyerror("too few arguments to function" BOLD " printf" RESET); }
;
scanf_statement
: SCANF '(' format_string ',' scanf_var_list ')' ';' { check_format_string($3, $5, SCANF_T); $$ = new_func_call_node(FCALL_T, $1, link_AstNode($3, $5)); }
| SCANF '(' format_string ')' ';' { check_format_string($3, NULL, SCANF_T); $$ = new_func_call_node(FCALL_T, $1, $3); }
| SCANF '(' ')' ';' { $$ = new_error_node(ERROR_NODE_T); yyerror("too few arguments to function" BOLD " scanf" RESET); }
;
scanf_var_list
: '&' var { check_var_reference($2); by_reference($2); $$ = $2; }
| var { check_var_reference($1); $$ = $1; }
| '&' var ',' scanf_var_list { check_var_reference($2); by_reference($2); $$ = link_AstNode($2, $4); }
| var ',' scanf_var_list { check_var_reference($1); link_AstNode($1, $3); }
;
format_string
: STRING_VALUE { $$ = new_value_node(VAL_T, STRING_T, $1); }
;
assignment_statement
: assignment ';' { $$ = $1; eval_expr_type($1); }
;
assignment
: var '=' expr { $$ = new_expression_node(EXPR_T, ASS_T, $1, $3); check_var_reference($1); }
;
expr
: var { $$ = $1; check_var_reference($1); }
| number
| format_string
| func_call
| expr '+' expr { $$ = new_expression_node(EXPR_T, ADD_T, $1, $3); }
| expr '-' expr { $$ = new_expression_node(EXPR_T, SUB_T, $1, $3); }
| expr '*' expr { $$ = new_expression_node(EXPR_T, MUL_T, $1, $3); }
| expr '/' expr { $$ = new_expression_node(EXPR_T, DIV_T, $1, $3); check_division($3); }
| NOT expr { $$ = new_expression_node(EXPR_T, NOT_T, NULL, $2); }
| expr AND expr { $$ = new_expression_node(EXPR_T, AND_T, $1, $3); }
| expr OR expr { $$ = new_expression_node(EXPR_T, OR_T, $1, $3); }
| expr '>' expr { $$ = new_expression_node(EXPR_T, G_T, $1, $3); }
| expr '<' expr { $$ = new_expression_node(EXPR_T, L_T, $1, $3); }
| expr GE expr { $$ = new_expression_node(EXPR_T, GE_T, $1, $3); }
| expr LE expr { $$ = new_expression_node(EXPR_T, LE_T, $1, $3); }
| expr EQ expr { $$ = new_expression_node(EXPR_T, EQ_T, $1, $3); }
| expr NE expr { $$ = new_expression_node(EXPR_T, NE_T, $1, $3); }
| '(' expr ')' { $$ = new_expression_node(EXPR_T, PAR_T, NULL, $2); }
| '-' expr %prec MINUS { $$ = new_expression_node(EXPR_T, NEG_T, NULL, $2); }
;
number
: INT_VALUE { $$ = new_value_node(VAL_T, INT_T, $1); }
| FLOAT_VALUE { $$ = new_value_node(VAL_T, FLOAT_T, $1); }
;
func_call
: ID '(' args ')' { $$ = new_func_call_node(FCALL_T, $1, $3); check_fcall($1, $3); }
| ID '(' ')' { $$ = new_func_call_node(FCALL_T, $1, NULL); check_fcall($1, NULL); }
;
args
: expr
| expr ',' args { $$ = link_AstNode($1, $3); }
;
%%
// Function to open a new scope
void scope_enter() {
// create a new empty table
current_symbol_table = create_symbol_table(current_scope_lvl, current_symbol_table);
// increment the current scope level
current_scope_lvl++;
// if the new scope is a function scope, add 1) possible parameters
if(param_list) {
fill_symbol_table(current_symbol_table, param_list, -1, PARAMETER);
param_list = NULL;
}
// and 2) function return type
if(ret_type != -1) {
insert_symbol(current_symbol_table, "return", ret_type, RETURN_S, NULL, yylineno, line);
ret_type = -1;
}
}
// Function to close a scope
void scope_exit() {
// eventually print the Symbol Table
if(print_symtab_flag)
print_symbol_table(current_symbol_table);
check_usage(current_symbol_table);
// delete the current table
current_symbol_table = delete_symbol_table(current_symbol_table);
// increment the current scope number
current_scope_lvl--;
}
// Function to insert one or more symbols in the Symbol Table
void fill_symbol_table(struct symbol_list * symlist, struct AstNode *node, enum TYPE type, enum symbol_type sym_type){
/*
symlist : pointer to current_symbol_table
n : pointer to the AST node
type : data type
sym_type : symbol type (only parameter or variable)
*/
while(node){
if(node -> node_type == VAR_T) {
insert_symbol(symlist, node -> node.var -> name, type, sym_type, NULL, yylineno, line);
if(node -> node.var -> array_dim){
// if it is an array (so array_dim in the node is not null), modify the flag in the symbol table
struct symbol *s = find_symbol(symlist, node -> node.var -> name);
s -> array = 1;
}
}else if(node -> node_type == EXPR_T){
// Declarations with assignment, so in this case EXPR_T will always be an assignment
insert_symbol(symlist, node -> node.expr -> l -> node.var -> name, type, sym_type, NULL, yylineno, line);
if(node -> node.expr -> l -> node.var -> array_dim){
// if it is an array (so array_dim in the node is not null), modify the flag in the symbol table
yyerror("invalid array initializer");
struct symbol *s = find_symbol(symlist, node -> node.expr -> l -> node.var -> name);
s -> array = 1;
}
// evaluate the expression type
eval_expr_type(node);
}else if(node -> node_type == DECL_T){
// the first passed node will always be DECL_T type from which type will be extracted to be used in subsequent cases as well
type = node -> node.decl -> type;
fill_symbol_table(symlist, node -> node.decl -> var, type, sym_type);
}
node = node -> next;
}
}
int main(int argc, char **argv) {
//debug
/* #ifdef YYDEBUG
yydebug = 1;
#endif */
int file_count = 0;
if(argc < 2) {
// no input file
fprintf(stderr, RED "fatal error:" RESET " no input file\n");
exit(1);
}else{
for(int i = 1; i < argc; i++){
// -s to print symbol tables
if(strcmp(argv[i], "-s") == 0)
print_symtab_flag = 1;
// -t to print AST
else if(strcmp(argv[i], "-t") == 0)
print_ast_flag = 1;
// -h --help to print usage
else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0){
print_usage();
exit(0);
}else {
if(argv[i][0] == '-'){
// unrecognized command line option
fprintf(stderr, RED "error:" RESET " unrecognized command line option " BOLD "%s \n" RESET, argv[i]);
print_usage();
exit(1);
}else{
// check on the number of input files
if(file_count == 0){
filename = argv[i];
file_count++;
}else{
// more than one input file
fprintf(stderr, RED "error:" RESET " only one input file accepted \n");
exit(1);
}
}
}
}
}
if(file_count == 0){
// no input file
fprintf(stderr, RED "fatal error:" RESET " no input file\n");
exit(1);
}else{
yyin = fopen(filename, "r");
}
if(!yyin) {
fprintf(stderr, RED "error:" RESET " %s: ", filename);
perror("");
fprintf(stderr, RED "fatal error:" RESET" no input file\n");
exit(1);
}
// global variables initialization
error_num = 0;
current_scope_lvl = 0;
current_symbol_table = NULL;
// yyparse: function to parse an input stream
if(yyparse() == 0) {
if(print_ast_flag) {
printf("\nABSTRACT SYNTAX TREE\n\n");
print_ast(root);
}
if(error_num == 0){
translate(root);
}
}
fclose(yyin);
}
// If the --help or -h flags have been used, this function displays the compiler usage
void print_usage(){
printf("Usage: ./compiler [options] file \n");
printf("options: \n");
printf(" --help \t Diplay this information. \n");
printf(" -h \t\t Diplay this information. \n");
printf(" -s \t\t Print Symbol Table. \n");
printf(" -t \t\t Print Abstract Syntax Tree. \n");
}