-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass5_15CS30043.y
1882 lines (1688 loc) · 81 KB
/
ass5_15CS30043.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
%{
#include<stdio.h>
#include "ass5_15CS30043_translator.h"
#include "ass5_15CS30043_target_translator.cxx"
#include <string>
#include <sstream>
#include <cstring>
int yyerror(string);
extern int yylex(void);
//exp_attr exp;
dec_attr curr; //global variable to replicate inherited attribute functioning. We need to pass on the type information from type speciifer to the
bool dynallocerror; //to check if a matrix has been dynamically allocated and is not a function return type
string func_name; //global variable to store the function name to generate the quad at appropriate place in the quadarray
%}
%union{
id_attr id;
int intVal;
char charVal;
double dbVal;
char* stringVal;
struct exp_attr exp;
dec_attr dec;
int instr;
vector<int>* nextList;
vector<exp_attr>* arglist;
init inr;
}
%token<id> IDENTIFIER
%token<intVal> INT_CONSTANT
%token<dbVal> FLOAT_CONSTANT
%token<charVal> CHAR_CONSTANT
%token<intVal> ZERO_CONSTANT
%token<stringVal> STRING_LITERAL
%token UNSIGNED
%token BREAK
%token VOID
%token RETURN
%token CASE
%token FLOAT
%token SHORT
%token CHAR
%token FOR
%token SIGNED
%token WHILE
%token GOTO
%token BOOL
%token CONTINUE
%token IF
%token DEFAULT
%token DO
%token INT
%token SWITCH
%token DOUBLE
%token LONG
%token ELSE
%token MATRIX
%token DASHARROW /* -> */
%token PLUSPLUS /* ++ */
%token MINMIN /* -- */
%token DOTQUOTE /* .' */
%token LSHIFT /* << */
%token RSHIFT /* >> */
%token LTE /* <= */
%token GTE /* >= */
%token EQUALS /* == */
%token NEQUALS /* != */
%token LAND /* && */
%token LOR /* || */
%token STAREQ /* *= */
%token DIVEQ /* /= */
%token MODEQ /* %= */
%token PLUSEQ /* += */
%token MINEQ /* -= */
%token LSHIFTEQ /* <<= */
%token RSHIFTEQ /* >>= */
%token ANDEQ /* &= */
%token POWEQ /* ^= */
%token OREQ /* |= */
%token ERR
%type <exp> expression_opt parameter_type_list parameter_list parameter_declaration primary_expression postfix_expression unary_expression cast_expression multiplicative_expression additive_expression shift_expression relational_expression equality_expression AND_expression exclusive_OR_expression inclusive_OR_expression logical_AND_expression logical_OR_expression conditional_expression assignment_expression expression constant_expression direct_declarator init_declarator init_declarator_list init_declarator_list_opt declarator
%type <charVal> unary_operator assignment_operator
%type <instr> M
%type <nextList> N
%type <dec> type_specifier
%type <dec> K
%type <nextList> statement compound_statement selection_statement iteration_statement jump_statement block_item_list_opt block_item_list block_item
%type <arglist> argument_expression_list argument_expression_list_opt initializer_row initializer_row_list
%type <inr> initializer
//%type <dec>
%start translation_unit
%%
/*Expressions*/
primary_expression: IDENTIFIER {
//We see if the identifier is present in the currentsymboltable
if(currentsymboltable->isPresent($1.name))
{
$$.addr = currentsymboltable->lookup($1.name);
$$.t_exp = &($$.addr->t);
$$.falselist = NULL;
$$.isConst = false;
$$.num_stars = 0;
$$.isString = false;
$$.strkey = -1;
}
else
if(globalsymboltable->isPresent($1.name)) //We look in the globalsymboltable
{
$$.addr = globalsymboltable->lookup($1.name);
$$.t_exp = &($$.addr->t);
$$.truelist = NULL;
$$.falselist = NULL;
$$.isConst = false;
$$.num_stars = 0;
$$.isString = false;
$$.strkey = -1;
}
else //If not found in both we throw error
{
cout << "Identifier not declared" << endl;
exit(-1); //error
}
}
|INT_CONSTANT { //We will create a new temporary and initialize it to the INT_CONSTANT.
basictype t = t_integer;
type* t_int = new type(t);
symentry* tmp = currentsymboltable->gentemp(*t_int);
$$.addr = tmp;
initialVal i;
i.intVal = $1;
$$.addr->setInitialVal(i);
$$.addr->isConst = true;
ostringstream str1;
str1 << $1;
quads->emit(str1.str(),tmp->name); //Generate appropriate copy quad
$$.t_exp = t_int;
$$.truelist = NULL;
$$.falselist = NULL;
$$.isConst = true;
$$.num_stars = 0;
$$.isString = false;
$$.strkey = -1;
}
|FLOAT_CONSTANT {
//We will create a new temporary and initialize it to the FLOAT_CONSTANT.
basictype t = t_double;
type* t_doub = new type(t);
symentry* tmp = currentsymboltable->gentemp(*t_doub);
$$.addr = tmp;
initialVal i;
i.dbVal.db = $1;
$$.addr->setInitialVal(i);
$$.addr->isConst = true;
ostringstream str1;
str1 << $1;
quads->emit(str1.str(),tmp->name); //Generate appropriate copy quad
$$.t_exp = t_doub;
$$.truelist = NULL;
$$.falselist = NULL;
$$.isConst = true;
$$.num_stars = 0;
$$.isString = false;
$$.strkey = -1;
}
|CHAR_CONSTANT {
//We will create a new temporary and initialize it to the CHAR_CONSTANT.
basictype t = t_char;
type* t_char = new type(t);
symentry* tmp = currentsymboltable->gentemp(*t_char);
$$.addr = tmp;
initialVal i;
i.charVal = $1;
$$.addr->setInitialVal(i);
$$.addr->isConst = true;
ostringstream str1;
str1 << $1;
quads->emit(str1.str(),tmp->name); //Generate appropriate copy quad
$$.t_exp = t_char;
$$.truelist = NULL;
$$.falselist = NULL;
$$.isConst = true;
$$.num_stars = 0;
$$.isString = false;
$$.strkey = -1;
}
|ZERO_CONSTANT { //We will create a new temporary and initialize it to 0.
basictype t = t_integer;
type* t_int = new type(t);
symentry* tmp = currentsymboltable->gentemp(*t_int);
$$.addr = tmp;
initialVal i;
i.intVal = $1;
$$.addr->setInitialVal(i);
$$.addr->isConst = true;
ostringstream str1;
str1 << $1;
quads->emit(str1.str(),tmp->name); //Generate appropriate copy quad
$$.t_exp = t_int;
$$.truelist = NULL;
$$.falselist = NULL;
$$.isConst = true;
$$.num_stars = 0;
$$.isString = false;
$$.strkey = -1;
}
|STRING_LITERAL {
//Not supported
basictype t = t_char;
type* t1 = new type(t);
t = t_pointer;
$$.t_exp = new type(t);
$$.t_exp->pointerto = t1;
$$.isString = true;
string s($1);
ostringstream stream;
if(strings.count(s))
{
$$.strkey = strings[s];
}
else
{
strings[s] = stringkeys.size();
stream << stringkeys.size();
string s1 = ".msg" + stream.str();
stringkeys.push_back(s1);
$$.strkey = strings[s];
}
}
|'(' expression ')' { $$=$2; }
;
postfix_expression: primary_expression { $$=$1;
$$.isMattype = false;
}
|primary_expression '[' expression ']' '[' expression ']' {
if($1.t_exp->actype!=t_mat) //If the primary_expression was not a matrix its an error
{
cout << "Not a matrix type" <<endl;
exit(-1);
}
else
{ //We calculate the offset which is being accesed by generating appropriate quads
$$=$1;
$$.isMattype = true; //This marks that the array should be dereferenced before being used
type* t = new type(t_integer);
symentry* temp = currentsymboltable->gentemp(*t);
//ostringstream str1;
//str1 << $1.t_exp->cols;
quads->emit("=[]",$1.addr->name,"4",temp->name); //Load cols
quads->emit("*",$3.addr->name,temp->name,temp->name); //Multiply cols by the row index
//str1 << $1.t_exp->rows;
quads->emit("+",temp->name,$6.addr->name,temp->name); //Add the column index
quads->emit("*",temp->name,"8",temp->name); //Multiply by 8(size of double)
quads->emit("+",temp->name,"8",temp->name); //Offset by because first element starts at basepointer + 8
$$.addr = temp;
$$.mat = $1.addr;
$$.t_exp = new type(t_double);
}
$$.isConst = false; //No longer a static constant
}
|postfix_expression '(' argument_expression_list_opt ')' {
$$=$1;
if(!globalsymboltable->isPresent($1.addr->name)) //Check if function is present
{
cout << "No such function defined" << endl;
exit(-1);
}
symentry* func = globalsymboltable->lookup($1.addr->name); //We lookup the function in the global symboltable
symboltable* functable = func->nested_table;
int paramn = 0; //No. of parameter
if($3!=NULL)
{
//Match parameters and emit appropriate quads
if(parametercheck(functable,$3))
{
int argsize = $3->size();
for(int i=0;i<argsize;i++)
{
if((*$3)[i].isString == true)
{
quads->emit("param","",stringkeys[(*$3)[i].strkey]);
paramn++;
}
else
{
quads->emit("param","",(*$3)[i].addr->name);
paramn++;
}
}
}
else
{
cout << "Error while fucntion call" << endl;
exit(-1);
}
}
$$.t_exp = &(functable->symentries[0]->t); //return type of the function
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
ostringstream s1;
s1 << paramn;
quads->emit("=call",$1.addr->name,s1.str(),$$.addr->name);
$$.isConst = false;
}
|postfix_expression '.' IDENTIFIER {
//Not supported
}
|postfix_expression DASHARROW IDENTIFIER {
//Not supported
}
|postfix_expression PLUSPLUS {
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
//Check if the expression needs to be dereferenced first if its a Matrix. If its a pointer dereferencing then postfix++ has higher precedence that * so that need to be dereferenced
if($1.isMattype==true)
{
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$1.mat->name,$1.addr->name,$$.addr->name);
$$.t_exp = ty;
$$.addr->t = *ty;
$$.addr->size = ty->get_size();
quads->emit("+",$$.addr->name,"1",tmp->name);
quads->emit("[]=",tmp->name,$1.addr->name,$1.mat->name);
}
else{ //If does not need dereferencing generate normal quads
quads->emit($1.addr->name,$$.addr->name);
quads->emit("+",$1.addr->name,"1",$1.addr->name);
}
$$.isMattype = false;
$$.isConst = false;
}
|postfix_expression MINMIN {
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
//Check if the expression needs to be dereferenced first if its a Matrix. If its a pointer dereferencing then postfix-- has higher precedence that * so that need to be dereferenced
if($1.isMattype==true)
{
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$1.mat->name,$1.addr->name,$$.addr->name);
$$.addr->t = *ty;
$$.t_exp = ty;
$$.addr->size = ty->get_size();
cout << $$.addr->name << ":" << $$.addr->t.size;
quads->emit("-",$$.addr->name,"1",tmp->name);
quads->emit("[]=",tmp->name,$1.addr->name,$1.mat->name);
}
else{ //If does not need dereferencing generate normal quads
quads->emit($1.addr->name,$$.addr->name);
quads->emit("-",$1.addr->name,"1",$1.addr->name);
}
$$.isMattype = false;
$$.isConst = false;
}
|postfix_expression DOTQUOTE {
if($1.t_exp->actype!=t_mat) //check if it is Matrix
{
cout << "Error : Transpose not defined on this type" << endl;
exit(-1);
}
//Generate quad for Transpose
$$=$1;
type* mattrans = new type(t_mat,$1.t_exp->cols,$1.t_exp->rows);
$$.addr = currentsymboltable->gentemp(*mattrans);
$$.t_exp = mattrans;
quads->emit(".'",$1.addr->name,$$.addr->name);
}
;
argument_expression_list_opt: %empty { $$=NULL; }
|argument_expression_list {
$$=$1;
}
;
argument_expression_list: assignment_expression { //creates a new vector pushes the argument on a vector
$$ = new vector<exp_attr>();
$$->push_back($1);
}
|argument_expression_list ',' assignment_expression{
//pushes the argument on a vector
$$=$1;
$$->push_back($3);
}
;
unary_expression: postfix_expression { $$ = $1; }
|PLUSPLUS unary_expression { //Check if the expression needs to be dereferenced first if its a Matrix or a Pointer
$$=$2;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
if($2.isMattype==true)
{
//If it is a matrix that has to be dereferenced then it generates the appropriate quads
type* ty = new type(t_double);
quads->emit("=[]",$2.mat->name,$2.addr->name,$$.addr->name);
$$.addr->t = *ty;
$$.t_exp = ty;
$$.addr->size = ty->get_size();
quads->emit("+",$$.addr->name,"1",$$.addr->name);
quads->emit("[]=",$$.addr->name,$2.addr->name,$2.mat->name);
}
else if($2.isPtrtype == true)
{ //If it is a pointer that has to be dereferenced then it generates the appropriate quads
$$.isPtrtype = false;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("*val",$2.addr->name,$$.addr->name);
quads->emit("+",$$.addr->name,"1",$$.addr->name);
quads->emit("*copy",$$.addr->name,$2.addr->name);
}
else{
quads->emit("+",$2.addr->name,"1",$2.addr->name);
quads->emit($2.addr->name,$$.addr->name);
}
$$.isMattype = false;
$$.isConst = false;
}
|MINMIN unary_expression {
$$=$2;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
if($2.isMattype==true)
{ //If it is a matrix that has to be dereferenced then it generates the appropriate quads
type* ty = new type(t_double);
quads->emit("=[]",$2.mat->name,$2.addr->name,$$.addr->name);
$$.addr->t = *ty;
$$.t_exp = ty;
$$.addr->size = ty->get_size();
quads->emit("-",$$.addr->name,"1",$$.addr->name);
quads->emit("[]=",$$.addr->name,$2.addr->name,$2.mat->name);
}
else if($2.isPtrtype == true)
{ //If it is a pointer that has to be dereferenced then it generates the appropriate quads
$$.isPtrtype = false;
while($$.num_stars--)
{
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("*val",$2.addr->name,$$.addr->name);
quads->emit("-",$$.addr->name,"1",$$.addr->name);
quads->emit("*copy",$$.addr->name,$2.addr->name);
}
}
else{
quads->emit("-",$2.addr->name,"1",$2.addr->name);
quads->emit($2.addr->name,$$.addr->name);
}
$$.isMattype = false;
$$.isConst = false;
}
|unary_operator cast_expression {
if( $1 == '&')
{
//create a temporary of type pointer to the type of cast_expression
$$ = $2;
basictype t = t_pointer;
$$.t_exp = new type(t);
$$.t_exp->pointerto = $2.t_exp;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
if($2.isMattype==false){
quads->emit("&",$2.addr->name,$$.addr->name);
}
else
{ //If we want the address of a dereferenced matrix that is essentially adding the offset to the base pointer
quads->emit("&",$2.mat->name,$$.addr->name);
quads->emit("+",$$.addr->name,$2.addr->name,$$.addr->name);
$$.isMattype = false;
}
}
else if($1 == '*')
{
if($2.t_exp->pointerto == NULL) //if not a pointer throw error
{
cout << "Error - not a pointer";
exit(-1);
}
//else I change the type of the expression to the type which the pointer points to and set isPresent = true. I also keep a count of the no. of *s encountered
$$ = $2;
$$.t_exp = $2.t_exp->pointerto;
$$.isPtrtype = true;
$$.num_stars ++;
}
else if($1 == '+')
{
//Copy the expression and generate appropriate quads only when dereferencing is needed
$$ = $2;
if($2.isMattype == true)
{
$$.isMattype = false;
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$2.mat->name,$2.addr->name,tmp->name);
$$.addr = tmp;
$$.t_exp = ty;
}
else if($2.isPtrtype == true)
{
$$.isPtrtype = false;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("*val",$2.addr->name,$$.addr->name);
}
}
else if($1 == '-')
{
$$=$2;
//generate appropriate quads if dereferencing is needed
if($2.isMattype == true)
{
$$.isMattype = false;
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$2.mat->name,$2.addr->name,tmp->name);
$$.t_exp = ty;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("-u",tmp->name,$$.addr->name);
}
else if($2.isPtrtype == true)
{
$$.isPtrtype = false;
symentry* tmp= currentsymboltable->gentemp(*$$.t_exp);
quads->emit("*val",$2.addr->name,tmp->name);
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("-u",tmp->name,$$.addr->name);
}
//Check if the expression corresponds to an address i.e. we cannot negate a pointer
else if((($2.isPtrtype == false && $2.t_exp->actype == t_pointer)))
{
cout << "Error - Cannot negate a pointer reference" << endl;
exit(-1);
}
/*else if((($2.isMattype == false && $2.t_exp->actype == t_mat)))
{
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
int rows = $$.addr->t.rows;
int cols = $$.addr->t.cols;
for(int i=0;i<(rows*cols);i++)
{
ostringstream s1;
s1 << i;
quads->emit("negmat",s1.str(),$$.addr->name);
}
}*/
else
{
//Generate appropriate quads for the negate operation if deferencing was not needed
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("-u",$2.addr->name,$$.addr->name);
}
}
$$.isConst = false;
}
;
unary_operator: '&' { $$ = '&'; }
|'*' { $$ = '*'; }
|'+' { $$ = '+'; }
|'-' { $$ = '-'; }
;
cast_expression: unary_expression { $$ = $1;
}
multiplicative_expression: cast_expression { //If we reach here we know the matrix or pointer cannot occur on the LHS of an assignment_expression so we can dereference
$$=$1;
if($1.isMattype==true)
{
type* ty = new type(t_double);
$$.addr = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$1.mat->name,$1.addr->name,$$.addr->name);
$$.t_exp = ty;
$$.isMattype = false;
$$.isConst = false;
}
else if($1.isPtrtype==true)
{
$$.isPtrtype = false;
while($$.num_stars--)
{
type* ty = $$.addr->t.pointerto;
symentry* tmp= currentsymboltable->gentemp(*ty);
quads->emit("*val",$$.addr->name,tmp->name);
$$.addr = tmp;
}
$$.isConst = false;
}
}
|multiplicative_expression '*' cast_expression {
//dereference matrices and pointers
if($3.isMattype == true)
{
$3.isMattype = false;
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$3.mat->name,$3.addr->name,tmp->name);
$3.addr = tmp;
$3.t_exp=ty;
}
else if($3.isPtrtype == true)
{
$3.isPtrtype = false;
while($3.num_stars--)
{
type* ty = $3.addr->t.pointerto;
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("*val",$3.addr->name,tmp->name);
$3.addr = tmp;
}
}
//We need to handle matrix multiplication differently because the typecheck in that case is different
else if($3.t_exp->actype == t_mat && $1.t_exp->actype== t_mat )
{
if($1.t_exp->cols == $3.t_exp->rows)
{
$$=$1;
type* matmul = new type(t_mat,$1.t_exp->rows,$3.t_exp->cols);
$$.t_exp = matmul;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("*",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
else
{
yyerror("Incompatible matrix size for multiplication");
exit(-1);
}
}
else{
//For all other cases we simply typecheck and generate appropriate quads for the multiplication operation
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("*",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
}
|multiplicative_expression '/' cast_expression {
//dereference matrices and pointers
if($3.isMattype == true)
{
$3.isMattype = false;
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$3.mat->name,$3.addr->name,tmp->name);
$3.addr = tmp;
$3.t_exp=ty;
}
else if($3.isPtrtype == true)
{
$3.isPtrtype = false;
while($3.num_stars--)
{
type* ty = $3.addr->t.pointerto;
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("*val",$3.addr->name,tmp->name);
$3.addr = tmp;
}
}
//Matrix division or pointer division gives an error
else if(($3.isMattype == false && $3.t_exp->actype == t_mat)||(($3.isPtrtype == false && $3.t_exp->actype == t_pointer)))
{
cout << "Error -Pointer/Matrix divisions not supported" << endl;
exit(-1);
}
//After handling the above intricacies we simply typecheck and generate appropriate quads for the division operation
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("/",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
|multiplicative_expression '%' cast_expression { //dereference matrices and pointers
if($3.isMattype == true)
{
$3.isMattype = false;
type* ty = new type(t_double);
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("=[]",$3.mat->name,$3.addr->name,tmp->name);
$3.addr = tmp;
$3.t_exp=ty;
}
else if($3.isPtrtype == true)
{
$3.isPtrtype = false;
while($3.num_stars--)
{
type* ty = $3.addr->t.pointerto;
symentry* tmp = currentsymboltable->gentemp(*ty);
quads->emit("*val",$3.addr->name,tmp->name);
$3.addr = tmp;
}
}
//Matrix division or pointer division gives an error
else if(($3.isMattype == false && $3.t_exp->actype == t_mat)||(($3.isPtrtype == false && $3.t_exp->actype == t_pointer)))
{
cout << "Error -Pointer/Matrix modulo not supported" << endl;
exit(-1);
}
//After handling the above intricacies we simply typecheck and generate appropriate quads for the division operation
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("%",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
additive_expression: multiplicative_expression { $$=$1; }
|additive_expression '+' multiplicative_expression {
//we simply typecheck, generate new temporary and generate appropriate quads for the addition operation
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("+",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
|additive_expression '-' multiplicative_expression {
//we simply typecheck, generate new temporary and generate appropriate quads for the subtraction operation
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("-",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
shift_expression: additive_expression { $$=$1; }
|shift_expression LSHIFT additive_expression {
//Shift operation only supported for integer expressions or the ones that can be converterd to integers except double
if(($1.t_exp->actype != t_integer && $1.t_exp->actype != t_char && $1.t_exp->actype != t_Bool) || ($3.t_exp->actype != t_integer && $3.t_exp->actype != t_char && $3.t_exp->actype != t_Bool))
{
cout << "Incompatible Types for Shift operation" << endl;
exit(-1);
}
//After the check is done we create a temporary and generate appropriate quad
if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit("<<",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
|shift_expression RSHIFT additive_expression { //Shift operation only supported for integer expressions or the ones that can be converterd to integers except double
if(($1.t_exp->actype != t_integer && $1.t_exp->actype != t_char && $1.t_exp->actype != t_Bool) || ($3.t_exp->actype != t_integer && $3.t_exp->actype != t_char && $3.t_exp->actype != t_Bool))
{
cout << "Incompatible Types for Shift operation" << endl;
exit(-1);
}
//After the check is done we create a temporary and generate appropriate quad
if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
$$=$1;
$$.addr = currentsymboltable->gentemp(*$$.t_exp);
quads->emit(">>",$1.addr->name,$3.addr->name,$$.addr->name);
$$.isConst = false;
}
relational_expression: shift_expression { $$=$1; }
|relational_expression '<' shift_expression {
//Type check the two operations to be compared
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
//Assign the implicit boolean type to the expression
basictype t = t_Bool;
$$.t_exp = new type(t);
//create the truelist
//create the falselist
//Generate if goto quad
//Generate goto quad required when expression evaluates to false
$$.truelist = makelist(quads->nextinstr);
$$.falselist =makelist(quads->nextinstr+1);
quads->emit("if<",$1.addr->name, $3.addr->name,"");
quads->emit("goto","","");
$$.isConst = false;
}
|relational_expression '>' shift_expression {
//Type check the two operations to be compared
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
//Assign the implicit boolean type to the expression
basictype t = t_Bool;
$$.t_exp = new type(t);
//create the truelist
//create the falselist
//Generate if goto quad
//Generate goto quad required when expression evaluates to false
$$.truelist = makelist(quads->nextinstr);
$$.falselist =makelist(quads->nextinstr+1);
quads->emit("if>",$1.addr->name, $3.addr->name,"");
quads->emit("goto","","");
$$.isConst = false;
}
|relational_expression LTE shift_expression {
//Type check the two operations to be compared
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
//Assign the implicit boolean type to the expression
basictype t = t_Bool;
$$.t_exp = new type(t);
//create the truelist
//create the falselist
//Generate if goto quad
//Generate goto quad required when expression evaluates to false
$$.truelist = makelist(quads->nextinstr);
$$.falselist =makelist(quads->nextinstr+1);
quads->emit("if<=",$1.addr->name, $3.addr->name,"");
quads->emit("goto","","");
$$.isConst = false;
}
|relational_expression GTE shift_expression {
//Type check the two operations to be compared
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
//Assign the implicit boolean type to the expression
basictype t = t_Bool;
$$.t_exp = new type(t);
//create the truelist
//create the falselist
//Generate if goto quad
//Generate goto quad required when expression evaluates to false
$$.truelist = makelist(quads->nextinstr);
$$.falselist =makelist(quads->nextinstr+1);
quads->emit("if>=",$1.addr->name, $3.addr->name,"");
quads->emit("goto","","");
$$.isConst = false;
}
equality_expression: relational_expression { $$=$1; }
|equality_expression EQUALS relational_expression {
//Type check the two operations to be compared
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
//Assign the implicit boolean type to the expression
basictype t = t_Bool;
$$.t_exp = new type(t);
//create the truelist
//create the falselist
//Generate if goto quad
//Generate goto quad required when expression evaluates to false
$$.truelist = makelist(quads->nextinstr);
$$.falselist =makelist(quads->nextinstr+1);
quads->emit("if==",$1.addr->name, $3.addr->name,"");
quads->emit("goto","","");
$$.isConst = false;
}
|equality_expression NEQUALS relational_expression {
//Type check the two operations to be compared
if($1.t_exp->actype==t_double && $3.t_exp->actype==t_integer)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_char)
typecheck(&($3),&($1));
else if($1.t_exp->actype==t_integer && $3.t_exp->actype==t_Bool)
typecheck(&($3),&($1));
else
typecheck(&($1),&($3));
//Assign the implicit boolean type to the expression
basictype t = t_Bool;