-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbolTable.c
2039 lines (1807 loc) · 90 KB
/
symbolTable.c
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
/*
Group Number : 2
1 Dhruv Rawat 2019B3A70537P thedhruvrawat
2 Chirag Gupta 2019B3A70555P Chirag5128
3 Swastik Mantry 2019B1A71019P Swastik-Mantry
4 Shreyas Sheeranali 2019B3A70387P ShreyasSR
5 Vaibhav Prabhu 2019B3A70593P prabhuvaibhav
*/
#include <stdio.h>
#include <string.h>
#include "symbolTableDef.h"
#include "lexerDef.h"
#include "colorCodes.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
SymbolTable* symbolTable;
bool SEMANTIC_ERROR = false;
// Fowler–Noll–Vo hash function
// https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-03
unsigned int hash(const char *str) {
unsigned int res = 0;
const unsigned int prime = 16777619;
int len = strlen(str);
for (int i = 0; i < len; i++) {
res = (res * prime) ^ str[i];
}
return (((res % HASH_TABLE_SIZE) + HASH_TABLE_SIZE) % HASH_TABLE_SIZE);
}
SymbolTableNode* initSymbolTableNode(void) {
SymbolTableNode* newNode = malloc(sizeof(SymbolTableNode));
// newNode->hashTable is a fixed-length array
memset(newNode->hashTable, 0, sizeof(Record*) * HASH_TABLE_SIZE);
newNode->children = NULL;
newNode->scopeStart = 0;
newNode->scopeEnd = 0;
newNode->nextOffset = 0;
newNode->nestingLevel = 0;
newNode->funcInputST = NULL;
newNode->funcOutputST = NULL;
newNode->next = NULL;
newNode->parent = NULL;
return newNode;
}
SymbolTable* initSymbolTable(void) {
SymbolTable* symTab = malloc(sizeof(SymbolTable));
// Global Record Table is a fixed-length array
memset(symTab->global, 0, sizeof(GlobalRecord*) * HASH_TABLE_SIZE);
symTab->height = 0;
return symTab;
}
void pushToSymbolTableStack(SymbolTableStack* st, ASTNode* node, unsigned int offset) {
SymbolTableStackNode* newNode = malloc(sizeof(SymbolTableStackNode));
newNode->node = node;
newNode->offset = offset;
if (st->size == 0) {
newNode->next = NULL;
} else {
newNode->next = st->top;
}
st->top = newNode;
st->size++;
return;
}
SymbolTableStackNode* peekSymbolTableStack(SymbolTableStack* st) {
return st->top;
}
void popSymbolTableStack(SymbolTableStack* st) {
if (st->size == 0) {
printf("SymbolTableStack is empty!\n");
exit(1);
}
SymbolTableStackNode* currTop = st->top;
st->top = st->top->next;
st->size--;
free(currTop);
return;
}
SymbolTableStack* initSymbolTableStack(void) {
SymbolTableStack* st = malloc(sizeof(SymbolTableStack));
st->top = NULL;
st->size = 0;
return st;
}
GlobalRecord* moduleExists(char* name, unsigned int hashVal) {
GlobalRecord* moduleNode = symbolTable->global[hashVal];
while (moduleNode != NULL) {
if (strcmp(moduleNode->name, name) == 0) {
return moduleNode;
}
moduleNode = moduleNode->next;
}
return NULL;
}
Record* variableExists(SymbolTableNode* symbolTableNode, char* name, unsigned int hashVal) {
if (symbolTableNode == NULL) {
return NULL;
}
Record* varRecord = variableExists(symbolTableNode->funcOutputST, name, hashVal);
if (varRecord != NULL) {
return varRecord;
}
varRecord = symbolTableNode->hashTable[hashVal];
while (varRecord != NULL) {
if (strcmp(varRecord->name, name) == 0) {
return varRecord;
}
varRecord = varRecord->next;
}
if (symbolTableNode->parent == NULL) {
return variableExists(symbolTableNode->funcInputST, name, hashVal);
} else {
return variableExists(symbolTableNode->parent, name, hashVal);
}
}
Record* generateRecord(SymbolTableNode* symbolTableNode, ASTNode* idNode, ASTNode* dataTypeNode, unsigned int* nextOffset, bool parameterVariable) {
char* name = idNode->leaf.tok->lexeme;
Record* newRec = malloc(sizeof(Record));
strcpy(newRec->name, name);
newRec->iterator = false;
newRec->assigned = 0;
newRec->offset = *nextOffset;
newRec->next = NULL;
switch (dataTypeNode->leaf.tok->tok) {
case INTEGER: {
newRec->type.varType = INT;
newRec->width = SIZEOF_INT;
*nextOffset += SIZEOF_INT;
break;
}
case REAL: {
newRec->type.varType = DOUBLE;
newRec->width = SIZEOF_REAL;
*nextOffset += SIZEOF_REAL;
break;
}
case BOOLEAN: {
newRec->type.varType = BOOL;
newRec->width = SIZEOF_BOOL;
*nextOffset += SIZEOF_BOOL;
break;
}
case ARRAY: {
newRec->type.varType = ARR;
int elementSize;
// Setting the datatype of the elements of the array
switch (dataTypeNode->rightMostChild->leaf.tok->tok) {
case INTEGER: {
newRec->type.array.arrType = INT;
elementSize = SIZEOF_INT;
break;
}
case REAL: {
newRec->type.array.arrType = DOUBLE;
elementSize = SIZEOF_REAL;
break;
}
case BOOLEAN: {
newRec->type.array.arrType = BOOL;
elementSize = SIZEOF_BOOL;
break;
}
default: {
// Should never reach here
break;
}
}
ASTNode* rangeArrNode = dataTypeNode->leftMostChild;
// Resolving left bound
ASTNode* leftBound = rangeArrNode->leftMostChild;
ASTNode* leftIDNum = leftBound->leftMostChild;
if (leftBound->numChildren == 2) {
newRec->type.array.leftNegative = (leftIDNum->label[0] == 'M');
leftIDNum = leftIDNum->next;
} else {
newRec->type.array.leftNegative = false;
}
if (leftIDNum->leaf.tok->tok == ID) {
newRec->type.array.isLeftID = true;
strcpy(newRec->type.array.leftID, leftIDNum->leaf.tok->lexeme);
} else {
newRec->type.array.isLeftID = false;
newRec->type.array.left = leftIDNum->leaf.tok->num;
}
// Resolving right bound
ASTNode* rightBound = rangeArrNode->rightMostChild;
ASTNode* rightIDNum = rightBound->leftMostChild;
if (rightBound->numChildren == 2) {
newRec->type.array.rightNegative = (rightIDNum->label[0] == 'M');
rightIDNum = rightIDNum->next;
} else {
newRec->type.array.rightNegative = false;
}
if (rightIDNum->leaf.tok->tok == ID) {
newRec->type.array.isRightID = true;
strcpy(newRec->type.array.rightID, rightIDNum->leaf.tok->lexeme);
} else {
newRec->type.array.isRightID = false;
newRec->type.array.right = rightIDNum->leaf.tok->num;
}
// The static flag of the ast node itself (required d6433b4dcdee72uring code generation (maybe))
bool isStatic = !(newRec->type.array.isLeftID || newRec->type.array.isRightID);
// Calculating next offset
if (isStatic) {
int left = newRec->type.array.left * (newRec->type.array.leftNegative ? -1 : 1);
int right = newRec->type.array.right * (newRec->type.array.rightNegative ? -1 : 1);
if (left > right) {
printf(RED BOLD "[Semantic Analyser] Line %d: Specified subrange for array \"%s\" are illegal. (%d > %d)\n" RESET, idNode->leaf.tok->linenum,name, left, right);
SEMANTIC_ERROR = true;
return NULL;
}
if (parameterVariable) {
newRec->width = sizeof(void*) + 2 * SIZEOF_INT;
*nextOffset += newRec->width;
} else {
newRec->width = sizeof(void*) + elementSize * (right - left + 1);
*nextOffset += newRec->width;
}
} else {
// Adding the size of pointer
newRec->width = sizeof(void*);
*nextOffset += sizeof(void*);
// Check whether both the dynamic bounds have been declared and are of integer type
if (newRec->type.array.isLeftID) {
char* leftID = newRec->type.array.leftID;
Record* leftRec = variableExists(symbolTableNode, leftID, hash(leftID));
if (leftRec == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: The left bound of array \"%s\" is not declared.\n" RESET, idNode->leaf.tok->linenum,name);
SEMANTIC_ERROR = true;
} else if (leftRec->type.varType != INT) {
printf(RED BOLD "[Semantic Analyser] Line %d: The left bound of array \"%s\" is not of type integer.\n" RESET, idNode->leaf.tok->linenum,name);
SEMANTIC_ERROR = true;
}
}
if (newRec->type.array.isRightID) {
char* rightID = newRec->type.array.rightID;
Record* rightRec = variableExists(symbolTableNode, rightID, hash(rightID));
if (rightRec == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: The right bound of array \"%s\" is not declared.\n" RESET, idNode->leaf.tok->linenum,name);
SEMANTIC_ERROR = true;
} else if (rightRec->type.varType != INT) {
printf(RED BOLD "[Semantic Analyser] Line %d: The right bound of array \"%s\" is not of type integer.\n" RESET, idNode->leaf.tok->linenum,name);
SEMANTIC_ERROR = true;
}
}
}
break;
}
default: {
// Should never reach here
break;
}
}
return newRec;
}
GlobalRecord* findFunction(char* name, unsigned int hashVal) {
if (symbolTable->global[hashVal] == NULL) {
return NULL;
}
GlobalRecord* curr = symbolTable->global[hashVal];
while (curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return curr;
}
curr = curr->next;
}
return curr;
}
Record* findVariableInsertion(SymbolTableNode* symbolTableNode, char* name, unsigned int hashVal) {
if (symbolTableNode->hashTable[hashVal] == NULL) {
return NULL;
}
Record* curr = symbolTableNode->hashTable[hashVal];
while (curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return curr;
}
curr = curr->next;
}
return curr;
}
void populateInputOutputList(GlobalRecord* funcRecord, ASTNode* inputList, ASTNode* outputList) {
unsigned int* offset = &funcRecord->funcST->nextOffset;
SymbolTableNode* symbolTableNode = funcRecord->funcST;
SymbolTableNode* inputST = funcRecord->inputST;
SymbolTableNode* outputST = funcRecord->outputST;
// Creating the inputList
int inputListSize = 0;
Record* sentinel = malloc(sizeof(Record));
sentinel->next = NULL;
Record* curr = sentinel;
ASTNode* inputNode = inputList->leftMostChild;
while (inputNode != NULL) {
// Adding the input variable to the inputList
Record* newRecord = generateRecord(inputST, inputNode->leftMostChild, inputNode->rightMostChild, offset, true);
if (newRecord == NULL) {
funcRecord->error = true;
inputNode = inputNode->next;
continue;
}
curr->next = newRecord;
curr = curr->next;
inputListSize++;
// Adding the input variable to the symbolTableNode
char* name = newRecord->name;
unsigned int hashVal = hash(name);
Record* varRecord = findVariableInsertion(inputST, name, hashVal);
int linenum = inputNode->leftMostChild->leaf.tok->linenum;
if (varRecord == NULL) {
varRecord = malloc(sizeof(Record));
memcpy(varRecord, newRecord, sizeof(Record));
varRecord->next = NULL;
inputST->hashTable[hashVal] = varRecord;
} else if (strcmp(varRecord->name, name) == 0) {
printf(RED BOLD "[Semantic Analyser] Line %d: Redeclaration of variable %s in input list of module %s\n" RESET, linenum, name, funcRecord->name);
SEMANTIC_ERROR = true;
} else {
varRecord->next = malloc(sizeof(Record));
varRecord = varRecord->next;
memcpy(varRecord, newRecord, sizeof(Record));
varRecord->next = NULL;
}
inputNode = inputNode->next;
}
curr->next = NULL;
funcRecord->inputList = sentinel->next;
funcRecord->inputListSize = inputListSize;
// Creating the outputList
int outputListSize = 0;
sentinel->next = NULL;
curr = sentinel;
ASTNode* outputNode = outputList->leftMostChild;
while (outputNode != NULL) {
// Adding the output variable to the outputList
Record* newRecord = generateRecord(symbolTableNode, outputNode->leftMostChild, outputNode->rightMostChild, offset, true);
if (newRecord == NULL) {
funcRecord->error = true;
outputNode = outputNode->next;
continue;
}
curr->next = newRecord;
curr = curr->next;
outputListSize++;
// Adding the output variable to the outputSymbolTableNode
symbolTableNode = funcRecord->outputST;
char* name = newRecord->name;
unsigned int hashVal = hash(name);
// Checking if the variable has already been declared in the inputList
Record* varRecord = variableExists(funcRecord->funcST, name, hashVal);
if (varRecord != NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Redeclaration of variable %s in output list of module %s\n" RESET, outputNode->leftMostChild->leaf.tok->linenum, name, funcRecord->name);
SEMANTIC_ERROR = true;
outputNode = outputNode->next;
continue;
}
varRecord = findVariableInsertion(outputST, name, hashVal);
int linenum = outputNode->leftMostChild->leaf.tok->linenum;
if (varRecord == NULL) {
varRecord = malloc(sizeof(Record));
memcpy(varRecord, newRecord, sizeof(Record));
varRecord->next = NULL;
outputST->hashTable[hashVal] = varRecord;
} else if (strcmp(varRecord->name, name) == 0) {
printf(RED BOLD "[Semantic Analyser] Line %d: Redeclaration of variable %s in output list of module %s\n" RESET, linenum, name, funcRecord->name);
SEMANTIC_ERROR = true;
} else {
varRecord->next = malloc(sizeof(Record));
varRecord = varRecord->next;
memcpy(varRecord, newRecord, sizeof(Record));
varRecord->next = NULL;
}
outputNode = outputNode->next;
}
curr->next = NULL;
funcRecord->outputList = sentinel->next;
funcRecord->outputListSize = outputListSize;
free(sentinel);
return;
}
VAR_TYPE typeExtractor(ASTNode* exprNode, SymbolTableNode* symbolTableNode) {
// We are entering the function for the first time
if (strcmp(exprNode->label, "EXPR") == 0) {
// If unary operator
if (exprNode->numChildren == 2) {
VAR_TYPE rhsType = typeExtractor(exprNode->rightMostChild, symbolTableNode);
exprNode->type = rhsType;
// Unary operations are not allowed on arrays
if (rhsType == ARR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Unary operations are not allowed on arrays\n" RESET, exprNode->rightMostChild->leaf.tok->linenum);
SEMANTIC_ERROR = true;
return ERROR;
} else {
return rhsType;
}
} else {
VAR_TYPE rhsType = typeExtractor(exprNode->leftMostChild, symbolTableNode);
exprNode->type = rhsType;
return rhsType;
}
}
// If array (since there is no token in that case)
if (strcmp(exprNode->label, "ARRAY") == 0) {
char* name = exprNode->leftMostChild->leaf.tok->lexeme;
unsigned int hashVal = hash(name);
Record* varRecord = variableExists(symbolTableNode, name, hashVal);
if (varRecord == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined variable %s.\n" RESET, exprNode->leftMostChild->leaf.tok->linenum, name);
exprNode->type = ERROR;
SEMANTIC_ERROR = true;
return ERROR;
}
// Type checking for the index
ASTNode* indexNode = exprNode->rightMostChild;
VAR_TYPE indexType = typeExtractor(indexNode->rightMostChild, symbolTableNode);
if (indexType != INT) {
printf(RED BOLD "[Semantic Analyser] Line %d: Array index must be of type INTEGER.\n" RESET, exprNode->leftMostChild->leaf.tok->linenum);
indexNode->type = ERROR;
SEMANTIC_ERROR = true;
return ERROR;
}
indexNode->type = INT;
// Bound checking for the array
if (strcmp(indexNode->rightMostChild->label, "NUM") == 0) {
int index = indexNode->rightMostChild->leaf.tok->num;
if (indexNode->leftMostChild->leaf.tok->tok == MINUS) {
index = -index;
}
if (!varRecord->type.array.isLeftID && !varRecord->type.array.isRightID) {
int left = varRecord->type.array.left;
if (varRecord->type.array.leftNegative) {
left = -left;
}
int right = varRecord->type.array.right;
if (varRecord->type.array.rightNegative) {
right = -right;
}
if (index < left || index > right) {
printf(RED BOLD "[Semantic Analyser] Line %d: Index %d is out of bounds for array %s.\n" RESET, indexNode->rightMostChild->leaf.tok->linenum, index, name);
SEMANTIC_ERROR = true;
}
}
}
exprNode->type = varRecord->type.array.arrType;
return varRecord->type.array.arrType;
}
// If not array
switch (exprNode->leaf.tok->tok) {
case PLUS:
case MINUS:
case MUL: {
int leftType = typeExtractor(exprNode->leftMostChild, symbolTableNode);
int rightType = typeExtractor(exprNode->rightMostChild, symbolTableNode);
if (leftType == INT && rightType == INT) {
return exprNode->type = INT;
} else if (leftType == DOUBLE && rightType == DOUBLE) {
return exprNode->type = DOUBLE;
} else if (leftType == ERROR || rightType == ERROR) {
return exprNode->type = ERROR;
} else {
printf(RED BOLD "[Semantic Analyser] Line %d: Type Mismatch. Invalid types for %s.\n" RESET, exprNode->leaf.tok->linenum, token_types[exprNode->leaf.tok->tok]);
SEMANTIC_ERROR = true;
return exprNode->type = ERROR;
}
break;
}
case DIV: {
int leftType = typeExtractor(exprNode->leftMostChild, symbolTableNode);
int rightType = typeExtractor(exprNode->rightMostChild, symbolTableNode);
if ((leftType == INT || leftType == DOUBLE) && (rightType == INT || rightType == DOUBLE)) {
return exprNode->type = DOUBLE;
} else if (leftType == ERROR || rightType == ERROR) {
return exprNode->type = ERROR;
} else {
printf(RED BOLD "[Semantic Analyser] Line %d: Type Mismatch. Invalid types for DIV.\n" RESET, exprNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
return exprNode->type = ERROR;
}
break;
}
case AND:
case OR:
{
int leftType = typeExtractor(exprNode->leftMostChild, symbolTableNode);
int rightType = typeExtractor(exprNode->rightMostChild, symbolTableNode);
if (leftType == BOOL && rightType == BOOL) {
return exprNode->type = BOOL;
} else if (leftType == ERROR || rightType == ERROR) {
return exprNode->type = ERROR;
} else {
printf(RED BOLD "[Semantic Analyser] Line %d: Type Mismatch. Invalid types for %s.\n" RESET, exprNode->leaf.tok->linenum, token_types[exprNode->leaf.tok->tok]);
SEMANTIC_ERROR = true;
return exprNode->type = ERROR;
}
break;
}
case GT:
case LT:
case GE:
case LE:
case EQ:
case NE:
{
int leftType = typeExtractor(exprNode->leftMostChild, symbolTableNode);
int rightType = typeExtractor(exprNode->rightMostChild, symbolTableNode);
if (leftType == INT && rightType == INT) {
return exprNode->type = BOOL;
} else if (leftType == DOUBLE && rightType == DOUBLE) {
return exprNode->type = BOOL;
} else if (leftType == ERROR || rightType == ERROR) {
return exprNode->type = ERROR;
} else {
printf(RED BOLD "[Semantic Analyser] Line %d: Type Mismatch. Invalid types for %s.\n" RESET,exprNode->leaf.tok->linenum, token_types[exprNode->leaf.tok->tok]);
SEMANTIC_ERROR = true;
return exprNode->type = ERROR;
}
break;
}
case NUM: {
return exprNode->type = INT;
break;
}
case RNUM: {
return exprNode->type = DOUBLE;
break;
}
case TRUE:
case FALSE: {
return exprNode->type = BOOL;
break;
}
case ID: {
char* name = exprNode->leaf.tok->lexeme;
unsigned int hashVal = hash(name);
Record* varRecord = variableExists(symbolTableNode, name, hashVal);
if (varRecord == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined variable %s\n" RESET, exprNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
return exprNode->type = ERROR;
} else {
return exprNode->type = varRecord->type.varType;
}
break;
}
default: {
// Should never reach here
return exprNode->type = ERROR;
break;
}
}
}
void createWhileExprIDListRec(RecordList* recordList, ASTNode* exprNode, SymbolTableNode* symbolTableNode) {
if (exprNode == NULL) {
return;
}
if (exprNode->numChildren == 2) {
createWhileExprIDListRec(recordList, exprNode->leftMostChild, symbolTableNode);
createWhileExprIDListRec(recordList, exprNode->rightMostChild, symbolTableNode);
} else {
if (strcmp(exprNode->label, "ID") == 0) {
char* name = exprNode->leaf.tok->lexeme;
Record* varRecord = variableExists(symbolTableNode, name, hash(name));
if (varRecord == NULL) {
return;
}
RecordListNode* newNode = malloc(sizeof(RecordListNode));
newNode->record = varRecord;
if (recordList->size == 0) {
recordList->head = recordList->tail = newNode;
} else {
recordList->tail->next = newNode;
recordList->tail = newNode;
}
recordList->size++;
} else if (strcmp(exprNode->label, "ARRAY_INDEX") == 0) {
char* name = exprNode->leftMostChild->leaf.tok->lexeme;
Record* varRecord = variableExists(symbolTableNode, name, hash(name));
if (varRecord == NULL) {
return;
}
RecordListNode* newNode = malloc(sizeof(RecordListNode));
newNode->record = varRecord;
if (recordList->size == 0) {
recordList->head = recordList->tail = newNode;
} else {
recordList->tail->next = newNode;
recordList->tail = newNode;
}
recordList->size++;
} else {
return;
}
}
return;
}
RecordList* createWhileExprIDList(ASTNode* exprNode, SymbolTableNode* symbolTableNode) {
RecordList* recordList = malloc(sizeof(RecordList));
recordList->head = recordList->tail = NULL;
recordList->size = 0;
createWhileExprIDListRec(recordList, exprNode, symbolTableNode);
return recordList;
}
void populateSymbolTable(SymbolTableNode* symbolTableNode, ASTNode* statement, int level) {
/*
Types of statements possible:
1. GET_VALUE Use
2. PRINT Use
3. ASSIGN_STMT Use
4. MODULE_REUSE_STMT Use
5. DECLARE Populate
6. SWITCH New and Use
7. FOR New and Populate and Use
8. WHILE New and Populate and Use
*/
SymbolTableNode* sentinel = initSymbolTableNode();
SymbolTableNode* currChild = sentinel;
int lastLineNum = 0;
while (statement != NULL) {
switch (statement->label[0]) {
case 'G': { // GET_VALUE
ASTNode* idNode = statement->leftMostChild;
char* name = idNode->leaf.tok->lexeme;
// Setting scopeStart and lastLineNum
if (symbolTableNode->scopeStart == 0) {
symbolTableNode->scopeStart = idNode->leaf.tok->linenum;
}
lastLineNum = idNode->leaf.tok->linenum;
// Check if variable exists
Record* varRecord = variableExists(symbolTableNode, name, hash(name));
if (varRecord == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined variable %s\n" RESET, idNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
break;
} else if (varRecord->iterator) {
printf(RED BOLD "[Semantic Analyser] Line %d: Cannot get value of iterator %s \n" RESET, idNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
break;
}
varRecord->assigned++;
break;
}
case 'P': { // PRINT
ASTNode* printNode = statement->leftMostChild;
// Setting scopeStart and lastLineNum
if (symbolTableNode->scopeStart == 0) {
symbolTableNode->scopeStart = printNode->leaf.tok->linenum;
}
lastLineNum = printNode->leaf.tok->linenum;
if (strcmp(printNode->label, "ID") == 0) { // ID
char* name = printNode->leaf.tok->lexeme;
Record* varRecord = variableExists(symbolTableNode, name, hash(name));
if (varRecord == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined variable %s\n" RESET, printNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
break;
}
if (printNode->next == NULL) { break; }
// Case where there is a subscript
ASTNode* indexNode = printNode->next;
if (varRecord->type.varType != ARR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Variable %s is not an array\n" RESET, printNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
} else {
indexNode->type = INT;
indexNode->rightMostChild->type = INT;
// If array is static, check if the index is within bounds
if (!varRecord->type.array.isLeftID && !varRecord->type.array.isRightID && strcmp(indexNode->rightMostChild->label, "NUM") == 0) {
int left = varRecord->type.array.left;
if (varRecord->type.array.leftNegative) {
left = -left;
}
int right = varRecord->type.array.right;
if (varRecord->type.array.rightNegative) {
right = -right;
}
int index = indexNode->rightMostChild->leaf.tok->num;
// If MINUS is present, the index is negative
if (indexNode->numChildren == 2) {
if (indexNode->leftMostChild->leaf.tok->tok == MINUS) {
index = -index;
}
}
if (index < left || index > right) {
printf(RED BOLD "[Semantic Analyser] Line %d: Index %d is out of bounds for array %s.\n" RESET, indexNode->leftMostChild->leaf.tok->linenum, index, name);
SEMANTIC_ERROR = true;
}
} else if (strcmp(indexNode->rightMostChild->label, "ID") == 0) {
// Checking for existence of the index variable and the type
char* indexName = indexNode->rightMostChild->leaf.tok->lexeme;
Record* indexRecord = variableExists(symbolTableNode, indexName, hash(indexName));
if (indexRecord == NULL || strcmp(indexRecord->name, indexName) != 0) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined variable %s\n" RESET, indexNode->rightMostChild->leaf.tok->linenum, indexName);
SEMANTIC_ERROR = true;
} else if (indexRecord->type.varType != INTEGER) {
printf(RED BOLD "[Semantic Analyser] Line %d: Index %s is not an integer\n" RESET, indexNode->leaf.tok->linenum, indexName);
SEMANTIC_ERROR = true;
}
}
}
}
break;
}
case 'A': { // ASSIGN_STMT
// Check whether the LHS is defined
ASTNode* idNode = statement->leftMostChild;
bool arrayAccess = !(strcmp(statement->leftMostChild->label, "ID") == 0);
if (arrayAccess) {
idNode = statement->leftMostChild->leftMostChild;
}
// Setting scopeStart and lastLineNum
if (symbolTableNode->scopeStart == 0) {
symbolTableNode->scopeStart = idNode->leaf.tok->linenum;
}
lastLineNum = idNode->leaf.tok->linenum;
// Check existence of idNode and store type
VAR_TYPE idType;
char* name = idNode->leaf.tok->lexeme;
Record* varRecord = variableExists(symbolTableNode, name, hash(name));
if (varRecord == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined variable %s\n" RESET, idNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
} else {
idType = varRecord->type.varType;
}
if (varRecord != NULL && arrayAccess && idType != ARR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Variable %s is not an array\n" RESET, idNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
}
if (varRecord != NULL && varRecord->iterator) {
printf(RED BOLD "[Semantic Analyser] Line %d: Cannot assign to iterator %s\n" RESET, idNode->leaf.tok->linenum, name);
SEMANTIC_ERROR = true;
}
if (varRecord == NULL) {
typeExtractor(statement->rightMostChild, symbolTableNode);
if (arrayAccess) {
ASTNode* indexNode = statement->leftMostChild->rightMostChild;
typeExtractor(indexNode->rightMostChild, symbolTableNode);
}
break;
}
switch (idType) {
case INT: {
// Check if RHS is an integer
VAR_TYPE rhsType = typeExtractor(statement->rightMostChild, symbolTableNode);
if (rhsType != INTEGER && rhsType != ERROR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Type mismatch. Expected INTEGER type on the RHS.\n" RESET, idNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
}
break;
}
case DOUBLE: {
// Check if RHS is a double
VAR_TYPE rhsType = typeExtractor(statement->rightMostChild, symbolTableNode);
if (rhsType != DOUBLE && rhsType != ERROR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Type mismatch. Expected REAL type on the RHS.\n" RESET, idNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
}
break;
}
case BOOL: {
// Check if RHS is a boolean
VAR_TYPE rhsType = typeExtractor(statement->rightMostChild, symbolTableNode);
if (rhsType != BOOLEAN && rhsType != ERROR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Type mismatch. Expected BOOLEAN type on the RHS.\n" RESET, idNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
}
break;
}
case ARR: {
if (arrayAccess) {
VAR_TYPE arrayType = varRecord->type.array.arrType;
char *typeStrings[] = {
"INTEGER",
"REAL",
"BOOLEAN"
};
// Check if RHS type matches the array type
VAR_TYPE rhsType = typeExtractor(statement->rightMostChild, symbolTableNode);
if (rhsType != arrayType && rhsType != ERROR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Type mismatch. Expected %s on the RHS.\n" RESET, idNode->leaf.tok->linenum, typeStrings[arrayType]);
SEMANTIC_ERROR = true;
}
// Check if the index is within bounds
ASTNode* indexNode = statement->leftMostChild->rightMostChild;
VAR_TYPE indexType = typeExtractor(indexNode->rightMostChild, symbolTableNode);
if (indexType != INTEGER && indexType != ERROR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Index is not an integer\n" RESET, indexNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
} else if (indexType == INTEGER) {
if (strcmp(indexNode->rightMostChild->label, "NUM") != 0) {
break;
}
bool isStatic = !varRecord->type.array.isLeftID && !varRecord->type.array.isRightID;
if (!isStatic) { break; }
int index = indexNode->rightMostChild->leaf.tok->num;
int left = varRecord->type.array.left;
int right = varRecord->type.array.right;
if (indexNode->leftMostChild->leaf.tok->tok == MINUS) {
index = -index;
}
if (varRecord->type.array.leftNegative) {
left = -left;
}
if (varRecord->type.array.rightNegative) {
right = -right;
}
if (index < left || index > right) {
printf(RED BOLD "[Semantic Analyser] Line %d: Index %d is out of bounds for array %s.\n" RESET, indexNode->leftMostChild->leaf.tok->linenum, index, name);
SEMANTIC_ERROR = true;
}
}
} else {
// Check if RHS is an array
VAR_TYPE rhsType = typeExtractor(statement->rightMostChild, symbolTableNode);
if (rhsType != ARR && rhsType != ERROR) {
printf(RED BOLD "[Semantic Analyser] Line %d: Type mismatch. Expected ARRAY type on the RHS.\n" RESET, idNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
break;
} else if (rhsType == ERROR) {
break;
}
// Check if the dimensions match if both static
ASTNode* arrayNode = statement->rightMostChild->leftMostChild;
char* arrayName = arrayNode->leaf.tok->lexeme;
Record* arrRecord = variableExists(symbolTableNode, arrayName, hash(arrayName));
bool isLHSStatic = !varRecord->type.array.isLeftID && !varRecord->type.array.isRightID;
bool isRHSStatic = !arrRecord->type.array.isLeftID && !arrRecord->type.array.isRightID;
if (isLHSStatic && isRHSStatic) {
int leftLHS = varRecord->type.array.left;
int rightLHS = varRecord->type.array.right;
int leftRHS = arrRecord->type.array.left;
int rightRHS = arrRecord->type.array.right;
if (varRecord->type.array.leftNegative) {
leftLHS = -leftLHS;
}
if (varRecord->type.array.rightNegative) {
rightLHS = -rightLHS;
}
if (arrRecord->type.array.leftNegative) {
leftRHS = -leftRHS;
}
if (arrRecord->type.array.rightNegative) {
rightRHS = -rightRHS;
}
int sizeLHS = rightLHS - leftLHS + 1;
int sizeRHS = rightRHS - leftRHS + 1;
if (sizeLHS != sizeRHS) {
printf(RED BOLD "[Semantic Analyser] Line %d: Array sizes do not match.\n" RESET, idNode->leaf.tok->linenum);
SEMANTIC_ERROR = true;
}
}
}
break;
}
default: {
// Should never reach here
break;
}
}
// To check whether return variables have been assigned
if (varRecord != NULL) {
varRecord->assigned++;
}
break;
}
case 'M': { // MODULE_REUSE_STMT
// Check if the module exists
ASTNode* outputListNode = statement->leftMostChild;
ASTNode* moduleNode = outputListNode->next;
ASTNode* inputListNode = moduleNode->next;
// Setting scopeStart and lastLineNum
if (symbolTableNode->scopeStart == 0) {
symbolTableNode->scopeStart = moduleNode->leaf.tok->linenum;
}
lastLineNum = moduleNode->leaf.tok->linenum;
char* moduleName = moduleNode->leaf.tok->lexeme;
GlobalRecord* moduleRecord = moduleExists(moduleName, hash(moduleName));
if (moduleRecord == NULL) {
printf(RED BOLD "[Semantic Analyser] Line %d: Undefined module %s\n" RESET, moduleNode->leaf.tok->linenum, moduleName);
SEMANTIC_ERROR = true;
break;
}
if (moduleRecord->error) {
printf(RED BOLD "[Semantic Analyser] Line %d: Erroneous Module %s called\n" RESET, moduleNode->leaf.tok->linenum, moduleName);
SEMANTIC_ERROR = true;
break;
}
// Check if the module has been declared or defined
if (!moduleRecord->declared && !moduleRecord->defined) {
printf(RED BOLD "[Semantic Analyser] Line %d: Module %s has not been declared or defined yet.\n" RESET, moduleNode->leaf.tok->linenum, moduleName);
SEMANTIC_ERROR = true;