Implementando a symbol_table
[toypasc.git] / parser.y
blob1528727819c4305a294ee3a22f02f0e14918d610
1 %{
2 #include <stdio.h>
3 #include <string.h>
4 #include "parser.h"
5 #include "base.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;
30 %token T_INTEGER
31 %token T_BOOLEAN
33 %token T_VAR
34 %token T_BEGIN
35 %token T_END
36 %token T_PROCEDURE
37 %token T_FUNCTION
39 %token T_PRINT_INT
40 %token T_PRINT_BOOL
41 %token T_PRINT_LINE
43 %token T_ASSIGNMENT
45 %token T_ADD
46 %token T_SUB
47 %token T_DIV
48 %token T_MULT
50 %token T_MAJOR
51 %token T_MINOR
52 %token T_EQUAL
53 %token T_NOTEQUAL
54 %token T_MAJOREQUAL
55 %token T_MINOREQUAL
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 <lexeme> IDENTIFIER
65 %token <integer> NUMBER
66 %token <boolean> BOOLEAN
68 /*%type <symbol> program
69 %type <symbol> var_decl_list
70 %type <symbol> var_decl
71 %type <symbol> var_type
72 %type <symbol> proc_func_list
73 %type <symbol> proc_func_decl
74 %type <symbol> proc_func_body
75 %type <symbol> proc_decl
76 %type <symbol> func_decl
77 %type <symbol> param_list
78 %type <symbol> single_param
79 %type <symbol> multi_param
80 %type <symbol> code_block
81 %type <symbol> statements
82 %type <symbol> statement*/
84 %type <lexeme> assignment
85 %type <integer> numeric_expression
86 %type <boolean> relational_expression
87 %type <integer> number_literal
89 /*%type <symbol> print_statement
90 %type <symbol> print_integer
91 %type <symbol> print_boolean
92 %type <symbol> print_line*/
94 %start program
97 program:
98 var_decl_list proc_func_list code_block T_DOT { printf("program\n"); }
101 var_decl_list:
102 var_decl_list var_decl { printf("\n"); }
103 | /* empty */
106 var_decl:
107 T_VAR IDENTIFIER T_COLON var_type T_SEMICOLON
108 { printf("var_decl\n") }
111 var_type:
112 T_INTEGER
113 { printf("var_type\n") }
114 | T_BOOLEAN
115 { printf("var_type\n") }
118 proc_func_list:
119 /* empty */
120 { printf("proc_func_list\n") }
121 | proc_func_list proc_decl
122 { printf("proc_func_list\n") }
123 | proc_func_list func_decl
124 { printf("proc_func_list\n") }
127 proc_func_decl:
128 IDENTIFIER T_LPAR param_list T_RPAR
129 { printf("proc_func_decl\n") }
132 proc_func_body:
133 var_decl_list code_block T_SEMICOLON
134 { printf("proc_func_body\n") }
137 proc_decl:
138 T_PROCEDURE proc_func_decl T_SEMICOLON proc_func_body
139 { printf("proc_decl\n") }
142 func_decl:
143 T_FUNCTION proc_func_decl T_COLON var_type T_SEMICOLON proc_func_body
144 { printf("func_decl\n") }
147 param_list:
148 /* empty */
149 { printf("param_list\n") }
150 | single_param multi_param
151 { printf("param_list\n") }
154 single_param:
155 IDENTIFIER T_COLON var_type
156 { printf("single_param\n") }
159 multi_param:
160 /* empty */
161 { printf("multi_param\n") }
162 | T_COMMA single_param multi_param
163 { printf("multi_param\n") }
166 code_block:
167 T_BEGIN statements T_END
168 { printf("code_block\n") }
171 statements:
172 /* empty */
173 | statements statement T_SEMICOLON
176 statement:
177 assignment
178 | print_statement
181 assignment:
182 IDENTIFIER T_ASSIGNMENT expression
183 | IDENTIFIER T_ASSIGNMENT NUMBER { $$ = $1; }
186 expression:
187 numeric_expression
188 | relational_expression
191 numeric_expression:
192 numeric_expression T_ADD numeric_expression { $$ = $1 + $3; }
193 | numeric_expression T_SUB numeric_expression { $$ = $1 - $3; }
194 | number_literal { $$ = $1; }
197 number_literal:
198 NUMBER { $$ = $1; }
201 relational_expression:
202 numeric_expression relational_operator numeric_expression
205 relational_operator:
206 T_MINOR
207 | T_MINOREQUAL
208 | T_MAJOR
209 | T_MAJOREQUAL
210 | T_EQUAL
211 | T_NOTEQUAL
214 print_statement:
215 print_integer
216 | print_boolean
217 | print_line
220 print_integer:
221 T_PRINT_INT T_LPAR NUMBER T_RPAR { printf ("%d", $3); }
223 print_boolean:
224 T_PRINT_BOOL T_LPAR BOOLEAN T_RPAR { printf ("%s", ($3 == 0 ? "False" : "True")); }
226 print_line:
227 T_PRINT_LINE T_LPAR T_RPAR { printf ("\n"); }
230 static void
231 yyerror (/*YYLTYPE *locp,*/ const char *msg)
233 fprintf(stderr,"error: %s\n", msg);
237 main(int argc, char **argv)
239 if (argc > 1)
240 yyin = fopen(argv[1], "r");
241 else
242 yyin = stdin;
244 /*yylloc.first_line = yylloc.last_line = 1;
245 yylloc.first_column = yylloc.last_column = 0;*/
247 return yyparse();