-
Notifications
You must be signed in to change notification settings - Fork 2
/
typeExtractor.c
203 lines (188 loc) · 5.35 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
/*
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* findTypeString(int n)
{
if(n == integer)
return "integer";
if(n == real)
return "real";
if(n == boolean)
return "boolean";
return "ERROR";
}
int gettype(stacknode* type) // expects ID->type
{
if(strcmp(type->ntortinfo->str, "INTEGER") == 0)
return integer;
else if(strcmp(type->ntortinfo->str, "REAL") == 0)
return real;
else if(strcmp(type->ntortinfo->str, "BOOLEAN") == 0)
return boolean;
else
return gettype(type->child->sibling);
}
int typeofexpr(stacknode* curr)
{
if(strcmp(curr->ntortinfo->str, "<expression>") == 0)
return typeofexpr(curr->child);
if(strcmp(curr->ntortinfo->str, "<var>") == 0)
{
if(strcmp(curr->child->ntortinfo->str, "NUM") == 0)
return integer;
else if(strcmp(curr->child->ntortinfo->str, "RNUM") == 0)
return real;
else
{
if(curr->child->idst == NULL)
{
semanticCorrect = 1;
return error;
}
else{
idsymboltablenode* temp = getidsymboltablenode(curr->child->tokinfo->lexeme, curr->child->idst);
return gettype(temp->type);
}
}
}
if((strcmp(curr->ntortinfo->str, "TRUE") == 0) || (strcmp(curr->ntortinfo->str, "FALSE") == 0))
return boolean;
if(strcmp(curr->ntortinfo->str, "MINUS") == 0)
{
if(curr->sibling != NULL)
return typeofexpr(curr->sibling);
}
if((strcmp(curr->ntortinfo->str, "PLUS") == 0) || (strcmp(curr->ntortinfo->str, "MINUS") == 0) || (strcmp(curr->ntortinfo->str, "MUL") == 0) || (strcmp(curr->ntortinfo->str, "DIV") == 0))
{
int t1 = typeofexpr(curr->child);
int t2 = typeofexpr(curr->child->sibling);
if(t1 == error || t2 == error)
{
semanticCorrect = 1;
return error;
}
else if(t1==t2 && t1==integer)
return integer;
else if(t1==t2 && t1==real)
return real;
else
{
char* type1 = findTypeString(t1);
char* type2 = findTypeString(t2);
printf("ERROR_T at line %d : Type mismatch of %s and %s operands with %s operator.\n", curr->tokinfo->linenumber, type1, type2, curr->ntortinfo->str);
semanticCorrect = 1;
return error;
}
}
if((strcmp(curr->ntortinfo->str, "LT") == 0) || (strcmp(curr->ntortinfo->str, "LE") == 0) || (strcmp(curr->ntortinfo->str, "GT") == 0) || (strcmp(curr->ntortinfo->str, "GE") == 0) || (strcmp(curr->ntortinfo->str, "EQ") == 0) || (strcmp(curr->ntortinfo->str, "NE") == 0))
{
int t1 = typeofexpr(curr->child);
int t2 = typeofexpr(curr->child->sibling);
if(t1 == error || t2 == error)
{
semanticCorrect = 1;
return error;
}
else if(t1==t2 && t1==integer)
return boolean;
else if(t1==t2 && t1==real)
return boolean;
else
{
char* type1 = findTypeString(t1);
char* type2 = findTypeString(t2);
printf("ERROR_T at line %d : Type mismatch of %s and %s with %s operator.\n", curr->tokinfo->linenumber, type1, type2, curr->ntortinfo->str);
semanticCorrect = 1;
return error;
}
}
if((strcmp(curr->ntortinfo->str, "AND") == 0) || (strcmp(curr->ntortinfo->str, "OR") == 0))
{
int t1 = typeofexpr(curr->child);
int t2 = typeofexpr(curr->child->sibling);
if(t1 == error || t2 == error)
{
semanticCorrect = 1;
return error;
}
if(t1==t2 && t1==boolean)
return boolean;
else
{
char* type1 = findTypeString(t1);
char* type2 = findTypeString(t2);
printf("ERROR_T at line %d : Type mismatch of %s and %s with logical %s.\n", curr->tokinfo->linenumber, type1, type2, curr->ntortinfo->str);
semanticCorrect = 1;
return error;
}
}
return error;
}
void traverseAST_fortypechecking(stacknode* curr)
{
if(curr == NULL)
return;
if(strcmp(curr->ntortinfo->str, "<assignmentStmt>") == 0)
{
if(curr->child->idst == NULL)
return;
idsymboltablenode* temp = getidsymboltablenode(curr->child->tokinfo->lexeme, curr->child->idst);
int idType = gettype(temp->type);
if(strcmp(curr->child->sibling->ntortinfo->str, "<lvalueIDStmt>") == 0)
{
int exprType = typeofexpr(curr->child->sibling->child);
if(idType == error || exprType == error)
return;
if(idType != exprType)
{
semanticCorrect = 1;
printf("ERROR_T at line %d : Type mismatch value on the left is of type %s and on the right is %s.\n", curr->child->tokinfo->linenumber, findTypeString(idType), findTypeString(exprType));
}
}
else
{
int exprType = typeofexpr(curr->child->sibling);
if(idType == error || exprType == error)
return;
if(idType != exprType){
semanticCorrect = 1;
printf("ERROR_T at line %d : Type mismatch value on the left is of type %s and on the right is %s.\n", curr->child->tokinfo->linenumber, findTypeString(idType), findTypeString(exprType));
}
}
return;
}
if(strcmp(curr->ntortinfo->str, "<iterativeStmt>") == 0)
{
int idType = boolean;
if(strcmp(curr->child->ntortinfo->str, "WHILE") == 0)
{
int exprType = typeofexpr(curr->child->sibling);
if(idType == error || exprType == error)
return;
if(idType != exprType){
semanticCorrect = 1;
printf("ERROR_T at line %d : While should have boolean expression.\n", curr->child->tokinfo->linenumber);
}
}
return;
}
stacknode* temp = curr->child;
while(temp != NULL)
{
traverseAST_fortypechecking(temp);
temp = temp->sibling;
}
return;
}