Funcoes em pascal
[toypasc.git] / parser.y
blob47f583baa7a2dc53abb1703efa94784bfeb10030
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);*/
17 %defines
18 %locations
19 %pure-parser
20 %error-verbose
21 /*%parse-param {ValaParser *parser}
22 %lex-param {ValaParser *parser}*/
24 %union {
25 char* lexeme;
26 int integer;
27 int boolean;
28 char character;
29 int type;
30 struct AstNode *astnode;
33 %token T_VAR
35 %token T_PROCEDURE
36 %token T_FUNCTION
37 %token T_BEGIN
38 %token T_END
40 %token T_IF
41 %token T_ELSE
42 %token T_WHILE
43 %token T_FOR
44 %token T_TO
45 %token T_DO
47 %token T_PRINT_INT
48 %token T_PRINT_CHAR
49 %token T_PRINT_BOOL
50 %token T_PRINT_LINE
52 %token T_ASSIGNMENT
53 %token T_ADD
54 %token T_SUB
55 %token T_DIV
56 %token T_MULT
57 %token T_AND
58 %token T_OR
59 %token T_NOT
61 %token T_MAJOR
62 %token T_MINOR
63 %token T_EQUAL
64 %token T_NOTEQUAL
65 %token T_MAJOREQUAL
66 %token T_MINOREQUAL
68 %token T_LPAR
69 %token T_RPAR
70 %token T_SEMICOLON
71 %token T_COLON
72 %token T_COMMA
73 %token T_DOT
75 %token <type> TYPE_IDENTIFIER
76 %token <lexeme> IDENTIFIER
77 %token <integer> INT_LITERAL
78 %token <boolean> BOOL_LITERAL
79 %token <character> CHAR_LITERAL
81 %type <astnode> Program
82 %type <astnode> VarDeclList
83 %type <astnode> VarDecl
84 %type <astnode> ProcFuncList
85 %type <astnode> MainCodeBlock
86 %type <astnode> CodeBlock
88 %type <astnode> Assignment
89 %type <astnode> Identifier
90 %type <astnode> SimpleType
91 %type <astnode> Literal
93 %start Program
96 Program:
97 VarDeclList ProcFuncList MainCodeBlock
99 struct AstNode *ast_node;
100 ast_node = ast_node_new("Program", -1, -1,
101 yylloc.last_line, NULL);
102 ast_node->children[0] = $1;
103 ast_node->children[1] = $2;
104 ast_node->children[2] = $3;
105 $$ = ast_node;
107 //ast_node_print(ast_node);
108 //symbol_table_dump(sym_table);
109 //ast_node_print_graph(ast_node);
113 VarDeclList:
114 /* empty */ { $$ = NULL; }
115 | VarDeclList VarDecl
117 ((struct AstNode *) $2)->next = (struct AstNode *) $1;
118 $$ = $2;
122 VarDecl:
123 T_VAR Identifier T_COLON SimpleType T_SEMICOLON
125 struct AstNode *ast_node;
126 ast_node = ast_node_new("VarDecl", -1, -1,
127 yylloc.last_line, NULL);
128 ast_node->children[0] = $2;
129 //ast_node->children[1] = $4;
130 $$ = ast_node;
134 ProcFuncList:
135 /* empty */ { $$ = NULL; }
138 MainCodeBlock:
139 /* empty */ { $$ = NULL; }
140 | CodeBlock T_DOT { $$ = $1; }
143 CodeBlock:
144 T_BEGIN Assignment T_END
146 $$ = $2;
150 Assignment:
151 Identifier T_ASSIGNMENT Literal T_SEMICOLON
153 struct AstNode *ast_node;
154 ast_node = ast_node_new("Assignment", -1, -1,
155 yylloc.last_line, NULL);
156 ast_node->children[0] = $1;
157 ast_node->children[1] = $3;
158 ast_node_print($3);
159 $$ = ast_node;
163 SimpleType:
164 TYPE_IDENTIFIER
166 struct AstNode *ast_node;
167 ast_node = ast_node_new("SimpleType", -1, -1,
168 yylloc.last_line, NULL);
169 //ast_node->children[0] = $2;
170 //ast_node->children[1] = $4;
171 $$ = ast_node;
175 Identifier:
176 IDENTIFIER
178 Symbol *symbol;
179 struct AstNode *ast_node;
181 sym_table = symbol_insert(sym_table, $1);
182 symbol = sym_table;
184 ast_node = ast_node_new("Identifier", -1, -1,
185 yylloc.last_line, NULL);
186 $$ = ast_node;
190 Literal:
191 INT_LITERAL
193 struct AstNode *ast_node;
194 ast_node = ast_node_new("Literal", INT_LITERAL, INTEGER,
195 yylloc.last_line, NULL);
196 value_set_from_int(&ast_node->value, $1);
197 $$ = ast_node;
199 | BOOL_LITERAL
201 struct AstNode *ast_node;
202 ast_node = ast_node_new("Literal", BOOL_LITERAL, BOOLEAN,
203 yylloc.last_line, NULL);
204 value_set_from_int(&ast_node->value, $1);
205 $$ = ast_node;
207 | CHAR_LITERAL
209 struct AstNode *ast_node;
210 ast_node = ast_node_new("Literal", CHAR_LITERAL, CHAR,
211 yylloc.last_line, NULL);
212 value_set_from_int(&ast_node->value, $1);
213 $$ = ast_node;
219 static void
220 yyerror (/*YYLTYPE *locp,*/ const char *msg)
222 fprintf(stderr,"error: %s\n", msg);
226 main(int argc, char **argv)
228 if (argc > 1)
229 yyin = fopen(argv[1], "r");
230 else
231 yyin = stdin;
233 /*yylloc.first_line = yylloc.last_line = 1;
234 yylloc.first_column = yylloc.last_column = 0;*/
236 symbol_table_init();
238 return yyparse();