-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_hw3.y
677 lines (653 loc) · 13.5 KB
/
compiler_hw3.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
/* Please feel free to modify any content */
/* Definition section */
%{
#include "compiler_hw_common.h" //Extern variables that communicate with lex
#include "mySymbolTable.h"
#include "myCodeGen.h"
#include "mySwitch.h"
//#define YYDEBUG 1
//int yydebug = 1;
char* free_cat(char*a,char*b){
int n=strlen(a)+strlen(b)+1;
char* ret=malloc(n);
strcpy(ret,a);
strcat(ret,b);
free(a);
free(b);
return ret;
}
char* cdup(char c){
static char temp[2];
temp[1]=0;
temp[0]=c;
return strdup(temp);
}
extern int yylineno;
extern int yylex();
extern FILE *yyin;
int reduce_line=1;
int yylex_destroy ();
void yyerror (char const *s)
{
printf("error:%d: %s\n", reduce_line, s);
g_has_error=true;
}
/* Used to generate code */
/* As printf; the usage: CODEGEN("%d - %s\n", 100, "Hello world"); */
/* We do not enforce the use of this macro */
/* Global variables */
bool g_has_error = false;
%}
%error-verbose
/* Use variable or self-defined structure to represent
* nonterminal and token type
* - you can add new fields if needed.
*/
%union {
char* str_lit;
int int_lit;
float float_lit;
bool bool_lit;
char* func_sig;
char* name;
unsigned char type;
int label;
struct forloop_node for_labels;
struct case_node case_data;
struct switch_node switch_data;
/* ... */
}
/* Token without return */
%token VAR NEWLINE
%token INT FLOAT BOOL STRING
%token INC DEC
%token GEQ LOR LAND EQL
%token ADD_ASSIGN SUB_ASSIGN QUO_ASSIGN MUL_ASSIGN REM_ASSIGN
%token IF ELSE FOR SWITCH CASE DEFAULT
%token PRINTLN PACKAGE FUNC PRINT RETURN
/* Token with return, which need to sepcify type */
%token <int_lit> INT_LIT
%token <float_lit> FLOAT_LIT
%token <str_lit> STRING_LIT
%token <bool_lit> BOOL_LIT
%token <name> IDENT
%type <type> expr fac term int_lit float_lit bool_lit equ_expr cmp_expr land_expr lor_expr string_lit ident dec_assign
%type <type> type nonvoid_type exprable_type
%type <func_sig> paradec paradecs paradecstart
%type <name> funchead
%type <label> for ifbody ifblockelsebody
%type <for_labels> forbody
%type <case_data> casebody defaultbody
%type <switch_data> switchhalfblock
%type <int_lit> case_lit
%start Program
%%
newline
: NEWLINE {
reduce_line++;
}
;
for
: FOR {
$$=label_cnt++;
LABELGEN($$);
}
ident
: IDENT {
$$=myload($1);
}
;
Program
: newlines package methods {
}
;
package
: PACKAGE IDENT {
code_begin();
}
;
methods
: method newline methods
| method
;
method
: funcbody newlines '{' lines '}' {
dump_symbol();
method_end();
}
|
;
lines
: line newline lines
| line
;
newlines
: newline newlines
|
;
int_lit
: INT_LIT {
$$=type_i;
CODEGEN("ldc %d\n",$1);
}
;
float_lit
: FLOAT_LIT {
$$=type_f;
CODEGEN("ldc %f\n",$1);
}
;
bool_lit
: BOOL_LIT {
$$=type_b;
if($1){
CODEGEN("iconst_1\n");
}else{
CODEGEN("iconst_0\n");
}
}
;
string_lit
: '"' STRING_LIT '"' {
$$=type_s;
CODEGEN("ldc \"%s\"\n",$2);
}
;
parastart
: paras
|
;
paras
: paras ',' lor_expr
| lor_expr
;
paradecstart
: paradecs {
$$=$1;
}
| {
$$=strdup("");
}
;
paradecs
: paradecs ',' paradec {
$$=free_cat($1,$3);
}
| paradec {
$$=$1;
}
;
exprable_type
: INT {
$$=type_i;
}
| FLOAT {
$$=type_f;
}
| BOOL {
$$=type_b;
}
;
nonvoid_type
: exprable_type {
$$=$1;
}
| STRING {
$$=type_s;
}
;
type
: nonvoid_type {
$$=$1;
}
| {
$$=type_void;
}
paradec
: IDENT nonvoid_type {
reduce_line++;
insert_symbol($1,$2);
reduce_line--;
$$=cdup(get_type($2));
}
;
funchead
: FUNC IDENT {
create_symbol();
$$=$2;
}
;
funcbody
: funchead '(' paradecstart ')' type {
if(strcmp($1,"main")==0&&strcmp($3,"")==0&&$5==type_void){
main_method_begin();
}else{
method_begin($1,$3,$5);
}
}
;
blockbody
: {
create_symbol();
}
;
dec_assign
: '=' lor_expr {
$$=$2;
}
| {
$$=type_void;
}
;
forbody
: for lor_expr {
$$.again=$1;
$$.out=label_cnt++;
CODEGEN("ifeq Label%d\n",$$.out);
check_bool_condition($2);
create_symbol();
}
;
ifbody
: IF lor_expr {
check_bool_condition($2);
$$=label_cnt++;
CODEGEN("ifeq Label%d\n",$$);
create_symbol();
}
;
ifblockelsebody
: ifbody newlines '{' lines '}' ELSE {
$$=label_cnt++;
dump_symbol();
CODEGEN("goto Label%d",$$);
LABELGEN($1);
create_symbol();
}
casebody
: CASE case_lit ':' {
$$.key=$2;
$$.label=label_cnt++;
$$.isdefault=false;
$$.next=NULL;
LABELGEN($$.label);
create_symbol();
}
;
defaultbody
: DEFAULT ':' {
$$.label=label_cnt++;
$$.isdefault=true;
$$.next=NULL;
LABELGEN($$.label);
create_symbol();
}
;
switchhalfblock
: SWITCH lor_expr '{' newlines {
$$.enter_label=label_cnt++;
$$.exit_label=label_cnt++;
$$.first=NULL;
CODEGEN("goto Label%d\n",$$.enter_label);
}
| switchhalfblock casebody newlines '{' lines '}' newlines {
struct case_node* newnode=casedup($2);
newnode->next=$$.first;
$$.first=newnode;
CODEGEN("goto Label%d\n",$1.exit_label);
dump_symbol();
}
| switchhalfblock defaultbody newlines '{' lines '}' newlines {
struct case_node* newnode=casedup($2);
newnode->next=$$.first;
$$.first=newnode;
CODEGEN("goto Label%d\n",$1.exit_label);
dump_symbol();
}
;
line
: blockbody '{' lines '}' {
dump_symbol();
}
| equation
| VAR IDENT exprable_type dec_assign {
insert_symbol($2,$3);
if($4!=type_void){
check_match("ASSIGN",$3,$4);
mystore($2);
}
}
| VAR IDENT STRING{
insert_symbol($2,type_s);
CODEGEN("ldc \"\"\n");
mystore($2);
}
| VAR IDENT STRING '=' string_lit {
insert_symbol($2,type_s);
mystore($2);
}
| IDENT INC {
unsigned char tp=myload($1);
check_type_define("INC",tp,type_i|type_f);
if(tp==type_f){
CODEGEN("ldc 1.0\n");
CODEGEN("fadd\n");
}else{
CODEGEN("iconst_1\n");
CODEGEN("iadd\n");
}
mystore($1);
}
| IDENT DEC {
unsigned char tp=myload($1);
check_type_define("DEC",tp,type_i|type_f);
if(tp==type_f){
CODEGEN("ldc 1.0\n");
CODEGEN("fsub\n");
}else{
CODEGEN("iconst_1\n");
CODEGEN("isub\n");
}
mystore($1);
}
| PRINTLN '(' lor_expr ')' {
println_post($3);
}
| PRINTLN '(' string_lit ')' {
println_post($3);
}
| PRINT '(' lor_expr ')' {
print_post($3);
}
| PRINT '(' string_lit ')' {
print_post($3);
}
| ifbody newlines '{' lines '}' {
LABELGEN($1);
dump_symbol();
}
| ifblockelsebody newlines '{' lines '}' {
LABELGEN($1);
dump_symbol();
}
| forbody newlines '{' lines '}' {
CODEGEN("goto Label%d\n",$1.again);
LABELGEN($1.out);
dump_symbol();
}
| switchhalfblock '}' {
my_lookupswitch($1);
}
| RETURN lor_expr {
switch($2){
case type_i:
CODEGEN("ireturn\n");
break;
case type_f:
CODEGEN("freturn\n");
break;
case type_b:
CODEGEN("breturn\n");
break;
}
}
| RETURN string_lit {
CODEGEN("sreturn\n");
}
| RETURN {
CODEGEN("return\n");
}
|
;
equation
: IDENT '=' lor_expr {
unsigned char tp=ident_type($1);
check_match("ASSIGN",tp,$3);
mystore($1);
}
| IDENT ADD_ASSIGN lor_expr {
unsigned char tp=myload($1);
check_match("ADD",tp,$3);
CODEGEN("swap\n");
if(tp==type_f){
CODEGEN("fadd\n");
}else{
CODEGEN("iadd\n");
}
mystore($1);
}
| IDENT SUB_ASSIGN lor_expr {
unsigned char tp=myload($1);
check_match("SUB",tp,$3);
CODEGEN("swap\n");
if(tp==type_f){
CODEGEN("fsub\n");
}else{
CODEGEN("isub\n");
}
mystore($1);
}
| IDENT MUL_ASSIGN lor_expr {
unsigned char tp=myload($1);
check_match("MUL",tp,$3);
CODEGEN("swap\n");
if(tp==type_f){
CODEGEN("fmul\n");
}else{
CODEGEN("imul\n");
}
mystore($1);
}
| IDENT QUO_ASSIGN lor_expr {
unsigned char tp=myload($1);
check_match("QUO",tp,$3);
CODEGEN("swap\n");
if(tp==type_f){
CODEGEN("fdiv\n");
}else{
CODEGEN("idiv\n");
}
mystore($1);
}
| IDENT REM_ASSIGN lor_expr {
unsigned char tp=myload($1);
check_type_define("REM",tp,type_i);
check_type_define("REM",$3,type_i);
CODEGEN("swap\n");
CODEGEN("irem\n");
mystore($1);
}
| IDENT '=' string_lit {
unsigned char tp=ident_type($1);
check_match("ASSIGN",tp,$3);
mystore($1);
}
| lor_expr
;
lor_expr
: lor_expr LOR land_expr {
check_type_define("LOR",$1,type_b);
check_type_define("LOR",$3,type_b);
$$=type_b;
CODEGEN("ior\n");
}
| land_expr
;
land_expr
: land_expr LAND equ_expr {
check_type_define("LAND",$1,type_b);
check_type_define("LAND",$3,type_b);
$$=type_b;
CODEGEN("iand\n");
}
| equ_expr
;
equ_expr
: equ_expr EQL cmp_expr {
check_match("EQL",$1,$3);
$$=type_b;
if($1==type_f){
my_fcmp("eq");
}else{
my_icmp("eq");
}
}
| cmp_expr
;
cmp_expr
: cmp_expr '>' expr {
check_match("GTR",$1,$3);
$$=type_b;
if($1==type_f){
my_fcmp("gt");
}else{
my_icmp("gt");
}
}
| cmp_expr GEQ expr {
check_match("GEQ",$1,$3);
$$=type_b;
if($1==type_f){
my_fcmp("ge");
}else{
my_icmp("ge");
}
}
| expr
;
expr
: expr '+' term {
$$=check_match("ADD",$1,$3);
if($1==type_f){
CODEGEN("fadd\n");
}else{
CODEGEN("iadd\n");
}
}
| expr '-' term {
$$=check_match("SUB",$1,$3);
if($1==type_f){
CODEGEN("fsub\n");
}else{
CODEGEN("isub\n");
}
}
| term
;
term
: term '*' fac {
$$=check_match("MUL",$1,$3);
if($1==type_f){
CODEGEN("fmul\n");
}else{
CODEGEN("imul\n");
}
}
| term '/' fac {
$$=check_match("QUO",$1,$3);
if($1==type_f){
CODEGEN("fdiv\n");
}else{
CODEGEN("idiv\n");
}
}
| term '%' fac {
check_type_define("REM",$1,type_i);
check_type_define("REM",$3,type_i);
$$=type_i;
CODEGEN("irem\n");
}
| fac
;
fac
: '+' fac {
$$=check_type_define("POS",$2,type_i|type_f)?$2:type_error;
}
| '-' fac {
$$=check_type_define("NEG",$2,type_i|type_f)?$2:type_error;
if($2==type_f){
CODEGEN("fneg\n");
}else{
CODEGEN("ineg\n");
}
}
| '!' fac {
$$=check_type_define("NOT",$2,type_b)?type_b:type_error;
CODEGEN("iconst_1\n");
CODEGEN("ixor\n");
}
| '(' lor_expr ')' {
$$=$2;
}
| FLOAT '(' lor_expr ')' {
check_type_define("i2f",$3,type_i);
CODEGEN("i2f\n");
$$=type_f;
}
| INT '(' lor_expr ')' {
check_type_define("f2i",$3,type_f);
CODEGEN("f2i\n");
$$=type_i;
}
| IDENT '(' parastart ')' {
int i=func_find($1);
if(i!=-1){
$$=func_table[i].type;
CODEGEN("invokestatic Main/%s(%s)%c\n",$1,func_table[i].func_sig,get_type($$));
}else{
printf("error:%d: undefined: %s\n",reduce_line,$1);
g_has_error=true;
}
}
| ident {
$$=$1;
}
| int_lit {
$$=$1;
}
| float_lit {
$$=$1;
}
| bool_lit {
$$=$1;
}
;
case_lit
: INT_LIT {
$$=$1;
}
| '-' INT_LIT {
$$=-$2;
}
;
%%
/* C code section */
int main(int argc, char *argv[])
{
if (argc == 2) {
yyin = fopen(argv[1], "r");
} else {
yyin = stdin;
}
if (!yyin) {
printf("file `%s` doesn't exists or cannot be opened\n", argv[1]);
exit(1);
}
/* Codegen output init */
char *bytecode_filename = "hw3.j";
fout = fopen(bytecode_filename, "w");
/* Symbol table init */
// Add your code
yylineno = 0;
yyparse();
/* Symbol table dump */
// Add your code
printf("Total lines: %d\n", reduce_line-1);
fclose(fout);
fclose(yyin);
if (g_has_error) {
remove(bytecode_filename);
}
yylex_destroy();
return 0;
}