7 #include "symbol_table.h"
9 /*extern char *yytext;*/
12 static void yyerror (/*YYLTYPE *locp, */const char *msg
);
13 /*int yylex (YYSTYPE *yylval_param, YYLTYPE *yylloc_param);*/
20 /*%parse-param {ValaParser *parser}
21 %lex-param {ValaParser *parser}*/
29 struct AstNode
*astnode
;
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
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
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
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);
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);
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);
168 /* empty */ { $$
= NULL
; }
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);
180 /* empty */ { $$
= NULL
; }
181 | VarDecl MultiVarDecl
183 ast_node_add_sibling
($1, $2);
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);
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);
213 /* empty */ { $$
= NULL
; }
214 | T_COMMA SingleIdentifier MultiIdentifier
216 ast_node_add_sibling
($2, $3);
222 Identifier
{ $$
= $1; }
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);
240 /* empty */ { $$
= NULL
; }
241 | ProcFuncDecl MultiProcFuncDecl
243 ast_node_add_sibling
($1, $2);
249 ProcDecl
{ $$
= $1; }
250 | FuncDecl
{ $$
= $1; }
254 T_PROCEDURE Identifier T_LPAR ParamList T_RPAR T_SEMICOLON VarDeclList
255 CodeBlock T_SEMICOLON
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
);
278 T_FUNCTION Identifier T_LPAR ParamList T_RPAR T_COLON TYPE_IDENTIFIER
279 T_SEMICOLON VarDeclList CodeBlock T_SEMICOLON
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
);
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);
317 /* empty */ { $$
= NULL
; }
318 | T_COMMA SingleParam MultiParam
320 ast_node_add_sibling
($2, $3);
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);
338 /* empty */ { $$
= NULL
; }
339 | CodeBlock T_DOT
{ $$
= $1; }
343 T_BEGIN StatementList T_END
{ $$
= $2; }
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);
359 /* empty */ { $$
= NULL
; }
360 | Statement MultiStatement
362 ast_node_add_sibling
($1, $2);
368 Assignment T_SEMICOLON
{ $$
= $1; }
369 | IfStatement
{ $$
= $1; }
370 | WhileStatement
{ $$
= $1; }
371 | ForStatement
{ $$
= $1; }
373 | PrintStatement
{ $$
= $1; }
377 PrintIntStatement
{ $$
= $1; }
378 | PrintCharStatement
{ $$
= $1; }
379 | PrintBoolStatement
{ $$
= $1; }
380 | PrintLineStatement
{ $$
= $1; }
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);
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);
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);
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);
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);
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);
453 /* empty */ { $$
= NULL
; }
454 | T_ELSE CodeBlock
{ $$
= $2; }
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);
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);
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);
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);
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);
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);
539 Identifier
{ $$
= $1; }
540 | Literal
{ $$
= $1; }
542 | T_LPAR Expression T_RPAR
{ $$
= $2; }
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);
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);
571 /* empty */ { $$
= NULL
; }
572 | T_COMMA Expression MultiCallParam
574 ast_node_add_sibling
($2, $3);
582 struct AstNode
*ast_node
;
583 ast_node
= ast_node_new
($1, T_PLUS
, NONE_TYPE
,
584 yylloc.last_line
, NULL
);
589 struct AstNode
*ast_node
;
590 ast_node
= ast_node_new
($1, T_MINUS
, NONE_TYPE
,
591 yylloc.last_line
, NULL
);
596 struct AstNode
*ast_node
;
597 ast_node
= ast_node_new
($1, T_OR
, NONE_TYPE
,
598 yylloc.last_line
, NULL
);
606 struct AstNode
*ast_node
;
607 ast_node
= ast_node_new
($1, T_STAR
, NONE_TYPE
,
608 yylloc.last_line
, NULL
);
613 struct AstNode
*ast_node
;
614 ast_node
= ast_node_new
($1, T_SLASH
, NONE_TYPE
,
615 yylloc.last_line
, NULL
);
620 struct AstNode
*ast_node
;
621 ast_node
= ast_node_new
($1, T_AND
, NONE_TYPE
,
622 yylloc.last_line
, NULL
);
630 struct AstNode
*ast_node
;
631 ast_node
= ast_node_new
($1, T_LESSER
, NONE_TYPE
,
632 yylloc.last_line
, NULL
);
637 struct AstNode
*ast_node
;
638 ast_node
= ast_node_new
($1, T_LESSEREQUAL
, NONE_TYPE
,
639 yylloc.last_line
, NULL
);
644 struct AstNode
*ast_node
;
645 ast_node
= ast_node_new
($1, T_GREATER
, NONE_TYPE
,
646 yylloc.last_line
, NULL
);
651 struct AstNode
*ast_node
;
652 ast_node
= ast_node_new
($1, T_GREATEREQUAL
, NONE_TYPE
,
653 yylloc.last_line
, NULL
);
658 struct AstNode
*ast_node
;
659 ast_node
= ast_node_new
($1, T_EQUAL
, NONE_TYPE
,
660 yylloc.last_line
, NULL
);
665 struct AstNode
*ast_node
;
666 ast_node
= ast_node_new
($1, T_NOTEQUAL
, NONE_TYPE
,
667 yylloc.last_line
, NULL
);
675 struct AstNode
*ast_node
;
676 ast_node
= ast_node_new
($1, T_NOT
, NONE_TYPE
,
677 yylloc.last_line
, NULL
);
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);
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);
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);
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);
725 yyerror (/*YYLTYPE *locp,*/ const char *msg
)
727 fprintf
(stderr
, "[Error] line %d: %s\n", yyget_lineno
(), msg
);
731 main
(int argc
, char **argv
)
734 yyin
= fopen
(argv
[1], "r");
738 /*yylloc.first_line = yylloc.last_line = 1;
739 yylloc.first_column = yylloc.last_column = 0;*/