This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
minic.y
475 lines (410 loc) · 10 KB
/
minic.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
%{
#include <stdio.h>
#include <string.h>
#include "minic_ast.h"
#include "minic.h"
#include "ucode.h"
extern FILE *yyin;
int yylex();
void yyerror(const char *s);
Node* root;
%}
%union {
struct nodeType *ast;
int ival;
char* string;
}
%token TCONST TELSE TIF TINT TRETURN TVOID TWHILE TEQUAL TNOTEQU
TLESSE TGREATE TAND TOR TINC TDEC TADDASSIGN TSUBASSIGN TMULASSIGN
TDIVASSIGN TMODASSIGN
%token <string> TIDENT TNUMBER
%type <ast> function_def translation_unit external_dcl function_name function_header compound_st
dcl_spec formal_param opt_dcl_list declaration_list declaration dcl_specifiers dcl_specifier
type_specifier type_qualifier opt_formal_param formal_param_list param_dcl init_dcl_list init_declarator
declarator opt_number opt_stat_list statement_list statement expression_st opt_expression if_st
while_st return_st expression assignment_exp actual_param actual_param_list unary_exp postfix_exp primary_exp
logical_or_exp logical_and_exp equality_exp relational_exp additive_exp multiplicative_exp opt_actual_param
%nonassoc LOWER_THAN_TELSE
%nonassoc TELSE
%%
mini_c: translation_unit {
root = buildTree(PROGRAM, $1);
}
;
translation_unit: external_dcl {
$$ = $1;
}
| translation_unit external_dcl {
appendNext($1, $2);
$$ = $1;
}
;
external_dcl: function_def {
$$ = $1;
}
| declaration {
$$ = $1;
}
;
function_def: function_header compound_st {
appendNext($1, $2);
$$ = buildTree(FUNC_DEF, $1);
}
;
function_header: dcl_spec function_name formal_param {
appendNext($1, $2);
appendNext($2, $3);
$$ = buildTree(FUNC_HEAD, $1);
}
;
dcl_spec: dcl_specifiers {
$$ = buildTree(DCL_SPEC, $1);
}
;
dcl_specifiers: dcl_specifier {
$$ = $1;
}
| dcl_specifiers dcl_specifier {
appendNext($1, $2);
$$ = $1;
}
;
dcl_specifier: type_qualifier { $$ = $1; }
| type_specifier { $$ = $1; }
;
type_qualifier: TCONST {
$$ = buildTree(CONST_NODE, NULL);
}
;
type_specifier: TINT {
$$ = buildTree(INT_NODE, NULL);
}
| TVOID {
$$ = buildTree(VOID_NODE, NULL);
}
;
function_name: TIDENT { $$ = buildNode(IDENT, $1); }
;
formal_param: '(' opt_formal_param ')' {
$$ = buildTree(FORMAL_PARA, $2);
}
;
opt_formal_param: formal_param_list { $$ = $1; }
| { $$ = NULL; }
;
formal_param_list: param_dcl {
$$ = $1;
}
| formal_param_list ',' param_dcl {
appendNext($1, $3);
$$ = $1;
}
;
param_dcl: dcl_spec declarator {
appendNext($1, $2);
$$ = buildTree(PARAM_DCL, $1);
}
;
compound_st: '{' opt_dcl_list opt_stat_list '}' {
appendNext($2, $3);
$$ = buildTree(COMPOUND_ST, $2);
}
;
opt_dcl_list: declaration_list {
$$ = buildTree(DCL_LIST, $1);
}
| {
$$ = buildTree(DCL_LIST, NULL);
}
;
declaration_list: declaration {
$$ = $1;
}
| declaration_list declaration {
appendNext($1, $2);
$$ = $1;
}
;
declaration: dcl_spec init_dcl_list ';' {
appendNext($1, $2);
$$ = buildTree(DCL, $1);
}
;
init_dcl_list: init_declarator {
$$ = $1;
}
| init_dcl_list ',' init_declarator {
appendNext($1, $3);
$$ = $1;
}
;
init_declarator: declarator {
$$ = $1;
}
| declarator '=' TNUMBER {
appendNext($1->son, buildNode(IDENT, $3)); // @todo 이거 구조 맞나?
$$ = $1;
}
;
declarator: TIDENT {
Node* ptr = buildTree(SIMPLE_VAR, buildNode(IDENT, $1));
$$ = buildTree(DCL_ITEM, ptr);
}
| TIDENT '[' opt_number ']' {
Node* ptr = buildNode(IDENT, $1);
appendNext(ptr, $3);
$$ = buildTree(DCL_ITEM, buildTree(ARRAY_VAR, ptr));
}
;
opt_number: TNUMBER {
$$ = buildNode(NUMBER, $1);
}
| {
$$ = NULL;
}
;
opt_stat_list: statement_list {
$$ = buildTree(STAT_LIST, $1);
}
| { $$ = NULL; }
;
statement_list: statement {
$$ = $1;
}
| statement_list statement {
appendNext($1, $2);
$$ = $1;
}
;
statement: compound_st { $$ = $1; }
| expression_st { $$ = $1; }
| if_st { $$ = $1; }
| while_st { $$ = $1; }
| return_st { $$ = $1; }
;
expression_st: opt_expression ';' {
$$ = buildTree(EXP_ST, $1);
}
;
opt_expression: expression { $$ = $1; }
| { $$ = NULL; }
;
if_st: TIF '(' expression ')' statement %prec LOWER_THAN_TELSE {
appendNext($3, $5);
$$ = buildTree(IF_ST, $3);
}
| TIF '(' expression ')' statement TELSE statement {
appendNext($3, $5);
appendNext($5, $7);
$$ = buildTree(IF_ELSE_ST, $3);
}
;
while_st: TWHILE '(' expression ')' statement {
appendNext($3, $5);
$$ = buildTree(WHILE_ST, $3);
}
;
return_st: TRETURN opt_expression ';' {
$$ = buildTree(RETURN_ST, $2);
}
;
expression: assignment_exp { $$ = $1; }
;
assignment_exp: logical_or_exp { $$ = $1; }
| unary_exp '=' assignment_exp {
appendNext($1, $3);
$$ = buildTree(ASSIGN_OP, $1);
}
| unary_exp TADDASSIGN assignment_exp {
appendNext($1, $3);
$$ = buildTree(ADD_ASSIGN, $1);
}
| unary_exp TSUBASSIGN assignment_exp {
appendNext($1, $3);
$$ = buildTree(SUB_ASSIGN, $1);
}
| unary_exp TMULASSIGN assignment_exp {
appendNext($1, $3);
$$ = buildTree(MUL_ASSIGN, $1);
}
| unary_exp TDIVASSIGN assignment_exp {
appendNext($1, $3);
$$ = buildTree(DIV_ASSIGN, $1);
}
| unary_exp TMODASSIGN assignment_exp {
appendNext($1, $3);
$$ = buildTree(MOD_ASSIGN, $1);
}
;
logical_or_exp: logical_and_exp { $$ = $1; }
| logical_or_exp TOR logical_and_exp {
appendNext($1, $3);
$$ = buildTree(LOGICAL_OR, $1);
}
;
logical_and_exp: equality_exp { $$ = $1; }
| logical_and_exp TAND equality_exp {
appendNext($1, $3);
$$ = buildTree(LOGICAL_AND, $1);
}
;
equality_exp: relational_exp { $$ = $1; }
| equality_exp TEQUAL relational_exp {
appendNext($1, $3);
$$ = buildTree(EQ, $1);
}
| equality_exp TNOTEQU relational_exp {
appendNext($1, $3);
$$ = buildTree(NE, $1);
}
;
relational_exp: additive_exp { $$ = $1; }
| relational_exp '>' additive_exp {
appendNext($1, $3);
$$ = buildTree(GT, $1);
}
| relational_exp '<' additive_exp {
appendNext($1, $3);
$$ = buildTree(LT, $1);
}
| relational_exp TGREATE additive_exp {
appendNext($1, $3);
$$ = buildTree(GE, $1);
}
| relational_exp TLESSE additive_exp {
appendNext($1, $3);
$$ = buildTree(LE, $1);
}
;
additive_exp: multiplicative_exp { $$ = $1; }
| additive_exp '+' multiplicative_exp {
appendNext($1, $3);
$$ = buildTree(ADD, $1);
}
| additive_exp '-' multiplicative_exp {
appendNext($1, $3);
$$ = buildTree(SUB, $1);
}
;
multiplicative_exp: unary_exp { $$ = $1; }
| multiplicative_exp '*' unary_exp {
appendNext($1, $3);
$$ = buildTree(MUL, $1);
}
| multiplicative_exp '/' unary_exp {
appendNext($1, $3);
$$ = buildTree(DIV, $1);
}
| multiplicative_exp '%' unary_exp {
appendNext($1, $3);
$$ = buildTree(MOD, $1);
}
;
unary_exp: postfix_exp { $$ = $1; }
| '-' unary_exp {
$$ = buildTree(UNARY_MINUS, $2);
}
| '!' unary_exp {
$$ = buildTree(LOGICAL_NOT, $2);
}
| TINC unary_exp {
$$ = buildTree(PRE_INC, $2);
}
| TDEC unary_exp {
$$ = buildTree(PRE_DEC, $2);
}
;
postfix_exp: primary_exp { $$ = $1; }
| postfix_exp '[' expression ']' {
appendNext($1, $3);
$$ = buildTree(INDEX, $1);
}
| postfix_exp '(' opt_actual_param ')' {
appendNext($1, $3);
$$ = buildTree(CALL, $1);
}
| postfix_exp TINC {
$$ = buildTree(POST_INC, $1);
}
| postfix_exp TDEC {
$$ = buildTree(POST_DEC, $1);
}
;
opt_actual_param: actual_param { $$ = $1; }
| { $$ = NULL; }
;
actual_param: actual_param_list {
$$ = buildTree(ACTUAL_PARAM, $1);
}
;
actual_param_list: assignment_exp {
$$ = $1;
}
| actual_param_list ',' assignment_exp {
appendNext($1, $3);
$$ = $1;
}
;
primary_exp: TIDENT {
$$ = buildNode(IDENT, $1);
}
| TNUMBER {
$$ = buildNode(NUMBER, $1);
}
| '(' expression ')' { // ex. (30 * 20) + 4
$$ = $2;
}
;
%%
char* toString(char* string)
{
char* str;
str = (char*)malloc(strlen(string) + 1);
strcpy(str, string);
return str;
}
Node* parse(FILE *sourceFile)
{
yyin = sourceFile;
do{
yyparse();
} while(!feof(yyin));
return root;
}
int main(int argc, char *argv[]){
FILE *sourceFile;
FILE *astFile, *ucoFile;
char filename[100];
Node *root;
if (argc != 2) {
fprintf(stderr, "arguments not valid.");
return -1;
}
strcpy(filename, argv[1]);
sourceFile = fopen(filename, "r");
astFile = fopen(strcat(strtok(filename, "."), ".ast"), "w");
ucoFile = fopen(strcat(strtok(filename, "."), ".uco"), "w");
if(!sourceFile) {
fprintf(stderr, "source file not open!\n");
return -1;
}
if(!astFile) {
fprintf(stderr, "ast file not not open!\n");
return -1;
}
if(!ucoFile) {
fprintf(stderr, "uco file not not open!\n");
return -1;
}
printf("Start Parsing..\n");
root = parse(sourceFile);
printTree(root, 0, astFile);
printf("End Parsing!\n");
printf("Start Code Generate..\n");
codeGen(root, ucoFile);
printf("End Code Generate!\n");
fclose(sourceFile);
fclose(astFile);
fclose(ucoFile);
return 1;
}