-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_hw3.y
More file actions
1673 lines (1573 loc) · 39.1 KB
/
compiler_hw3.y
File metadata and controls
1673 lines (1573 loc) · 39.1 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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Definition section */
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
extern int yylineno;
extern int yylex();
extern void yyerror(char *s);
extern char* yytext; // Get current token from lex
extern char buf[256]; // Get current code line from lex
FILE *file; // To generate .j file for Jasmin
int has_error = 0;
void semantic_error(char errormsg[100], int sem_line);
int error_flag;
char sem_error_msg[100];
void syntax_error(char errormsg[100]);
int syntax_flag;
char syn_error_msg[100];
char function_name[100];
/* Symbol table function - you can add new function if needed. */
struct symbol
{
int index;
char name[100];
char kind[100];
char type[100];
int scope;
char attribute[100];
int register_num;
int func_decla;
};
struct scope
{
struct symbol table[30];
};
int lookup_symbol(char[], char[]);
void create_symbol();
void insert_symbol(char[], char[], char[], char[], int);
void dump_symbol(int);
int dump_flag;
struct scope global_table[40];
int table_num;
struct scope temp_dump_table;
void clear_temp_table();
void fill_temp_table();
void init_func_table();
void insert_func_table(char[]);
int lookup_func_table(char[]);
struct func_table
{
int flag;
char name[100];
}implement_func_table[30];
int lookup_register_num(char[]);
char* lookup_type(char[]);
int register_site;
char func_para_type[100];
char* lookup_attribute(char[]);
char* changeto_java_type();
void gen_print(char[]);
void gen_ID(char[]);
int func_index;
void gen_function();
struct check_index {
int has;
int cur_func_index;
}check_index;
void set_para(char[]);
void set_func_decla(int);
int get_func_decla();
void gen_arith_post(char[], char[]);
void gen_arith_pre(char[], char[]);
struct arith_flag {
int flag;
char operator[3];
int global;
int regnum;
char name[30];
}arith_flag;
void gen_asgn(char[], char[]);
void gen_load_arith();
char* create_label_name(char[], int);
void gen_relation(char[], char[]);
int if_label;
#define MAXSTACK 100
int if_label_stack[MAXSTACK];
int top;
void push_if_label_stack(int);
int pop_if_label_stack();
int get_top_if_stack();
int if_label_table[100];
void gen_relation_inverse(char[], char[]);
void init();
%}
/* Use variable or self-defined structure to represent
* nonterminal and token type
*/
%union {
struct atom {
int i_val;
double f_val;
char* string_val;
char type[30];
int id_reg;
}atom;
}
/* Token without return */
%token ADD SUB MUL DIV MOD
%token AND OR NOT
%token PRINT
%token RET
%token START_COMMENT END_COMMENT CPLUS_COMMENT
/* Token with return, which need to sepcify type */
%token <atom> I_CONST
%token <atom> F_CONST
%token <atom> S_CONST
%token <atom> TRUE
%token <atom> FALSE
%token <atom> INC
%token <atom> DEC
%token <atom> MT LT MTE LTE EQ NE
%token <atom> ID INT FLOAT BOOL STRING VOID
%token <atom> SEMICOLON
%token <atom> LB RB LCB RCB LSB RSB COMMA
%token <atom> ASGN ADDASGN SUBASGN MULASGN DIVASGN MODASGN
%token <atom> IF ELSE FOR WHILE
/* Nonterminal with return, which need to sepcify type */
%type <atom> type
%type <atom> func_end func func_parameter
%type <atom> initializer
%type <atom> factor mul_expression_stat add_expression_stat expression_stat arithmetic_postfix
%type <atom> relational
%type <atom> const
%type <atom> print_func print_type
%type <atom> expression
%type <atom> asgn
%type <atom> iteration_stat
/* Yacc will start at this nonterminal */
%start program
/* Grammar section */
%%
program
: program stat
|
;
stat
: declaration
| declaration_func
| comment_stat
;
func_stat
: declaration
| expression
| iteration_stat
| print_func
| comment_stat
| RET return_stat
;
func_mul_stat
: func_stat func_mul_stat
| func_stat
;
return_stat
: initializer SEMICOLON
| expression_stat SEMICOLON
| SEMICOLON
;
func
: func_parameter RB
{
char attr[100];
bzero(attr, 100);
int flag = 0;
for(int i=0;i<30;i++) {
if(global_table[1].table[i].index != -1) {
if(!strcmp(global_table[1].table[i].kind, "parameter")) {
if(flag == 1) {
strcat(attr, ", ");
}
strcat(attr, global_table[1].table[i].type);
flag = 1;
}
}
else break;
}
if(check_index.has == 0) {
// first
set_para(attr);
// fill func_para_type
strcpy(func_para_type, global_table[0].table[check_index.cur_func_index].attribute);
}
else {
// check parameter
if(strcmp(attr, global_table[0].table[check_index.cur_func_index].attribute)) {
// paramater is not the same
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "function formal parameter is not the same");
}
}
}
func_end
{
//if(!strcmp($3.string_val, ";")) {
if(!strcmp($4.string_val, ";")) {
table_num--;
}
}
| RB func_end
{
if(!strcmp($2.string_val, ";")) {
table_num--;
}
}
;
func_end
: SEMICOLON
{
if(get_func_decla() == 1) {
// redefine function
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Redeclared variable ");
strcat(sem_error_msg, global_table[0].table[check_index.cur_func_index].name);
}
else set_func_decla(1);
$$.string_val = ";";
register_site = 0;
}
| LCB
{
gen_function();
}
func_mul_stat RCB
{
if(!strcmp(global_table[0].table[check_index.cur_func_index].type, "void")) fprintf(file, "\treturn\n");
else fprintf(file, "\tfreturn\n");
fprintf(file, ".end method\n");
register_site = 0;
dump_flag = 1;
clear_temp_table();
fill_temp_table();
table_num--;
}
;
declaration
: type ID ASGN initializer SEMICOLON
{
int index = lookup_symbol($2.string_val, "insert");
if(index != -1) {
insert_symbol($2.string_val, "variable", $1.string_val, "\0", index);
// global declaration
if(table_num == 0) {
char type_descriptor[20];
if(!strcmp($1.string_val, "bool")) {
strcpy(type_descriptor,"F");
float booltype;
if(!strcmp($4.string_val, "true")) booltype = 1.0;
else booltype = 2.0;
fprintf(file, ".field public static %s %s = %f\n", $2.string_val, type_descriptor, booltype);
}
else if(!strcmp($1.string_val, "string")) {
strcpy(type_descriptor,"Ljava/lang/String;");
char *strconst = strtok($4.string_val, "\"");
fprintf(file, ".field public static %s %s = \"%s\"\n", $2.string_val, type_descriptor, strconst);
}
}
// local declaration
else {
if(!strcmp($1.string_val, "bool")) {
int booltype;
if(!strcmp($4.string_val, "true")) booltype = 1.0;
else booltype = 2.0;
fprintf(file, "\tldc %d\n", booltype);
fprintf(file, "\tistore %d\n", index);
}
else if(!strcmp($1.string_val, "string")) {
char *strconst = strtok($4.string_val, "\"");
fprintf(file, "\tldc \"%s\"\n", strconst);
fprintf(file, "\tastore %d\n", index);
}
}
}
else {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Redeclared variable ");
strcat(sem_error_msg, $2.string_val);
}
}
| type ID SEMICOLON
{
int index = lookup_symbol($2.string_val, "insert");
if(index != -1) {
insert_symbol($2.string_val, "variable", $1.string_val, "\0", index);
// global declaration
if(table_num == 0) {
char type_descriptor[20];
if(!strcmp($1.string_val, "string")) {
strcpy(type_descriptor, "Ljava/lang/String;");
fprintf(file, ".field public static %s %s\n", $2.string_val, type_descriptor);
}
else {
// float, int, bool
strcpy(type_descriptor, "F");
fprintf(file, ".field public static %s %s\n", $2.string_val, type_descriptor);
}
}
// local declaration
else {
char type_descriptor[20];
fprintf(file, "\tldc 0.0\n");
int regnum = lookup_register_num($2.string_val);
if(!strcmp($1.string_val, "string")) {
strcpy(type_descriptor, "Ljava/lang/String;");
fprintf(file, "\tastore %d\n", regnum);
}
else {
// float, int, bool
strcpy(type_descriptor, "F");
fprintf(file, "\tfstore %d\n", regnum);
}
}
}
else {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Redeclared variable ");
strcat(sem_error_msg, $2.string_val);
}
}
| type ID ASGN expression_stat SEMICOLON
{
int index = lookup_symbol($2.string_val, "insert");
if(index != -1) {
insert_symbol($2.string_val, "variable", $1.string_val, "\0", index);
// global declaration
if(table_num == 0) {
char type_descriptor[30];
// float, int
strcpy(type_descriptor, "F");
fprintf(file, ".field public static %s %s = %f\n", $2.string_val, type_descriptor, $4.f_val);
}
// local declaration
else {
gen_load_arith();
int regnum = lookup_register_num($2.string_val);
if(!strcmp($1.string_val, "string")) {
fprintf(file, "\tastore %d\n", regnum);
}
else {
// float, int, bool
fprintf(file, "\tfstore %d\n", regnum);
}
}
}
else {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Redeclared variable ");
strcat(sem_error_msg, $2.string_val);
}
}
;
declaration_func
: type ID LB
{
int has = lookup_symbol($2.string_val, "global");
int index = lookup_symbol($2.string_val, "insert");
if(has == -1) {
// first time
check_index.cur_func_index = index;
check_index.has = 0;
if(index != -1) {
insert_symbol($2.string_val, "function", $1.string_val, "", index);
}
}
else {
// declare or define before
check_index.cur_func_index = has;
check_index.has = 1;
if(strcmp($1.string_val, global_table[0].table[has].type)) {
// return type not the same
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "function return type is not the same");
}
}
table_num++;
create_symbol();
}
func
;
func_parameter
: func_parameter COMMA type ID
{
int index = lookup_symbol($4.string_val, "insert");
if(index != -1) {
insert_symbol($4.string_val, "parameter", $3.string_val, "\0", index);
}
}
| type ID
{
int index = lookup_symbol($2.string_val, "insert");
if(index != -1) {
insert_symbol($2.string_val, "parameter", $1.string_val, "\0", index);
}
}
;
func_call
: func_call COMMA const
{
if(strcmp(func_para_type, "")) {
strcat(func_para_type, ", ");
strcat(func_para_type, $3.type);
}
else {
strcpy(func_para_type, $3.type);
}
}
| const
{
if(strcmp(func_para_type, "")) {
strcat(func_para_type, ", ");
strcat(func_para_type, $1.type);
}
else {
strcpy(func_para_type, $1.type);
}
}
;
const
: S_CONST
{
fprintf(file, "\tldc \"%s\"\n", $1.string_val);
strcpy($$.type, "string");
}
| expression_stat
| TRUE
{
fprintf(file, "\tldc 1.0\n");
strcpy($$.type, "bool");
}
| FALSE
{
fprintf(file, "\tldc 2.0\n");
strcpy($$.type, "bool");
}
;
expression
: ID asgn
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared variable ");
strcat(sem_error_msg, $1.string_val);
}
else {
int regnum = lookup_register_num($1.string_val);
// int, float, bool
if(strcmp($2.string_val, "=")) {
fprintf(file, "\tfload %d\n", regnum);
}
}
}
expression_stat SEMICOLON
{
if(!strcmp($2.string_val, "%=")) {
// check int type
//if(!strcmp($1.type, "int") && !strcmp($3.type, "int")) {
if(!strcmp($1.type, "int") && !strcmp($4.type, "int")) {
// expre is TOS
//if($3.id_reg != -1) {
if($4.id_reg != -1) {
// ID
//fprintf(file, "\tfstore %d\n", $3.id_reg);
fprintf(file, "\tfstore %d\n", $4.id_reg);
fprintf(file, "\tf2i\n");
//fprintf(file, "\tfload %d\n", $3.id_reg);
fprintf(file, "\tfload %d\n", $4.id_reg);
fprintf(file, "\tf2i\n");
}
else {
// constant
fprintf(file, "\tfstore 49\n");
fprintf(file, "\tf2i\n");
fprintf(file, "\tfload 49\n");
fprintf(file, "\tf2i\n");
}
fprintf(file, "\tirem\n");
fprintf(file, "\ti2f\n");
}
else {
// semantic error
// Modulo operator (%) with floating point operands
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Modulo operator (%) with floating point operands");
}
}
else gen_asgn($1.string_val, $2.string_val);
gen_load_arith();
int regnum = lookup_register_num($1.string_val);
fprintf(file, "\tfstore %d\n", regnum);
}
| ID ASGN initializer SEMICOLON
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared variable ");
strcat(sem_error_msg, $1.string_val);
}
else {
// local declaration
int regnum = lookup_register_num($1.string_val);
strcpy($1.type, lookup_type($1.string_val));
if(!strcmp($1.type, "bool")) {
int booltype;
if(!strcmp($3.string_val, "true")) booltype = 1.0;
else booltype = 2.0;
fprintf(file, "\tldc %d\n", booltype);
fprintf(file, "\tfstore %d\n", regnum);
}
else if(!strcmp($1.type, "string")) {
char *strconst = strtok($3.string_val, "\"");
fprintf(file, "\tldc \"%s\"\n", strconst);
fprintf(file, "\tastore %d\n", regnum);
}
}
}
| ID arithmetic_postfix SEMICOLON
{
gen_arith_post($1.string_val, $2.string_val);
if(lookup_symbol($1.string_val, "semantic") != -1) {
// assign to factor
$$.f_val = -1.0;
strcpy($$.type, lookup_type($1.string_val));
}
arith_flag.flag = 0;
}
| arithmetic_postfix ID SEMICOLON
{
gen_arith_pre($2.string_val, $1.string_val);
if(lookup_symbol($2.string_val, "semantic") != -1) {
// assign to factor
$$.f_val = -1.0;
strcpy($$.type, lookup_type($2.string_val));
}
arith_flag.flag = 0;
}
| ID LB func_call RB
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared function ");
strcat(sem_error_msg, $1.string_val);
}
else if(strcmp(lookup_attribute($1.string_val), func_para_type)) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "function formal parameter is not the same");
}
else {
char paratype[100];
strcpy(paratype, changeto_java_type());
char* returntype = lookup_type($1.string_val);
if(!strcmp(returntype, "void")) returntype = "V";
else returntype = "F";
fprintf(file, "\tinvokestatic compiler_hw3/%s(%s)%s\n", $1.string_val, paratype, returntype);
}
} SEMICOLON
{
memset(func_para_type, '\0', sizeof(func_para_type));
}
| ID LB RB SEMICOLON
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared function ");
strcat(sem_error_msg, $1.string_val);
}
if(strcmp(lookup_attribute($1.string_val), "")) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "function formal parameter is not the same");
}
char* returntype = lookup_type($1.string_val);
if(!strcmp(returntype, "void")) returntype = "V";
else returntype = "F";
fprintf(file, "\tinvokestatic compiler_hw3/%s()%s\n", $1.string_val, returntype);
// assign function return to factor
$$.f_val = -1.0;
}
| SEMICOLON
;
expression_stat
: expression_stat relational add_expression_stat
{
fprintf(file, "\tfsub\n");
fprintf(file, "\tf2i\n");
$$.string_val = $2.string_val;
strcpy($$.type, "bool");
}
| add_expression_stat
{
$$ = $1;
}
;
add_expression_stat
: add_expression_stat ADD mul_expression_stat
{
gen_load_arith();
fprintf(file, "\tfadd\n");
$$.f_val = -1.0;
}
| add_expression_stat SUB mul_expression_stat
{
gen_load_arith();
fprintf(file, "\tfsub\n");
$$.f_val = -1.0;
}
| mul_expression_stat
{
$$ = $1;
}
;
mul_expression_stat
: mul_expression_stat MUL factor
{
gen_load_arith();
fprintf(file, "\tfmul\n");
$$.f_val = -1.0;
}
| mul_expression_stat DIV factor
{
gen_load_arith();
if((!strcmp($3.type, "float") && $3.f_val == 0) ||
(!strcmp($3.type, "int") && $3.i_val == 0)) {
// semantic error
// Variables of numbers that divided by zero
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Variables of numbers that divided by zero");
}
fprintf(file, "\tfdiv\n");
$$.f_val = -1.0;
}
| mul_expression_stat MOD factor
{
char type1[30], type2[30];
strcpy(type1, $1.type);
strcpy(type2, $3.type);
if(!strcmp(type1, "int") && !strcmp(type2, "int")) {
// $3 is TOS
if($3.id_reg != -1) {
// ID
fprintf(file, "\tfstore %d\n", $3.id_reg);
fprintf(file, "\tf2i\n");
fprintf(file, "\tfload %d\n", $3.id_reg);
fprintf(file, "\tf2i\n");
}
else {
// constant
fprintf(file, "\tfstore 49\n");
fprintf(file, "\tf2i\n");
fprintf(file, "\tfload 49\n");
fprintf(file, "\tf2i\n");
}
fprintf(file, "\tirem\n");
fprintf(file, "\ti2f\n");
}
else {
// semantic error
// Modulo operator (%) with floating point operands
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Modulo operator (%) with floating point operands");
}
$$.f_val = -1.0;
}
| factor
{
$$ = $1;
}
;
factor
: I_CONST
{
// local declaration
if(table_num != 0) {
fprintf(file, "\tldc %f\n", (float)$1.i_val);
}
// assign to factor for checking global declaration
$$.f_val = (float)$1.i_val;
$$.id_reg = -1;
strcpy($$.type, "int");
}
| F_CONST
{
// local declaration
if(table_num != 0) {
fprintf(file, "\tldc %f\n", $1.f_val);
}
// assign to factor for checking global declaration
$$.f_val = $1.f_val;
$$.id_reg = -1;
strcpy($$.type, "float");
}
| SUB I_CONST
{
// local declaration
if(table_num != 0) {
fprintf(file, "\tldc -%f\n", (float)$2.i_val);
}
// assign to factor for checking global declaration
$$.f_val = -(float)$2.i_val;
$$.id_reg = -1;
strcpy($$.type, "int");
}
| SUB F_CONST
{
// local declaration
if(table_num != 0) {
fprintf(file, "\tldc -%f\n", $2.f_val);
}
// assign to factor for checking global declaration
$$.f_val = -$2.f_val;
$$.id_reg = -1;
strcpy($$.type, "float");
}
| ID
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared variable ");
strcat(sem_error_msg, $1.string_val);
}
else {
gen_ID($1.string_val);
// assign to factor
$$.id_reg = lookup_register_num($1.string_val);
strcpy($$.type, lookup_type($1.string_val));
}
}
| LB expression_stat RB
{
// assign to factor
$$.f_val = -1.0;
//strcpy($$.type, lookup_type($2.string_val));
}
| ID arithmetic_postfix
{
gen_arith_post($1.string_val, $2.string_val);
if(lookup_symbol($1.string_val, "semantic") != -1) {
// assign to factor
$$.f_val = -1.0;
strcpy($$.type, lookup_type($1.string_val));
}
}
| arithmetic_postfix ID
{
gen_arith_pre($2.string_val, $1.string_val);
if(lookup_symbol($2.string_val, "semantic") != -1) {
// assign to factor
$$.f_val = -1.0;
strcpy($$.type, lookup_type($2.string_val));
}
}
| ID LB func_call RB
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared function ");
strcat(sem_error_msg, $1.string_val);
}
if(strcmp(lookup_attribute($1.string_val), func_para_type)) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "function formal parameter is not the same");
}
char paratype[100];
strcpy(paratype, changeto_java_type());
char* returntype = lookup_type($1.string_val);
if(!strcmp(returntype, "void")) returntype = "V";
else returntype = "F";
fprintf(file, "\tinvokestatic compiler_hw3/%s(%s)%s\n", $1.string_val, paratype, returntype);
memset(func_para_type, '\0', sizeof(func_para_type));
// assign function return to factor
$$.f_val = -1.0;
}
| ID LB RB
{
if(lookup_symbol($1.string_val, "semantic") == -1) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "Undeclared function ");
strcat(sem_error_msg, $1.string_val);
}
if(strcmp(lookup_attribute($1.string_val), "")) {
error_flag = 1;
bzero(sem_error_msg, 100);
strcat(sem_error_msg, "function formal parameter is not the same");
}
char* returntype = lookup_type($1.string_val);
if(!strcmp(returntype, "void")) returntype = "V";
else returntype = "F";
fprintf(file, "\tinvokestatic compiler_hw3/%s()%s\n", $1.string_val, returntype);
// assign function return to factor
$$.f_val = -1.0;
}
;
iteration_stat
: WHILE LB
{
char label_name[30];
strcpy(label_name, create_label_name("BEGIN_WHILE_", $1.i_val));
fprintf(file, "%s:\n", label_name);
}
expression_stat RB
{
char exit_label_name[30], true_label_name[30];
strcpy(true_label_name, create_label_name("TRUE_WHILE_", $1.i_val));
gen_relation($4.string_val, true_label_name);
strcpy(exit_label_name, create_label_name("EXIT_WHILE_", $1.i_val));
fprintf(file, "\tgoto %s\n", exit_label_name);
}
LCB
{
table_num++;
create_symbol();
char label_name[30];
strcpy(label_name, create_label_name("TRUE_WHILE_", $1.i_val));
fprintf(file, "%s:\n", label_name);
}
func_mul_stat RCB
{
dump_flag = 1;
clear_temp_table();
fill_temp_table();
table_num--;
char begin_label_name[30], label_name[30];
strcpy(begin_label_name, create_label_name("BEGIN_WHILE_", $1.i_val));
fprintf(file, "\tgoto %s\n", begin_label_name);
strcpy(label_name, create_label_name("EXIT_WHILE_", $1.i_val));
fprintf(file, "%s:\n", label_name);
}
| IF LB expression_stat RB
{
if_label++;
push_if_label_stack(if_label);
char if_label_name[30], else_label_name[30];
strcpy(if_label_name, create_label_name("IF_", if_label));
strcat(if_label_name, "_");
strcpy(if_label_name, create_label_name(if_label_name, ++if_label_table[if_label]));
gen_relation_inverse($3.string_val, if_label_name);
}
LCB { table_num++; create_symbol(); } func_mul_stat RCB
{
dump_flag = 1;
clear_temp_table();
fill_temp_table();
table_num--;
char exit_label_name[30], label_name[30];
int tos = get_top_if_stack();
// goto EXIT_IF_x
strcpy(exit_label_name, create_label_name("EXIT_IF_", tos));
fprintf(file, "\tgoto %s\n", exit_label_name);
// IF_x_x:
strcpy(label_name, create_label_name("IF_", tos));
strcat(label_name, "_");
strcpy(label_name, create_label_name(label_name, if_label_table[tos]));
fprintf(file, "%s:\n", label_name);
} haselse {
char exit_label_name[30];
int tos = get_top_if_stack();
strcpy(exit_label_name, create_label_name("EXIT_IF_", tos));
fprintf(file, "%s:\n", exit_label_name);
pop_if_label_stack();
}
;
haselse
: ELSE haselseif
|
;
haselseif
: IF LB expression_stat
{
char label_name[30];
int tos = get_top_if_stack();
strcpy(label_name, create_label_name("IF_", tos));
strcat(label_name, "_");
strcpy(label_name, create_label_name(label_name, ++if_label_table[tos]));
gen_relation_inverse($3.string_val, label_name);
}
RB LCB { table_num++; create_symbol(); } func_mul_stat RCB
{
dump_flag = 1;
clear_temp_table();
fill_temp_table();
table_num--;
char exit_label_name[30], label_name[30];
int tos = get_top_if_stack();
// goto EXIT_IF_x
strcpy(exit_label_name, create_label_name("EXIT_IF_", tos));
fprintf(file, "\tgoto %s\n", exit_label_name);
// IF_x_x:
strcpy(label_name, create_label_name("IF_", tos));
strcat(label_name, "_");
strcpy(label_name, create_label_name(label_name, if_label_table[tos]));
fprintf(file, "%s:\n", label_name);
} moreelseif
| LCB { table_num++; create_symbol(); } func_mul_stat RCB
{
dump_flag = 1;
clear_temp_table();
fill_temp_table();
table_num--;
char exit_label_name[30], label_name[30];
int tos = get_top_if_stack();
// goto EXIT_IF_x
strcpy(exit_label_name, create_label_name("EXIT_IF_", tos));
fprintf(file, "\tgoto %s\n", exit_label_name);
}
;
moreelseif
: ELSE haselseif
|
;
relational
: MT
| LT
| MTE
| LTE
| EQ
| NE
;
print_func
: PRINT LB print_type
{