Tabelas de simbolos locais comecando a funcionar.
[toypasc.git] / parser.y
blob06ceb606c00175e83dac5ec720d9e2f6f24f64a6
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 struct AstNode *ast_node;
136 struct AstNode *temp;
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 global_symbol_table = symbol_new(NULL);
146 ast_node->symbol = global_symbol_table;
147 for (temp = ast_node->children; temp != NULL; temp = temp->sibling)
148 ast_node_fill_symbol_table(temp, global_symbol_table);
150 //ast_node_print(ast_node);
151 ast_node_print_graph(ast_node);
152 //symbol_table_dump(ast_node->symbol);
156 ProgramDecl:
157 T_PROGRAM Identifier T_SEMICOLON
159 struct AstNode *ast_node;
160 ast_node = ast_node_new("ProgramDecl", PROG_DECL, NONE_TYPE,
161 yylloc.last_line, NULL);
162 ast_node_add_child(ast_node, $2);
163 $$ = ast_node;
167 VarDeclList:
168 /* empty */ { $$ = NULL; }
169 | MultiVarDecl
171 struct AstNode *ast_node;
172 ast_node = ast_node_new("VarDeclList", NONE_KIND, NONE_TYPE,
173 yylloc.last_line, NULL);
174 ast_node_add_child(ast_node, $1);
175 $$ = ast_node;
179 MultiVarDecl:
180 /* empty */ { $$ = NULL; }
181 | VarDecl MultiVarDecl
183 ast_node_add_sibling($1, $2);
184 $$ = $1;
188 VarDecl:
189 T_VAR IdentifierList T_COLON TYPE_IDENTIFIER T_SEMICOLON
191 struct AstNode *ast_node;
192 ast_node = ast_node_new("VarDecl", NONE_TYPE, $4,
193 yylloc.last_line, NULL);
194 ast_node_add_child(ast_node, $2);
195 ast_node_set_type($2, $4);
196 $$ = ast_node;
200 IdentifierList:
201 SingleIdentifier MultiIdentifier
203 struct AstNode *ast_node;
204 ast_node = ast_node_new("IdentifierList", IDENT_LIST, NONE_TYPE,
205 yylloc.last_line, NULL);
206 ast_node_add_sibling($1, $2);
207 ast_node_add_child(ast_node, $1);
208 $$ = ast_node;
212 MultiIdentifier:
213 /* empty */ { $$ = NULL; }
214 | T_COMMA SingleIdentifier MultiIdentifier
216 ast_node_add_sibling($2, $3);
217 $$ = $2;
221 SingleIdentifier:
222 Identifier { $$ = $1; }
226 ProcFuncList:
227 /* empty */ { $$ = NULL; }
228 | ProcFuncDecl MultiProcFuncDecl
230 struct AstNode *ast_node;
231 ast_node = ast_node_new("ProcFuncList", NONE_KIND, NONE_TYPE,
232 yylloc.last_line, NULL);
233 ast_node_add_sibling($1, $2);
234 ast_node_add_child(ast_node, $1);
235 $$ = ast_node;
239 MultiProcFuncDecl:
240 /* empty */ { $$ = NULL; }
241 | ProcFuncDecl MultiProcFuncDecl
243 ast_node_add_sibling($1, $2);
244 $$ = $1;
248 ProcFuncDecl:
249 ProcDecl { $$ = $1; }
250 | FuncDecl { $$ = $1; }
253 ProcDecl:
254 T_PROCEDURE Identifier T_LPAR ParamList T_RPAR T_SEMICOLON VarDeclList
255 CodeBlock T_SEMICOLON
257 Symbol *symtab;
258 struct AstNode *ast_node;
259 struct AstNode *temp;
261 ast_node = ast_node_new("ProcDecl", PROCEDURE, NONE_TYPE,
262 yylloc.last_line, NULL);
263 ast_node_add_child(ast_node, $2); // Identifier
264 ast_node_add_child(ast_node, $4); // ParamList
265 ast_node_add_child(ast_node, $7); // VarDeclList
266 ast_node_add_child(ast_node, $8); // CodeBlock
268 symtab = symbol_new(NULL);
269 ast_node->symbol = symtab;
270 for (temp = ast_node->children; temp != NULL; temp = temp->sibling)
271 ast_node_fill_symbol_table(temp, symtab);
273 $$ = ast_node;
277 FuncDecl:
278 T_FUNCTION Identifier T_LPAR ParamList T_RPAR T_COLON TYPE_IDENTIFIER
279 T_SEMICOLON VarDeclList CodeBlock T_SEMICOLON
281 Symbol *symtab;
282 struct AstNode *ast_node;
283 struct AstNode *temp;
285 ast_node = ast_node_new("FuncDecl", FUNCTION, $7,
286 yylloc.last_line, NULL);
287 ast_node_add_child(ast_node, $2); // Identifier
288 ast_node_add_child(ast_node, $4); // ParamList
289 ast_node_add_child(ast_node, $9); // VarDeclList
290 ast_node_add_child(ast_node, $10); // CodeBlock
292 ast_node_set_type($2, $7);
294 symtab = symbol_new(NULL);
295 ast_node->symbol = symtab;
296 for (temp = ast_node->children; temp != NULL; temp = temp->sibling)
297 ast_node_fill_symbol_table(temp, symtab);
299 $$ = ast_node;
303 ParamList:
304 /* empty */ { $$ = NULL; }
305 | SingleParam MultiParam
307 struct AstNode *ast_node;
308 ast_node = ast_node_new("ParamList", NONE_KIND, NONE_TYPE,
309 yylloc.last_line, NULL);
310 ast_node_add_sibling($1, $2);
311 ast_node_add_child(ast_node, $1);
312 $$ = ast_node;
316 MultiParam:
317 /* empty */ { $$ = NULL; }
318 | T_COMMA SingleParam MultiParam
320 ast_node_add_sibling($2, $3);
321 $$ = $2;
325 SingleParam:
326 Identifier T_COLON TYPE_IDENTIFIER
328 struct AstNode *ast_node;
329 ast_node = ast_node_new("SingleParam", NONE_KIND, NONE_TYPE,
330 yylloc.last_line, NULL);
331 ast_node_add_child(ast_node, $1); // Identifier
332 ast_node_set_type(ast_node, $3);
333 $$ = ast_node;
337 MainCodeBlock:
338 /* empty */ { $$ = NULL; }
339 | CodeBlock T_DOT { $$ = $1; }
342 CodeBlock:
343 T_BEGIN StatementList T_END { $$ = $2; }
346 StatementList:
347 Statement MultiStatement
349 struct AstNode *ast_node;
350 ast_node = ast_node_new("StatementList", NONE_KIND, NONE_TYPE,
351 yylloc.last_line, NULL);
352 ast_node_add_sibling($1, $2);
353 ast_node_add_child(ast_node, $1);
354 $$ = ast_node;
358 MultiStatement:
359 /* empty */ { $$ = NULL; }
360 | Statement MultiStatement
362 ast_node_add_sibling($1, $2);
363 $$ = $1;
367 Statement:
368 Assignment T_SEMICOLON { $$ = $1; }
369 | IfStatement { $$ = $1; }
370 | WhileStatement { $$ = $1; }
371 | ForStatement { $$ = $1; }
372 | Call { $$ = $1; }
373 | PrintStatement { $$ = $1; }
376 PrintStatement:
377 PrintIntStatement { $$ = $1; }
378 | PrintCharStatement { $$ = $1; }
379 | PrintBoolStatement { $$ = $1; }
380 | PrintLineStatement { $$ = $1; }
383 PrintIntStatement:
384 T_PRINT_INT T_LPAR Expression T_RPAR
386 struct AstNode *ast_node;
387 ast_node = ast_node_new("PrintIntStatement", NONE_KIND, NONE_TYPE,
388 yylloc.last_line, NULL);
389 ast_node_add_child(ast_node, $3);
390 $$ = ast_node;
394 PrintCharStatement:
395 T_PRINT_CHAR T_LPAR Expression T_RPAR
397 struct AstNode *ast_node;
398 ast_node = ast_node_new("PrintCharStatement", NONE_KIND, NONE_TYPE,
399 yylloc.last_line, NULL);
400 ast_node_add_child(ast_node, $3);
401 $$ = ast_node;
405 PrintBoolStatement:
406 T_PRINT_BOOL T_LPAR Expression T_RPAR
408 struct AstNode *ast_node;
409 ast_node = ast_node_new("PrintBoolStatement", NONE_KIND, NONE_TYPE,
410 yylloc.last_line, NULL);
411 ast_node_add_child(ast_node, $3);
412 $$ = ast_node;
416 PrintLineStatement:
417 T_PRINT_LINE T_LPAR Expression T_RPAR
419 struct AstNode *ast_node;
420 ast_node = ast_node_new("PrintLineStatement", NONE_KIND, NONE_TYPE,
421 yylloc.last_line, NULL);
422 ast_node_add_child(ast_node, $3);
423 $$ = ast_node;
427 Assignment:
428 Identifier T_ASSIGNMENT Expression
430 struct AstNode *ast_node;
431 ast_node = ast_node_new("Assignment", NONE_KIND, NONE_TYPE,
432 yylloc.last_line, NULL);
433 ast_node_add_child(ast_node, $1);
434 ast_node_add_child(ast_node, $3);
435 $$ = ast_node;
439 IfStatement:
440 T_IF Expression T_THEN CodeBlock ElseStatement
442 struct AstNode *ast_node;
443 ast_node = ast_node_new("IfStatement", NONE_KIND, NONE_TYPE,
444 yylloc.last_line, NULL);
445 ast_node_add_child(ast_node, $2);
446 ast_node_add_child(ast_node, $4);
447 ast_node_add_child(ast_node, $5);
448 $$ = ast_node;
452 ElseStatement:
453 /* empty */ { $$ = NULL; }
454 | T_ELSE CodeBlock { $$ = $2; }
457 WhileStatement:
458 T_WHILE T_LPAR Expression T_RPAR T_DO CodeBlock
460 struct AstNode *ast_node;
461 ast_node = ast_node_new("WhileStatement", NONE_KIND, NONE_TYPE,
462 yylloc.last_line, NULL);
463 ast_node_add_child(ast_node, $3);
464 ast_node_add_child(ast_node, $6);
465 $$ = ast_node;
469 ForStatement:
470 T_FOR Assignment T_TO Expression T_DO CodeBlock
472 struct AstNode *ast_node;
473 ast_node = ast_node_new("ForStatement", NONE_KIND, NONE_TYPE,
474 yylloc.last_line, NULL);
475 ast_node_add_child(ast_node, $2);
476 ast_node_add_child(ast_node, $4);
477 ast_node_add_child(ast_node, $6);
478 $$ = ast_node;
482 Expression:
483 SimpleExpression { $$ = $1; }
484 | SimpleExpression RelOp SimpleExpression
486 struct AstNode *ast_node;
487 ast_node = ast_node_new("RelExpression", NONE_KIND, NONE_TYPE,
488 yylloc.last_line, NULL);
489 ast_node_add_child(ast_node, $1);
490 ast_node_add_child(ast_node, $2);
491 ast_node_add_child(ast_node, $3);
492 $$ = ast_node;
496 SimpleExpression:
497 Term { $$ = $1; }
498 | SimpleExpression AddOp Term
500 struct AstNode *ast_node;
501 ast_node = ast_node_new("AddExpression", NONE_KIND, NONE_TYPE,
502 yylloc.last_line, NULL);
503 ast_node_add_child(ast_node, $1);
504 ast_node_add_child(ast_node, $2);
505 ast_node_add_child(ast_node, $3);
506 $$ = ast_node;
510 Term:
511 NotFactor { $$ = $1; }
512 | Term MulOp NotFactor
514 struct AstNode *ast_node;
515 ast_node = ast_node_new("MulExpression", NONE_KIND, NONE_TYPE,
516 yylloc.last_line, NULL);
517 ast_node_add_child(ast_node, $1);
518 ast_node_add_child(ast_node, $2);
519 ast_node_add_child(ast_node, $3);
520 $$ = ast_node;
524 NotFactor:
525 Factor { $$ = $1; }
526 | NotOp Factor
528 struct AstNode *ast_node;
529 struct AstNode *op_ast_node;
530 ast_node = ast_node_new("NotFactor", NONE_KIND, NONE_TYPE,
531 yylloc.last_line, NULL);
532 ast_node_add_child(ast_node, $1);
533 ast_node_add_child(ast_node, $2);
534 $$ = ast_node;
538 Factor:
539 Identifier { $$ = $1; }
540 | Literal { $$ = $1; }
541 | Call { $$ = $1; }
542 | T_LPAR Expression T_RPAR { $$ = $2; }
545 Call:
546 Identifier T_LPAR CallParamList T_RPAR
548 struct AstNode *ast_node;
549 ast_node = ast_node_new("Call", NONE_KIND, NONE_TYPE,
550 yylloc.last_line, NULL);
551 ast_node_add_child(ast_node, $1);
552 ast_node_add_child(ast_node, $3);
553 $$ = ast_node;
557 CallParamList:
558 /* empty */ { $$ = NULL; }
559 | Expression MultiCallParam
561 struct AstNode *ast_node;
562 ast_node = ast_node_new("CallParamList", NONE_KIND, NONE_TYPE,
563 yylloc.last_line, NULL);
564 ast_node_add_sibling($1, $2);
565 ast_node_add_child(ast_node, $1);
566 $$ = ast_node;
570 MultiCallParam:
571 /* empty */ { $$ = NULL; }
572 | T_COMMA Expression MultiCallParam
574 ast_node_add_sibling($2, $3);
575 $$ = $2;
579 AddOp:
580 T_PLUS
582 struct AstNode *ast_node;
583 ast_node = ast_node_new($1, T_PLUS, NONE_TYPE,
584 yylloc.last_line, NULL);
585 $$ = ast_node;
587 | T_MINUS
589 struct AstNode *ast_node;
590 ast_node = ast_node_new($1, T_MINUS, NONE_TYPE,
591 yylloc.last_line, NULL);
592 $$ = ast_node;
594 | T_OR
596 struct AstNode *ast_node;
597 ast_node = ast_node_new($1, T_OR, NONE_TYPE,
598 yylloc.last_line, NULL);
599 $$ = ast_node;
603 MulOp:
604 T_STAR
606 struct AstNode *ast_node;
607 ast_node = ast_node_new($1, T_STAR, NONE_TYPE,
608 yylloc.last_line, NULL);
609 $$ = ast_node;
611 | T_SLASH
613 struct AstNode *ast_node;
614 ast_node = ast_node_new($1, T_SLASH, NONE_TYPE,
615 yylloc.last_line, NULL);
616 $$ = ast_node;
618 | T_AND
620 struct AstNode *ast_node;
621 ast_node = ast_node_new($1, T_AND, NONE_TYPE,
622 yylloc.last_line, NULL);
623 $$ = ast_node;
627 RelOp:
628 T_LESSER
630 struct AstNode *ast_node;
631 ast_node = ast_node_new($1, T_LESSER, NONE_TYPE,
632 yylloc.last_line, NULL);
633 $$ = ast_node;
635 | T_LESSEREQUAL
637 struct AstNode *ast_node;
638 ast_node = ast_node_new($1, T_LESSEREQUAL, NONE_TYPE,
639 yylloc.last_line, NULL);
640 $$ = ast_node;
642 | T_GREATER
644 struct AstNode *ast_node;
645 ast_node = ast_node_new($1, T_GREATER, NONE_TYPE,
646 yylloc.last_line, NULL);
647 $$ = ast_node;
649 | T_GREATEREQUAL
651 struct AstNode *ast_node;
652 ast_node = ast_node_new($1, T_GREATEREQUAL, NONE_TYPE,
653 yylloc.last_line, NULL);
654 $$ = ast_node;
656 | T_EQUAL
658 struct AstNode *ast_node;
659 ast_node = ast_node_new($1, T_EQUAL, NONE_TYPE,
660 yylloc.last_line, NULL);
661 $$ = ast_node;
663 | T_NOTEQUAL
665 struct AstNode *ast_node;
666 ast_node = ast_node_new($1, T_NOTEQUAL, NONE_TYPE,
667 yylloc.last_line, NULL);
668 $$ = ast_node;
672 NotOp:
673 T_NOT
675 struct AstNode *ast_node;
676 ast_node = ast_node_new($1, T_NOT, NONE_TYPE,
677 yylloc.last_line, NULL);
678 $$ = ast_node;
682 Identifier:
683 IDENTIFIER
685 Symbol *symbol;
686 struct AstNode *ast_node;
688 ast_node = ast_node_new("Identifier", IDENTIFIER, NONE_TYPE,
689 yylloc.last_line, NULL);
690 ast_node->symbol = symbol_new($1);
691 $$ = ast_node;
695 Literal:
696 INT_LITERAL
698 struct AstNode *ast_node;
699 ast_node = ast_node_new("IntLiteral", INT_LITERAL, INTEGER,
700 yylloc.last_line, NULL);
701 value_set_from_int(&ast_node->value, $1);
702 $$ = ast_node;
704 | BOOL_LITERAL
706 struct AstNode *ast_node;
707 ast_node = ast_node_new("BoolLiteral", BOOL_LITERAL, BOOLEAN,
708 yylloc.last_line, NULL);
709 value_set_from_bool(&ast_node->value, $1);
710 $$ = ast_node;
712 | CHAR_LITERAL
714 struct AstNode *ast_node;
715 ast_node = ast_node_new("CharLiteral", CHAR_LITERAL, CHAR,
716 yylloc.last_line, NULL);
717 value_set_from_char(&ast_node->value, $1);
718 $$ = ast_node;
724 static void
725 yyerror (/*YYLTYPE *locp,*/ const char *msg)
727 fprintf(stderr, "[Error] line %d: %s\n", yyget_lineno(), msg);
731 main(int argc, char **argv)
733 if (argc > 1)
734 yyin = fopen(argv[1], "r");
735 else
736 yyin = stdin;
738 /*yylloc.first_line = yylloc.last_line = 1;
739 yylloc.first_column = yylloc.last_column = 0;*/
741 return yyparse();