-
Notifications
You must be signed in to change notification settings - Fork 2
/
1705010.y
3579 lines (2677 loc) · 108 KB
/
1705010.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
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
%code requires{
#include <bits/stdc++.h>
}
%{
#include<bits/stdc++.h>
#include <typeinfo>
using namespace std;
#include "SymbolTable.h"
#include "SymbolInfo.h"
#include "ScopeTable.h"
#include "Helper.h"
ofstream logout;
ofstream errout;
ofstream codeout;
ofstream opt_codeout;
extern int line_count;
int err_count = 0;
// #define YYSTYPE SymbolInfo*
extern FILE *yyin;
void yyerror(string s){
logout<<"Error at line "<<line_count<<": "<<s<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": "<<s<<"\n"<<endl;
err_count++;
}
int yyparse(void);
int yylex(void);
//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// SYMBOL TABLE //////////////////////////////////////////////////
int hashF(string s)
{
int h = 0;
for(int i=0;i<(int)s.size();i++)
{
h = (h + s[i]);
}
return h;
}
int bucket_size = 30;
SymbolTable *sym_tab = new SymbolTable(bucket_size,hashF);
//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// FUNCTION PARAMETER ///////////////////////////////////////////
bool is_function_now = false;
vector<SymbolInfo>function_params;
/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// ERROR HANDLING ///////////////////////////////////////////////
string do_implicit_typecast(string left_op,string right_op)
{
if(left_op == "NULL" || right_op == "NULL") return "NULL"; // already reported , now supressing more errors
if(left_op == "void" || right_op == "void") return "error";
if((left_op == "float" || left_op == "float_array") && (right_op == "float" || right_op == "float_array")) return "float";
if((left_op == "float" || left_op == "float_array") && (right_op == "int" || right_op == "int_array")) return "float";
if((left_op == "int" || left_op=="int_array") && (right_op == "float" || right_op == "float_array")) return "float";
if((left_op == "int" || left_op=="int_array") && (right_op == "int" || right_op == "int_array")) return "int";
return "error";
}
bool is_param_typecast_ok(string og_p,string pass_p)
{
if(pass_p == "NULL") return true; // already error reported and converted to NULL , this is made true to supress more error
if(og_p == "void") return pass_p == "void";
if(og_p == "int") return (pass_p == "int" || pass_p == "int_array");
if(og_p == "float") return pass_p != "void";
}
bool check_assignop(string left_op,string right_op)
{
if(left_op == "NULL" || right_op == "NULL") return true; // already error reported and converted to NULL , this is made true to supress more error
if(left_op == "void" || right_op == "void") return false;
if(left_op == "" || right_op == "") return false;
if((left_op == "int" || left_op == "int_array") && (right_op == "int" || right_op == "int_array") ) return true;
if((left_op == "float" || left_op == "float_array") && (right_op != "void") )return true;
return false;
}
void print_grammar_rule(string left_part,string right_part)
{
logout<<"Line "<<line_count<<": "<<left_part<<" : "<<right_part<<"\n"<<endl;
}
void print_log_text(string log_text)
{
logout<<log_text<<"\n"<<endl;
}
void error_multiple_declaration(string error_symbol)
{
logout<<"Error at line "<<line_count<<": Multiple declaration of "<<error_symbol<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Multiple declaration of "<<error_symbol<<"\n"<<endl;
err_count++;
}
void error_array_size_float()
{
logout<<"Error at line "<<line_count<<": Non-integer Array Size\n"<<endl;
errout<<"Error at line "<<line_count<<": Non-integer Array Size\n"<<endl;
err_count++;
}
void error_array_index_invalid()
{
logout<<"Error at line "<<line_count<<": Expression inside third brackets not an integer\n"<<endl;
errout<<"Error at line "<<line_count<<": Expression inside third brackets not an integer\n"<<endl;
err_count++;
}
void error_type_cast()
{
logout<<"Error at line "<<line_count<<": Incompatible Operand\n"<<endl;
errout<<"Error at line "<<line_count<<": Incompatible Operand\n"<<endl;
err_count++;
}
void error_type_cast_void()
{
logout<<"Error at line "<<line_count<<": Void function used in expression\n"<<endl;
errout<<"Error at line "<<line_count<<": Void function used in expression\n"<<endl;
err_count++;
}
void error_type_cast_mod()
{
logout<<"Error at line "<<line_count<<": Non-Integer operand on modulus operator\n"<<endl;
errout<<"Error at line "<<line_count<<": Non-Integer operand on modulus operator\n"<<endl;
err_count++;
}
void error_type_cast_mod_zero()
{
logout<<"Error at line "<<line_count<<": Modulus by Zero\n"<<endl;
errout<<"Error at line "<<line_count<<": Modulus by Zero\n"<<endl;
err_count++;
}
void error_undeclared_variable(string var_name)
{
logout<<"Error at line "<<line_count<<": Undeclared variable "<<var_name<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Undeclared variable "<<var_name<<"\n"<<endl;
err_count++;
}
void error_undeclared_function(string var_name)
{
logout<<"Error at line "<<line_count<<": Undeclared function "<<var_name<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Undeclared function "<<var_name<<"\n"<<endl;
err_count++;
}
void error_type_mismatch(string msg="")
{
logout<<"Error at line "<<line_count<<": Type mismatch "<<msg<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Type mismatch "<<msg<<"\n"<<endl;
err_count++;
}
void error_function_parameter_type(int param_id,string extra_s="")
{
logout<<"Error at line "<<line_count<<": "<<param_id<<"th argument mismatch in function "<<extra_s<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": "<<param_id<<"th argument mismatch in function "<<extra_s<<"\n"<<endl;
err_count++;
}
void error_function_parameter_number(string name,bool declaration_definition_clash = false)
{
if(declaration_definition_clash){
logout<<"Error at line "<<line_count<<": Total number of arguments mismatch with declaration in function "<<name<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Total number of arguments mismatch with declaration in function "<<name<<"\n"<<endl;
}
else{
logout<<"Error at line "<<line_count<<": Total number of arguments mismatch in function "<<name<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Total number of arguments mismatch in function "<<name<<"\n"<<endl;
}
err_count++;
}
void error_function_not_implemented()
{
logout<<"Error at line "<<line_count<<": Function not implemented\n"<<endl;
errout<<"Error at line "<<line_count<<": Function not implemented\n"<<endl;
err_count++;
}
void error_function_return_condflict(string name)
{
logout<<"Error at line "<<line_count<<": Return type mismatch with function declaration in function "<<name<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": Return type mismatch with function declaration in function "<<name<< "\n"<<endl;
err_count++;
}
void error_not_function(string f_name)
{
logout<<"Error at line "<<line_count<<": "<<f_name<<" not a function\n"<<endl;
errout<<"Error at line "<<line_count<<": "<<f_name<<" not a function\n"<<endl;
err_count++;
}
void error_nested_function()
{
logout<<"Error at line "<<line_count<<": A function is defined inside a function\n"<<endl;
errout<<"Error at line "<<line_count<<": A function is defined inside a function\n"<<endl;
err_count++;
}
void error_var_type()
{
logout<<"Error at line "<<line_count<<": Variable type cannot be void\n"<<endl;
errout<<"Error at line "<<line_count<<": Variable type cannot be void\n"<<endl;
err_count++;
}
void error_not_array(string name)
{
logout<<"Error at line "<<line_count<<": "<<name<<" not an array\n"<<endl;
errout<<"Error at line "<<line_count<<": "<<name<<" not an array\n"<<endl;
err_count++;
}
void error_parameter_name_missing(int param_id,string func_name)
{
logout<<"Error at line "<<line_count<<": "<<param_id<<"th parameter's name not given in function definition of "<<func_name<<"\n"<<endl;
errout<<"Error at line "<<line_count<<": "<<param_id<<"th parameter's name not given in function definition of "<<func_name<<"\n"<<endl;
err_count++;
}
///////////////////////////////////////////
string cur_function_name = "";
void insert_function_to_global(SymbolInfo* temp_s,string var_type)
{
cur_function_name = temp_s->key;
// insert function ID to SymbolTable with VAR_TYPE
temp_s->setVarType(var_type);
temp_s->isFunction = true;
// update parameter type
for(auto temp_p : function_params)
{
temp_s->param_v.push_back(temp_p.var_type);
}
if(!sym_tab->insert_symbol(*temp_s))
{
SymbolInfo* ret_symbol = sym_tab->lookup(temp_s->key);
if(ret_symbol->isFunctionDeclaration == false){
error_multiple_declaration(temp_s->key);
}
else{
// declared before , now definition happening
// check if any clash between declaration and definition
if(ret_symbol->var_type != temp_s->var_type)
{
error_function_return_condflict(temp_s->key);
}
if(ret_symbol->param_v.size() != temp_s->param_v.size())
{
error_function_parameter_number(temp_s->key,true);
}
else
{
for(int i=0;i<ret_symbol->param_v.size();i++)
{
if(ret_symbol->param_v[i] != temp_s->param_v[i]){
error_function_parameter_type(i+1,temp_s->key);
break;
}
}
}
// the following line is commented out because in case of clash , use the declaration info
// ret_symbol->param_v = $2->param_v;
ret_symbol->isFunctionDeclaration = false; // declaration +
}
// cout<<"Dec -> "<<ret_symbol->key<<" :: "<<ret_symbol->isFunctionDeclaration<<endl;
}
else{
// Finalizing Definition
SymbolInfo* ret_symbol = sym_tab->lookup(temp_s->key);
ret_symbol->isFunctionDeclaration = false;
// cout<<"Dec ->> "<<ret_symbol->key<<" :: "<<ret_symbol->isFunctionDeclaration<<endl;
for(int i=0;i<function_params.size();i++)
{
if(function_params[i].key == "dummy_key"){
error_parameter_name_missing(i+1,ret_symbol->key);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// FREE MEMORY /////////////////////////////////////////
void erm_s(SymbolInfo* s) // erase memory of SymbolInfo pointer
{
if(s!=NULL) free(s);
}
void erm_h(Helper* h) // erase memory of Helper pointer
{
if(h!=NULL) free(h);
}
///////////////////////////////////////
///////// MACHINE CODE GEN ////////////
// void fileToCode(ofstream &out_s,string sourceFileName)
// {
// ifstream file_src;
// file_src.open(sourceFileName);
// string to_copy;
// if(file_src && out_s){
// while(getline(file_src,to_copy)){
// out_s << to_copy << "\n";
// }
// } else {
// //Something went wrong
// cout<<"Cannot read File"<<endl;
// }
// }
string newWordVariable(string name)
{
return name+" dw ?";
}
int labelCount=0;
int tempCount=0;
vector<string>DATA_vector;
int SP_VAL = 0;
void incSP(int ara_size = -1)
{
if(ara_size == -1) SP_VAL += 2;
else SP_VAL += ara_size*2; // 2 for word
}
char *newLabel()
{
char *lb= new char[4];
strcpy(lb,"L");
char b[3];
sprintf(b,"%d", labelCount);
labelCount++;
strcat(lb,b);
return lb;
}
char *newTemp()
{
char *t= new char[4];
strcpy(t,"t");
char b[3];
sprintf(b,"%d", tempCount);
tempCount++;
strcat(t,b);
incSP();
return t;
}
string getJumpText(string relop)
{
if(relop=="<") return "jl";
if(relop=="<=") return "jle";
if(relop==">") return "jg";
if(relop==">=") return "jge";
if(relop=="==") return "je";
if(relop=="!=") return "jne";
}
string stk_address(string stk_offset)
{
return "[bp-"+stk_offset+"]";
}
string stk_address_param(string stk_offset)
{
return "[bp+"+stk_offset+"]";
}
string stk_address_typecast(string stk_offset)
{
return "WORD PTR [bp-"+stk_offset+"]";
}
string cur_function_label(string name)
{
return "L_"+name;
}
string process_global_variable(string str)
{
vector<string> ret;
char delim = '[';
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != string::npos)
{
end = str.find(delim, start);
ret.push_back(str.substr(start, end - start));
}
int sz = ret.size();
if(sz == 1) return ret[0];
else return ret[0]+"[BX]";
}
vector<string> tokenize(string str,char delim)
{
vector<string> ret;
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != string::npos)
{
end = str.find(delim, start);
ret.push_back(str.substr(start, end - start));
}
return ret;
}
void optimize_code(string code)
{
vector<string>line_v = tokenize(code,'\n');
int line_v_sz = line_v.size();
string prev_line_cmd = "";
vector<string>prev_line_token;
for(int i=0;i<line_v_sz;i++)
{
string cur_line = line_v[i];
vector<string>cur_line_token;
if(cur_line[0] == ';')
{
opt_codeout<<cur_line<<endl;
continue;
}
vector<string>token_v = tokenize(cur_line,' ');
if(token_v[0] == "MOV" || token_v[0]=="mov")
{
if(token_v[1] == "WORD")
{
cur_line_token = tokenize(token_v[3],',');
}
else
{
cur_line_token = tokenize(token_v[1],',');
}
if(prev_line_cmd == "MOV" || prev_line_cmd == "mov")
{
if(i>0)
{
// for(auto x:prev_line_token)
// cout<<x<<endl;
// cout<<endl;
// cout<<"---"<<endl;
// cout<<endl;
// for(auto x:cur_line_token)
// cout<<x<<endl;
// cout<<"==========="<<endl;
if(cur_line_token[0] == prev_line_token[1] && cur_line_token[1] == prev_line_token[0])
{
// optimize
}
else
{
opt_codeout<<cur_line<<endl;
}
}
else
{
opt_codeout<<cur_line<<endl;
}
}
else
{
opt_codeout<<cur_line<<endl;
}
prev_line_token = cur_line_token;
}
else
{
int sz_token_v = token_v.size();
if(sz_token_v >= 2)
{
if(token_v[1] == "PROC")
opt_codeout<<endl;
}
opt_codeout<<cur_line<<endl;
prev_line_token.clear();
}
prev_line_cmd = token_v[0];
}
}
vector<string>temp_SP_vector;
bool isATempVariable(string s)
{
for(string x:temp_SP_vector)
if(x == s) return true;
return false;
}
%}
%error-verbose
%union{
SymbolInfo* symbol_info;
string* symbol_info_str;
string* temp_str;
Helper* helper;;
}
%token IF ELSE LOWER_THAN_ELSE FOR WHILE DO BREAK CHAR DOUBLE RETURN SWITCH CASE DEFAULT CONTINUE PRINTLN INCOP DECOP ASSIGNOP NOT LPAREN RPAREN LCURL RCURL LTHIRD RTHIRD COMMA SEMICOLON
%token <symbol_info> ID INT FLOAT VOID ADDOP MULOP RELOP LOGICOP CONST_INT CONST_FLOAT ERROR_FLOAT
%type <helper> start program unit variable var_declaration type_specifier func_declaration func_definition parameter_list
%type <helper> expression factor unary_expression term simple_expression rel_expression statement statements compound_statement logic_expression expression_statement
%type <helper> arguments argument_list
%type <helper> declaration_list
%type <helper> dummy_scope_function
%destructor { erm_h($$); } <helper>
%destructor { erm_s($$); } <symbol_info>
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
%%
start: program
{
//write your code in this block in all the similar blocks below
print_grammar_rule("start","program");
$$ = new Helper();
// update type
$$->HelperType = $1->HelperType;
$$->text = $1->text;
// code
$$->code = $1->code;
// print_log_text($$->text);
if(err_count == 0)
{
string asm_header = ".MODEL SMALL\n\n.STACK 100H";
string output_proc = "\r\nOUTPUT PROC\r\n \r\n MOV CX , 0FH \r\n PUSH CX ; marker\r\n \r\n MOV IS_NEG, 0H\r\n MOV AX , FOR_PRINT\r\n TEST AX , 8000H\r\n JE OUTPUT_LOOP\r\n \r\n MOV IS_NEG, 1H\r\n MOV AX , 0FFFFH\r\n SUB AX , FOR_PRINT\r\n ADD AX , 1H\r\n MOV FOR_PRINT , AX\r\n\r\n OUTPUT_LOOP:\r\n \r\n ;MOV AH, 1\r\n ;INT 21H\r\n \r\n MOV AX , FOR_PRINT\r\n XOR DX,DX\r\n MOV BX , 10D\r\n DIV BX ; QUOTIENT : AX , REMAINDER : DX \r\n \r\n MOV FOR_PRINT , AX\r\n \r\n PUSH DX\r\n \r\n CMP AX , 0H\r\n JNE OUTPUT_LOOP\r\n \r\n ;LEA DX, NEWLINE ; DX : USED IN IO and MUL,DIV\r\n ;MOV AH, 9 ; AH,9 used for character string output\r\n ;INT 21H;\r\n\r\n MOV AL , IS_NEG\r\n CMP AL , 1H\r\n JNE OP_STACK_PRINT\r\n \r\n MOV AH, 2\r\n MOV DX, '-' ; stored in DL for display \r\n INT 21H\r\n \r\n \r\n OP_STACK_PRINT:\r\n \r\n ;MOV AH, 1\r\n ;INT 21H\r\n \r\n POP BX\r\n \r\n CMP BX , 0FH\r\n JE EXIT_OUTPUT\r\n \r\n \r\n MOV AH, 2\r\n MOV DX, BX ; stored in DL for display \r\n ADD DX , 30H\r\n INT 21H\r\n \r\n JMP OP_STACK_PRINT\r\n\r\n EXIT_OUTPUT:\r\n \r\n ;POP CX \r\n\r\n LEA DX, NEWLINE\r\n MOV AH, 9 \r\n INT 21H\r\n \r\n RET \r\n \r\nOUTPUT ENDP";
codeout<<asm_header<<endl;
codeout<<".DATA"<<endl;
for(auto dv:DATA_vector) codeout<<dv<<endl;
codeout<<endl;
codeout<<".CODE"<<endl;
// fileToCode(codeout,"output_proc.txt");
codeout<<output_proc<<endl;
codeout<<"\n"<<$$->code<<"\n"<<endl;
///////////
opt_codeout<<asm_header<<endl;
opt_codeout<<".DATA"<<endl;
for(auto dv:DATA_vector) opt_codeout<<dv<<endl;
opt_codeout<<endl;
opt_codeout<<".CODE"<<endl;
// fileToCode(opt_codeout,"output_proc.txt");
opt_codeout<<output_proc<<endl;
opt_codeout<<"\n"<<endl;
optimize_code($$->code);
}
erm_h($1);
}
;
program: program unit {
print_grammar_rule("program","program unit");
$$ = new Helper();
$$->text = $1->text;
$$->text += "\n";
$$->text += $2->text;
print_log_text($$->text);
// code
$$->tempVar = $1->tempVar;
$$->stk_offset = $1->stk_offset;
$$->code = $1->code;
$$->code += $2->code;
erm_h($1); erm_h($2);
}
| unit {
print_grammar_rule("program","unit");
$$ = new Helper();
$$->text = $1->text;
// update type
$$->HelperType = $1->HelperType;
print_log_text($$->text);
// code
$$->code = $1->code;
$$->tempVar = $1->tempVar;
$$->stk_offset = $1->stk_offset;
erm_h($1);
}
;
unit: var_declaration {
print_grammar_rule("unit","var_declaration");
$$ = new Helper();
$$->text = $1->text;
// update type
$$->HelperType = $1->HelperType;
print_log_text($$->text);
// code
$$->code = $1->code;
$$->tempVar = $1->tempVar;
$$->stk_offset = $1->stk_offset;
erm_h($1);
}
| func_declaration {
print_grammar_rule("unit","func_declaration");
$$ = new Helper();
$$->text = $1->text;
// update type
$$->HelperType = $1->HelperType;
print_log_text($1->text);
// code
$$->code = $1->code;
$$->tempVar = $1->tempVar;
$$->stk_offset = $1->stk_offset;
SP_VAL = 0;
erm_h($1);
}
| func_definition {
print_grammar_rule("unit","func_definition");
$$ = new Helper();
$$->text = $1->text;
// update type
$$->HelperType = $1->HelperType;
print_log_text($1->text);
// code
$$->code = $1->code;
$$->tempVar = $1->tempVar;
$$->stk_offset = $1->stk_offset;
SP_VAL = 0;
erm_h($1);
}
;
func_declaration: type_specifier ID LPAREN parameter_list RPAREN SEMICOLON {
print_grammar_rule("func_declaration","type_specifier ID LPAREN parameter_list RPAREN SEMICOLON");
$$ = new Helper();
// update text
$$->text = $1->text;
$$->text += " ";
$$->text += $2->key;
$$->text += "(";
$$->text += $4->text;
$$->text += ")";
$$->text += ";";
// insert function ID to SymbolTable with VAR_TYPE
$2->setVarType($1->text);
$2->isFunction = true;
// update parameter type
for(auto temp_s : function_params)
{
$2->param_v.push_back(temp_s.var_type);
}
if(sym_tab->insert_symbol(*$2))
{
SymbolInfo* ret_symbol = sym_tab->lookup($2->key);
ret_symbol->isFunctionDeclaration = true; // mark as function declaration
}
else
{
error_multiple_declaration($2->key);
}
print_log_text($$->text);
// clear param_info
function_params.clear();
erm_h($1); erm_s($2) ; erm_h($4);
}
| type_specifier ID LPAREN parameter_list RPAREN error {
print_grammar_rule("func_declaration","type_specifier ID LPAREN parameter_list RPAREN");
$$ = new Helper();
// update text
$$->text = $1->text;
$$->text += " ";
$$->text += $2->key;
$$->text += "(";
$$->text += $4->text;
$$->text += ")";
// insert function ID to SymbolTable with VAR_TYPE
$2->setVarType($1->text);
$2->isFunction = true;
// update parameter type
for(auto temp_s : function_params)
{
$2->param_v.push_back(temp_s.var_type);
}
if(sym_tab->insert_symbol(*$2))
{
SymbolInfo* ret_symbol = sym_tab->lookup($2->key);
ret_symbol->isFunctionDeclaration = true; // mark as function declaration
}
else
{
error_multiple_declaration($2->key);
}
print_log_text($$->text);
// clear param_info
function_params.clear();
erm_h($1); erm_s($2) ; erm_h($4);
}
| type_specifier ID LPAREN parameter_list error RPAREN SEMICOLON {
/**
To handle errors like:
void foo(int x-y);
**/
print_grammar_rule("func_declaration","type_specifier ID LPAREN parameter_list RPAREN SEMICOLON");
$$ = new Helper();
// update text
$$->text = $1->text;
$$->text += " ";
$$->text += $2->key;
$$->text += "(";
$$->text += $4->text;
$$->text += ")";
$$->text += ";";
// insert function ID to SymbolTable with VAR_TYPE
$2->setVarType($1->text);
$2->isFunction = true;
// update parameter type
for(auto temp_s : function_params)
{
$2->param_v.push_back(temp_s.var_type);
}
if(sym_tab->insert_symbol(*$2))
{
SymbolInfo* ret_symbol = sym_tab->lookup($2->key);
ret_symbol->isFunctionDeclaration = true; // mark as function declaration
}
else
{
error_multiple_declaration($2->key);
}
print_log_text($$->text);
// clear param_info
function_params.clear();
erm_h($1); erm_s($2) ; erm_h($4);
}
| type_specifier ID LPAREN parameter_list error RPAREN error {
/**
To handle errors like:
void foo(int x-y)
**/
print_grammar_rule("func_declaration","type_specifier ID LPAREN parameter_list RPAREN");
$$ = new Helper();
// update text
$$->text = $1->text;
$$->text += " ";
$$->text += $2->key;
$$->text += "(";
$$->text += $4->text;
$$->text += ")";
// insert function ID to SymbolTable with VAR_TYPE
$2->setVarType($1->text);
$2->isFunction = true;
// update parameter type
for(auto temp_s : function_params)
{
$2->param_v.push_back(temp_s.var_type);
}
if(sym_tab->insert_symbol(*$2))
{
SymbolInfo* ret_symbol = sym_tab->lookup($2->key);
ret_symbol->isFunctionDeclaration = true; // mark as function declaration
}
else
{
error_multiple_declaration($2->key);
}
print_log_text($$->text);
// clear param_info
function_params.clear();
erm_h($1); erm_s($2) ; erm_h($4);
}
| type_specifier ID LPAREN RPAREN SEMICOLON {
print_grammar_rule("func_declaration","type_specifier ID LPAREN RPAREN SEMICOLON");
$$ = new Helper();
// update text
$$->text = $1->text;
$$->text += " ";
$$->text += $2->key;
$$->text += "(";
$$->text += ")";
$$->text += ";";
// insert function ID to SymbolTable with VAR_TYPE
$2->setVarType($1->text);
$2->isFunction = true;
if(sym_tab->insert_symbol(*$2))
{
SymbolInfo* ret_symbol = sym_tab->lookup($2->key);
ret_symbol->isFunctionDeclaration = true; // mark as function declaration
}
else
{
error_multiple_declaration($2->key);
}
print_log_text($$->text);
function_params.clear();
erm_h($1); erm_s($2) ;
}
| type_specifier ID LPAREN RPAREN error {
/**
To handle errors like:
void foo()
**/
print_grammar_rule("func_declaration","type_specifier ID LPAREN RPAREN");
$$ = new Helper();
// update text
$$->text = $1->text;
$$->text += " ";
$$->text += $2->key;
$$->text += "(";
$$->text += ")";
// insert function ID to SymbolTable with VAR_TYPE
$2->setVarType($1->text);
$2->isFunction = true;
if(sym_tab->insert_symbol(*$2))
{
SymbolInfo* ret_symbol = sym_tab->lookup($2->key);
ret_symbol->isFunctionDeclaration = true; // mark as function declaration
}
else
{
error_multiple_declaration($2->key);
}
print_log_text($$->text);