-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsemanticanalyzer.c
280 lines (236 loc) · 7.69 KB
/
semanticanalyzer.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
/*
GROUP NUMBER: 11
NIKKI GUPTA 2016A7PS0057P
SAHIL RANADIVE 2016A7PS0097P
ADITI AGARWAL 2016A7PS0095P
ADITYA LADDHA 2016A7PS0038P
*/
#include "semanticanalyzer.h"
variable checkifdeclared(char* var_name, symbolTableElement stentry, variable globalVarList){
variable i = NULL;
i=searchInListStr(stentry->inputpars, var_name);
if(i==NULL){
i=searchInListStr(stentry->outputpars, var_name);
if(i==NULL){
i=searchInListStr(stentry->localvars, var_name);
if(i==NULL){
i= searchInListStr(globalVarList, var_name);
}
}
}
if(i==NULL){
return NULL;
}
else
return i;
}
void checkonefuncall(treenode funcallstmt, char* scope, symbolTable stable, variable globalVarList)
{
char* fun_name= funcallstmt->children->next->lexeme;//function being called
if(strcmp(fun_name, scope)==0)
{
printf("Line : %d ERROR : Function cannot be recursive \n", funcallstmt->line);
}
treenode outputpars=funcallstmt->children->children;
treenode inputpars= funcallstmt->children->next->next->children;
symbolTableElement selement =findInSTable(stable, fun_name);
symbolTableElement selementscope =findInSTable(stable, scope);
variable formalinput= selement->inputpars;
variable formaloutput= selement->outputpars;
while(outputpars!=NULL && formaloutput!=NULL) //checking output parameters list
{
char* var_name= outputpars->children->lexeme;
variable var2=checkifdeclared(var_name, selementscope, globalVarList);
if(var2==NULL)
{
// searchInList()
printf("Line : %d ERROR : Variable %s not declared in function %s\n",funcallstmt->line, var_name, scope);
outputpars=outputpars->next;
formaloutput=formaloutput->next;
continue;
}
if(var2->type!=formaloutput->type)
{
printf("Line : %d ERROR : Type mismatch actual output parameter: %s and formal output parameter: %s \n",funcallstmt->line, outputpars->children->lexeme, formaloutput->lexeme);
}
outputpars=outputpars->next;
formaloutput=formaloutput->next;
}
//check if number is different
if(outputpars!=NULL && formaloutput==NULL)
{
printf("Line : %d ERROR : Less number of formal output parameters for function %s\n",funcallstmt->line, fun_name);
}
else if(outputpars==NULL && formaloutput!=NULL)
{
printf("Line : %d ERROR : Insufficient actual output paramters for function %s\n",funcallstmt->line, fun_name);
}
while(inputpars!=NULL && formalinput!=NULL) //checking input parameters list
{
char* var_name= inputpars->children->lexeme;
variable var2=checkifdeclared(var_name, selementscope, globalVarList);
if(var2==NULL)
{
// searchInList()
printf("Line : %d ERROR : Variable %s not declared in function %s\n",funcallstmt->line, var_name, scope);
inputpars=inputpars->next;
formalinput=formalinput->next;
continue;
}
if(var2->type!=formalinput->type)
{
printf("Line : %d ERROR : Type mismatch actual input parameter: %s and formal output parameter: %s \n",funcallstmt->line, inputpars->children->lexeme, formalinput->lexeme);
}
inputpars=inputpars->next;
formalinput=formalinput->next;
}
//check if number is different
if(inputpars!=NULL && formalinput==NULL)
{
printf("Line : %d ERROR : Less number of formal input parameters for function %s\n",funcallstmt->line, fun_name);
}
else if(inputpars==NULL && formalinput!=NULL)
{
printf("Line :%d ERROR : Insufficient actual input paramters for function %s\n",funcallstmt->line, fun_name);
}
}
int searchInFuncList(char** funlist, char* name, int num)
{
for(int i=0; i<num; i++)
{
if(strcmp(funlist[i], name)==0)
{
return 1;
}
}
return 0;
}
void checkIOstmt(treenode iostmt, char* scope, symbolTable stable,variable globalVarList,recordVar recList)
{
if(iostmt->id==34)//read
checkSingleorRec(iostmt->children, scope, stable, globalVarList,recList);
else {
if(iostmt->children->id!=5 && iostmt->children->id!=6){//If not num or rnum i.e is id or id.fieldid
checkSingleorRec(iostmt, scope, stable, globalVarList,recList);
}
}
}
void checkSingleorRec(treenode node, char* scope, symbolTable stable,variable globalVarList,recordVar recList){
symbolTableElement stentry= findInSTable(stable, scope);
if(node->children->next==NULL){//only id
// printf("in if\n");
if(stentry==NULL)
{
printf("1 Wrong Scope passed to function %s\n",scope);
return;
}
variable i;
i=searchInListStr(stentry->inputpars, node->children->lexeme);
if(i==NULL){
i=searchInListStr(stentry->outputpars, node->children->lexeme);
if(i==NULL){
i=searchInListStr(stentry->localvars, node->children->lexeme);
if(i==NULL){
i= searchInListStr(globalVarList, node->children->lexeme);
}
}
}
// printf("lexeme %s\n",i->lexeme);
if(i==NULL)
{
printf("Line : %d ERROR : Variable %s not previously declared\n",node->line, node->children->lexeme );
}
}
else{// id.fieldid
char* fieldid= node->children->next->next->lexeme;
variable i;
i=searchInListStr(stentry->inputpars, node->children->lexeme);
if(i==NULL){
i=searchInListStr(stentry->outputpars, node->children->lexeme);
if(i==NULL){
i=searchInListStr(stentry->localvars, node->children->lexeme);
if(i==NULL){
i= searchInListStr(globalVarList, node->children->lexeme);
return;
}
}
}
if(i==NULL){
printf("Line : %d ERROR : Record Instance %s not previously declared\n",node->line, node->children->lexeme );
return;
}
recordVar record= searchInRecList(recList, i->recordName);
if(record==NULL)
{printf("Line : %d ERROR : Record definition of %s not previously declared\n", node->line, node->children->lexeme);
return;}
recordField recf= record->head;
// printf("Inside condition if\n");
while(recf!=NULL)
{
// printf("Inside recf while\n");
if(strcmp(fieldid, recf->lexeme)==0)
return;
recf=recf->next;
}
printf("Line : %d ERROR : Record %s does not contain field %s\n", node->line, i->recordName, fieldid);
return;
}
}
void checkfuncallstmts(treenode root, symbolTable stable, variable globalVarList,recordVar recList)
{
treenode functions = root->children;
char* scope=(char*) malloc(sizeof(char)*50);
char** funlist= (char**)malloc(sizeof(char *)*1000);
int num_func=0;
if(functions->children==NULL)
functions=functions->next;
while(functions!=NULL)
{
treenode stmts = NULL;
if(functions->next==NULL) //if main function
{
stmts = functions->children->children->next->next;
}
else{ // other functions
stmts = functions->children->next->next->next->children->next->next;
}
if(functions->id==101)
strcpy(scope,"main");
else
strcpy(scope,functions->children->lexeme);
// printf("Inside function %s\n", scope);
if(searchInFuncList(funlist, scope, num_func)!=0)//scope already exists
{
treenode stmt= stmts->children;
printf("Line : %d ERROR : Function overloading not allowed for function: %s\n", stmt->line, scope);
}
else{
funlist[num_func]=(char*)malloc(sizeof(char)*50);
strcpy(funlist[num_func], scope);
// printf("adding to funlist%s\n", funlist[num_func]);
num_func++;
treenode stmt= stmts->children;
while(stmt!=NULL)
{
// printf("Inside while\n");
if(stmt->children!=NULL &&stmt->children->id==125)//funcallstmt
{
// printf("Inside if dude\n");
if(searchInFuncList(funlist, stmt->children->children->next->lexeme, num_func)==0)
{
printf("Line : %d ERROR : Function called: %s not defined previously\n", stmt->line, stmt->children->children->next->lexeme);
}
else{
checkonefuncall(stmt->children, scope, stable,globalVarList);
}
}
else if(stmt->children!=NULL &&(stmt->children->id==34 || stmt->children->id==35) )
{
checkIOstmt(stmt->children, scope, stable,globalVarList,recList);
}
stmt=stmt->next;
}
}
functions= functions->next;
}
}