-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.y
1627 lines (1426 loc) · 82.4 KB
/
source.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
%{
#define ST_LENGTH 2
#define MAX_LINE_LENGTH 256
#define NUMBER_OF_ST 100
#define MAX_NUMBER_OF_PARAM 100
#define NO_EXT 99999
#define FUNCPARAM 99998
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdbool.h>
#include<math.h>
#define Trace(t) printf(t)
int yylex(void);
int yyerror(char *s);
extern FILE *yyin;
extern int yylineno;
extern char* yytext;
char funcNameBuf[MAX_LINE_LENGTH];
union Value {
int integer;
double real;
char* string;
bool boolean;
};
struct MultiValue {
int type; // 1: int 2: real 3: string 4: bool
union Value value;
};
enum ValueType { // or DECLARATION TYPE
VALNONE,
VALINT,
VALREAL,
VALSTR,
VALBOOL,
VALARRAYREF,
};
enum StoreType {
NONE,
CONSTANT,
VARIABLE,
MULTIVAR, // ARRAY
FUNC,
PROC
};
struct ArrayAtr {
int startIndex;
int capacity;
};
struct FuncAtr {
int size;
int paramType[MAX_NUMBER_OF_PARAM];
struct ArrayAtr arrayAtr[MAX_NUMBER_OF_PARAM];
};
struct SymbolTable {
int index;
char** strList;
enum ValueType* valueType;
enum StoreType* storeType;
struct ArrayAtr* arrayAtr;
struct FuncAtr* funcAtr;
size_t capacity;
size_t size;
struct SymbolTable* parent;
};
struct TermStack {
struct SymbolTable* chosenTableStack;
int foundIndex;
};
struct FuncCallingStack {
bool funcCalling;
int atParentIndex;
int currentParamIndex;
};
struct SymbolTable** Col_ST;
int currentSize;
int currentTable;
struct TermStack termStack[10];
int topTerm;
struct FuncCallingStack funcCallingStack[10];
int topFuncCalling;
struct SymbolTable symbols;
void create();
void createChildTable();
int lookup(char* s);
int insert(char* s);
void dump();
int lookupInTable(char* s, struct SymbolTable* parentTable);
void printSymbols();
void printChosenSymbols(int index);
void returnToParent();
void initFlags();
void resetFunctionGrammarFlags();
enum ValueType valTypeBuffer = VALINT;
enum ValueType constExpTypeBuffer = VALINT;
// Flags
bool funcScope;
bool procScope;
bool needConstExp;
bool funcDeclaration;
bool funcCalling;
int currentParamIndex;
struct SymbolTable* functionParent;
int atParentIndex;
int foundIndex;
int scopeReturnType;
bool isCallingArray;
%}
%union {
int ival;
double dval;
char* sval;
bool bval;
struct {
int type;
union {
int uint;
double ureal;
char* ustr;
bool ubool;
struct {
int arrayType;
int startIndex;
int capacity;
} uarray;
} value;
} multival;
};
/* tokens */
%token ARRAY BEGINT CHAR BOOL CONST DECREASING DEFAULT DO ELSE END EXIT FOR FUNCTION GET IF LOOP OF PUT PROCEDURE RESULT RETURN SKIP THEN VAR WHEN
%token RELOPL RELOPLT RELOPG RELOPGT RELOPEQ AND OR NOT
%token DOT COMMA COLON SEMICOLON POPEN PCLOSE SOPEN SCLOSE BOPEN BCLOSE
%token ADD SUB MUL DIV MOD
%token ASSIGN
%token NEWLINE
%token <sval> STRING
%token <ival> INT ID
%token <dval> REAL
%token <bval> TRUE FALSE
%type <bval> bool_exp
%type <multival> exp
%type <ival> array_ref id_ext
%left OR
%left AND
%right NOT
%left RELOPLT RELOPLE RELOPGT RELOPGE RELOPEQ RELOPNE
%left ADD SUB
%left MUL DIV MOD
%nonassoc UMINUS
%start program
%%
/* ----------- Main Structure ----------- */
program: /* empty */
| decls
| stmts
| decls stmts
;
// newline: /* empty */ | NEWLINE
decls: /* empty */
| decl decls
;
stmts: /* empty */
| stmt stmts
;
/* ----------- Main Structure ----------- */
// const_exp: INT {
// constExpTypeBuffer = VALINT;
// $<ival>$ = $1;
// }
// | REAL {
// constExpTypeBuffer = VALREAL;
// $<dval>$ = $1;
// }
// | STRING {
// constExpTypeBuffer = VALSTR;
// $<sval>$ = $1;
// }
// | TRUE {
// constExpTypeBuffer = VALBOOL;
// $<bval>$ = $1;
// }
// | FALSE {
// constExpTypeBuffer = VALBOOL;
// $<bval>$ = $1;
// }
// ;
/* Value Type Non-Terminal */
val_type: INT {
valTypeBuffer = VALINT;
}
| REAL {
valTypeBuffer = VALREAL;
}
| STRING {
valTypeBuffer = VALSTR;
}
| BOOL {
valTypeBuffer = VALBOOL;
}
;
/* Declaration is divided into variables and functions declaration */
decl: var_decl | func_decl
/* Variable Declaration */
var_decl:
/* const id := const_exp */
CONST ID ASSIGN {needConstExp = true; }
exp {
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = CONSTANT;
// Col_ST[currentTable]->valueType[$2] = constExpTypeBuffer;
switch($<multival.type>5) {
case (int)VALREAL:
Col_ST[currentTable]->valueType[$2] = VALREAL;
break;
case (int)VALSTR:
Col_ST[currentTable]->valueType[$2] = VALSTR;
break;
case (int)VALBOOL:
Col_ST[currentTable]->valueType[$2] = VALBOOL;
break;
}
// TODO: store value
printSymbols();
needConstExp = false;
}
/* const id :val_type := const_exp */
| CONST ID COLON val_type ASSIGN {needConstExp = true;}
exp {
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = CONSTANT;
switch($<multival.type>7) {
case (int)VALREAL:
if(valTypeBuffer == VALINT) {
Col_ST[currentTable]->valueType[$2] = VALINT;
} else if(valTypeBuffer == VALREAL) {
Col_ST[currentTable]->valueType[$2] = VALREAL;
} else {
return yyerror("Error: Different value type");
}
break;
case (int)VALSTR:
if(valTypeBuffer != VALSTR) {
return yyerror("Error: Different value type");
}
Col_ST[currentTable]->valueType[$2] = VALSTR;
break;
case (int)VALBOOL:
if(valTypeBuffer != VALBOOL) {
return yyerror("Error: Different value type");
}
Col_ST[currentTable]->valueType[$2] = VALBOOL;
break;
}
// TODO: store value
printSymbols();
needConstExp = false;
}
/* var id := const_exp */
| VAR ID ASSIGN {needConstExp = true;}
exp {
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = VARIABLE;
switch($<multival.type>5) {
case (int)VALREAL:
Col_ST[currentTable]->valueType[$2] = VALREAL;
break;
case (int)VALSTR:
Col_ST[currentTable]->valueType[$2] = VALSTR;
break;
case (int)VALBOOL:
Col_ST[currentTable]->valueType[$2] = VALBOOL;
break;
}
// TODO: store value
printSymbols();
needConstExp = false;
}
/* var id :val_type := const_exp */
| VAR ID COLON val_type ASSIGN {needConstExp = true;}
exp {
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = VARIABLE;
switch($<multival.type>7) {
case (int)VALREAL:
if(valTypeBuffer == VALINT) {
Col_ST[currentTable]->valueType[$2] = VALINT;
} else if(valTypeBuffer == VALREAL) {
Col_ST[currentTable]->valueType[$2] = VALREAL;
} else {
return yyerror("Error: Different value type");
}
break;
case (int)VALSTR:
if(valTypeBuffer != VALSTR) {
return yyerror("Error: Different value type");
}
Col_ST[currentTable]->valueType[$2] = VALSTR;
break;
case (int)VALBOOL:
if(valTypeBuffer != VALBOOL) {
return yyerror("Error: Different value type");
}
Col_ST[currentTable]->valueType[$2] = VALBOOL;
break;
}
// TODO: store value
printSymbols();
needConstExp = false;
}
/* var id :val_type */
| VAR ID COLON val_type {
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = VARIABLE;
Col_ST[currentTable]->valueType[$2] = valTypeBuffer;
printSymbols();
}
/* var id :array int..int of val_type */
| VAR ID COLON ARRAY INT DOT DOT INT OF val_type {
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = MULTIVAR;
Col_ST[currentTable]->valueType[$2] = valTypeBuffer;
if($5 != 0 && $5 != 1) {
return yyerror("Error: Start index should be either 0 or 1");
}
if($5 > $8) {
return yyerror("Error: Invalid index range");
}
Col_ST[currentTable]->arrayAtr[$2].startIndex = $5;
Col_ST[currentTable]->arrayAtr[$2].capacity = $8 - $5 + 1;
// TODO: store value
printSymbols();
}
;
/* Simultanoues variable declaration */
var_decls: /* empty */
| var_decl var_decls
;
/* Statement Body */
stmt_body: var_decls stmts ;
/* For Function argument declaration */
func_args: POPEN formal_args PCLOSE
;
formal_args: /*empty*/
| formal_arg nextFormal_args
;
nextFormal_args: /* empty */
| COMMA formal_args
;
formal_arg: ID COLON val_type { // formal argument usual form
if(Col_ST[currentTable]->storeType[$1] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$1] = VARIABLE;
Col_ST[currentTable]->valueType[$1] = valTypeBuffer;
functionParent->funcAtr[atParentIndex].paramType[currentParamIndex] = (int)valTypeBuffer;
currentParamIndex++;
}
| ID COLON ARRAY INT DOT DOT INT OF val_type { // formal argument array form
if(Col_ST[currentTable]->storeType[$1] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$1] = MULTIVAR;
Col_ST[currentTable]->valueType[$1] = valTypeBuffer;
Col_ST[currentTable]->arrayAtr[$1].startIndex = $4;
Col_ST[currentTable]->arrayAtr[$1].capacity = $7 - $4 + 1;
functionParent->funcAtr[atParentIndex].paramType[currentParamIndex] = (int)valTypeBuffer;
functionParent->funcAtr[atParentIndex].arrayAtr[currentParamIndex].startIndex = $4;
functionParent->funcAtr[atParentIndex].arrayAtr[currentParamIndex].capacity = $7 - $4 + 1;
currentParamIndex++;
}
;
/* --------------------------------- */
/* Function or Procedure declaration */
func_decl:
/* FUNCTION Declaration */
FUNCTION ID {
funcScope = true;
funcDeclaration = true;
functionParent = Col_ST[currentTable];
atParentIndex = $2;
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = FUNC;
createChildTable();
printSymbols();
}
func_args { printSymbols(); functionParent->funcAtr[atParentIndex].size = currentParamIndex;}
COLON val_type { scopeReturnType = (int)valTypeBuffer;}
stmt_body END {
returnToParent();
// assign return type
Col_ST[currentTable]->valueType[$2] = valTypeBuffer;
printSymbols();
}
ID {
if($2 != $12) {
return yyerror("Error: Function closure does not match the function name");
}
// Reset
resetFunctionGrammarFlags();
}
/* PROCEDURE Declaration */
| PROCEDURE ID {
procScope = true;
scopeReturnType = -1;
funcDeclaration = true;
functionParent = Col_ST[currentTable];
atParentIndex = $2;
if(Col_ST[currentTable]->storeType[$2] != NONE) {
return yyerror("Error: Identifier existed");
}
Col_ST[currentTable]->storeType[$2] = PROC;
Col_ST[currentTable]->valueType[$2] = VALNONE;
createChildTable();
printSymbols();
}
func_args { printSymbols(); functionParent->funcAtr[atParentIndex].size = currentParamIndex; }
stmt_body END {
returnToParent();
printSymbols();
}
ID {
if($2 != $9) {
return yyerror("Error: Function closure does not match the function name");
}
// Reset
resetFunctionGrammarFlags();
}
;
/* Statements */
stmt: block
| simple
| proc_invoke
| if_stmt
| loop_stmt
;
/* Blocks */
block: BEGINT {
createChildTable();
printSymbols();
}
stmt_body END {
returnToParent();
printSymbols();
}
;
/* Simple Statement */
simple:
ID array_ref ASSIGN exp {
struct SymbolTable* chosenTable = NULL;
struct SymbolTable* parentTable = NULL;
int foundIndex = -1;
// Check whether is initialized
if(Col_ST[currentTable]->storeType[$1] == NONE) {
// Search in its ancestor symbol table
bool found = false;
parentTable = Col_ST[currentTable]->parent;
while (!found) {
if(parentTable == NULL) {
return yyerror("Error: Identifier is not assigned yet anywhere");
}
foundIndex = lookupInTable(Col_ST[currentTable]->strList[$1], parentTable);
if(foundIndex != -1 && parentTable->storeType[foundIndex] != NONE) {
found = true;
break;
}
parentTable = parentTable->parent;
}
printf("FOUND in parent %s (table %d, index %d)! for assignment\n", Col_ST[currentTable]->strList[$1],parentTable->index, foundIndex );
// CHOSEN TABLE TO PARENT TABLE
chosenTable = parentTable;
}
if(parentTable == NULL) {
// JUST FOR CURRENT TABLE VARIABLE
chosenTable = Col_ST[currentTable];
foundIndex = $1;
}
if(chosenTable->storeType[foundIndex] == CONSTANT) {
return yyerror("Error: Cannot assign to constant variables");
}
if(chosenTable->storeType[foundIndex] == FUNC) {
return yyerror("Error: Cannot assign to functions");
}
if(chosenTable->storeType[foundIndex] == PROC) {
return yyerror("Error: Cannot assign to procedures");
}
// Check whether this is array or not AND grammar exception
if($2 == -1) { // grammar WITHOUT array reference
if(chosenTable->storeType[foundIndex] == MULTIVAR) {
return yyerror("Error: Expressions of array must have reference form");
}
// TODO: Do assignment to normal variable
if(chosenTable->valueType[foundIndex] == VALINT && $<multival.type>4 == (int)VALREAL) {
printf("Convert real(rhs) to int(lhs)! Do assignment\n");
} else if((int)chosenTable->valueType[foundIndex] == $<multival.type>4) {
printf("Value match! Do assignment\n");
} else {
return yyerror("Error: Left hand side does not match right hand side value type");
}
} else { // grammar WITH array reference
if(chosenTable->storeType[foundIndex] != MULTIVAR) {
return yyerror("Error: Non array expression must not have reference form");
}
// check start index
int sIndex = chosenTable->arrayAtr[foundIndex].startIndex;
if($2 < sIndex) {
return yyerror("Error: Wrong start index");
}
// TODO: Do assignment to array
if(chosenTable->valueType[foundIndex] == VALINT && $<multival.type>4 == (int)VALREAL) {
printf("Convert real(rhs) to int(lhs)! Do assignment\n");
} else if((int)chosenTable->valueType[foundIndex] == $<multival.type>4) {
printf("Value match! Do assignment\n");
} else {
return yyerror("Error: Left hand side does not match right hand side value type");
}
}
}
| PUT exp {
switch($<multival.type>2) {
case (int)VALREAL:
printf("arth_exp: %lf\n", $<multival.value.ureal>2);
break;
case (int)VALSTR:
printf("string: %s\n", $<multival.value.ustr>2);
break;
case (int)VALBOOL:
printf("bool_exp: %d\n", $<multival.value.ubool>2);
break;
case (int)VALARRAYREF:
printf("array: print whole array");
break;
}
}
| GET ID array_ref {
struct SymbolTable* chosenTable = NULL;
struct SymbolTable* parentTable = NULL;
int foundIndex;
// Check whether is initialized
if(Col_ST[currentTable]->storeType[$2] == NONE) {
// Search in its ancestor symbol table
bool found = false;
parentTable = Col_ST[currentTable]->parent;
while (!found) {
if(parentTable == NULL) {
return yyerror("Error: Identifier is not assigned yet anywhere");
}
foundIndex = lookupInTable(Col_ST[currentTable]->strList[$2], parentTable);
if(foundIndex != -1 && parentTable->storeType[foundIndex] != NONE) {
found = true;
break;
}
parentTable = parentTable->parent;
}
printf("FOUND in parent %s (table %d, index %d)! for get ID\n", Col_ST[currentTable]->strList[$2], parentTable->index, foundIndex);
// CHOSEN TABLE TO PARENT TABLE
chosenTable = parentTable;
}
if(parentTable == NULL) {
// JUST FOR CURRENT TABLE VARIABLE
chosenTable = Col_ST[currentTable];
foundIndex = $2;
}
if(chosenTable->storeType[foundIndex] == CONSTANT) {
return yyerror("Error: Cannot process input to constant variables");
}
if(chosenTable->storeType[foundIndex] == FUNC) {
return yyerror("Error: Cannot process input to functions");
}
if(chosenTable->storeType[foundIndex] == PROC) {
return yyerror("Error: Cannot process input to procedures");
}
// Check whether this is array or not AND grammar exception
if($2 == -1) { // grammar WITHOUT array reference
if(chosenTable->storeType[foundIndex] == MULTIVAR) {
return yyerror("Error: Expressions of array must have reference form");
}
// TODO: Do scanning into normal variable
printf("Scan input to noraml variable");
} else { // grammar WITH array reference
if(chosenTable->storeType[foundIndex] != MULTIVAR) {
return yyerror("Error: Non array expression must not have reference form");
}
// check start index
int sIndex = chosenTable->arrayAtr[foundIndex].startIndex;
if($3 < sIndex) {
return yyerror("Error: Wrong start index");
}
// TODO: Do scanning into array
printf("Scan input to noraml variable");
}
}
| RESULT exp {
if(procScope) {
return yyerror("Error: RESULT EXPRESSION is only used in function scope");
}
if(funcScope) {
// check return type
int expType = $<multival.type>2;
if(expType == VALREAL && scopeReturnType == (int)VALINT) {
// Convert real to int
} else if (expType == scopeReturnType) {
// ok
} else{
return yyerror("Error: Expression of result must be the same as return type of the function");
}
}
}
| RETURN {
if(funcScope) {
return yyerror("Error: RETURN is only used in procedure scope");
}
if(procScope) {
// do nothing
}
}
| EXIT exit_cond
| SKIP
;
/* Exit Condition */
exit_cond: /* empty */
| WHEN exp {
if($<multival.type>2 != (int)VALBOOL) {
return yyerror("Error: Exit condition must be bool expression");
}
}
/* Expression Forms */
exp:
/* Parantheses */
POPEN exp PCLOSE {
// if($<multival.type>2 == (int)VALSTR) {
// return yyerror("Error: Parentheses cannot accept string");
// }
if($<multival.type>2 == (int)VALREAL) {
$<multival.value.ureal>$ = $<multival.value.ureal>2;
$<multival.type>$ = (int)VALREAL;
// printf("%lf\n", $<multival.value.ureal>$);
} else if ($<multival.type>2 == (int)VALBOOL) {
$<multival.value.ubool>$ = $<multival.value.ubool>2;
$<multival.type>$ = (int)VALBOOL;
// printf("%d\n", $<multival.value.ubool>$);
} else if ($<multival.type>2 == (int)VALSTR) {
$<multival.value.ustr>$ = $<multival.value.ustr>2;
$<multival.type>$ = (int)VALSTR;
// printf("%d\n", $<multival.value.ubool>$);
}
}
/* Arithmetic Expression */
| arth_exp {
if($<multival.type>1 != (int)VALREAL) {
return yyerror("Error: Arithmethic expression only can receive numbers");
}
$<multival.value.ureal>$ = $<multival.value.ureal>1;
$<multival.type>$ = (int)VALREAL;
// printf("%lf\n", $<multival.value.ureal>$);
}
/* Boolean Expression */
| bool_exp {
$<multival.value.ubool>$ = $1;
$<multival.type>$ = (int)VALBOOL;
}
/* Expression Components */
| term {
switch($<multival.type>1) {
case (int)VALREAL:
$<multival.value.ureal>$ = $<multival.value.ureal>1;
$<multival.type>$ = (int)VALREAL;
break;
case (int)VALSTR:
$<multival.value.ustr>$ = strdup($<multival.value.ustr>1);
$<multival.type>$ = (int)VALSTR;
break;
case (int)VALBOOL:
$<multival.value.ubool>$ = $<multival.value.ubool>1;
$<multival.type>$ = (int)VALBOOL;
break;
case (int)VALARRAYREF:
$<multival.value.uarray.arrayType>$ = $<multival.value.uarray.arrayType>1;
$<multival.value.uarray.startIndex>$ = $<multival.value.uarray.startIndex>1;
$<multival.value.uarray.capacity>$ = $<multival.value.uarray.capacity>1;
$<multival.type>$ = (int)VALARRAYREF;
break;
}
}
;
bool_exp:
exp RELOPLT exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$$ = $<multival.value.ureal>1 < $<multival.value.ureal>3;
}
| exp RELOPLE exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$$ = $<multival.value.ureal>1 <= $<multival.value.ureal>3;
}
| exp RELOPGT exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$$ = $<multival.value.ureal>1 > $<multival.value.ureal>3;
}
| exp RELOPGE exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$$ = $<multival.value.ureal>1 >= $<multival.value.ureal>3;
}
| exp RELOPEQ exp {
if($<multival.type>1 != $<multival.type>3) {
return yyerror("Error: Cannot compare different expression type");
}
// --- RECHECK and COMPARE---
if($<multival.type>1 == (int)VALREAL || $<multival.type>3 == (int)VALREAL) {
// Compare REAL
$$ = $<multival.value.ureal>1 == $<multival.value.ureal>3;
} else if($<multival.type>1 == (int)VALSTR || $<multival.type>3 == (int)VALSTR) {
// Compare STRING
if(strcmp($<multival.value.ustr>1, $<multival.value.ustr>3) == 0) {
$$ = true;
} else {
$$ = false;
}
// Need to free cuz of strdup previously
free($<multival.value.ustr>1);
free($<multival.value.ustr>3);
} else if($<multival.type>1 == (int)VALBOOL || $<multival.type>3 == (int)VALBOOL) {
// Compare BOOL
$$ = $<multival.value.ubool>1 == $<multival.value.ubool>3;
}
}
| exp RELOPNE exp {
if($<multival.type>1 != $<multival.type>3) {
return yyerror("Error: Cannot compare different expression type");
}
// --- RECHECK and COMPARE---
if($<multival.type>1 == (int)VALREAL || $<multival.type>3 == (int)VALREAL) {
// Compare REAL
$$ = $<multival.value.ureal>1 != $<multival.value.ureal>3;
} else if($<multival.type>1 == (int)VALSTR || $<multival.type>3 == (int)VALSTR) {
// Compare STRING
if(strcmp($<multival.value.ustr>1, $<multival.value.ustr>3) != 0) {
$$ = true;
} else {
$$ = false;
}
// Need to free cuz of strdup previously
free($<multival.value.ustr>1);
free($<multival.value.ustr>3);
} else if($<multival.type>1 == (int)VALBOOL || $<multival.type>3 == (int)VALBOOL) {
// Compare BOOL
$$ = $<multival.value.ubool>1 != $<multival.value.ubool>3;
}
}
| exp AND exp {
if($<multival.type>1 != (int)VALBOOL || $<multival.type>3 != (int)VALBOOL) {
return yyerror("Error: Boolean expressions can only receive boolean");
}
$$ = $<multival.value.ubool>1 && $<multival.value.ubool>3;
}
| exp OR exp {
if($<multival.type>1 != (int)VALBOOL || $<multival.type>3 != (int)VALBOOL) {
return yyerror("Error: Boolean expressions can only receive boolean");
}
$$ = $<multival.value.ubool>1 || $<multival.value.ubool>3;
}
| NOT exp {
if($<multival.type>2 != (int)VALBOOL) {
return yyerror("Error: Boolean expressions can only receive boolean");
}
$$ = !$<multival.value.ubool>2;
}
;
arth_exp:
exp ADD exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$<multival.value.ureal>$ = $<multival.value.ureal>1 + $<multival.value.ureal>3;
$<multival.type>$ = (int)VALREAL;
printf("%lf\n", $<multival.value.ureal>$);
}
| exp SUB exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$<multival.value.ureal>$ = $<multival.value.ureal>1 - $<multival.value.ureal>3;
$<multival.type>$ = (int)VALREAL;
printf("%lf\n", $<multival.value.ureal>$);
}
| exp MUL exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$<multival.value.ureal>$ = $<multival.value.ureal>1 * $<multival.value.ureal>3;
$<multival.type>$ = (int)VALREAL;
printf("%lf\n", $<multival.value.ureal>$);
}
| exp DIV exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
if($<multival.value.ureal>3 == 0) return yyerror("divide by zero");
else {
$<multival.value.ureal>$ = $<multival.value.ureal>1 / $<multival.value.ureal>3;
$<multival.type>$ = (int)VALREAL;
printf("%lf\n", $<multival.value.ureal>$);
}
}
| exp MOD exp {
if($<multival.type>1 != (int)VALREAL || $<multival.type>3 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$<multival.value.ureal>$ = fmod($<multival.value.ureal>1, $<multival.value.ureal>3);
$<multival.type>$ = (int)VALREAL;
printf("%lf\n", $<multival.value.ureal>$);
}
| SUB exp %prec UMINUS {
if($<multival.type>2 != (int)VALREAL) {
return yyerror("Error: Expressions only can receive numbers");
}
$<multival.value.ureal>$ = -$<multival.value.ureal>2;
$<multival.type>$ = (int)VALREAL;
printf("%lf", $<multival.value.ureal>$);
}
;
term:
/* variable names and array reference */
ID {
topTerm++;
termStack[topTerm].chosenTableStack = NULL;
termStack[topTerm].foundIndex = -1;
struct SymbolTable* parentTable = NULL;
// Check whether is initialized
if(Col_ST[currentTable]->storeType[$1] == NONE) {
// Search in its ancestor symbol table
bool found = false;
parentTable = Col_ST[currentTable]->parent;
while (!found) {
if(parentTable == NULL) {
return yyerror("Error: Identifier is not assigned yet anywhere");
}
termStack[topTerm].foundIndex = lookupInTable(Col_ST[currentTable]->strList[$1], parentTable);
if(termStack[topTerm].foundIndex != -1 && parentTable->storeType[termStack[topTerm].foundIndex] != NONE) {
found = true;
break;
}