Implementacao dos prints
[toypasc.git] / parser.y
blobf5ccf13491c83759a1dc0651c0b768efc0718052
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 T_OR
34 %left T_AND
35 %left T_EQUAL T_NOTEQUAL
36 %left T_LESSER T_GREATER T_LESSEREQUAL T_GREATEREQUAL
37 %left T_PLUS T_MINUS
38 %left T_STAR T_SLASH
39 %left T_NOT
41 %token T_VAR
42 %token T_PROCEDURE
43 %token T_FUNCTION
44 %token T_BEGIN
45 %token T_END
47 %token T_IF
48 %token T_THEN
49 %token T_ELSE
50 %token T_WHILE
51 %token T_FOR
52 %token T_TO
53 %token T_DO
55 %token T_ASSIGNMENT
57 %token T_LPAR
58 %token T_RPAR
59 %token T_SEMICOLON
60 %token T_COLON
61 %token T_COMMA
62 %token T_DOT
64 %token T_PRINT_INT
65 %token T_PRINT_CHAR
66 %token T_PRINT_BOOL
67 %token T_PRINT_LINE
69 %token <type> TYPE_IDENTIFIER
70 %token <lexeme> IDENTIFIER
71 %token <integer> INT_LITERAL
72 %token <boolean> BOOL_LITERAL
73 %token <character> CHAR_LITERAL
75 /* Tipos das Regras */
76 %type <astnode> Program
78 %type <astnode> VarDeclList
79 %type <astnode> MultiVarDecl
80 %type <astnode> VarDecl
81 %type <astnode> IdentifierList
82 %type <astnode> MultiIdentifier
83 %type <astnode> SingleIdentifier
85 %type <astnode> ProcFuncList
86 %type <astnode> MultiProcFuncDecl
87 %type <astnode> ProcFuncDecl
88 %type <astnode> ProcDecl
89 %type <astnode> FuncDecl
90 %type <astnode> ParamList
91 %type <astnode> SingleParam
92 %type <astnode> MultiParam
94 %type <astnode> MainCodeBlock
95 %type <astnode> CodeBlock
96 %type <astnode> StatementList
97 %type <astnode> MultiStatement
98 %type <astnode> Statement
99 %type <astnode> IfStatement
100 %type <astnode> WhileStatement
101 %type <astnode> ForStatement
102 %type <astnode> PrintCharStatement
103 %type <astnode> PrintIntStatement
104 %type <astnode> PrintBoolStatement
105 %type <astnode> PrintLnStatement
107 %type <astnode> Expression
108 %type <astnode> SimpleExpression
109 %type <astnode> NFactor
110 %type <astnode> Factor
111 %type <astnode> Term
113 %type <astnode> Call
114 %type <astnode> CallParamList
115 %type <astnode> MultiCallParam
117 %type <astnode> Assignment
118 %type <astnode> Identifier
119 %type <astnode> SimpleType
120 %type <astnode> Literal
122 %type <integer> AddOp
123 %type <integer> MulOp
124 %type <integer> RelOp
125 %type <integer> UnaryOp
127 %start Program
130 Program:
131 VarDeclList ProcFuncList MainCodeBlock
133 struct AstNode *ast_node;
134 ast_node = ast_node_new("Program", -1, -1,
135 yylloc.last_line, NULL);
136 ast_node_add_child(ast_node, $1);
137 ast_node_add_child(ast_node, $2);
138 ast_node_add_child(ast_node, $3);
139 $$ = ast_node;
141 //ast_node_print(ast_node);
142 //symbol_table_dump(sym_table);
143 ast_node_print_graph(ast_node);
147 VarDeclList:
148 /* empty */ { $$ = NULL; }
149 | MultiVarDecl
151 struct AstNode *ast_node;
152 ast_node = ast_node_new("VarDeclList", -1, -1,
153 yylloc.last_line, NULL);
154 ast_node_add_child(ast_node, $1);
155 $$ = ast_node;
159 MultiVarDecl:
160 /* empty */ { $$ = NULL; }
161 | VarDecl MultiVarDecl
163 ast_node_add_sibling($1, $2);
164 $$ = $1;
168 VarDecl:
169 T_VAR IdentifierList T_COLON SimpleType T_SEMICOLON
171 struct AstNode *ast_node;
172 ast_node = ast_node_new("VarDecl", -1, -1,
173 yylloc.last_line, NULL);
174 ast_node_add_child(ast_node, $2);
175 ast_node_add_child(ast_node, $4);
176 $$ = ast_node;
180 IdentifierList:
181 SingleIdentifier MultiIdentifier
183 struct AstNode *ast_node;
184 ast_node = ast_node_new("IdentifierList", -1, -1,
185 yylloc.last_line, NULL);
186 ast_node_add_sibling($1, $2);
187 ast_node_add_child(ast_node, $1);
188 $$ = ast_node;
192 MultiIdentifier:
193 /* empty */ { $$ = NULL; }
194 | T_COMMA SingleIdentifier MultiIdentifier
196 ast_node_add_sibling($2, $3);
197 $$ = $2;
201 SingleIdentifier:
202 Identifier { $$ = $1; }
206 ProcFuncList:
207 /* empty */ { $$ = NULL; }
208 | ProcFuncDecl MultiProcFuncDecl
210 struct AstNode *ast_node;
211 ast_node = ast_node_new("ProcFuncList", -1, -1,
212 yylloc.last_line, NULL);
213 ast_node_add_sibling($1, $2);
214 ast_node_add_child(ast_node, $1);
215 $$ = ast_node;
219 MultiProcFuncDecl:
220 /* empty */ { $$ = NULL; }
221 | ProcFuncDecl MultiProcFuncDecl
223 ast_node_add_sibling($1, $2);
224 $$ = $1;
228 ProcFuncDecl:
229 ProcDecl { $$ = $1; }
230 | FuncDecl { $$ = $1; }
233 ProcDecl:
234 T_PROCEDURE Identifier T_LPAR ParamList T_RPAR T_SEMICOLON VarDeclList
235 CodeBlock T_SEMICOLON
237 struct AstNode *ast_node;
238 ast_node = ast_node_new("ProcDecl", -1, PROCEDURE,
239 yylloc.last_line, NULL);
240 ast_node_add_child(ast_node, $2); // Identifier
241 ast_node_add_child(ast_node, $4); // ParamList
242 ast_node_add_child(ast_node, $7); // VarDeclList
243 ast_node_add_child(ast_node, $8); // CodeBlock
244 $$ = ast_node;
248 FuncDecl:
249 T_FUNCTION Identifier T_LPAR ParamList T_RPAR T_COLON SimpleType
250 T_SEMICOLON VarDeclList CodeBlock T_SEMICOLON
252 struct AstNode *ast_node;
253 ast_node = ast_node_new("FuncDecl", -1, FUNCTION,
254 yylloc.last_line, NULL);
255 ast_node_add_child(ast_node, $2); // Identifier
256 ast_node_add_child(ast_node, $4); // ParamList
257 ast_node_add_child(ast_node, $7); // SimpleType
258 ast_node_add_child(ast_node, $9); // VarDeclList
259 ast_node_add_child(ast_node, $10); // CodeBlock
260 $$ = ast_node;
264 ParamList:
265 /* empty */ { $$ = NULL; }
266 | SingleParam MultiParam
268 struct AstNode *ast_node;
269 ast_node = ast_node_new("ParamList", -1, -1,
270 yylloc.last_line, NULL);
271 ast_node_add_sibling($1, $2);
272 ast_node_add_child(ast_node, $1);
273 $$ = ast_node;
277 MultiParam:
278 /* empty */ { $$ = NULL; }
279 | T_COMMA SingleParam MultiParam
281 ast_node_add_sibling($2, $3);
282 $$ = $2;
286 SingleParam:
287 Identifier T_COLON SimpleType
289 struct AstNode *ast_node;
290 ast_node = ast_node_new("SingleParam", -1, -1,
291 yylloc.last_line, NULL);
292 ast_node_add_child(ast_node, $1); // Identifier
293 ast_node_add_child(ast_node, $3); // SimpleType
294 $$ = ast_node;
298 MainCodeBlock:
299 /* empty */ { $$ = NULL; }
300 | CodeBlock T_DOT { $$ = $1; }
303 CodeBlock:
304 T_BEGIN StatementList T_END { $$ = $2; }
307 StatementList:
308 Statement MultiStatement
310 struct AstNode *ast_node;
311 ast_node = ast_node_new("StatementList", -1, -1,
312 yylloc.last_line, NULL);
313 ast_node_add_sibling($1, $2);
314 ast_node_add_child(ast_node, $1);
315 $$ = ast_node;
319 MultiStatement:
320 /* empty */ { $$ = NULL; }
321 | Statement MultiStatement
323 ast_node_add_sibling($1, $2);
324 $$ = $1;
328 Statement:
329 Assignment T_SEMICOLON { $$ = $1; }
330 | IfStatement { $$ = $1; }
331 | WhileStatement { $$ = $1; }
332 | ForStatement { $$ = $1; }
333 | Call { $$ = $1; }
334 | PrintIntStatement { $$ = $1; }
335 | PrintCharStatement { $$ = $1; }
336 | PrintBoolStatement { $$ = $1; }
337 | PrintLnStatement { $$ = $1; }
340 PrintIntStatement:
341 T_PRINT_INT T_LPAR Expression T_RPAR
343 struct AstNode *ast_node;
344 ast_node = ast_node_new("PrintIntStatement", -1, -1,
345 yylloc.last_line, NULL);
346 ast_node_add_child(ast_node, $3);
347 $$ = ast_node;
351 PrintCharStatement:
352 T_PRINT_CHAR T_LPAR Expression T_RPAR
354 struct AstNode *ast_node;
355 ast_node = ast_node_new("PrintCharStatement", -1, -1,
356 yylloc.last_line, NULL);
357 ast_node_add_child(ast_node, $3);
358 $$ = ast_node;
362 PrintBoolStatement:
363 T_PRINT_BOOL T_LPAR Expression T_RPAR
365 struct AstNode *ast_node;
366 ast_node = ast_node_new("PrintBoolStatement", -1, -1,
367 yylloc.last_line, NULL);
368 ast_node_add_child(ast_node, $3);
369 $$ = ast_node;
373 PrintLnStatement:
374 T_PRINT_LINE T_LPAR Expression T_RPAR
376 struct AstNode *ast_node;
377 ast_node = ast_node_new("PrintLnStatement", -1, -1,
378 yylloc.last_line, NULL);
379 ast_node_add_child(ast_node, $3);
380 $$ = ast_node;
384 Assignment:
385 Identifier T_ASSIGNMENT Expression
387 struct AstNode *ast_node;
388 ast_node = ast_node_new("Assignment", -1, -1,
389 yylloc.last_line, NULL);
390 ast_node_add_child(ast_node, $1);
391 ast_node_add_child(ast_node, $3);
392 $$ = ast_node;
396 IfStatement:
397 T_IF Expression T_THEN CodeBlock T_ELSE CodeBlock
399 struct AstNode *ast_node;
400 ast_node = ast_node_new("IfStatement", -1, -1,
401 yylloc.last_line, NULL);
402 ast_node_add_child(ast_node, $2);
403 ast_node_add_child(ast_node, $4);
404 ast_node_add_child(ast_node, $6);
405 $$ = ast_node;
409 WhileStatement:
410 T_WHILE T_LPAR Expression T_RPAR T_DO CodeBlock
412 struct AstNode *ast_node;
413 ast_node = ast_node_new("WhileStatement", -1, -1,
414 yylloc.last_line, NULL);
415 ast_node_add_child(ast_node, $3);
416 ast_node_add_child(ast_node, $6);
417 $$ = ast_node;
421 ForStatement:
422 T_FOR Assignment T_TO Expression T_DO CodeBlock
424 struct AstNode *ast_node;
425 ast_node = ast_node_new("ForStatement", -1, -1,
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, $6);
430 $$ = ast_node;
434 Expression:
435 SimpleExpression { $$ = $1; }
436 | SimpleExpression RelOp SimpleExpression
438 struct AstNode *ast_node;
439 ast_node = ast_node_new("RelExpression", $2, -1,
440 yylloc.last_line, NULL);
441 ast_node_add_child(ast_node, $1);
442 ast_node_add_child(ast_node, $3);
443 $$ = ast_node;
447 SimpleExpression:
448 Term { $$ = $1; }
449 | SimpleExpression AddOp Term
451 struct AstNode *ast_node;
452 ast_node = ast_node_new("AddExpression", $2, -1,
453 yylloc.last_line, NULL);
454 ast_node_add_child(ast_node, $1);
455 ast_node_add_child(ast_node, $3);
456 $$ = ast_node;
460 Term:
461 NFactor { $$ = $1; }
462 | Term MulOp NFactor
464 struct AstNode *ast_node;
465 ast_node = ast_node_new("MulExpression", $2, -1,
466 yylloc.last_line, NULL);
467 ast_node_add_child(ast_node, $1);
468 ast_node_add_child(ast_node, $3);
469 $$ = ast_node;
473 NFactor:
474 Factor { $$ = $1; }
475 | UnaryOp Factor
477 struct AstNode *ast_node;
478 ast_node = ast_node_new("NFactor", $1, -1,
479 yylloc.last_line, NULL);
480 ast_node_add_child(ast_node, $2);
481 $$ = ast_node;
485 Factor:
486 Identifier { $$ = $1; }
487 | Literal { $$ = $1; }
488 | Call { $$ = $1; }
489 | T_LPAR Expression T_RPAR { $$ = $2; }
492 Call:
493 Identifier T_LPAR CallParamList T_RPAR
495 struct AstNode *ast_node;
496 ast_node = ast_node_new("Call", -1, -1,
497 yylloc.last_line, NULL);
498 ast_node_add_child(ast_node, $1);
499 ast_node_add_child(ast_node, $3);
500 $$ = ast_node;
504 CallParamList:
505 Expression MultiCallParam
507 struct AstNode *ast_node;
508 ast_node = ast_node_new("CallParamList", -1, -1,
509 yylloc.last_line, NULL);
510 ast_node_add_sibling($1, $2);
511 ast_node_add_child(ast_node, $1);
512 $$ = ast_node;
516 MultiCallParam:
517 /* empty */ { $$ = NULL; }
518 | T_COMMA Expression MultiCallParam
520 ast_node_add_sibling($2, $3);
521 $$ = $2;
525 AddOp:
526 T_PLUS { $$ = T_PLUS; }
527 | T_MINUS { $$ = T_MINUS; }
528 | T_OR { $$ = T_OR; }
531 MulOp:
532 T_STAR { $$ = T_STAR; }
533 | T_SLASH { $$ = T_SLASH; }
534 | T_AND { $$ = T_AND; }
537 RelOp:
538 T_LESSER { $$ = T_LESSER; }
539 | T_LESSEREQUAL { $$ = T_LESSEREQUAL; }
540 | T_GREATER { $$ = T_GREATER; }
541 | T_GREATEREQUAL { $$ = T_GREATEREQUAL; }
542 | T_EQUAL { $$ = T_EQUAL; }
543 | T_NOTEQUAL { $$ = T_NOTEQUAL; }
546 UnaryOp:
547 T_NOT { $$ = T_NOT; }
548 | T_MINUS { $$ = T_MINUS; }
551 SimpleType:
552 TYPE_IDENTIFIER
554 struct AstNode *ast_node;
555 ast_node = ast_node_new("SimpleType", -1, $1,
556 yylloc.last_line, NULL);
557 $$ = ast_node;
561 Identifier:
562 IDENTIFIER
564 Symbol *symbol;
565 struct AstNode *ast_node;
567 //sym_table = symbol_insert(sym_table, $1);
568 symbol_add(sym_table, $1);
569 symbol = sym_table;
571 ast_node = ast_node_new("Identifier", -1, -1,
572 yylloc.last_line, NULL);
573 ast_node->symbol = symbol;
574 $$ = ast_node;
578 Literal:
579 INT_LITERAL
581 struct AstNode *ast_node;
582 ast_node = ast_node_new("IntLiteral", INT_LITERAL, INTEGER,
583 yylloc.last_line, NULL);
584 value_set_from_int(&ast_node->value, $1);
585 $$ = ast_node;
587 | BOOL_LITERAL
589 struct AstNode *ast_node;
590 ast_node = ast_node_new("BoolLiteral", BOOL_LITERAL, BOOLEAN,
591 yylloc.last_line, NULL);
592 value_set_from_bool(&ast_node->value, $1);
593 $$ = ast_node;
595 | CHAR_LITERAL
597 struct AstNode *ast_node;
598 ast_node = ast_node_new("CharLiteral", CHAR_LITERAL, CHAR,
599 yylloc.last_line, NULL);
600 value_set_from_char(&ast_node->value, $1);
601 $$ = ast_node;
607 static void
608 yyerror (/*YYLTYPE *locp,*/ const char *msg)
610 fprintf(stderr,"error: %s\n", msg);
614 main(int argc, char **argv)
616 if (argc > 1)
617 yyin = fopen(argv[1], "r");
618 else
619 yyin = stdin;
621 /*yylloc.first_line = yylloc.last_line = 1;
622 yylloc.first_column = yylloc.last_column = 0;*/
624 symbol_table_init();
626 return yyparse();