-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.y
More file actions
361 lines (314 loc) · 12 KB
/
parser.y
File metadata and controls
361 lines (314 loc) · 12 KB
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
%{
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "lyutils.h"
#include "astree.h"
%}
%debug
%defines
%error-verbose
%token-table
%verbose
// %destructor { destroy ($$); } <>
// %printer { astree::dump (yyoutput, $$); } <>
%initial-action {
if (parser::root == nullptr) {
parser::root = new astree (TOK_ROOT, {0, 0, 0}, "");
} else {
errprintf("Found a duplicate root!\n");
}
}
%token TOK_VOID TOK_INT TOK_STRING
%token TOK_IF TOK_ELSE TOK_WHILE TOK_RETURN TOK_STRUCT
%token TOK_NULL TOK_NEW TOK_ARRAY TOK_ARROW TOK_INDEX
%token TOK_EQ TOK_NE TOK_LT TOK_LE TOK_GT TOK_GE TOK_NOT
%token TOK_ROOT TOK_BLOCK TOK_CALL TOK_IFELSE TOK_VARDECL TOK_DECLID
%token TOK_POS TOK_NEG TOK_NEWARRAY TOK_TYPEID TOK_FIELD TOK_FUNCTION
%token TOK_IDENT TOK_INTCON TOK_CHARCON TOK_STRINGCON
%token TOK_PROTO TOK_PARAM TOK_NEWSTR
%right TOK_IF TOK_ELSE
%right '='
%left TOK_EQ TOK_NE TOK_LT TOK_LE TOK_GT TOK_GE
%left '+' '-'
%left '*' '/' '%'
%right TOK_POS TOK_NEG TOK_NOT TOK_NEW
%left TOK_ARROW TOK_ARRAY TOK_FIELD TOK_FUNCTION '['
// %right TOK_VOID TOK_INT TOK_STRING
// %left '(' ')' '[' ']'
//%nonassoc '('
// %nonassoc '['
%start start
%%
start : program { $$ = $1 = nullptr; }
;
program : program structdef { $$ = $1->adopt($2); }
| program function { $$ = $1->adopt($2); }
| program globaldecl { $$ = $1->adopt($2); }
| program error '}' {
$$ = $1;
destroy($3);
}
| program error ';' {
$$ = $1;
destroy($3);
}
| { $$ = parser::root; }
;
structdef : TOK_STRUCT TOK_IDENT '{' '}' {
$2 -> adopt_sym(nullptr, TOK_TYPEID);
$$ = $1 -> adopt($2);
destroy($3, $4);
}
| TOK_STRUCT TOK_IDENT fielddecls '}' {
$2 -> adopt_sym(nullptr, TOK_TYPEID);
$$ = $1 -> adopt($2, $3);
destroy($4);
}
;
fielddecls : '{' fielddecl {
$$ = $1 -> adopt($2);
}
| fielddecls fielddecl {
$$ = $1 -> adopt($2);
}
;
fielddecl : basetype TOK_ARRAY TOK_IDENT ';' {
$$ = $2->adopt($1);
$$ = $2->adopt_sym($3, TOK_FIELD);
destroy($4);
}
| basetype TOK_IDENT ';' {
$$ = $1->adopt_sym($2, TOK_FIELD);
destroy($3);
}
;
basetype : TOK_VOID { $$ = $1; }
| TOK_INT { $$ = $1; }
| TOK_STRING { $$ = $1; }
| TOK_IDENT {
$$ = $1 -> adopt_sym(nullptr, TOK_TYPEID);
}
;
globaldecl : identdecl '=' constant ';' {
$$ = $2 -> adopt_sym($1, TOK_VARDECL, $3);
destroy($4);
}
;
function : identdecl '(' ')' ';' {
astree *as = new astree(TOK_PROTO, $1 -> lloc, "");
$2 -> adopt_sym(nullptr, TOK_PARAM);
$$ = as -> adopt($1, $2);
destroy($3, $4);
}
| identdecl params ')' ';' {
astree *as = new astree(TOK_PROTO, $1 -> lloc, "");
$$ = as->adopt($1, $2);
destroy($3, $4);
}
| identdecl '(' ')' fnbody '}' {
astree *as = new astree(TOK_FUNCTION, $1 -> lloc,
"");
$2 -> adopt_sym(nullptr, TOK_PARAM);
$$ = as -> adopt($1, $2, $4);
destroy($3, $5);
}
| identdecl params ')' fnbody '}' {
astree *as = new astree(TOK_FUNCTION, $1 -> lloc,
"");
$$ = as -> adopt($1, $2, $4);
destroy($3, $5);
}
| identdecl '(' ')' '{' '}' {
astree *as = new astree(TOK_FUNCTION, $1 -> lloc,
"");
$2 -> adopt_sym(nullptr, TOK_PARAM);
$4 -> adopt_sym(nullptr, TOK_BLOCK);
$$ = as -> adopt($1, $2, $4);
destroy($3, $5);
}
| identdecl params ')' '{' '}' {
astree *as = new astree(TOK_FUNCTION, $1 -> lloc,
"");
$4 -> adopt_sym(nullptr, TOK_BLOCK);
$$ = as -> adopt($1, $2, $4);
destroy($3, $5);
}
;
identdecl : basetype TOK_ARRAY TOK_IDENT {
$3 -> adopt_sym(nullptr, TOK_DECLID);
$$ = $2 -> adopt($1, $3);
}
| basetype TOK_IDENT {
$2 -> adopt_sym(nullptr, TOK_DECLID);
$$ = $1 -> adopt($2);
}
;
fnbody : '{' fnstmt {
$$ = $1 -> adopt_sym($2, TOK_BLOCK);
}
| fnbody fnstmt {
$$ = $1 -> adopt($2);
}
;
fnstmt : statement { $$ = $1; }
| localdecl { $$ = $1; }
;
localdecl : identdecl '=' expr ';' {
$$ = $2 -> adopt_sym($1, TOK_VARDECL, $3);
destroy($4);
}
;
// left recursion again
params : '(' identdecl {
$$ = $1 -> adopt_sym($2, TOK_PARAM);
}
| params ',' identdecl {
$$ = $1 -> adopt($3);
destroy($2);
}
;
statements : '{' statement {
$$ = $1 -> adopt_sym($2, TOK_BLOCK);
}
| statements statement {
$$ = $1 -> adopt($2);
}
;
statement : block { $$ = $1; }
| while { $$ = $1; }
| ifelse { $$ = $1; }
| return { $$ = $1; }
| expr ';' {
$$ = $1;
destroy($2);
}
| ';' { $$ = $1; }
;
block : '{' '}' {
$$ = $1 -> adopt_sym(nullptr, TOK_BLOCK);
destroy($2);
}
| statements '}' {
$$ = $1;
destroy($2);
}
;
while : TOK_WHILE '(' expr ')' statement {
// nice and simple, we only want $3 and $5
$$ = $1 -> adopt($3, $5);
destroy($2, $4);
}
;
ifelse : TOK_IF '(' expr ')' statement TOK_ELSE statement {
$$ = $1 -> adopt($3, $5, $7);
destroy($2, $4, $6);
}
/* See https://www.gnu.org/software/bison/manual/
html_node/Contextual-Precedence.html */
| TOK_IF '(' expr ')' statement %prec TOK_ELSE {
$$ = $1 -> adopt($3, $5);
destroy($2, $4);
}
;
return : TOK_RETURN expr ';' {
$$ = $1 -> adopt($2);
destroy($3);
}
| TOK_RETURN ';' {
$$ = $1;
destroy($2);
}
;
expr : expr binop expr {
$$ = $2 -> adopt($1, $3);
}
| unop { $$ = $1; }
| allocation {
$$ = $1;
}
| call { $$ = $1; }
| '(' expr ')' {
$$ = $2;
destroy($1, $3);
}
| expr TOK_ARROW TOK_IDENT {
$3 -> adopt_sym(nullptr, TOK_FIELD);
$$ = $2 -> adopt($1, $3);
}
| variable { $$ = $1; }
| constant { $$ = $1; }
;
binop : '=' { $$ = $1; }
| TOK_EQ { $$ = $1; }
| TOK_NE { $$ = $1; }
| TOK_LT { $$ = $1; }
| TOK_LE { $$ = $1; }
| TOK_GT { $$ = $1; }
| TOK_GE { $$ = $1; }
| '+' { $$ = $1; }
| '-' { $$ = $1; }
| '*' { $$ = $1; }
| '/' { $$ = $1; }
| '%' { $$ = $1; }
;
unop : '+' expr %prec TOK_POS {
$$ = $1 -> adopt_sym($2, TOK_POS);
}
| '-' expr %prec TOK_NEG {
$$ = $1 -> adopt_sym($2, TOK_NEG);
}
| TOK_NOT expr {
$$ = $1 -> adopt($2);
}
;
// We have to have TOK_NEW in here to set symbol
allocation : TOK_NEW TOK_IDENT {
$2 -> adopt_sym(nullptr, TOK_TYPEID);
$$ = $1 -> adopt($2);
}
| TOK_NEW TOK_STRING '(' expr ')' {
$$ = $1 -> adopt_sym($4, TOK_NEWSTR);
destroy($2, $3, $5);
}
| TOK_NEW basetype '[' expr ']' {
$$ = $1 -> adopt_sym($2, TOK_NEWARRAY, $4);
destroy($3, $5);
}
;
// Clean way with left recursion
callexprs : TOK_IDENT '(' expr {
$$ = $2 -> adopt_sym($1, TOK_CALL, $3);
}
| callexprs ',' expr {
$$ = $1 -> adopt($3);
destroy($2);
}
;
call : TOK_IDENT '(' ')' {
$$ = $2 -> adopt_sym($1, TOK_CALL);
destroy($3);
}
| callexprs ')' {
$$ = $1;
destroy($2);
}
;
variable : TOK_IDENT { $$ = $1; }
| expr '[' expr ']' {
$$ = $2 -> adopt_sym($1, TOK_INDEX, $3);
destroy($4);
}
;
constant : TOK_INTCON { $$ = $1; }
| TOK_CHARCON { $$ = $1; }
| TOK_STRINGCON { $$ = $1; }
| TOK_NULL { $$ = $1; }
;
%%
const char *parser::get_tname (int symbol) {
return yytname [YYTRANSLATE (symbol)];
}
bool is_defined_token (int symbol) {
return YYTRANSLATE (symbol) > YYUNDEFTOK;
}