-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.y
353 lines (321 loc) · 11.5 KB
/
parser.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
%{
#include <stdio.h>
#include <stdlib.h>
#include "code.h"
typedef char* string;
extern FILE* codegen;
extern char* yytext;
int local_var_idx = 1;
%}
%union {
int ival;
char* sval;
}
%token DIGITALWRITE DELAY
%token TYPE_INT TYPE_VOID IDENT INT
%token ASSIGN COMMA INT IF ELSE DO WHILE FOR RETURN
%token LESS LESS_EQUAL GREATER GREATER_EQUAL EQUAL NOT_EQUAL
%token PLUS MINUS INCREMENT DECREMENT MULTIPLE DIVIDE
%token AND OR L_BRACKET R_BRACKET L_PARENTHESIS R_PARENTHESIS EOL IDENT
%type <sval> function_declaration expression declaration param prefunction
%type <sval> IDENT primary
%type <ival> INT
%left EOL
%left ASSIGN
%left IDENT
%left COMMA
%left OR
%left AND
%left EQUAL NOT_EQUAL
%left GREATER GREATER_EQUAL LESS LESS_EQUAL
%left PLUS MINUS
%left MULTIPLY DIVIDE
%right INCREMENT DECREMENT
%start program
%%
program: functions;
functions: functions function
| function
;
function: prefunction
declarations {
int index = look_up_symbol($1);
for (int i = index + 1; i < cur_counter; ++i) {
int val_idx = look_up_symbol_kw(table[i].name) + 1;
if (table[i].scope != cur_scope) break;
if (symbol_t[val_idx].token != INT) continue;
local_var_idx = 0;
fprintf(codegen, " \n");
}
}
statements {
pop_up_symbol(cur_scope);
--cur_scope;
code_gen_at_end_of_function_body($1);
} R_PARENTHESIS
| prefunction statements {
pop_up_symbol(cur_scope);
--cur_scope;
code_gen_at_end_of_function_body($1);
}
R_PARENTHESIS
| function_declaration EOL;
prefunction: function_declaration {
++cur_scope;
code_gen_func_header($1);
set_scope_and_offset_of_param($1);
$$ = $1;
}
L_PARENTHESIS;
function_declaration: type IDENT {
if (look_up_symbol($2) == -1)
$$ = install_symbol($2);
}
L_BRACKET param_list R_BRACKET {
$$ = $2;
}
;
param_list: param_list COMMA param
| param
;
param: /* empty */ {}
| type IDENT {
$$ = install_symbol($2);
}
;
declarations: declaration
| declarations declaration;
declaration: type IDENT {
$$ = install_symbol($2);
} ASSIGN expression EOL {
int index = look_up_symbol($2);
table[index].scope = cur_scope;
table[index].mode = LOCAL_MODE;
table[index].offset = local_var_idx;
++local_var_idx;
}
| type IDENT EOL {
$$ = install_symbol($2);
int index = look_up_symbol($2);
table[index].scope = cur_scope;
table[index].mode = LOCAL_MODE;
table[index].offset = local_var_idx;
++local_var_idx;
}
;
statements: statement
| statements statement;
statement: IDENT ASSIGN expression EOL { // assign statement
int index = look_up_symbol($1);
fprintf(codegen, " lw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
fprintf(codegen, " \n");
}
| DIGITALWRITE L_BRACKET INT COMMA INT R_BRACKET EOL{
fprintf(codegen, " li t0, %d\n", $5);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
fprintf(codegen, " li t0, %d\n", $3);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
fprintf(codegen, " lw a0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw a1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " sw ra, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
fprintf(codegen, " jal ra, digitalWrite\n");
fprintf(codegen, " lw ra, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " \n");
}
| DELAY L_BRACKET expression R_BRACKET EOL {
fprintf(codegen, " lw a0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " sw ra, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
fprintf(codegen, " jal ra, delay\n");
fprintf(codegen, " lw ra, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " \n");
}
| if_stmt ELSE {
fprintf(codegen, " j L3\n");
fprintf(codegen, " \n");
fprintf(codegen, "L2:\n");
} L_PARENTHESIS statements R_PARENTHESIS {
fprintf(codegen, " \n");
fprintf(codegen, "L3:\n");
}
| if_stmt {
fprintf(codegen, " \n");
fprintf(codegen, "L2:\n");
}
| WHILE L_BRACKET {
fprintf(codegen, " \n");
fprintf(codegen, "C2:\n");
} expression {
if ($4 != NULL) {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " beq zero, t0, L2\n");
}
} R_BRACKET L_PARENTHESIS {
} statements {
fprintf(codegen, " j C2\n");
fprintf(codegen, " \n");
fprintf(codegen, "L2:\n");
} R_PARENTHESIS
| DO L_PARENTHESIS {
fprintf(codegen, " \n");
fprintf(codegen, "L2:\n");
} statements R_PARENTHESIS WHILE L_BRACKET expression {
if ($8 != NULL) {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " beq zero, to, L2\n");
}
} R_BRACKET EOL
;
if_stmt: IF L_BRACKET expression {
if ($3 != NULL) {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " beq zero, t0, L2\n");
}
} R_BRACKET L_PARENTHESIS statements R_PARENTHESIS;
type: TYPE_VOID
| TYPE_INT;
expression: primary {
$$ = $1;
}
| L_BRACKET expression R_BRACKET {
$$ = NULL;
}
| expression PLUS expression {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw t1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " add t0, t0, t1\n");
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = NULL;
}
| expression MINUS expression {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw t1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " sub t0, t1, t0\n");
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = NULL;
}
| expression MULTIPLY expression {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw t1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " mul t0, t0, t1\n");
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = NULL;
}
| expression DIVIDE expression {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw t1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " div t0, t1, t0\n");
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = NULL;
}
| expression EQUAL expression {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw t1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " bne t0, t1, L2\n");
$$ = NULL;
}
| expression LESS_EQUAL expression {
fprintf(codegen, " lw t0, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw t1, 0(sp)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " addi t0, t0, 1\n");
fprintf(codegen, " blt t1, t0, L2\n");
$$ = NULL;
}
;
primary: IDENT DECREMENT {
int index = look_up_symbol($1);
fprintf(codegen, " lw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " addi t1, t0, -1\n");
fprintf(codegen, " sw t1, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = $1;
}
| IDENT INCREMENT {
int index = look_up_symbol($1);
fprintf(codegen, " lw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " addi t1, t0, 1\n");
fprintf(codegen, " sw t1, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = $1;
}
| DECREMENT IDENT {
int index = look_up_symbol($2);
fprintf(codegen, " lw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " addi t0, t0, -1\n");
fprintf(codegen, " sw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = $2;
}
| INCREMENT IDENT {
int index = look_up_symbol($2);
fprintf(codegen, " lw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " addi t0, t0, 1\n");
fprintf(codegen, " sw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = $2;
}
| IDENT {
int index = look_up_symbol($1);
fprintf(codegen, " lw t0, %d(s0)\n", table[index].offset * (-4) - 48);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = $1;
}
| INT {
fprintf(codegen, " li t0, %d\n", $1);
fprintf(codegen, " sw t0, -4(sp)\n");
fprintf(codegen, " addi sp, sp, -4\n");
$$ = NULL;
};
%%
int yylex();
int main(void) {
if ((codegen = fopen("codegen.S", "w")) == NULL) {
printf("Error\n");
exit(1);
}
fprintf(codegen, ".global codegen\n");
init_symbol_table();
yyparse();
printf("parse successfully.\n");
fprintf(codegen, "\n");
return 0;
}
void yyerror(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
fprintf(stderr, "Unmatched: %s\n", yytext);
exit(1);
}