-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.y
696 lines (691 loc) · 19.3 KB
/
syntax.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
%{
#include"lex.yy.c"
void yyerror(const char*);
struct ASTNode *root;
struct ASTNode *currentSpecifier;
int structCheck;
int structFather;
int functionStart;
int firstAssign;
int functionCheck;
struct ArrayList *conditionStack;
struct ArrayList *jumpBlockStack;
struct ArrayList *whileIRStartStack;
void RPError(const int);
void SEMIError(const int);
void STRUCTError(const int, const char *);
void DEFError(const int);
void initial();
void fillJumpBlock();
void fillFalseList();
void fillTrueList();
#define MAX_LINE 1024
%}
%error-verbose
%union {
struct ASTNode *node;
}
%token <node> INT FLOAT CHAR ID TYPE STRUCT IF ELSE WHILE RETURN DOT SEMI COMMA FOR
%token <node> ASSIGN LT LE GT GE NE EQ PLUS MINUS MUL DIV AND OR NOT LP RP LB RB LC RC INCLUDE UNKNOW
%token <node> BREAK CONTINUE WRITE READ
%type <node> Program ExtDefList ExtDef ExtDecList
%type <node> Specifier StructSpecifier
%type <node> VarDec FunDec VarList ParamDec
%type <node> CompSt StmtList Stmt DefList Def DecList Dec
%type <node> Exp Args
%type <node> FunID STRUCTTrigger LCTrigger RCTrigger ConditionExp WHILETrigger
%type <node> M1 M2 M3
%right ASSIGN
%left OR
%left AND
%left NE EQ GE GT LE LT
%left PLUS MINUS
%left MUL DIV
%right HIGH_MINUS NOT
%left LP RP LB RB DOT
%right "M2"
%nonassoc LOWER_ELSE
%nonassoc ELSE
%nonassoc UNKNOW
%%
/* High-Level Definition */
Program: ExtDefList {
$$ = newNode("Program", @$.first_line);
root = $$;
appendChild($$, 1, $1);
};
ExtDefList: ExtDef ExtDefList {
$$ = newNode("ExtDefList", @1.first_line);
appendChild($$, 2, $1, $2);
}
| {
$$ = newNode("NONE", @$.first_line);
};
ExtDef: Specifier ExtDecList SEMI {
$$ = newNode("ExtDef", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Specifier ExtDecList error {
SEMIError(@$.first_line);
}
| Specifier SEMI {
$$ = newNode("ExtDef", @$.first_line);
appendChild($$, 2, $1, $2);
put_struct_specifier(currentSpecifier);
}
| Specifier error {
SEMIError(@$.first_line);
}
| Specifier FunDec CompSt {
$$ = newNode("ExtDef", @$.first_line);
appendChild($$, 3, $1, $2, $3);
if (functionCheck == 0) {
// function end
if (currentFunction->re == 0) {
fprintf(out, "Error at Line %d: function has no return value", @$.first_line);
}
function_stack_pop();
hash_table_stack_pop();
}
functionCheck = 0;
};
ExtDecList: VarDec {
$$ = newNode("ExtDecList", @$.first_line);
appendChild($$, 1, $1);
// put varDec
put_var(currentSpecifier, $1);
// IR var num allocation finish
}
| VarDec COMMA ExtDecList {
$$ = newNode("ExtDecList", @$.first_line);
appendChild($$, 3, $1, $2, $3);
// put varDec
put_var(currentSpecifier, $1);
// IR var num allocation finish
};
/* specifier */
Specifier: TYPE {
$$ = newNode("Specifier", @$.first_line);
appendChild($$, 1, $1);
currentSpecifier = $1;
}
| StructSpecifier {
$$ = newNode("Specifier", @$.first_line);
appendChild($$, 1, $1);
currentSpecifier = $1;
};
StructSpecifier: STRUCTTrigger ID LC DefList RC {
$$ = newNode("StructSpecifier", @$.first_line);
appendChild($$, 5, $1, $2, $3, $4, $5);
if (structFather) {
structCheck = 1;
structFather = 0;
} else {
structCheck = 0;
}
}
| STRUCTTrigger ID {
$$ = newNode("StructSpecifier", @$.first_line);
appendChild($$, 2, $1, $2);
if (structFather) {
structCheck = 1;
structFather = 0;
} else {
structCheck = 0;
}
};
STRUCTTrigger: STRUCT {
if (structCheck) {
structFather = 1;
} else {
structCheck = 1;
}
$$ = $1;
};
/* declarator */
VarDec: ID {
$$ = newNode("VarDec", @$.first_line);
appendChild($$, 1, $1);
}
| VarDec LB INT RB {
$$ = newNode("VarDec", @$.first_line);
appendChild($$, 4, $1, $2, $3, $4);
};
FunDec: FunID LP VarList RP {
$$ = newNode("FunDec", @$.first_line);
appendChild($$, 4, $1, $2, $3, $4);
append_new_block();
}
| FunID LP VarList error {
RPError(@$.first_line);
}
| FunID LP RP {
$$ = newNode("FunDec", @$.first_line);
appendChild($$, 3, $1, $2, $3);
append_new_block();
}
| FunID LP error {
RPError(@$.first_line);
};
FunID: ID {
$$ = newNode("FunID", @$.first_line);
appendChild($$, 1, $1);
// function start
functionCheck = function_stack_push(currentSpecifier, $1->value, @$.first_line);
if (functionCheck == 0) {
currentScopeNumber++;
hash_table_stack_push(currentFunction->hashTable);
functionStart = 1;
// function IR start
IRInstruct *func_instruct = append_new_instruct(blockEnd);
func_instruct->funcName = (char *)$1->value;
func_instruct->type = _FUNCTION;
append_new_block();
// IR end
}
};
VarList: ParamDec COMMA VarList {
$$ = newNode("VarList", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| ParamDec {
$$ = newNode("VarList", @$.first_line);
appendChild($$, 1, $1);
};
ParamDec: Specifier VarDec {
$$ = newNode("ParamDec", @$.first_line);
appendChild($$, 2, $1, $2);
if (functionCheck == 0) {
// parameter IR start
put_para($1->child[0], $2);
// IR end
}
};
/* statement */
CompSt: LCTrigger DefList StmtList RCTrigger {
$$ = newNode("CompSt", @$.first_line);
appendChild($$, 4, $1, $2, $3, $4);
}
| LC error DefList StmtList RC {
DEFError(@2.first_line);
};
LCTrigger: LC {
$$ = $1;
if (functionCheck == 0) {
if (functionStart == 0) {
if (currentSymbolTable == NULL) {
currentSymbolTable = new_symbol_table();
} else {
SymbolTable *symbolTable = new_symbol_table();
symbolTable->next = currentSymbolTable;
currentSymbolTable = symbolTable;
}
currentScopeNumber++;
} else {
functionStart = 0;
}
}
}
RCTrigger: RC {
$$ = $1;
if (functionCheck == 0) {
SymbolTable *remove = currentSymbolTable;
if (remove) {
currentSymbolTable = currentSymbolTable->next;
symbol_table_remove(currentTable, remove);
}
currentScopeNumber--;
}
}
StmtList: Stmt StmtList {
$$ = newNode("StmtList", @$.first_line);
appendChild($$, 2, $1, $2);
}
| {
$$ = newNode("NONE", @$.first_line);
};
Stmt: Exp SEMI {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 2, $1, $2);
if (functionCheck == 0) {
check_exp($1);
}
}
| Exp error {
SEMIError(@$.first_line);
}
| CompSt {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 1, $1);
}
| RETURN Exp SEMI {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 3, $1, $2, $3);
if (functionCheck == 0) {
check_return_exp($2);
currentFunction->re = 1;
}
}
| RETURN Exp error {
SEMIError(@$.first_line);
}
| IF LP ConditionExp RP M1 Stmt %prec LOWER_ELSE {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 5, $1, $2, $3, $4, $6);
// end of condition
append_new_block();
fillFalseList();
// IR end
}
| IF LP ConditionExp error Stmt %prec LOWER_ELSE {
RPError(@$.first_line);
}
| IF LP ConditionExp RP M1 Stmt ELSE M2 Stmt {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 7, $1, $2, $3, $4, $6, $7, $8);
// end of if IR start
IRBlock *b1 = get_last_from_array_list(jumpBlockStack);
jumpBlockStack->memberNum--;
append_new_block();
b1->next = blockEnd;
// IR end
}
| IF LP ConditionExp error Stmt ELSE M2 Stmt {
RPError(@$.first_line);
}
| WHILETrigger LP ConditionExp RP M3 Stmt {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 5, $1, $2, $3, $4, $6);
if (currentLoop) {
LoopStack *temp = currentLoop;
currentLoop = currentLoop->next;
temp->next = NULL;
free(temp);
}
// end of while IR start
IRBlock *before = blockEnd;
append_new_block();
IRBlock *whileIRStart = get_last_from_array_list(whileIRStartStack);
before->next = whileIRStart;
whileIRStartStack->memberNum--;
fillFalseList();
// IR end
}
| WHILETrigger LP ConditionExp error Stmt {
RPError(@$.first_line);
}
| BREAK SEMI {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 2, $1, $2);
if (currentLoop == NULL) {
fprintf(out, "Error at Line %d: break is used outside loop\n", @$.first_line);
}
}
| CONTINUE SEMI {
$$ = newNode("Stmt", @$.first_line);
appendChild($$, 2, $1, $2);
if (currentLoop == NULL) {
fprintf(out, "Error at Line %d: continue is used outside loop\n", @$.first_line);
}
}
;
M1: {
$$ = newNode("NONE", @$.first_line);
// true jump IR start
append_new_block();
fillTrueList();
// IR end
}
;
M2: {
$$ = newNode("NONE", @$.first_line);
// false jump IR start
append_to_array_list(jumpBlockStack, blockEnd);
append_new_block();
fillFalseList();
// IR end
}
;
M3: {
$$ = newNode("NONE", @$.first_line);
// true jump in while IR start
append_new_block();
fillTrueList();
// IR end
}
;
WHILETrigger: WHILE {
$$ = $1;
if (currentLoop == NULL) {
currentLoop = new_loop_stack();
} else {
LoopStack *newLoop = new_loop_stack();
newLoop->next = currentLoop;
currentLoop = newLoop;
}
// while start IR start
append_new_block();
append_to_array_list(whileIRStartStack, blockEnd);
// IR end
};
ConditionExp: Exp {
$$ = $1;
if (functionCheck == 0) {
append_to_array_list(conditionStack, check_condition($1));
}
};
/* local definition */
DefList: Def DefList {
$$ = newNode("DefList", @$.first_line);
appendChild($$, 2, $1, $2);
if (functionCheck == 0) {
if (strcmp($2->type, "NONE") == 0) {
$$->value = malloc(sizeof(int));
*(int *)$$->value = *(int*)$1->value + 0;
} else {
$$->value = malloc(sizeof(int));
memcpy($$->value, $2->value, sizeof(int));
*(int*)$$->value = *(int *)$$->value + *(int *)$1->value;
}
}
}
| {
$$ = newNode("NONE", @$.first_line);
};
Def: Specifier DecList SEMI {
$$ = newNode("Def", @$.first_line);
appendChild($$, 3, $1, $2, $3);
if (functionCheck == 0) {
$$->value = $2->value;
}
}
| Specifier DecList error {
SEMIError(@$.first_line);
};
DecList: Dec {
$$ = newNode("DecList", @$.first_line);
appendChild($$, 1, $1);
$$->value = malloc(sizeof(int));
*((int *)$$->value) = 1;
}
| Dec COMMA DecList {
$$ = newNode("DecList", @$.first_line);
appendChild($$, 3, $1, $2, $3);
$$->value = malloc(sizeof(int));
*((int *)$$->value) = *((int *)$3->value) + 1;
}
;
Dec: VarDec {
$$ = newNode("Dec", @$.first_line);
appendChild($$, 1, $1);
if (structCheck == 0) {
// put varDec
put_var(currentSpecifier, $1);
// IR var num allocation
}
}
| VarDec ASSIGN Exp {
$$ = newNode("Dec", @$.first_line);
appendChild($$, 3, $1, $2, $3);
if (structCheck == 0) {
// put varDec
int re = put_var(currentSpecifier, $1);
// IR var num allocation
if (re == 0) {
// assign check here
check_assign_exp($1, $3);
}
}
};
/* Expression */
Exp: Exp ASSIGN Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp AND Exp
{
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp OR Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp LT Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp LE Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp GT Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp GE Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp NE Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp EQ Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp PLUS Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp MINUS Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp MUL Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp DIV Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp UNKNOW Exp {
STRUCTError(@$.first_line, $2->value);
}
| LP Exp RP {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| LP Exp error {
RPError(@$.first_line);
}
| MINUS Exp %prec HIGH_MINUS {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 2, $1, $2);
}
| NOT Exp {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 2, $1, $2);
}
| ID LP Args RP {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 4, $1, $2, $3, $4);
}
| ID LP Args error {
RPError(@$.first_line);
}
| ID LP RP {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| ID LP error {
RPError(@$.first_line);
}
| Exp LB Exp RB {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 4, $1, $2, $3, $4);
}
| Exp DOT ID {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| ID {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 1, $1);
}
| INT {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 1, $1);
}
| FLOAT {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 1, $1);
}
| CHAR {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 1, $1);
}
| UNKNOW {
}
| READ LP RP {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| WRITE LP Exp RP {
$$ = newNode("Exp", @$.first_line);
appendChild($$, 4, $1, $2, $3, $4);
}
;
Args: Exp COMMA Args {
$$ = newNode("Args", @$.first_line);
appendChild($$, 3, $1, $2, $3);
}
| Exp {
$$ = newNode("Args", @$.first_line);
appendChild($$, 1, $1);
}
;
%%
void fillTrueList() {
TypeCheck *conditionType = get_last_from_array_list(conditionStack);
ArrayList *trueList = conditionType->trueList;
if (trueList) {
back_patching_true_list(trueList, blockEnd);
free_array_list(conditionType->trueList);
}
conditionType->trueList = NULL;
}
void fillFalseList() {
TypeCheck *conditionType = get_last_from_array_list(conditionStack);
ArrayList *falseList = conditionType->falseList;
if (falseList) {
back_patching_false_list(falseList, blockEnd);
free_array_list(conditionType->falseList);
}
conditionType->falseList = NULL;
conditionStack->memberNum--;
}
void RPError(const int lineno){
fprintf(out, "Error type B at Line %d: Missing closing parenthesis ')'\n", lineno);
error = 1;
}
void SEMIError(const int lineno){
fprintf(out, "Error type B at Line %d: Missing semicolon ';'\n", lineno);
error = 1;
}
void STRUCTError(const int lineno, const char *text){
fprintf(out, "Error type B at Line %d: Unknow operator %s\n", lineno, text);
error = 1;
}
void DEFError(const int lineno){
fprintf(out, "Error type B at Line %d: Invalid Definition\n", lineno);
error = 1;
}
void yyerror(const char *s){
// fprintf(out, "%s", s);
}
void initial(){
yycolno = 1;
yylineno = 1;
yytext = "";
yyleng = 0;
error = 0;
wrong = 0;
rec = "";
head = NULL;
node = NULL;
neasted_start = 0;
root = NULL;
currentTable = new_hash_table();
currentScopeNumber = 0;
structCheck = 0;
structFather = 0;
firstAssign = 0;
functionStart = 0;
functionCheck = 0;
init_number_control();
init_IR_block();
conditionStack = new_array_list();
whileIRStartStack = new_array_list();
jumpBlockStack = new_array_list();
}
#ifndef CALC_MAIN
#else
int main(int count, char **args){
if (count == 1){
initial();
out = stdout;
yyparse();
// if (error == 0) {
// dfsPrintf(root, 0);
// }
remove_empty_blocks();
rebuild_blocks();
check_label_reference();
propagation_folding_replacement();
dead_code_elimination();
printf_all_blocks();
} else {
for(int x = 1; x < count; x++){
initial();
int length = strlen(args[x]);
char *output = malloc(length + 1);
strcpy(output, args[x]);
output[length] = 0;
output[length - 1] = 0;
output[length - 2] = 'r';
output[length - 3] = 'i';
char buf[MAX_LINE];
int len;
if ((fp = fopen(args[x],"r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
out = fopen(output, "w");
yyrestart(fp);
yyparse();
// if (error == 0)
// dfsPrintf(root, 0);
remove_empty_blocks();
rebuild_blocks();
check_label_reference();
propagation_folding_replacement();
dead_code_elimination();
printf_all_blocks();
fclose(fp);
fclose(out);
}
return 0;
}
}
#endif
int evaluate(char *expr){
YY_BUFFER_STATE buf;
buf = yy_scan_string(expr);
yyparse();
yy_delete_buffer(buf);
return 0;
}