-
Notifications
You must be signed in to change notification settings - Fork 2
/
semantics.c
347 lines (320 loc) · 10.9 KB
/
semantics.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
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symboltable.h"
#include "hashtable.h"
#include "token.h"
#include "parserDef.h"
#include "mainsymboltable.h"
#include "idsymboltable.h"
#include "typeExtractor.h"
char* func_name;
void checkSemantics(stacknode* curr, mainsymboltable* globaltable)
{
if(curr == NULL)
return;
if(strcmp(curr->ntortinfo->str, "<var>") == 0)
{
if(curr->child == NULL || curr->child->idst == NULL)
return;
idsymboltablenode* helper = getidsymboltablenode(curr->child->tokinfo->lexeme, curr->child->idst);
if(strcmp(helper->type->ntortinfo->str, "ARRAY") == 0)
{
if(curr->child->sibling->child == NULL)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Array %s not indexed properly.\n", curr->child->tokinfo->linenumber, curr->child->tokinfo->lexeme);
return;
}
if(curr->child->sibling->child->idst == NULL)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Identifier %s used for indexing of %s is not present in this scope.\n", curr->child->sibling->child->tokinfo->linenumber, curr->child->sibling->child->tokinfo->lexeme, curr->child->tokinfo->lexeme);
return;
}
idsymboltablenode* helper2 = getidsymboltablenode(curr->child->sibling->child->tokinfo->lexeme, curr->child->sibling->child->idst);
if(strcmp(helper2->type->ntortinfo->str, "INTEGER") != 0)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Identifier %s used for indexing of %s is not of integer type.\n", curr->child->sibling->child->tokinfo->linenumber, curr->child->sibling->child->tokinfo->lexeme, curr->child->tokinfo->lexeme);
return;
}
}
else
{
if(curr->child->sibling->child != NULL)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Identifier %s can not be used as an array.\n", curr->child->tokinfo->linenumber, curr->child->tokinfo->lexeme);
}
}
}
if(strcmp(curr->ntortinfo->str, "<assignmentStmt>") == 0)
{
if(curr->child == NULL || curr->child->idst == NULL)
return;
idsymboltablenode* helper = getidsymboltablenode(curr->child->tokinfo->lexeme, curr->child->idst);
if(strcmp(helper->type->ntortinfo->str, "ARRAY") == 0)
{
if(strcmp(curr->child->sibling->ntortinfo->str, "<lvalueIDStmt>") == 0)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Array %s not indexed properly.\n", curr->child->tokinfo->linenumber, curr->child->tokinfo->lexeme);
return;
}
// must be lvalueARR
if(strcmp(curr->child->sibling->child->child->ntortinfo->str, "NUM") != 0)
{
if(curr->child->sibling->child->child->idst == NULL)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Identifier %s used for indexing of %s is not present in this scope.\n", curr->child->sibling->child->child->tokinfo->linenumber, curr->child->sibling->child->child->tokinfo->lexeme, curr->child->tokinfo->lexeme);
return;
}
// variable is valid now check for type
idsymboltablenode* helper2 = getidsymboltablenode(curr->child->sibling->child->child->tokinfo->lexeme, curr->child->sibling->child->child->idst);
if(strcmp(helper2->type->ntortinfo->str, "INTEGER") != 0)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Identifier %s used for indexing of %s is not of integer type.\n", curr->child->sibling->child->child->tokinfo->linenumber, curr->child->sibling->child->child->tokinfo->lexeme, curr->child->tokinfo->lexeme);
return;
}
}
}
else
{
if(strcmp(curr->child->sibling->ntortinfo->str, "<lvalueARRStmt>") == 0)
{
semanticCorrect = 1;
printf("ERROR_V at line %d : Identifier %s can not be used as an array.\n", curr->child->tokinfo->linenumber, curr->child->tokinfo->lexeme);
}
}
}
if(strcmp(curr->ntortinfo->str, "<output_plist>") == 0)
{
stacknode* temp = curr->child;
while(temp != NULL)
{
if(temp->idst == NULL)
return;
else
{
idsymboltablenode* helper = getidsymboltablenode(temp->tokinfo->lexeme, temp->idst);
if(helper->isAssigned == 0){
semanticCorrect = 1;
printf("ERROR_M at line %d : In module %s, the output parameter %s does not get assigned a value.\n", temp->tokinfo->linenumber, temp->idst->func_name, temp->tokinfo->lexeme);
}
}
temp = temp->sibling;
}
}
if(strcmp(curr->ntortinfo->str, "<moduleReuseStmt>") == 0)
{
func_name = curr->child->sibling->tokinfo->lexeme; // invoked function
mainsymboltablenode* pt = presentmainsymboltable(globaltable, func_name);
if(pt == NULL || !pt->isdefined){
semanticCorrect = 1;
printf("ERROR_M at line %d : Undefined reference to module %s.\n", curr->child->sibling->tokinfo->linenumber, curr->child->sibling->tokinfo->lexeme);
return;
}
if(curr->child->sibling->sibling->child->idst != NULL && strcmp(curr->child->sibling->sibling->child->idst->func_name, func_name) == 0)
{
semanticCorrect = 1;
printf("ERROR_M at line %d : In function %s, recursion not allowed.\n", curr->child->sibling->tokinfo->linenumber, curr->child->sibling->sibling->child->idst->func_name);
return;
}
int numActualReturn = 0, numOptional = 0;
if(pt->oplist != NULL)
{
stacknode* helper = pt->oplist->child; // pointing to ID
while(helper != NULL)
{
numActualReturn++;
helper = helper->sibling;
}
}
if(curr->child->child != NULL)
{
stacknode* helper = curr->child->child; // pointing to ID
while(helper != NULL)
{
numOptional++;
helper = helper->sibling;
}
}
if(numActualReturn != numOptional){
semanticCorrect = 1;
printf("ERROR_M at line %d : Output parameters numbers mismatch.\n", curr->child->child->tokinfo->linenumber);
}
else
{
// both return numbers are same check for types
if(pt->oplist != NULL)
{
stacknode* id1 = pt->oplist->child;
stacknode* id2 = curr->child->child;
while(id1 != NULL)
{
if(id1->idst == NULL || id2->idst == NULL)
return;
idsymboltablenode* temp1 = getidsymboltablenode(id1->tokinfo->lexeme, id1->idst); // id1->idst could be null, check exception handling
idsymboltablenode* temp2 = getidsymboltablenode(id2->tokinfo->lexeme, id2->idst);
int type1 = gettype(temp1->type);
int type2 = gettype(temp2->type);
if(type1 != type2)
{
semanticCorrect = 1;
printf("ERROR_M at line %d : Output parameters types mismatch.\n", curr->child->child->tokinfo->linenumber);
break;
}
id1 = id1->sibling;
id2 = id2->sibling;
}
}
}
int actualInput = 0, givenInput = 0;
if(pt->iplist != NULL)
{
stacknode* helper = pt->iplist->child; // pointing to ID
while(helper != NULL)
{
actualInput++;
helper = helper->sibling;
}
}
stacknode* helper = curr->child->sibling->sibling->child; // pointing to ID
while(helper != NULL)
{
givenInput++;
helper = helper->sibling;
}
if(actualInput != givenInput){
semanticCorrect = 1;
printf("ERROR_M at line %d : Input parameters numbers mismatch.\n", curr->child->sibling->sibling->child->tokinfo->linenumber);
}
else
{
// check for types
stacknode* id1 = pt->iplist->child;
stacknode* id2 = curr->child->sibling->sibling->child; // pointing to ID
while(id1 != NULL)
{
if(id1->idst == NULL || id2->idst == NULL)
return;
idsymboltablenode* temp1 = getidsymboltablenode(id1->tokinfo->lexeme, id1->idst); // id1->idst could be null, check exception handling
idsymboltablenode* temp2 = getidsymboltablenode(id2->tokinfo->lexeme, id2->idst);
int type1 = gettype(temp1->type);
int type2 = gettype(temp2->type);
if(type1 != type2)
{
semanticCorrect = 1;
printf("ERROR_M at line %d : Input parameters types mismatch.\n", curr->child->sibling->sibling->child->tokinfo->linenumber);
break;
}
id1 = id1->sibling;
id2 = id2->sibling;
}
}
}
// SEMANTICS FOR "SWITCH" STATEMENTS
if(strcmp(curr->ntortinfo->str, "<condionalStmt>") == 0)
{
if(curr->child->idst == NULL)
return;
idsymboltablenode* pt = getidsymboltablenode(curr->child->tokinfo->lexeme, curr->child->idst);
int type = gettype(pt->type);
if(type == integer)
{
stacknode* temp = curr->child->sibling->sibling->child;
while(temp != NULL)
{
if(strcmp(temp->child->ntortinfo->str, "NUM") != 0){
semanticCorrect = 1;
printf("ERROR_S at line %d : Integer case expected.\n", temp->child->tokinfo->linenumber);
}
temp = temp->sibling->sibling;
}
temp = curr->child->sibling->sibling->sibling;
if(temp->child == NULL){
semanticCorrect = 1;
printf("ERROR_S at line %d : Expected default case statement for integer type.\n", curr->child->sibling->sibling->sibling->sibling->tokinfo->linenumber);
}
}
else if(type == real)
{
semanticCorrect = 1;
printf("ERROR_S at line %d : Switch case does not work with real identifiers.\n", curr->child->tokinfo->linenumber);
}
else if(type == boolean)
{
stacknode* temp = curr->child->sibling->sibling->sibling; //for <default>
if(temp->child != NULL){
semanticCorrect = 1;
printf("ERROR_S at line %d : Switch case with boolean id should not have default.\n", temp->sibling->tokinfo->linenumber);
}
temp = curr->child->sibling->sibling->child;
int count = 0;
while(temp != NULL)
{
count++;
temp = temp->sibling->sibling;
}
if(count != 2)
{
semanticCorrect = 1;
printf("ERROR_S at line %d : Switch case with boolean id cannot have more than two cases.\n", curr->child->sibling->sibling->sibling->sibling->tokinfo->linenumber);
return;
}
temp = curr->child->sibling->sibling->child;
int tr=0, fl=0;
while(temp != NULL)
{
if(strcmp(temp->child->ntortinfo->str, "TRUE") == 0)
tr++;
else if(strcmp(temp->child->ntortinfo->str, "FALSE") == 0)
fl++;
else{
semanticCorrect = 1;
printf("ERROR_S at line %d : Switch case with boolean id cannot take NUM case value.\n", temp->child->tokinfo->linenumber);
}
temp = temp->sibling->sibling;
}
if(tr != 1 || fl != 1)
{
semanticCorrect = 1;
printf("ERROR_S at line %d : Switch case with boolean id should have both TRUE and FALSE cases.\n", curr->child->sibling->sibling->sibling->sibling->tokinfo->linenumber);
}
}
}
// SEMANTICS FOR "FOR" STATEMENTS
if(strcmp(curr->ntortinfo->str, "<iterativeStmt>") == 0)
{
if(strcmp(curr->child->ntortinfo->str, "FOR") == 0)
{
char* tempid = curr->child->sibling->tokinfo->lexeme;
stacknode* temp = curr->child->sibling->sibling->sibling->sibling->child;
while(temp != NULL)
{
if(strcmp(temp->ntortinfo->str, "<assignmentStmt>") == 0)
{
if(strcmp(temp->child->tokinfo->lexeme, tempid) == 0 && strcmp(temp->child->sibling->ntortinfo->str, "<lvalueIDStmt>") == 0){
semanticCorrect = 1;
printf("ERROR_V at line %d : Cannot redefine %s in for loop.\n", temp->child->tokinfo->linenumber, tempid);
}
}
temp = temp->sibling;
}
}
}
stacknode* temp = curr->child;
while(temp != NULL)
{
checkSemantics(temp, globaltable);
temp = temp->sibling;
}
}