-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemantic.c
1091 lines (1041 loc) · 50.2 KB
/
semantic.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 no. 50
* Aniruddha Mahajan -------- 2017A7PS0145P
* Ravindra Singh Shekhawat - 2017A7PS0146P
* Shreyas Srikrishna ------- 2017A7PS0162P
* Chetan Sharma ------------ 2017A7PS0182P
*/
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "symboltable.h"
#include "typeExtractor.h"
#include "semantic.h"
#include "functionCheck.h"
#include "offsetComputer.h"
#include <string.h>
#define sc startChild
#define rs rightSibling
ListOfErrors* semanticErrors;
bool whileLoopAssignsValue;
ListOfErrors* getSemanticErrorObject()
{
return semanticErrors;
}
//then call the typechecking functions from here
//for the same, we will pass error object to every function
void initializeErrorObject(){
semanticErrors = (ListOfErrors*)malloc(sizeof(ListOfErrors));
semanticErrors->head = NULL;
semanticErrors->numberOfErr = 0;
}
Error* createErrorObject()
{
Error *error = (Error *)malloc(sizeof(Error));
error->next = NULL;
return error;
}
/*Control function for semantic analysis.
This function calls for construction of symbol table, type checking of expressions
type checking for function calls etc*/
void semanticAnalyzer(){
ASTNode *astRoot = getAST();
initializeErrorObject();
processAST(astRoot,NULL,semanticErrors);
//construct symboltable
//processAST(astRoot,NULL,semanticErrors);
/*NOTE:In above function, following semantic checks have been performed arleady*/
/**
* Output parameter overloading inside module definition
* Duplicate module Declarations
* Duplicate variable declarations inside a block (doubt--is a variable visible inside inner scopes? can't we overshadow it? if we can, then change code in processAST where deep lookup is performed for checking duplicate declaration)
* */
printf("SYMBOL TABLE CREATED\n");
SymbolTable* rootSymbolTable = getsymbolTable();
//now perform semantic analysis
analyzeAST(astRoot, rootSymbolTable, semanticErrors);
initializeDeclaredList(astRoot, rootSymbolTable);
populateModuleSequenceMap(astRoot, rootSymbolTable);
checkModules(astRoot, semanticErrors);
computeOffsets(astRoot, rootSymbolTable);
SymbolTable* finalTable = getsymbolTable();
return;
}
int countLeaves(ASTNode* node)
{
if(node == NULL)
return 0;
if(node->sc == NULL)
return 1;
else{
int count = 0;
ASTNode* temp = node->sc;
while(temp != NULL)
{
count += countLeaves(temp);
temp = temp->rightSibling;
}
return count;
}
return 0;
}
//node opnode
void setWhileLoopConditionFlag(ASTNode* node, bool set, SymbolTable* currtable){
switch(node->type)
{
case opNode:{
setWhileLoopConditionFlag(node->sc,set,currtable);
setWhileLoopConditionFlag(node->sc->rs,set,currtable);
return;
}
case idNode:{
SymbolTableEntry* entry = lookupString(node->node.idnode.lexeme,currtable,idEntry,true,node->node.idnode.line_no);
if(entry == NULL)
return;
entry->symbol.idEntry.isWhileLoopCondition = set;
return;
}
case arrayIdNode:{
SymbolTableEntry* sym = lookupString(node->sc->node.idnode.lexeme, currtable, idEntry, true, node->sc->node.idnode.line_no);
if(sym == NULL)
return;
sym->symbol.idEntry.isWhileLoopCondition = set;
return;
}
case boolNode: {
return;
}
case nullNode: {
return;
}
}
}
/*This recursive function traverses AST and performs various semantic checks*/
void analyzeAST(ASTNode* node, SymbolTable* table, ListOfErrors* semanticErrors){
switch(node->type){
case programNode:{
ASTNode *t1 = node->sc->rs;
while(t1 !=NULL)
{
analyzeAST(t1, table, semanticErrors);
t1 = t1->next;
}
analyzeAST(node->sc->rs->rs, table, semanticErrors);
t1 = node->sc->rs->rs->rs;
while(t1 !=NULL)
{
analyzeAST(t1, table, semanticErrors);
t1 = t1->next;
}
break;
}
case moduleNode:{
//check if driverModule
if(node->sc->type == nullNode){
SymbolTableEntry* curr = lookupString("driverModule", table, driverEntry, false,-1);
if(curr==NULL) //not possible
printf("\nSymbotable populated wrongly\n");
//now process its statements
//curr->table gives us the table for this scope
ASTNode* traverse = node->sc->rs->rs->rs;
while(traverse != NULL){
analyzeAST(traverse, curr->table, semanticErrors);
traverse = traverse->next;
}
}
//if it's not a driver module
else{
//first obtain the symboltable corresponding to this module
SymbolTableEntry* curr = lookupString(node->sc->node.idnode.lexeme, table, functionEntry, false,-1);
if(curr == NULL){
printf("\nRoot Symboltable not populated with this function");
//not possible. check code
}
//now process its statements
//curr->table gives us the table for this scope
if(node->node.moduleNode.isOverloaded == false){
ASTNode* traverse = node->sc->rs->rs->rs;
while(traverse != NULL){
analyzeAST(traverse, curr->table, semanticErrors);
traverse = traverse->next;
}
//now staetmetnts have been processed. check if all the output parameters have been assigned a value or not
ASTNode* travOutputParam = node->sc->rs->rs;
if(travOutputParam->type != nullNode){
while(travOutputParam != NULL){
if(travOutputParam->node.outputParamNode.isAssigned == false){
//semantic error :
Error *err = createErrorObject(); err->lineNo = travOutputParam->sc->node.idnode.line_no; strcpy(err->error,"No value was assigned for output param: ");
strcat(err->error, travOutputParam->sc->node.idnode.lexeme);
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
travOutputParam = travOutputParam->next;
}
}
}
}
break;
}
case declareNode:{
break;
}
case opNode:{
PrimitiveType t = extractTypeOfExpression(node, table, semanticErrors);
if(t == -1) break;
SymbolTable* currFuncTable = table;
while(currFuncTable->tableType != functionBlock)
currFuncTable = currFuncTable->parent;
ASTNode* currModNode;
if(strcmp(currFuncTable->scope.scope, "driverModule") == 0){
SymbolTableEntry* driverFunction = lookupString("driverModule", getsymbolTable(), driverEntry, false, -1);
currModNode = driverFunction->symbol.driverEntry.driverNode;
}
else{
SymbolTableEntry* currFunction = lookupString(currFuncTable->scope.scope, getsymbolTable(), functionEntry, false, -1);
currModNode = currFunction->symbol.functionEntry.inputListHead->parent;
}
int leafCount = countLeaves(node);
if(t==integer){
if(leafCount > currModNode->node.moduleNode.maxTempInt)
currModNode->node.moduleNode.maxTempInt = leafCount;
}
else if(t==real){
if(leafCount > currModNode->node.moduleNode.maxTempReal)
currModNode->node.moduleNode.maxTempReal = leafCount;
}
else{ //boolean. in this case we might also need integer temporaries
if(leafCount-1 > currModNode->node.moduleNode.maxTempInt)
currModNode->node.moduleNode.maxTempInt = leafCount-1;
if(leafCount > currModNode->node.moduleNode.maxTempBool)
currModNode->node.moduleNode.maxTempBool = leafCount;
}
break;
}
case assignmentNode:{
//first check if lhs variable has been declared
//0. check if this assignment statement is inside a whileloop. if yes,then check if one of the variables in its condition is assigned a value
SymbolTable* tempTable = table;
while(tempTable != NULL){
if(tempTable->tableType == whileLoopBlock)
break;
tempTable = tempTable->parent;
}
if(tempTable != NULL){ //we are inside while loop
if(node->sc->type == idNode){
SymbolTableEntry* lhsSym = lookupString(node->sc->node.idnode.lexeme,table,idEntry,true,node->sc->node.idnode.line_no);
if(lhsSym != NULL){
if(lhsSym->symbol.idEntry.isWhileLoopCondition)
whileLoopAssignsValue = true;
}
}
else if(node->sc->type == arrayIdNode){ //arrayIdNode
SymbolTableEntry* lhsSym = lookupString(node->sc->sc->node.idnode.lexeme,table,idEntry,true,node->sc->sc->node.idnode.line_no);
if(lhsSym != NULL){
if(lhsSym->symbol.idEntry.isWhileLoopCondition)
whileLoopAssignsValue = true;
}
}
}
//1. check if 'for-loop' iterating variable has been assigned
ASTNode *temp = node->parent;
while(temp->parent != NULL){
if(temp->type == forLoopNode && strcmp(temp->sc->node.idnode.lexeme,node->sc->node.idnode.lexeme)==0){
//semantic error
Error *err = createErrorObject(); err->lineNo = node->sc->node.idnode.line_no; strcpy(err->error,"Assigning a value to for loop iterating variable is not allowed:");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
break;
}
else
temp = temp->parent;
}
//2. check if assignment is done for output parameter. if yes, set outputParamNode->isAssigned = true;
ASTNode* trav = node->parent;
while(trav->type != moduleNode)
trav = trav->parent;
if(trav->sc->type != nullNode){ //only if we are not iside drvire module
ASTNode* travOutputParam = trav->sc->rs->rs;
while(travOutputParam != NULL){
if(travOutputParam->type == nullNode)
break;
if(strcmp(travOutputParam->sc->node.idnode.lexeme, node->sc->node.idnode.lexeme) == 0){
travOutputParam->node.outputParamNode.isAssigned = true;
}
travOutputParam = travOutputParam->next;
}
}
//3. process the rhs of the assignment statements.
ASTNode* rhsProcess = node->sc->rs;
//first exempt the special case of A:=B where both A and B are arrays
if(node->sc->type == idNode && node->sc->rs->type == idNode){
SymbolTableEntry *sym1 = lookupString(node->sc->node.idnode.lexeme,table,idEntry,true,node->sc->node.idnode.line_no);
SymbolTableEntry *sym2 = lookupString(node->sc->rs->node.idnode.lexeme,table,idEntry,true,node->sc->rs->node.idnode.line_no);
if(sym1 == NULL){
Error *err_lhs = createErrorObject();
err_lhs->lineNo = node->sc->node.idnode.line_no;
sprintf(err_lhs->error,"Variable %s used in this expression has not been declared.",node->sc->node.idnode.lexeme);
//printf("LINE %d: %s\n",err_lhs->lineNo,err_lhs->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL){
semanticErrors->head = err_lhs;
semanticErrors->numberOfErr += 1;
}
else{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err_lhs;
semanticErrors->numberOfErr += 1;
}
break;
}
if(sym2 == NULL){
Error *err_lhs = createErrorObject();
err_lhs->lineNo = node->sc->rs->node.idnode.line_no;
sprintf(err_lhs->error,"Variable %s used in this expression has not been declared.",node->sc->rs->node.idnode.lexeme);
//printf("LINE %d: %s\n",err_lhs->lineNo,err_lhs->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL){
semanticErrors->head = err_lhs;
semanticErrors->numberOfErr += 1;
}
else{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err_lhs;
semanticErrors->numberOfErr += 1;
}
break;
}
if(sym1->symbol.idEntry.type.tag == array && sym2->symbol.idEntry.type.tag == array){
ArrayType* lhsarr = &sym1->symbol.idEntry.type.type.arrayType;
ArrayType* rhsarr = &sym2->symbol.idEntry.type.type.arrayType;
if(lhsarr->low != -1 && lhsarr->high != -1 && rhsarr->low != -1 && rhsarr->high != -1){ //static
if(lhsarr->low == rhsarr->low && lhsarr->high == rhsarr->high)
break;
}
else{ //dynamic
if(sym1->symbol.idEntry.type.type.arrayType.t == sym2->symbol.idEntry.type.type.arrayType.t){
}
else
{
Error *err = createErrorObject();
err->lineNo = node->sc->node.idnode.line_no;
strcpy(err->error,"Type MisMatch in Assignment Statement.");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
break;
}
}
}
while(rhsProcess != NULL)
{
analyzeAST(rhsProcess,table,semanticErrors);
rhsProcess = rhsProcess->next;
}
PrimitiveType t_lhs = extractTypeOfExpression(node->sc,table,semanticErrors);
PrimitiveType t_rhs = extractTypeOfExpression(node->sc->rs,table,semanticErrors);
if(t_rhs == -1 || t_lhs == -1)
break;
//type mismatch : int:=boolean
if(t_lhs != t_rhs)
{
Error *err_lhs_rhs = createErrorObject();
err_lhs_rhs->lineNo = node->sc->node.idnode.line_no;
strcpy(err_lhs_rhs->error,"Type MisMatch in Assignment Statement.");
//printf("LINE %d: %s\n",err_lhs_rhs->lineNo,err_lhs_rhs->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err_lhs_rhs;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err_lhs_rhs;
semanticErrors->numberOfErr += 1;
}
}
break;
}
case functionCallNode:{
SymbolTableEntry* func = lookupString(node->sc->rs->node.idnode.lexeme, table, functionEntry, true,-1);
//1.void functions handling
if(func->symbol.functionEntry.outputListHead->type == nullNode){ //actually a void function
if(node->sc->type != nullNode){
//semantic error: void function should not return anything
Error *err = createErrorObject(); err->lineNo = node->sc->rs->node.idnode.line_no; strcpy(err->error,"Void function must not return anything");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
}
else{ //check output parameters
FunctionType* f = &func->symbol.functionEntry.inOutType; ///Typeof* in o/p param types linkedlist
int i = 0;
ASTNode* travCallOut = node->sc; //idNode in output paramters linkedlist
while(travCallOut != NULL && i < f->noOfOutputs){
if(travCallOut->type == nullNode){
//semantic error : function must return something
Error *err = createErrorObject(); err->lineNo = node->sc->rs->node.idnode.line_no; strcpy(err->error,"Void function must not return anything");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
SymbolTableEntry* travCallOutput = lookupString(travCallOut->node.idnode.lexeme, table, idEntry, true,travCallOut->node.idnode.line_no);
if(travCallOutput == NULL)
{
Error *err = createErrorObject(); err->lineNo = travCallOut->node.idnode.line_no; strcpy(err->error,"Output Parameter not declared");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
else {
if(travCallOutput->symbol.idEntry.type.tag == array){
//semantic error : function cannot return array
Error *err = createErrorObject(); err->lineNo = travCallOut->node.idnode.line_no; strcpy(err->error,"Function cannot return array");
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
travCallOut = travCallOut->next;
i++;
continue;
}
if(travCallOutput->symbol.idEntry.type.type.primitiveType != f->outputList[i].type.primitiveType){
//semantic error : output type mismatch
Error *err = createErrorObject(); err->lineNo = travCallOut->node.idnode.line_no; strcpy(err->error,"Output types mismatch");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
travCallOut = travCallOut->next;
i++;
continue;
}
}
i++;
travCallOut = travCallOut->next;
}
if(!(travCallOut == NULL && i == f->noOfOutputs)){
//semantic error : output parameter count mismatch
Error *err = createErrorObject(); err->lineNo = node->sc->rs->node.idnode.line_no; strcpy(err->error,"Output parameter count mismatch");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
}
//2. types and no of input parameters of function must be same as declaration
FunctionType* inp = &func->symbol.functionEntry.inOutType;
struct Typeof* travDefIn = inp->inputList;
int i=0;
ASTNode *travCallIn = node->sc->rs->rs;
int counter = 0;
ASTNode* checkNoOfInputs = node->sc->rs->rs;
while(checkNoOfInputs != NULL)
{
counter++;
checkNoOfInputs = checkNoOfInputs->next;
}
if(counter != inp->noOfInputs)
{
Error *err = createErrorObject(); err->lineNo = node->sc->rs->node.idnode.line_no; strcpy(err->error,"Input parameter count mismatch");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
//else{
while(travCallIn != NULL && i < inp->noOfInputs)
{
SymbolTableEntry* sym = lookupString(travCallIn->node.idnode.lexeme, table, idEntry, true,travCallIn->node.idnode.line_no);
if(sym == NULL)
{
//input parameter must be declared
Error *err = createErrorObject(); err->lineNo = travCallIn->node.idnode.line_no; strcpy(err->error,"Input parameter must be declared before use");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
travCallIn = travCallIn->next;
i++;
continue;
}
else
{
if(sym->symbol.idEntry.type.type.primitiveType != travDefIn[i].type.primitiveType && sym->symbol.idEntry.type.tag == primitive && travDefIn[i].tag == primitive)
{
// semantic error
Error *err = createErrorObject(); err->lineNo = travCallIn->node.idnode.line_no; strcpy(err->error,"Input parameter type mismatch");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
travCallIn = travCallIn->next;
i++;
continue;
}
else if(sym->symbol.idEntry.type.tag == array && travDefIn[i].tag == array)
{
if((sym->symbol.idEntry.type.type.arrayType.t != travDefIn[i].type.arrayType.t)){
Error *err = createErrorObject(); err->lineNo = travCallIn->node.idnode.line_no; strcpy(err->error,"Array in formal parameter is of different type than actual parameter");
Error *temporary = semanticErrors->head;
//printf("LINE %d: %s\n",err->lineNo,err->error);
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
else{ //arrays' primitive types match, so check indexes only if both are static
if(sym->symbol.idEntry.type.type.arrayType.high>=0 && sym->symbol.idEntry.type.type.arrayType.low>=0 && travDefIn[i].type.arrayType.high>=0 && travDefIn[i].type.arrayType.low>=0){
if((sym->symbol.idEntry.type.type.arrayType.high != travDefIn[i].type.arrayType.high )||(sym->symbol.idEntry.type.type.arrayType.low != travDefIn[i].type.arrayType.low)){
Error *err = createErrorObject(); err->lineNo = travCallIn->node.idnode.line_no; strcpy(err->error,"Array in formal parameter is of different type than actual parameter(index ranges don't match)");
Error *temporary = semanticErrors->head;
//printf("LINE %d: %s\n",err->lineNo,err->error);
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
}
}
travCallIn = travCallIn->next;
i++;
continue;
}
else if(sym->symbol.idEntry.type.tag != travDefIn[i].tag){
//semantic error:input parameter type mismatch
Error *err = createErrorObject(); err->lineNo = travCallIn->node.idnode.line_no; strcpy(err->error,"Input parameter type mismatch ");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
travCallIn = travCallIn->next;
i++;
continue;
}
i++;
travCallIn = travCallIn->next;
}
}
//}
break;
}
case conditionalNode:{ //switch-case. assumes swtiching variable is declared and is not of type array
//1. check switching variable and apply semantic checks based on its type
SymbolTableEntry* sym = lookupString(node->sc->node.idnode.lexeme, table, idEntry, true,node->sc->node.idnode.line_no);
switch(sym->symbol.idEntry.type.type.primitiveType){
case integer:{
if(node->sc->rs->rs->type == nullNode)
{
// semantic error default node missing
Error *err = createErrorObject(); err->lineNo = node->node.conditionalNode.block.end; strcpy(err->error,"Default Statement missing line ");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
ASTNode *temp = node->sc->rs;
while(temp != NULL)
{
if(temp->sc->rs->type == numNode && strcmp(temp->sc->rs->node.numNode.token,"NUM")==0)
temp = temp->next;
else
{
//semantic error case node is not an integer
Error *err = createErrorObject(); err->lineNo = temp->node.caseNode.line; strcpy(err->error,"Case Node is not an integer ");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
temp = temp->next;
//break;
}
}
break;
}
case real:{
Error *err = createErrorObject(); err->lineNo = node->sc->node.idnode.line_no; strcpy(err->error,"Switching variable cannot be of real type :");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
break;
}
case boolean:{
if(node->sc->rs->rs->type != nullNode){
Error *err = createErrorObject(); err->lineNo = node->sc->rs->rs->node.caseNode.line; strcpy(err->error,"Switch case on boolean variable cannot have default case");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err; semanticErrors->numberOfErr += 1;
}
if(!(node->sc->rs->sc->rs->type == boolNode && node->sc->rs->next->sc->rs->type == boolNode && node->sc->rs->next->next == NULL)){
Error *err = createErrorObject(); err->lineNo = node->sc->rs->node.caseNode.line; strcpy(err->error,"Switch case on boolean variable must have exactly 2 cases, true and false");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
else
{
if(strcmp(node->sc->rs->sc->rs->node.boolNode.token,node->sc->rs->next->sc->rs->node.boolNode.token)==0)
{
Error *err = createErrorObject(); err->lineNo = node->sc->rs->node.caseNode.line; strcpy(err->error,"Duplicate case conditions");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
}
ASTNode *temp = node->sc->rs;
while(temp != NULL)
{
if(temp->sc->rs->type == boolNode && ((strcmp(temp->sc->rs->node.boolNode.token,"FALSE")==0)||(strcmp(temp->sc->rs->node.boolNode.token,"TRUE")==0)))
temp = temp->next;
else
{
//semantic error case node is not an integer
Error *err = createErrorObject(); err->lineNo = temp->node.caseNode.line; strcpy(err->error,"Case Node is not an boolean ");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
temp = temp->next;
//break;
}
}
}
}
//2. process its cases and default case
//first need to obtain the switch case scope's table
SymbolTableEntry* switchEntry = lookupBlock(&node->node.conditionalNode.block,table,switchCaseEntry,false);
ASTNode* case_node = node->sc->rs;
while(case_node != NULL){
analyzeAST(case_node, switchEntry->table, semanticErrors);
case_node = case_node->next;
}
if(node->sc->rs->rs->type != nullNode)
analyzeAST(node->sc->rs->rs, switchEntry->table, semanticErrors);
break;
}
case caseNode:{
//1. now process the case's statements
ASTNode* caseStmt = node->sc;
if(caseStmt->type != nullNode){
while(caseStmt != NULL){
analyzeAST(caseStmt, table, semanticErrors);
caseStmt = caseStmt->next;
}
}
break;
}
case forLoopNode : {
SymbolTableEntry *floop = lookupBlock(&node->node.forLoopNode.block,table,forLoopEntry,false);
ASTNode* forStmt = node->sc->rs->rs;
ASTNode* ranNode = node->sc->rs;
if(strcmp(ranNode->sc->node.numNode.token,"RNUM")==0 || strcmp(ranNode->sc->rs->node.numNode.token,"RNUM")==0){
Error* err = createErrorObject();
err->lineNo = ranNode->sc->node.numNode.line_no;
strcpy(err->error,"Range of For Loop variables should be integers.");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
break;
}
int high = (int)ranNode->sc->rs->node.numNode.value;
int low = (int)ranNode->sc->node.numNode.value;
if(low > high)
{
Error* err = createErrorObject();
err->lineNo = ranNode->sc->node.numNode.line_no;
strcpy(err->error,"Range of For Loop variable is not defined correctly.");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
break;
}
if(forStmt->type != nullNode){
while(forStmt != NULL)
{
analyzeAST(forStmt,floop->table,semanticErrors);
forStmt = forStmt->next;
}
}
break;
}
case whileLoopNode: {
//1. check the type of the expression in while loop condition
PrimitiveType t = extractTypeOfExpression(node->sc,table,semanticErrors);
if(t != boolean)
{
if(t==-1)
break;
//semantic error
Error* err = createErrorObject();
err->lineNo = node->node.whileLoopNode.block.start;
strcpy(err->error,"While Loop Condition doesn't evaluates to a boolean");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)
{
semanticErrors->head = err;
semanticErrors->numberOfErr += 1;
}
else
{
while(temporary->next != NULL)
temporary = temporary->next;
temporary->next = err;
semanticErrors->numberOfErr += 1;
}
}
else
{
setWhileLoopConditionFlag(node->sc, true, table); //mark the variables that appear in while loop condition
whileLoopAssignsValue = false;
SymbolTableEntry* wloop = lookupBlock(&node->node.whileLoopNode.block,table,whileLoopEntry,false);
// Now process the statements
ASTNode* whileStmt = node->sc->rs;
if(whileStmt->type != nullNode){
while(whileStmt != NULL)
{
analyzeAST(whileStmt,wloop->table,semanticErrors);
whileStmt = whileStmt->next;
}
}
setWhileLoopConditionFlag(node->sc, false,table); //unmark the variables that appear in while loop condition
//check if while loop assigns value to one of the variables in condition
if(!whileLoopAssignsValue){
//semantic error
Error* err = createErrorObject();
err->lineNo = node->node.whileLoopNode.block.start;
strcpy(err->error,"While Loop does not assign a value to one of the variables in its condition");
//printf("LINE %d: %s\n",err->lineNo,err->error);
Error *temporary = semanticErrors->head;
if(temporary == NULL)