forked from mayankgupta022/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeExtractor.c
298 lines (288 loc) · 8.9 KB
/
typeExtractor.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
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=
BATCH 26
-=-=-=-=-=-=-=-=-=-=-=-=-=
AAYUSH AHUJA 2010A7PS023P
MAYANK GUPTA 2010A7PS022P
-=-=-=-=-=-=-=-=-=-=-=-=-=
typeExtractor.c
-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include"lexerDef.h"
#include"lexer.h"
#include"parserDef.h"
#include"parser.h"
#include"symbolTable.h"
#include"semantic.h"
#include"typeExtractor.h"
extern bool any_error;
symbol getVarType(variable GT[], funTable FT[],char *varname,char *funname)//return type of variable varname
{
// printf("finding type of %s in %s\n",varname,funname );
int hashkey=100;
int hval=hash(varname, hashkey);
if(GT[hval].filled && !strcmp(GT[hval].name,varname))
return GT[hval].type;
else
{
int fval=hash(funname, hashkey);
if(FT[fval].filled && !strcmp(FT[fval].fname,funname))
{
if(FT[fval].table[hval].filled && !strcmp(FT[fval].table[hval].name,varname))
return FT[fval].table[hval].type;
}
}
return TK_ERROR;
}
symbol getRecType(recTable RT[],variable GT[], funTable FT[],char *recname, char *varname,char *funname)//return type of variable varname part of record recname
{
int hashkey=100;
int hval=hash(funname, hashkey);
int rval=hash(recname, hashkey);
int rindex=FT[hval].table[rval].recindex;
int vval=hash(varname, hashkey);
if(GT[rval].filled && !strcmp(GT[rval].name,recname))
{
rindex=GT[rval].recindex;
return RT[rindex].table[vval].type;
}
if(RT[rindex].table[vval].filled && !strcmp(RT[rindex].table[vval].name,varname))
return RT[rindex].table[vval].type;
return TK_ERROR;
}
symbol typeCheck(parseTree A,variable GT[], funTable FT[],recTable RT[],char *funname)//checks if types are compatible
{
symbol s1,s2,s3,s4,s5;
int i;
if(A->t->s == assignmentstmt)
{
if(A->next[0]->t->s==singleorrecid || A->next[0]->t->s==all)
{
//printf("singleorrecid %s %s\n", A->next[0]->next[0]->t->lexeme,A->next[0]->next[1]->t->lexeme);
s1=getRecType(RT,GT,FT,A->next[0]->next[0]->t->lexeme,A->next[0]->next[1]->t->lexeme,funname);
}
else if(A->next[0]->t->s==TK_ID)
s1=getVarType(GT,FT,A->next[0]->t->lexeme,funname);
s2=typeCheck(A->next[2],GT,FT,RT,funname);
//printf("%s %s %s %s\n",toStr(A->next[0]->t->s),toStr(s1),toStr(A->next[2]->t->s),toStr(s2));
if(s1==TK_ERROR || s2==TK_ERROR)
{
if(s1==TK_ERROR)
{
if((A->next[0]->t->s==singleorrecid || A->next[0]->t->s==all))
{
printf("ERROR: Variable %s of Record %s is not declared in this scope at line %d\n",A->next[0]->next[1]->t->lexeme,A->next[0]->next[0]->t->lexeme,A->next[0]->lineno);
any_error=1;
}
else if(A->next[0]->t->s==TK_ID && s1==TK_ERROR)
{
printf("ERROR: Variable %s is not declared in this scope at line %d\n",A->next[0]->t->lexeme,A->next[0]->lineno);
any_error=1;
}
return TK_ERROR;
}
}
else if(s1!=s2)
{
printf("ERROR: Mismatched Datatype. Datatype %s does not match %s at line %d\n",toStr(s1),toStr(s2),A->lineno);
any_error=1;
return TK_ERROR2;//mismatched datatype
}
else
{
return s1;
}
}
else if(A->t->s == iterativestmt)
{
return typeCheck(A->next[1],GT,FT,RT,funname);
}
else if(A->t->s == conditionalstmt)
{
return typeCheck(A->next[1],GT,FT,RT,funname);
}
else if(A->t->s == iostmt)
{
if(A->next[1]->t->s==singleorrecid || A->next[1]->t->s==all)
s1= getRecType(RT,GT,FT,A->next[1]->next[0]->t->lexeme,A->next[1]->next[1]->t->lexeme,funname);
else if(A->next[1]->t->s==TK_ID)
s1= getVarType(GT,FT,A->next[1]->t->lexeme,funname);
else
{
s2= typeCheck(A->next[1],GT,FT,RT,funname);
return s2;
}
if(s1==TK_ERROR)
{
if((A->next[1]->t->s==singleorrecid || A->next[1]->t->s==all))
{
printf("ERROR: Variable %s of Record %s is not declared in this scope at line %d\n",A->next[1]->next[1]->t->lexeme,A->next[1]->next[0]->t->lexeme,A->next[1]->lineno);
any_error=1;
}
else if(A->next[1]->t->s==TK_ID)
{
printf("ERROR: Variable %s is not declared in this scope at line %d\n",A->next[1]->t->lexeme,A->next[1]->lineno);
any_error=1;
}
}
return s1;
}
// else if(A->t->s == funcallstmt)
// {
// for(i=0;A->next[i]!=NULL;i++)
// {
// if(A->next[i]==outputparameters || A->next[i]==inputparameters)
// {
// temp=A->next[i]->next[0];
// offset=0;
// while(temp!=NULL)
// {
// s1=getVarType(GT,FT,temp->next[0]->t->lexeme,funname);
// //check if this type is correct as per function definition
// if(s1==TK_ERROR)
// if(checkRec(RT,temp->next[0]->t->lexeme))//check if record exists
// {
// //if this type is correct as per function definition
// }
// temp=temp->next[1];
// offset++;
// }
// }
// }
// }
else if(A->t->s == arithmeticexpression)
{
if(A->next[0]->t->s==singleorrecid || A->next[0]->t->s==all)
s1=getRecType(RT,GT,FT,A->next[0]->next[0]->t->lexeme,A->next[0]->next[1]->t->lexeme,funname);
else if(A->next[0]->t->s==TK_ID)
s1=getVarType(GT,FT,A->next[0]->t->lexeme,funname);
else
s1=typeCheck(A->next[0],GT,FT,RT,funname);
if(A->next[1]->t->s==singleorrecid || A->next[1]->t->s==all)
s2=getRecType(RT,GT,FT,A->next[1]->next[0]->t->lexeme,A->next[1]->next[1]->t->lexeme,funname);
else if(A->next[1]->t->s==TK_ID)
s2=getVarType(GT,FT,A->next[1]->t->lexeme,funname);
else
s2=typeCheck(A->next[1],GT,FT,RT,funname);
//printf("ae %s %s %s %s\n",toStr(A->next[0]->t->s),toStr(s1),toStr(A->next[1]->t->s),toStr(s2));
if(s1==TK_ERROR || s2==TK_ERROR)
{
if(s1==TK_ERROR)
{
if((A->next[0]->t->s==singleorrecid || A->next[0]->t->s==all) && s1==TK_ERROR)
{
printf("ERROR: Variable %s of Record %s is not declared in this scope at line %d\n",A->next[0]->next[1]->t->lexeme,A->next[0]->next[0]->t->lexeme,A->next[0]->lineno);
any_error=1;
}
else if(A->next[0]->t->s==TK_ID && s1==TK_ERROR)
s1=getVarType(GT,FT,A->next[0]->t->lexeme,funname);
printf("ERROR: Variable %s is not declared in this scope at line %d\n",A->next[0]->t->lexeme,A->next[0]->lineno);
any_error=1;
return TK_ERROR;
}
}
else if(s1!=s2)
{
printf("ERROR: Mismatched Datatype. Datatype %s does not match %s at line %d\n",toStr(s1),toStr(s2),A->lineno);
any_error=1;
return TK_ERROR2;//mismatched datatype
}
else
{
return s1;
}
}
else if(A->t->s == booleanexpression || A->t->s == TK_EQ || A->t->s == TK_LE || A->t->s == TK_LT || A->t->s == TK_GE || A->t->s == TK_GT || A->t->s == TK_AND || A->t->s == TK_OR)
{
if(A->next[0]->t->s==singleorrecid || A->next[0]->t->s==all)
s1=getRecType(RT,GT,FT,A->next[0]->next[0]->t->lexeme,A->next[0]->next[1]->t->lexeme,funname);
else if(A->next[0]->t->s==TK_ID)
s1=getVarType(GT,FT,A->next[0]->t->lexeme,funname);
else
s1=typeCheck(A->next[0],GT,FT,RT,funname);
if(A->next[1]->t->s==singleorrecid || A->next[1]->t->s==all)
s2=getRecType(RT,GT,FT,A->next[1]->next[0]->t->lexeme,A->next[1]->next[1]->t->lexeme,funname);
else if(A->next[1]->t->s==TK_ID)
s2=getVarType(GT,FT,A->next[1]->t->lexeme,funname);
else
s2=typeCheck(A->next[1],GT,FT,RT,funname);
//printf("be %s %s %s %s\n",toStr(A->next[0]->t->s),toStr(s1),toStr(A->next[1]->t->s),toStr(s2));
if(s1==TK_ERROR)
{
if((A->next[0]->t->s==singleorrecid || A->next[0]->t->s==all) && s1==TK_ERROR)
{
printf("ERROR: Variable %s of Record %s is not declared in this scope at line %d\n",A->next[0]->next[1]->t->lexeme,A->next[0]->next[0]->t->lexeme,A->next[0]->lineno);
any_error=1;
}
else if(A->next[0]->t->s==TK_ID && s1==TK_ERROR)
{
printf("ERROR: Variable %s is not declared in this scope at line\n",A->next[0]->t->lexeme,A->next[0]->lineno);
any_error=1;
}
return TK_ERROR;
}
else if(s1!=s2)
{
printf("ERROR: Mismatched Datatype. Datatype %s does not match %s at line %d\n",toStr(s1),toStr(s2),A->lineno);
any_error=1;
return TK_ERROR2;//mismatched datatype
}
else
{
return TK_INT;
}
}
else if(A->t->s==TK_NUM)
{
return TK_INT;
}
else if(A->t->s==TK_RNUM)
{
return TK_REAL;
}
else if(A->t->s==TK_NOT)
{
return TK_INT;
}
else if(A->t->s==TK_FUNID && A->t->lexeme==funname)
{
printf("ERROR: Function %s can't be called recursively\n", funname);
any_error=1;
return TK_ERROR;
}
else
{
s1=TK_ERROR;
if(A->next[0]!=NULL)
s1=typeCheck(A->next[0],GT,FT,RT,funname);
for(i=1;A->next[i]!=NULL && i<20;i++)
{
if(s1!=typeCheck(A->next[i],GT,FT,RT,funname))
{
return TK_ERROR;
}
}
return s1;
}
}
void typeParse(parseTree A,variable GT[], funTable FT[],recTable RT[],char *funname)
{
int i;
symbol s1;
if(A->t->s==assignmentstmt || A->t->s==iostmt || A->t->s==conditionalstmt || A->t->s==booleanexpression || A->t->s==arithmeticexpression || A->t->s==iterativestmt)
{
s1=typeCheck(A,GT,FT,RT,funname);
if(s1==TK_ERROR)
any_error=1;
return;
}
if(A->t->s==function || A->t->s==mainfunction)
strcpy(funname,A->next[0]->t->lexeme);
for(i=0;A->next[i]!=NULL && i<20;i++)
typeParse(A->next[i],GT,FT,RT,funname);
}