-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.y
393 lines (339 loc) · 10.6 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
%code requires{
#include <cstdio>
#include <cstdlib>
#include "lexer.hpp"
#include "util.hpp"
#include "ast.hpp"
#include "abstractlvalue.hpp"
#include "assign.hpp"
#include "binop.hpp"
#include "block.hpp"
#include "charconst.hpp"
#include "condition.hpp"
#include "expr.hpp"
#include "fpardef.hpp"
#include "funccall.hpp"
#include "funccallstmt.hpp"
#include "funcdecl.hpp"
#include "funcdef.hpp"
#include "header.hpp"
#include "id.hpp"
#include "if.hpp"
#include "intconst.hpp"
#include "lmatrix.hpp"
#include "localdef.hpp"
#include "logicalcond.hpp"
#include "lvalue.hpp"
#include "noop.hpp"
#include "numericcond.hpp"
#include "parsedtype.hpp"
#include "program.hpp"
#include "return.hpp"
#include "stmt.hpp"
#include "strlit.hpp"
#include "unaryop.hpp"
#include "vardef.hpp"
#include "while.hpp"
}
%code {
#include "returnstack.hpp"
#include <getopt.h>
extern FILE* yyin;
ReturnStack return_stack;
}
%define parse.error verbose /* For debug purposes*/
%expect 1
%union {
Stmt *stmt;
Expr *expr;
Block *block;
Condition *cond;
LocalDef *localdef;
FuncDef *funcdef;
AbstractLvalue *abstractlvalue;
FuncCall *funccall;
Id *id;
char char_val;
int int_val;
std::string *str_val;
std::vector<Expr *> *vector_expr;
std::vector<LocalDef *> *vector_local_def;
std::vector<FParDef *> *vector_fpar_def;
Header *header;
FParDef *fpar_def;
std::vector<Id *> *id_defs;
ParsedType *type;
DATA_TYPE data_type;
FuncDecl* func_decl;
VarDef* var_def;
std::vector<int> *int_vec;
Program *program;
}
%token T_and "and"
%token T_int "int"
%token T_then "then"
%token T_char "char"
%token<char_val> T_mod "mod"
%token T_var "var"
%token<char_val> T_div "div"
%token T_not "not"
%token T_while "while"
%token T_do "do"
%token T_nothing "nothing"
%token T_else "else"
%token T_or "or"
%token T_fun "fun"
%token T_ref "ref"
%token T_if "if"
%token T_return "return"
%token T_arr "arr" /* Short for arrow, the assignement op <- */
%token<char_val> T_leq "leq"
%token<char_val> T_geq "geq"
%token<str_val> T_id
/* %token T_writestring */
%token<int_val> T_int_lit
%token<char_val> T_char_lit
%token<str_val> T_string_lit
%left "or"
%left "and"
%precedence "not"
%nonassoc '=' '#' '>' '<' "leq" "geq"
%left '+' '-'
%left '*' "div" "mod"
%precedence UNARY /*unary minus and plus signs*/
%type<stmt> stmt
%type<expr> expr
%type<block> block stmt_list
%type<cond> cond
%type<abstractlvalue> l_value
%type<funccall> func_call
%type<char_val> '+' '-' '*' '=' '#' '<' '>'
%type<vector_expr> expr_list expr_rest
%type<funcdef> func_def
%type<vector_local_def> local_def_list
%type<header> header
%type<localdef> local_def
%type<vector_fpar_def> fpar_def_list fpar_def_rest
%type<fpar_def> fpar_def
%type<id_defs> id_rest
%type<type> fpar_type type
%type<data_type> data_type ret_type
%type<int_vec> int_const_bracket_list int_const_bracket_list_var
%type<func_decl> func_decl
%type<var_def> var_def
%type<program> program
%%
program:
func_def {
$$ = new Program($1);
// std::cout << *$$ << std::endl;
$$->sem();
$$->compile();
delete $$;
}
;
func_def:
header local_def_list block { $$ = new FuncDef($1, $2, $3); }
;
local_def_list:
/* nothing */ { $$ = new std::vector<LocalDef *>(); }
| local_def_list local_def { $1->push_back($2); $$ = $1; }
;
header:
"fun" T_id '(' fpar_def_list ')' ':' ret_type {
Id *id;
id = new Id(*$2);
$$ = new Header(id, $4, $7);
delete $2; /* delete str_val which was dynamically created in lexer */
}
;
fpar_def_list:
/* nothing */ { $$ = new std::vector<FParDef *>(); }
| fpar_def_rest fpar_def { $1->push_back($2); $$ = $1; }
;
fpar_def_rest:
/* nothing */ { $$ = new std::vector<FParDef *>(); }
| fpar_def_rest fpar_def ';' { $1->push_back($2); $$ = $1; }
;
fpar_def:
"ref" id_rest T_id ':' fpar_type {
Id *id;
id = new Id(*$3);
$2->push_back(id);
$$ = new FParDef($2, $5, true);
delete $3; /* delete str_val which was dynamically created in lexer */
}
| id_rest T_id ':' fpar_type {
Id *id;
id = new Id(*$2);
$1->push_back(id);
$$ = new FParDef($1, $4, false);
delete $2; /* delete str_val which was dynamically created in lexer */
}
;
id_rest:
/* nothing */ { $$ = new std::vector<Id *>(); }
| id_rest T_id ',' {
Id *id;
id = new Id(*$2);
$1->push_back(id);
$$ = $1;
delete $2; /* delete str_val which was dynamically created in lexer */
}
;
data_type:
"int" { $$ = DATA_TYPE_int; }
| "char" { $$ = DATA_TYPE_char; }
;
type:
data_type int_const_bracket_list_var { $$ = new ParsedType($1, $2); }
;
int_const_bracket_list_var:
/* nothing */ { $$ = new std::vector<int>(); }
| int_const_bracket_list_var '[' T_int_lit ']' { $1->push_back($3); $$ = $1; }
;
ret_type:
data_type { $$ = $1; }
| "nothing" { $$ = DATA_TYPE_void; }
;
int_const_bracket_list:
']' { $$ = new std::vector<int>(); $$->push_back(INT_CONST_BRACKET_LIST_DIMENSION_AUTOCOMPLETE); }
| T_int_lit ']' { $$ = new std::vector<int>(); $$->push_back($1); }
| int_const_bracket_list '[' T_int_lit ']' { $1->push_back($3); $$ = $1; }
;
fpar_type:
data_type { $$ = new ParsedType($1, new std::vector<int>()); }
| data_type '[' int_const_bracket_list { $$ = new ParsedType($1, $3); }
;
local_def:
func_def { $$ = $1; }
| func_decl { $$ = $1; }
| var_def { $$ = $1; }
;
func_decl:
header ';' { $$ = new FuncDecl($1); }
;
var_def:
"var" id_rest T_id ':' type ';' {
Id *id;
id = new Id(*$3);
$2->push_back(id);
$$ = new VarDef($2, $5);
delete $3; /* delete str_val which was dynamically created in lexer */
}
;
stmt:
';' { $$ = new NoOp(); }
| l_value "arr" expr ';' { $$ = new Assign($1, $3); }
| block { $$ = $1; }
| func_call ';' { $$ = new FuncCallStmt($1); }
| "if" cond "then" stmt "else" stmt { $$ = new If($2, $4, $6); }
| "if" cond "then" stmt { $$ = new If($2, $4); }
| "while" cond "do" stmt { $$ = new While($2, $4); }
| "return" ';' { $$ = new Return(); }
| "return" expr ';' { $$ = new Return($2); }
block:
'{' stmt_list '}' { $$ = $2; }
stmt_list:
/* nothing */ { $$ = new Block(); }
| stmt_list stmt { if ($2 != nullptr) {$1->append($2); } $$ = $1; }
;
func_call:
T_id '(' expr_list ')' {
Id *id;
id = new Id(*$1);
$$ = new FuncCall(id, $3);
delete $1; /* delete str_val which was dynamically created in lexer */
}
;
expr_list:
/* nothing */ { $$ = new std::vector<Expr *>(); }
| expr_rest expr { $1->push_back($2); $$ = $1; }
;
expr_rest:
/* nothing */ { $$ = new std::vector<Expr *>(); }
| expr_rest expr ',' { $1->push_back($2); $$ = $1; }
;
l_value:
T_id {
Id *id;
id = new Id(*$1);
$$ = new LValue(id);
delete $1; /* delete str_val which was dynamically created in lexer */
}
| T_string_lit { $$ = new StrLit(*$1); delete $1; }
| l_value '[' expr ']' { $$ = new LMatrix($1, $3); }
;
expr:
T_int_lit { $$ = new IntConst($1); }
| T_char_lit { $$ = new CharConst($1); }
| l_value { $$ = $1; }
| '(' expr ')' { $$ = $2; }
| func_call { $$ = $1; }
| '+' expr %prec UNARY { $$ = new UnaryOp($1, $2); }
| '-' expr %prec UNARY { $$ = new UnaryOp($1, $2); }
| expr '+' expr { $$ = new BinOp($1, $2, $3); }
| expr '-' expr { $$ = new BinOp($1, $2, $3); }
| expr '*' expr { $$ = new BinOp($1, $2, $3); }
| expr T_div expr { $$ = new BinOp($1, $2, $3); }
| expr T_mod expr { $$ = new BinOp($1, $2, $3); }
;
cond:
'(' cond ')' { $$ = $2; }
| "not" cond { $$ = new LogicalCond($2, 'n'); }
| cond "and" cond { $$ = new LogicalCond($1, 'a', $3); }
| cond "or" cond { $$ = new LogicalCond($1, 'o', $3); }
| expr '=' expr { $$ = new NumericCond($1, $2, $3); }
| expr '#' expr { $$ = new NumericCond($1, $2, $3); }
| expr '<' expr { $$ = new NumericCond($1, $2, $3); }
| expr '>' expr { $$ = new NumericCond($1, $2, $3); }
| expr T_leq expr { $$ = new NumericCond($1, $2, $3); }
| expr T_geq expr { $$ = new NumericCond($1, $2, $3); }
;
%%
int main(int argc, char *argv[]) {
/* Initialize symbol table with hash table of size 256 */
initSymbolTable(256);
/* Open initial scope for library functions */
openScope();
/* Add library functions to the most outer scope of the symbol table */
semInitLibraryFunctions();
/* Open program scope */
openScope();
emit_final_code_to_stdout = emit_imm_code_to_stdout = false;
/* Perform parsing and semantic analysis */
int c;
while ((c = getopt (argc, argv, "fio")) != -1)
{
switch (c)
{
case 'f':
emit_final_code_to_stdout = true;
break;
case 'i':
emit_imm_code_to_stdout = true;
break;
case 'o':
run_optimizations = true;
break;
}
}
bool read_from_stdin = emit_final_code_to_stdout || emit_imm_code_to_stdout;
/* std::cout << "f flag: " << emit_final_code_to_stdout << " i flag: " << emit_imm_code_to_stdout << " o flag: " << run_optimizations << " Read from stdin: " << read_from_stdin << std::endl; */
if (!read_from_stdin)
{
/* std::cout << "Reading from file: " << argv[argc-1] << std::endl; */
FILE* fp = fopen(argv[argc-1], "r");
filename = argv[argc-1];
if(fp)
yyin = fp;
}
int result = yyparse();
/* Close program scope */
closeScope();
/* Close initial scope for library functions */
closeScope();
/* Destroy symbol table */
destroySymbolTable();
return result;
}