forked from NesManrique/LenguajesII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
420 lines (355 loc) · 14.4 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
%{
#include "ast.cpp"
#include <cstdio>
#include <cstdarg>
#include "symtable.cpp"
#include <stdio.h>
extern int yylex (void);
//void yyerror (char const *a){printf("ERROR: %s\n",a);};
void yyerror(char const *s, ...);
NBlock *ProgramAST;
bool flagerror=false;
int flagfdecl=0;
Symtable Table;
%}
/* Ways to access data */
%union{
Node *node;
NBlock *block;
NExpression *expr;
NStatement *stmt;
NIdentifier *ident;
NLRExpression *lrexpr;
NVariableDeclaration *var_decl;
NArrayDeclaration *arr_decl;
NFunctionDeclaration *fun_decl;
NArrayAccess *arr_access;
NRegisterDeclaration *reg_decl;
NUnionDeclaration *union_decl;
NArray *cons_arr;
std::vector<NVarrayDeclaration*> *varvec;
std::vector<NExpression*> *exprvec;
std::string *string;
int token;
long long integer;
double floating;
char Char;
void * error;
}
/* Terminal Symbols */
%token <error> error
%token <string> STR
%token <integer> INT
%token <floating> FLOAT
/*%token <token> '=' '(' ')' '{' '}' ',' '.' '!' '<' '>' '%'
%token <token> '+' '-' '/' '*'*/
%token <token> IF THEN ELSE FROM TO IN NEXT STOP
%token <token> CHAR UNION ARRAY TRUE FALSE STRIN
%token <token> REGISTER DO WHILE RETURN FOR STEP
%token <string> ID
/* Type of node our nonterminal represent */
%type <expr> expr fun_call bool_expr arit_expr
%type <lrexpr> lrexpr
%type <ident> ident
%type <varvec> fun_decl_args var_decls fun_decl_args_list
%type <exprvec> fun_call_args expr_lst
%type <block> program stmts block decls
%type <fun_decl> fun_firm
%type <cons_arr> cons_arr arr_lst
%type <reg_decl> reg_decl
%type <union_decl> union_decl
%type <arr_decl> arr_decl str_decl
%type <stmt> stmt var_decl fun_decl ctrl_for
%type <stmt> ctrl_while ctrl_if var_asgn
/*%type <token> comparison*/
/* Matematical operators precedence */
%nonassoc <token> EQ NEQ GEQ LEQ '<' '>'
%left <token> '+' '-' AND OR
%left <token> '*' '/'
%left <token> NEG NOT
%left <token> ACCESS
%locations
%start program
%%
program : decls {ProgramAST = $1;}
;
decls : decl {$$ = new NBlock();
$$->statements.push_back($<stmt>1);
flagfdecl=0;}
| decls decl {$$->statements.push_back($<stmt>2);}
| error decl { fprintf(stderr,
"Error in declaration, l%d,c%d-l%d,c%d\n",
@1.first_line, @1.first_column,
@1.last_line, @1.last_column);
flagerror=1;
if(flagfdecl)
$$ = new NBlock();}
;
decl : varr_decl '.'
| reg_decl
| union_decl
| fun_decl
| error '.' {fprintf(stderr,
"Error in declaration, l%d,c%d-l%d,c%d\n",
@1.first_line, @1.first_column,
@1.last_line, @1.last_column);
flagerror=1;
}
;
varr_decl : var_decl
| arr_decl
| str_decl
;
var_decl : ident ident {
TElement * t;
$$ = new NVariableDeclaration(*$1,*$2);
if((t=Table.lookupScope($2->name))!=NULL){
flagerror = 1;
cerr<<"Variable `"<< $2->name<< "` was declare before: l"
<<@2.first_line<<",c"<<@2.first_column<<"-l"<<
@2.first_line<<",c"<<@2.first_column<<endl;
}
if((t=Table.lookupType($1->name))!=NULL){
#ifdef DEBUG
cerr<<" inserting variable "<< $2->name<<" as "<<$1->name<<endl;
#endif
Table.insert($2->name,new TVar($2->name,*((TType *)t)));
}else{
flagerror=1;
cerr<<"Error "<<$1->name<<" does not name a type"<<endl;
}
}
| ident ident '=' expr {$$ = new NVariableDeclaration(*$1,*$2,$4);
TElement * t;
if((t=Table.lookupScope($2->name))!=NULL){
flagerror = 1;
cerr<<"Variable `"<< $2->name<< "` was declare before: l"
<<@2.first_line<<",c"<<@2.first_column<<"-l"<<
@2.first_line<<",c"<<@2.first_column<<endl;
}
if((t=Table.lookupType($1->name))!=NULL){
Table.insert($2->name,
new TVar($1->name,*((TType *)t)));
}else{
flagerror=1;
cerr<<"Error "<<$1->name<<" does not name a type"<<endl;
}
}
/*| ident error {}*/
;
fun_decl : fun_firm block {$1->block = $2;$$ = $1;}
/*FIRMAS DE FUNCIONES PARA EL PROX TRIMESTRE | fun_firm {$$=$1;}*/
;
fun_firm : ident ident fun_decl_args {
$$ = new NFunctionDeclaration(*$1,*$2,*$3);
if($$->addSymtable(Table)==2){
flagerror=1;
cerr<<"Function `"<< $$->id.name<< "` was declare before: l"
<<@2.first_line<<",c"<<@2.first_column<<"-l"<<
@2.first_line<<",c"<<@2.first_column<<endl;
}}
str_decl : STRIN cons_arr ident {
$$ = new NArrayDeclaration(*$3,*(new NIdentifier(*( new std::string("char")))),*$2);
if($$->addSymtable(Table)==1)
cerr<<"Array `"<< $$->id.name<< "`. l"
<<@3.first_line<<",c"<<@3.first_column<<"-l"<<
@3.first_line<<",c"<<@3.first_column<<endl;
flagerror=1;
}
| STRIN cons_arr ident '=' STR {$$ = new NArrayDeclaration(*$3,*(new NIdentifier(*(new std::string("char")))),*$2,new NArray(*$5));
if($$->addSymtable(Table)==1)
cerr<<"Array `"<< $$->id.name<< "`. l"
<<@3.first_line<<",c"<<@3.first_column<<"-l"<<
@3.first_line<<",c"<<@3.first_column<<endl;
flagerror=1;
}
;
arr_decl : ARRAY cons_arr ident ident {$$ = new NArrayDeclaration(*$4,*$3,*$2);
if($$->addSymtable(Table)==1)
cerr<<"Array `"<< $$->id.name<< "`: l"
<<@4.first_line<<",c"<<@4.first_column<<"-l"<<
@4.first_line<<",c"<<@4.first_column<<endl;
flagerror=1;
}
| ARRAY cons_arr ident ident '=' arr_lst {$$ = new NArrayDeclaration(*$4,*$3,*$2,$6);
if($$->addSymtable(Table)==1)
cerr<<"Array `"<< $$->id.name<< "`: l"
<<@4.first_line<<",c"<<@4.first_column<<"-l"<<
@4.first_line<<",c"<<@4.first_column<<endl;
flagerror=1;
}
union_decl : UNION ident beg_block var_decls end_block {$$ = new NUnionDeclaration(*$2,*$4);}
| UNION ident beg_block error end_block {fprintf(stderr,
"Error in union member declarations, l%d,c%d-l%d,c%d\n",
@4.first_line, @4.first_column,
@4.last_line, @4.last_column);}
;
reg_decl : REGISTER ident beg_block var_decls '}' {$$ = new NRegisterDeclaration(*$2,*$4);$$->addToSymtable(Table);Table.endScope();}
| REGISTER ident beg_block error end_block {fprintf(stderr,
"Error in register member declarations, l%d,c%d-l%d,c%d\n",
@4.first_line, @4.first_column,
@4.last_line, @4.last_column);
flagerror=1;
}
;
var_decls : var_decl {$$ = new VariableList();$$->push_back($<var_decl>1);}
| var_decls ',' var_decl {$$->push_back($<var_decl>3);}
| var_decls error var_decl {fprintf(stderr,
"Missing ' character, l%d,c%d-l%d,c%d\n",
@2.first_line, @2.first_column,
@2.last_line, @2.last_column);
$$->push_back($<var_decl>3);
flagerror=1;
}
;
fun_decl_args: fun_scope ')' {$$ = new VariableList();}
| fun_scope fun_decl_args_list ')' {$$ = $2;}
| fun_scope fun_decl_args_list error {fprintf(stderr,
"Missing ) character, l%d,c%d-l%d,c%d\n",
@3.first_line, @3.first_column,
@3.last_line, @3.last_column);
$$= $2;}
;
fun_scope: '(' {Table.begScope();}
fun_decl_args_list: var_decl {$$ = new VariableList();$$->push_back($<var_decl>1);}
| fun_decl_args_list ',' var_decl {$$->push_back($<var_decl>3);}
| fun_decl_args_list error var_decl {fprintf(stderr,
"Missing ' character, l%d,c%d-l%d,c%d\n",
@2.first_line, @2.first_column,
@2.last_line, @2.last_column);
$$->push_back($<var_decl>3);
yyerrok;}
;
ident : ID {$$ = new NIdentifier(*$1);}
expr : lrexpr{$$ = $<expr>1;}
| STR {$$ = new NString(*$1);}
| CHAR {$$ = new NChar($1);}
| fun_call
| bool_expr
| arit_expr
| '(' expr ')' {$$=$2;}
/*| error ')' {@$.first_column = @1.first_column;
@$.first_line = @1.first_line;
@$.last_column = @2.last_column;
@$.last_line = @2.last_line;
fprintf(stderr, "Error detected, l%d,c%d-l%d,c%d",
@1.first_line, @1.first_column,
@2.last_line, @2.last_column);
}*/
;
arit_expr : expr '+' expr {$$=new NBinaryOperator($1,"+",$3);}
| INT {$$ = new NInteger($1);}
| FLOAT {$$ = new NDouble($1);}
| expr '-' expr {$$=new NBinaryOperator($1,"-",$3);}
| expr '/' expr {$$=new NBinaryOperator($1,"/",$3);}
| expr '*' expr {$$=new NBinaryOperator($1,"*",$3);}
| '-' expr %prec NEG {$$=new NUnaryOperator("-",$2);}
;
bool_expr : expr AND expr {$$=new NBinaryOperator($1,"and",$3);}
| TRUE {$$ = new NBool(true);}
| FALSE {$$ = new NBool(false);}
| expr OR expr {$$=new NBinaryOperator($1,"or",$3);}
| expr '<' expr {$$=new NBinaryOperator($1,"<",$3);}
| expr '>' expr {$$=new NBinaryOperator($1,">",$3);}
| expr GEQ expr {$$=new NBinaryOperator($1,">=",$3);}
| expr LEQ expr {$$=new NBinaryOperator($1,"<=",$3);}
| expr NEQ expr {$$=new NBinaryOperator($1,"!=",$3);}
| expr EQ expr {$$=new NBinaryOperator($1,"==",$3);}
| '!' expr %prec NOT {$$=new NUnaryOperator("not",$2);}
;
lrexpr : ident { if(Table.lookup($1->name)!=NULL){
$$=new NIdentifier(*$1);
}else{
fprintf(stderr,"var %s is not declared.\n",$1->name.c_str());
flagerror=1;
}
}
| lrexpr '[' expr ']' {$$=new NArrayAccess($1,$3);}
| lrexpr ACCESS ident {$$=new NStructAccess($1,*$3);}
/*| error ']' {}*/
;
fun_call_args : '(' ')' {$$= new ExpressionList();}
| '(' expr_lst ')' {$$=$2;}
;
cons_arr : '[' expr_lst ']' {$$ = new NArray(*$2);}
;
arr_lst : cons_arr {$$ = $1;}
| '['arr_lst ',' cons_arr']' {$$ = new NArray();
$$->add($4);
$$->add($2);}
| arr_lst error cons_arr {}
;
expr_lst : expr {$$=new ExpressionList();$$->push_back($1);}
| expr_lst ',' expr {$1->push_back($3);}
| expr_lst error expr {}
;
block : beg_block end_block {$$ = new NBlock();}
| beg_block stmts end_block {$$ =$2;}
;
beg_block : '{' {Table.begScope();}
;
end_block : '}' {Table.endScope();}
;
stmts : stmt {$$ = new NBlock();$$->statements.push_back($1);}
| stmts stmt {$$->statements.push_back($2);}
| error stmt { fprintf(stderr,
"Error in previous stament, l%d,c%d-l%d,c%d\n",
@2.first_line, @2.first_column,
@2.last_line, @2.last_column);
$$->statements.push_back($2);
flagerror=1;
}
;
stmt : ctrl_if
| ctrl_while
| ctrl_for
| block {$$=$<stmt>1;}
| var_asgn '.'
| varr_decl '.' {$$=$<stmt>1;}
| fun_call '.' {$$ = new NExpressionStatement($1);}
| RETURN '.' {$$ = new NReturn();}
| RETURN expr '.' {$$ = new NReturn($2);}
| STOP '.' {$$ = new NStop();}
| NEXT '.' {$$ = new NNext();}
| error '.' {}
;
fun_call : ident fun_call_args {$$ = new NFunctionCall(*$1,*$2);}
ctrl_if : IF expr THEN block {$$ = new NIf($2,*$4);}
| IF expr THEN block ELSE block {$$ = new NIf($2,*$4,$6);}
| IF expr THEN block ELSE ctrl_if {$$ = new NIf($2,*$4,$6);}
;
ctrl_while : WHILE expr DO block {$$ = new NWhileDo($2,*$4);}
| DO block WHILE expr '.' {$$ = new NDoWhile($4,*$2);}
;
ctrl_for : FOR ident FROM expr TO expr block {$$ = new NFor(*$2,$4,$6,*$7);}
| FOR ident FROM expr TO expr STEP expr block{$$ = new NFor(*$2,$4,$6,*$9,$8);}
| FOR ident IN ident block {$$ = new NFor(*$2,$4,*$5);}
| FOR ident IN cons_arr block {$$ = new NFor(*$2,$4,*$5);}
;
var_asgn : lrexpr '=' expr {$$ = new NAssignment($1,$3);}
| error '=' {}
;
%%
/* in code section at the end of the parser */
void yyerror(char const *s, ...){
va_list ap;
va_start(ap, s);
if(yylloc.first_line)
fprintf(stderr, "\nSyntax error in line %d", yylloc.first_line, yylloc.first_column,
yylloc.last_line, yylloc.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
flagerror = 1;
}
void lyyerror(YYLTYPE t, char const *s, ...){
va_list ap;
va_start(ap, s);
if(t.first_line)
fprintf(stderr, "\n%d.%d-%d.%d: error: ", t.first_line, t.first_column,
t.last_line, t.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
/*void yyerror(char *s){
printf("%d: %s at %s\n", yylineno, s, yytext);
}*/