Grafo da AST bem melhor. Loopando infinitamente ao montar tabela de simbolos.
[toypasc.git] / parser.y
blob1747114ca757f2c41f428c025bc09d487aa870c2
1 %{
2 #include <stdio.h>
3 #include <string.h>
4 #include "base.h"
5 #include "parser.h"
6 #include "ast.h"
7 #include "symbol_table.h"
9 /*extern char *yytext;*/
10 extern FILE *yyin;
12 static void yyerror (/*YYLTYPE *locp, */const char *msg);
13 /*int yylex (YYSTYPE *yylval_param, YYLTYPE *yylloc_param);*/
16 %defines
17 %locations
18 %pure-parser
19 %error-verbose
20 /*%parse-param {ValaParser *parser}
21 %lex-param {ValaParser *parser}*/
23 %union {
24 char* lexeme;
25 int integer;
26 int boolean;
27 char character;
28 int type;
29 struct AstNode *astnode;
32 /* Tokens */
33 %left <lexeme> T_OR
34 %left <lexeme> T_AND
35 %left <lexeme> T_EQUAL T_NOTEQUAL
36 %left <lexeme> T_LESSER T_GREATER T_LESSEREQUAL T_GREATEREQUAL
37 %left <lexeme> T_PLUS T_MINUS
38 %left <lexeme> T_STAR T_SLASH
39 %left <lexeme> T_NOT
41 %token T_PROGRAM
42 %token T_VAR
43 %token T_PROCEDURE
44 %token T_FUNCTION
45 %token T_BEGIN
46 %token T_END
48 %token T_IF
49 %token T_THEN
50 %token T_ELSE
51 %token T_WHILE
52 %token T_FOR
53 %token T_TO
54 %token T_DO
56 %token T_ASSIGNMENT
58 %token T_LPAR
59 %token T_RPAR
60 %token T_SEMICOLON
61 %token T_COLON
62 %token T_COMMA
63 %token T_DOT
65 %token T_PRINT_INT
66 %token T_PRINT_CHAR
67 %token T_PRINT_BOOL
68 %token T_PRINT_LINE
70 %token <type> TYPE_IDENTIFIER
71 %token <lexeme> IDENTIFIER
72 %token <integer> INT_LITERAL
73 %token <boolean> BOOL_LITERAL
74 %token <character> CHAR_LITERAL
76 /* Tipos das Regras */
77 %type <astnode> Program
78 %type <astnode> ProgramDecl
79 %type <astnode> VarDeclList
80 %type <astnode> MultiVarDecl
81 %type <astnode> VarDecl
82 %type <astnode> IdentifierList
83 %type <astnode> MultiIdentifier
84 %type <astnode> SingleIdentifier
86 %type <astnode> ProcFuncList
87 %type <astnode> MultiProcFuncDecl
88 %type <astnode> ProcFuncDecl
89 %type <astnode> ProcDecl
90 %type <astnode> FuncDecl
91 %type <astnode> ParamList
92 %type <astnode> SingleParam
93 %type <astnode> MultiParam
95 %type <astnode> MainCodeBlock
96 %type <astnode> CodeBlock
97 %type <astnode> StatementList
98 %type <astnode> MultiStatement
99 %type <astnode> Statement
100 %type <astnode> IfStatement
101 %type <astnode> ElseStatement
102 %type <astnode> WhileStatement
103 %type <astnode> ForStatement
104 %type <astnode> PrintStatement
105 %type <astnode> PrintCharStatement
106 %type <astnode> PrintIntStatement
107 %type <astnode> PrintBoolStatement
108 %type <astnode> PrintLineStatement
110 %type <astnode> Expression
111 %type <astnode> SimpleExpression
112 %type <astnode> NotFactor
113 %type <astnode> Factor
114 %type <astnode> Term
116 %type <astnode> Call
117 %type <astnode> CallParamList
118 %type <astnode> MultiCallParam
120 %type <astnode> Assignment
121 %type <astnode> Identifier
122 %type <astnode> Literal
124 %type <astnode> AddOp
125 %type <astnode> MulOp
126 %type <astnode> RelOp
127 %type <astnode> NotOp
129 %start Program
132 Program:
133 ProgramDecl VarDeclList ProcFuncList MainCodeBlock
135 Symbol *symtab;
136 struct AstNode *ast_node;
137 ast_node = ast_node_new("Program", PROGRAM, NONE_TYPE,
138 yylloc.last_line, NULL);
139 ast_node_add_child(ast_node, $1);
140 ast_node_add_child(ast_node, $2);
141 ast_node_add_child(ast_node, $3);
142 ast_node_add_child(ast_node, $4);
143 $$ = ast_node;
145 symtab = symbol_new(NULL);
146 ast_node->symbol = symtab;
147 ast_node_fill_symbol_table(ast_node, symtab);
149 //ast_node_print(ast_node);
150 //ast_node_print_graph(ast_node, ast_node->symbol);
151 //symbol_table_dump(ast_node->symbol);
155 ProgramDecl:
156 T_PROGRAM Identifier T_SEMICOLON
158 struct AstNode *ast_node;
159 ast_node = ast_node_new("ProgramDecl", NONE_KIND, NONE_TYPE,
160 yylloc.last_line, NULL);
161 ast_node_add_child(ast_node, $2);
162 $$ = ast_node;
166 VarDeclList:
167 /* empty */ { $$ = NULL; }
168 | MultiVarDecl
170 struct AstNode *ast_node;
171 ast_node = ast_node_new("VarDeclList", NONE_KIND, NONE_TYPE,
172 yylloc.last_line, NULL);
173 ast_node_add_child(ast_node, $1);
174 $$ = ast_node;
178 MultiVarDecl:
179 /* empty */ { $$ = NULL; }
180 | VarDecl MultiVarDecl
182 ast_node_add_sibling($1, $2);
183 $$ = $1;
187 VarDecl:
188 T_VAR IdentifierList T_COLON TYPE_IDENTIFIER T_SEMICOLON
190 struct AstNode *ast_node;
191 ast_node = ast_node_new("VarDecl", NONE_TYPE, $4,
192 yylloc.last_line, NULL);
193 ast_node_add_child(ast_node, $2);
194 ast_node_set_type($2, $4);
195 $$ = ast_node;
199 IdentifierList:
200 SingleIdentifier MultiIdentifier
202 struct AstNode *ast_node;
203 ast_node = ast_node_new("IdentifierList", NONE_KIND, NONE_TYPE,
204 yylloc.last_line, NULL);
205 ast_node_add_sibling($1, $2);
206 ast_node_add_child(ast_node, $1);
207 $$ = ast_node;
211 MultiIdentifier:
212 /* empty */ { $$ = NULL; }
213 | T_COMMA SingleIdentifier MultiIdentifier
215 ast_node_add_sibling($2, $3);
216 $$ = $2;
220 SingleIdentifier:
221 Identifier { $$ = $1; }
225 ProcFuncList:
226 /* empty */ { $$ = NULL; }
227 | ProcFuncDecl MultiProcFuncDecl
229 struct AstNode *ast_node;
230 ast_node = ast_node_new("ProcFuncList", NONE_KIND, NONE_TYPE,
231 yylloc.last_line, NULL);
232 ast_node_add_sibling($1, $2);
233 ast_node_add_child(ast_node, $1);
234 $$ = ast_node;
238 MultiProcFuncDecl:
239 /* empty */ { $$ = NULL; }
240 | ProcFuncDecl MultiProcFuncDecl
242 ast_node_add_sibling($1, $2);
243 $$ = $1;
247 ProcFuncDecl:
248 ProcDecl { $$ = $1; }
249 | FuncDecl { $$ = $1; }
252 ProcDecl:
253 T_PROCEDURE Identifier T_LPAR ParamList T_RPAR T_SEMICOLON VarDeclList
254 CodeBlock T_SEMICOLON
256 struct AstNode *ast_node;
257 ast_node = ast_node_new("ProcDecl", PROCEDURE, NONE_TYPE,
258 yylloc.last_line, NULL);
259 ast_node_add_child(ast_node, $2); // Identifier
260 ast_node_add_child(ast_node, $4); // ParamList
261 ast_node_add_child(ast_node, $7); // VarDeclList
262 ast_node_add_child(ast_node, $8); // CodeBlock
263 $$ = ast_node;
267 FuncDecl:
268 T_FUNCTION Identifier T_LPAR ParamList T_RPAR T_COLON TYPE_IDENTIFIER
269 T_SEMICOLON VarDeclList CodeBlock T_SEMICOLON
271 struct AstNode *ast_node;
272 ast_node = ast_node_new("FuncDecl", FUNCTION, $7,
273 yylloc.last_line, NULL);
274 ast_node_add_child(ast_node, $2); // Identifier
275 ast_node_add_child(ast_node, $4); // ParamList
276 ast_node_add_child(ast_node, $9); // VarDeclList
277 ast_node_add_child(ast_node, $10); // CodeBlock
279 ast_node_set_type($2, $7);
281 $$ = ast_node;
285 ParamList:
286 /* empty */ { $$ = NULL; }
287 | SingleParam MultiParam
289 struct AstNode *ast_node;
290 ast_node = ast_node_new("ParamList", NONE_KIND, NONE_TYPE,
291 yylloc.last_line, NULL);
292 ast_node_add_sibling($1, $2);
293 ast_node_add_child(ast_node, $1);
294 $$ = ast_node;
298 MultiParam:
299 /* empty */ { $$ = NULL; }
300 | T_COMMA SingleParam MultiParam
302 ast_node_add_sibling($2, $3);
303 $$ = $2;
307 SingleParam:
308 Identifier T_COLON TYPE_IDENTIFIER
310 struct AstNode *ast_node;
311 ast_node = ast_node_new("SingleParam", NONE_KIND, NONE_TYPE,
312 yylloc.last_line, NULL);
313 ast_node_add_child(ast_node, $1); // Identifier
314 ast_node_set_type(ast_node, $3);
315 $$ = ast_node;
319 MainCodeBlock:
320 /* empty */ { $$ = NULL; }
321 | CodeBlock T_DOT { $$ = $1; }
324 CodeBlock:
325 T_BEGIN StatementList T_END { $$ = $2; }
328 StatementList:
329 Statement MultiStatement
331 struct AstNode *ast_node;
332 ast_node = ast_node_new("StatementList", NONE_KIND, NONE_TYPE,
333 yylloc.last_line, NULL);
334 ast_node_add_sibling($1, $2);
335 ast_node_add_child(ast_node, $1);
336 $$ = ast_node;
340 MultiStatement:
341 /* empty */ { $$ = NULL; }
342 | Statement MultiStatement
344 ast_node_add_sibling($1, $2);
345 $$ = $1;
349 Statement:
350 Assignment T_SEMICOLON { $$ = $1; }
351 | IfStatement { $$ = $1; }
352 | WhileStatement { $$ = $1; }
353 | ForStatement { $$ = $1; }
354 | Call { $$ = $1; }
355 | PrintStatement { $$ = $1; }
358 PrintStatement:
359 PrintIntStatement { $$ = $1; }
360 | PrintCharStatement { $$ = $1; }
361 | PrintBoolStatement { $$ = $1; }
362 | PrintLineStatement { $$ = $1; }
365 PrintIntStatement:
366 T_PRINT_INT T_LPAR Expression T_RPAR
368 struct AstNode *ast_node;
369 ast_node = ast_node_new("PrintIntStatement", NONE_KIND, NONE_TYPE,
370 yylloc.last_line, NULL);
371 ast_node_add_child(ast_node, $3);
372 $$ = ast_node;
376 PrintCharStatement:
377 T_PRINT_CHAR T_LPAR Expression T_RPAR
379 struct AstNode *ast_node;
380 ast_node = ast_node_new("PrintCharStatement", NONE_KIND, NONE_TYPE,
381 yylloc.last_line, NULL);
382 ast_node_add_child(ast_node, $3);
383 $$ = ast_node;
387 PrintBoolStatement:
388 T_PRINT_BOOL T_LPAR Expression T_RPAR
390 struct AstNode *ast_node;
391 ast_node = ast_node_new("PrintBoolStatement", NONE_KIND, NONE_TYPE,
392 yylloc.last_line, NULL);
393 ast_node_add_child(ast_node, $3);
394 $$ = ast_node;
398 PrintLineStatement:
399 T_PRINT_LINE T_LPAR Expression T_RPAR
401 struct AstNode *ast_node;
402 ast_node = ast_node_new("PrintLineStatement", NONE_KIND, NONE_TYPE,
403 yylloc.last_line, NULL);
404 ast_node_add_child(ast_node, $3);
405 $$ = ast_node;
409 Assignment:
410 Identifier T_ASSIGNMENT Expression
412 struct AstNode *ast_node;
413 ast_node = ast_node_new("Assignment", NONE_KIND, NONE_TYPE,
414 yylloc.last_line, NULL);
415 ast_node_add_child(ast_node, $1);
416 ast_node_add_child(ast_node, $3);
417 $$ = ast_node;
421 IfStatement:
422 T_IF Expression T_THEN CodeBlock ElseStatement
424 struct AstNode *ast_node;
425 ast_node = ast_node_new("IfStatement", NONE_KIND, NONE_TYPE,
426 yylloc.last_line, NULL);
427 ast_node_add_child(ast_node, $2);
428 ast_node_add_child(ast_node, $4);
429 ast_node_add_child(ast_node, $5);
430 $$ = ast_node;
434 ElseStatement:
435 /* empty */ { $$ = NULL; }
436 | T_ELSE CodeBlock { $$ = $2; }
439 WhileStatement:
440 T_WHILE T_LPAR Expression T_RPAR T_DO CodeBlock
442 struct AstNode *ast_node;
443 ast_node = ast_node_new("WhileStatement", NONE_KIND, NONE_TYPE,
444 yylloc.last_line, NULL);
445 ast_node_add_child(ast_node, $3);
446 ast_node_add_child(ast_node, $6);
447 $$ = ast_node;
451 ForStatement:
452 T_FOR Assignment T_TO Expression T_DO CodeBlock
454 struct AstNode *ast_node;
455 ast_node = ast_node_new("ForStatement", NONE_KIND, NONE_TYPE,
456 yylloc.last_line, NULL);
457 ast_node_add_child(ast_node, $2);
458 ast_node_add_child(ast_node, $4);
459 ast_node_add_child(ast_node, $6);
460 $$ = ast_node;
464 Expression:
465 SimpleExpression { $$ = $1; }
466 | SimpleExpression RelOp SimpleExpression
468 struct AstNode *ast_node;
469 ast_node = ast_node_new("RelExpression", NONE_KIND, NONE_TYPE,
470 yylloc.last_line, NULL);
471 ast_node_add_child(ast_node, $1);
472 ast_node_add_child(ast_node, $2);
473 ast_node_add_child(ast_node, $3);
474 $$ = ast_node;
478 SimpleExpression:
479 Term { $$ = $1; }
480 | SimpleExpression AddOp Term
482 struct AstNode *ast_node;
483 ast_node = ast_node_new("AddExpression", NONE_KIND, NONE_TYPE,
484 yylloc.last_line, NULL);
485 ast_node_add_child(ast_node, $1);
486 ast_node_add_child(ast_node, $2);
487 ast_node_add_child(ast_node, $3);
488 $$ = ast_node;
492 Term:
493 NotFactor { $$ = $1; }
494 | Term MulOp NotFactor
496 struct AstNode *ast_node;
497 ast_node = ast_node_new("MulExpression", NONE_KIND, NONE_TYPE,
498 yylloc.last_line, NULL);
499 ast_node_add_child(ast_node, $1);
500 ast_node_add_child(ast_node, $2);
501 ast_node_add_child(ast_node, $3);
502 $$ = ast_node;
506 NotFactor:
507 Factor { $$ = $1; }
508 | NotOp Factor
510 struct AstNode *ast_node;
511 struct AstNode *op_ast_node;
512 ast_node = ast_node_new("NotFactor", NONE_KIND, NONE_TYPE,
513 yylloc.last_line, NULL);
514 ast_node_add_child(ast_node, $1);
515 ast_node_add_child(ast_node, $2);
516 $$ = ast_node;
520 Factor:
521 Identifier { $$ = $1; }
522 | Literal { $$ = $1; }
523 | Call { $$ = $1; }
524 | T_LPAR Expression T_RPAR { $$ = $2; }
527 Call:
528 Identifier T_LPAR CallParamList T_RPAR
530 struct AstNode *ast_node;
531 ast_node = ast_node_new("Call", NONE_KIND, NONE_TYPE,
532 yylloc.last_line, NULL);
533 ast_node_add_child(ast_node, $1);
534 ast_node_add_child(ast_node, $3);
535 $$ = ast_node;
539 CallParamList:
540 /* empty */ { $$ = NULL; }
541 | Expression MultiCallParam
543 struct AstNode *ast_node;
544 ast_node = ast_node_new("CallParamList", NONE_KIND, NONE_TYPE,
545 yylloc.last_line, NULL);
546 ast_node_add_sibling($1, $2);
547 ast_node_add_child(ast_node, $1);
548 $$ = ast_node;
552 MultiCallParam:
553 /* empty */ { $$ = NULL; }
554 | T_COMMA Expression MultiCallParam
556 ast_node_add_sibling($2, $3);
557 $$ = $2;
561 AddOp:
562 T_PLUS
564 struct AstNode *ast_node;
565 ast_node = ast_node_new($1, T_PLUS, NONE_TYPE,
566 yylloc.last_line, NULL);
567 $$ = ast_node;
569 | T_MINUS
571 struct AstNode *ast_node;
572 ast_node = ast_node_new($1, T_MINUS, NONE_TYPE,
573 yylloc.last_line, NULL);
574 $$ = ast_node;
576 | T_OR
578 struct AstNode *ast_node;
579 ast_node = ast_node_new($1, T_OR, NONE_TYPE,
580 yylloc.last_line, NULL);
581 $$ = ast_node;
585 MulOp:
586 T_STAR
588 struct AstNode *ast_node;
589 ast_node = ast_node_new($1, T_STAR, NONE_TYPE,
590 yylloc.last_line, NULL);
591 $$ = ast_node;
593 | T_SLASH
595 struct AstNode *ast_node;
596 ast_node = ast_node_new($1, T_SLASH, NONE_TYPE,
597 yylloc.last_line, NULL);
598 $$ = ast_node;
600 | T_AND
602 struct AstNode *ast_node;
603 ast_node = ast_node_new($1, T_AND, NONE_TYPE,
604 yylloc.last_line, NULL);
605 $$ = ast_node;
609 RelOp:
610 T_LESSER
612 struct AstNode *ast_node;
613 ast_node = ast_node_new($1, T_LESSER, NONE_TYPE,
614 yylloc.last_line, NULL);
615 $$ = ast_node;
617 | T_LESSEREQUAL
619 struct AstNode *ast_node;
620 ast_node = ast_node_new($1, T_LESSEREQUAL, NONE_TYPE,
621 yylloc.last_line, NULL);
622 $$ = ast_node;
624 | T_GREATER
626 struct AstNode *ast_node;
627 ast_node = ast_node_new($1, T_GREATER, NONE_TYPE,
628 yylloc.last_line, NULL);
629 $$ = ast_node;
631 | T_GREATEREQUAL
633 struct AstNode *ast_node;
634 ast_node = ast_node_new($1, T_GREATEREQUAL, NONE_TYPE,
635 yylloc.last_line, NULL);
636 $$ = ast_node;
638 | T_EQUAL
640 struct AstNode *ast_node;
641 ast_node = ast_node_new($1, T_EQUAL, NONE_TYPE,
642 yylloc.last_line, NULL);
643 $$ = ast_node;
645 | T_NOTEQUAL
647 struct AstNode *ast_node;
648 ast_node = ast_node_new($1, T_NOTEQUAL, NONE_TYPE,
649 yylloc.last_line, NULL);
650 $$ = ast_node;
654 NotOp:
655 T_NOT
657 struct AstNode *ast_node;
658 ast_node = ast_node_new($1, T_NOT, NONE_TYPE,
659 yylloc.last_line, NULL);
660 $$ = ast_node;
664 Identifier:
665 IDENTIFIER
667 Symbol *symbol;
668 struct AstNode *ast_node;
670 ast_node = ast_node_new("Identifier", IDENTIFIER, NONE_TYPE,
671 yylloc.last_line, NULL);
672 ast_node->symbol = symbol_new($1);
673 $$ = ast_node;
677 Literal:
678 INT_LITERAL
680 struct AstNode *ast_node;
681 ast_node = ast_node_new("IntLiteral", INT_LITERAL, INTEGER,
682 yylloc.last_line, NULL);
683 value_set_from_int(&ast_node->value, $1);
684 $$ = ast_node;
686 | BOOL_LITERAL
688 struct AstNode *ast_node;
689 ast_node = ast_node_new("BoolLiteral", BOOL_LITERAL, BOOLEAN,
690 yylloc.last_line, NULL);
691 value_set_from_bool(&ast_node->value, $1);
692 $$ = ast_node;
694 | CHAR_LITERAL
696 struct AstNode *ast_node;
697 ast_node = ast_node_new("CharLiteral", CHAR_LITERAL, CHAR,
698 yylloc.last_line, NULL);
699 value_set_from_char(&ast_node->value, $1);
700 $$ = ast_node;
706 static void
707 yyerror (/*YYLTYPE *locp,*/ const char *msg)
709 fprintf(stderr, "[Error] line %d: %s\n", yyget_lineno(), msg);
713 main(int argc, char **argv)
715 if (argc > 1)
716 yyin = fopen(argv[1], "r");
717 else
718 yyin = stdin;
720 /*yylloc.first_line = yylloc.last_line = 1;
721 yylloc.first_column = yylloc.last_column = 0;*/
723 return yyparse();