-
Notifications
You must be signed in to change notification settings - Fork 4
/
bis.y
400 lines (320 loc) · 8.24 KB
/
bis.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
%token ID COMMA OPEN_PAR CLOSE_PAR IF FOR INTEGER ELSE EQUAL STAR PLUS MINUS DIVIDE END_BR BEGIN_BR RETURN ASSIGN BREAK AND
%token ADDRESS_AND CONTINUE MINUS_MINUS PLUS_PLUS MOD NOT OR SEMICOLON STRING_CONST NOTEQUAL LESS MORE LESS_OR_EQUAL MORE_OR_EQUAL WHILE INT_VAR_TYPE BEGIN_CU END_CU EOF_
%{
#include "ast.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern void yyerror(char *msg);
extern int yylex();
extern int yyparse();
extern FILE* yyin;
%}
%union{
char *str;
struct ast_node *node;
int d;
}
%type <str> INTEGER ID STRING_CONST
%type <node> for_block continue_line break_line inits_ inits init lines_ parameters parameter parameters_def while_block integer exp line lines program id block blocks var_def func_def end_line params param call return_line exp_line assign_line array string_const if_block indexes index
%left OR
%left AND
%left EQUAL
%left NOTEQUAL
%left LESS
%left MORE
%left LESS_OR_EQUAL
%left MORE_OR_EQUAL
%left PLUS
%left MINUS
%left STAR
%left DIVIDE
%left MOD
%right NOT
%right ADDRESS_AND
%left PLUS_PLUS
%left MINUS_MINUS
%%
program: blocks { tree = $1; return NULL; };
blocks: blocks block
{
$1->add_child($2);
$$ = $1;
}
| block
{
$$ = new_node(node_type_blocks, 0);
$$->add_child($1);
};
block: var_def
{
$$ = $1;
}
| func_def
{
$$ = $1;
};
var_def: INT_VAR_TYPE id SEMICOLON
{
$$ = new_node(node_type_var_def, 0);
$$->add_child($2);
$$->add_child(NULL);
$$->add_child(NULL);
}
| INT_VAR_TYPE id ASSIGN exp SEMICOLON
{
$$ = new_node(node_type_var_def, 0);
$$->add_child($2);
$$->add_child($4);
$$->add_child(NULL);
}
| INT_VAR_TYPE id indexes SEMICOLON
{
$$ = new_node(node_type_var_def, 0);
$$->add_child($2);
$$->add_child(NULL);
$$->add_child($3);
}
| INT_VAR_TYPE id indexes ASSIGN inits SEMICOLON
{
$$ = new_node(node_type_var_def, 0);
$$->add_child($2);
$$->add_child($5);
$$->add_child($3);
};
func_def:
INT_VAR_TYPE id OPEN_PAR parameters_def CLOSE_PAR lines
{
$$ = new_node(node_type_func_def, 0);
$$->add_child($2); // id
$$->add_child($4); // parameters_def
$$->add_child($6); // lines
}
| INT_VAR_TYPE id OPEN_PAR parameters_def CLOSE_PAR end_line
{
$$ = new_node(node_type_func_def, 0);
$$->add_child($2); // id
$$->add_child($4); // parameters_def
$$->add_child(NULL); // lines
};
parameters_def: parameters
{
$$ = $1;
}
|
{
$$ = NULL;
};
parameters: parameters COMMA parameter
{
$$ = $1;
$$->add_child($3);
}
| parameter
{
$$ = new_node(node_type_parameters, 0);
$$->add_child($1);
};
parameter: INT_VAR_TYPE id
{
$$ = new_node(node_type_parameter, 0);
$$->add_child($2);
};
lines: BEGIN_CU lines_ END_CU
{
$$ = $2;
}
| BEGIN_CU END_CU
{
$$ = NULL;
};
lines_: lines_ line
{
$$ = $1;
$$->add_child($2);
}
| line
{
$$ = new_node(node_type_lines, 0);
$$->add_child($1);
};
if_block: IF OPEN_PAR exp CLOSE_PAR lines
{
$$ = new_node(node_type_if_block, 0);
$$->add_child($3); // exp
$$->add_child($5); // lines
$$->add_child(NULL); // else_lines
}
| IF OPEN_PAR exp CLOSE_PAR lines ELSE lines
{
$$ = new_node(node_type_if_block, 0);
$$->add_child($3); // exp
$$->add_child($5); // lines
$$->add_child($7); // else_lines
};
while_block: WHILE OPEN_PAR exp CLOSE_PAR lines
{
$$ = new_node(node_type_while_block, 0);
$$->add_child($3); // exp
$$->add_child($5); // lines
};
for_block: FOR OPEN_PAR id ASSIGN exp SEMICOLON exp SEMICOLON exp CLOSE_PAR lines
{
$$ = new_node(node_type_for_block, 0);
$$->add_child($3); // id
$$->add_child($5); // exp
$$->add_child($7); // exp
$$->add_child($9); // exp
$$->add_child($11); // lines
};
line: return_line {$$ = $1;}
| break_line {$$ = $1;}
| continue_line {$$ = $1;}
| var_def {$$ = $1;}
| exp_line {$$ = $1;}
| assign_line {$$ = $1;}
| if_block {$$ = $1;}
| for_block {$$ = $1;}
| while_block {$$ = $1;}
| end_line {$$ == NULL; };
end_line: SEMICOLON
{
$$ = new_node(node_type_end_line, 0);
};
return_line: RETURN exp end_line
{
$$ = new_node(node_type_return, 0);
$$->add_child($2);
};
exp_line: exp end_line
{
$$ = $1;
};
break_line: BREAK end_line
{
$$ = new_node(node_type_break_line, 0);
};
continue_line: CONTINUE end_line
{
$$ = new_node(node_type_continue_line, 0);
};
assign_line:
id ASSIGN exp end_line
{
$$ = new_node(node_type_assign, 0);
$$->add_child($1);
$$->add_child($3);
}
| array ASSIGN exp end_line
{
$$ = new_node(node_type_assign_array, 0);
$$->add_child($1);
$$->add_child($3);
};
exp: exp EQUAL exp {$$ = new_node(node_type_exp, op_equal); $$->add_child($1); $$->add_child($3); }
| exp NOTEQUAL exp {$$ = new_node(node_type_exp, op_not_equal); $$->add_child($1); $$->add_child($3); }
| exp LESS exp {$$ = new_node(node_type_exp, op_less); $$->add_child($1); $$->add_child($3); }
| exp MORE exp {$$ = new_node(node_type_exp, op_more); $$->add_child($1); $$->add_child($3); }
| exp PLUS exp {$$ = new_node(node_type_exp, op_plus); $$->add_child($1); $$->add_child($3); }
| exp MINUS exp {$$ = new_node(node_type_exp, op_minus); $$->add_child($1); $$->add_child($3); }
| exp STAR exp {$$ = new_node(node_type_exp, op_star); $$->add_child($1); $$->add_child($3); }
| exp DIVIDE exp {$$ = new_node(node_type_exp, op_divide); $$->add_child($1); $$->add_child($3); }
| exp OR exp {$$ = new_node(node_type_exp, op_or); $$->add_child($1); $$->add_child($3); }
| exp AND exp {$$ = new_node(node_type_exp, op_and); $$->add_child($1); $$->add_child($3); }
| exp MOD exp {$$ = new_node(node_type_exp, op_mod); $$->add_child($1); $$->add_child($3); }
| NOT exp {$$ = new_node(node_type_exp, op_not); $$->add_child($2); }
| ADDRESS_AND exp {$$ = new_node(node_type_exp, op_address); $$->add_child($2); }
| id PLUS_PLUS {$$ = new_node(node_type_exp, op_plus_plus); $$->add_child($1); }
| id MINUS_MINUS {$$ = new_node(node_type_exp, op_minus_minus); $$->add_child($1); }
| array PLUS_PLUS {$$ = new_node(node_type_exp, op_plus_plus); $$->add_child($1); }
| array MINUS_MINUS {$$ = new_node(node_type_exp, op_minus_minus); $$->add_child($1); }
| exp MORE_OR_EQUAL exp {$$ = new_node(node_type_exp, op_more_or_equal); $$->add_child($1); $$->add_child($3); }
| exp LESS_OR_EQUAL exp {$$ = new_node(node_type_exp, op_less_or_equal); $$->add_child($1); $$->add_child($3); }
| OPEN_PAR exp CLOSE_PAR {$$ = $2;}
| integer {$$ = $1;}
| id {$$ = $1;}
| call {$$ = $1;}
| array {$$ = $1;}
| string_const {$$ = $1;};
call: id OPEN_PAR params CLOSE_PAR
{
$$ = new_node(node_type_call, 0);
$$->add_child($1);
$$->add_child($3);
};
array: id indexes
{
$$ = new_node(node_type_array, 0);
$$->add_child($1);
$$->add_child($2);
};
indexes: indexes index
{
$$ = $1;
$$->add_child($2);
}
| index
{
$$ = new_node(node_type_indexes_set, 0);
$$->add_child($1);
};
index: BEGIN_BR exp END_BR
{
$$ = $2;
}
| BEGIN_BR END_BR
{
$$ = NULL;
};
integer: INTEGER {
$$ = new_node(node_type_int, atoi($1));
};
params: params COMMA param
{
$$ = $1;
$$->add_child($3);
}
| param
{
$$ = new_node(node_type_params_set, 0);
$$->add_child($1);
}
| {$$ = NULL;};
param: exp
{
$$ = $1;
};
inits: BEGIN_CU inits_ END_CU
{
$$ = $2;
}
inits_: inits_ COMMA init
{
$$ = $1;
$$->add_child($3);
}
| init
{
$$ = new_node(node_type_inits_set, 0);
$$->add_child($1);
};
init: exp
{
$$ = $1;
};
id: ID
{
$$ = new_node(node_type_id, 0);
$$->id = $1;
};
string_const: STRING_CONST
{
$$ = new_node(node_type_string, 0);
$$->str = $1;
};
%%
void yyerror(char *msg) {
printf("%s\n", msg);
//system("pause");
exit(1);
}