-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.c
More file actions
381 lines (345 loc) · 11.3 KB
/
analyze.c
File metadata and controls
381 lines (345 loc) · 11.3 KB
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
#include "globals.h"
#include "symtab.h"
#include "analyze.h"
static BucketList prmtQueueStart = NULL;
static BucketList prmtQueueEnd = NULL;
/*
static void traverse(TreeNode *t, int (*preProc)(TreeNode*), int (*postProc)(TreeNode*)){
int compFlag = 0;
if (t != NULL){
if ((t->nodekind == StmtK) && (t->kind.stmt == CompK)){
compFlag = 1;
locBefore = location;
location = 0;
intoNewScope();
}
preProc(t);
{
int i;
for (i = 0; i < MAXCHILDREN; i++)
traverse(t->child[i], preProc, postProc);
}
postProc(t);
traverse(t->sibling, preProc, postProc);
if (compFlag){
closeScope();
location = locBefore;
}
}
}
static int nullProc(TreeNode *t){
return 0;
}
*/
static void insertPrmtQueue(char *name, int lineno, PrmtKind pkind){
if (prmtQueueEnd){
prmtQueueEnd->next = (BucketList)malloc(sizeof(struct BucketListRec));
prmtQueueEnd = prmtQueueEnd->next;
}
else{
prmtQueueEnd = prmtQueueStart = (BucketList)malloc(sizeof(struct BucketListRec));
}
prmtQueueEnd->name = name;
prmtQueueEnd->loc = 0;
prmtQueueEnd->kind = PrmtK;
prmtQueueEnd->subKind = pkind;
prmtQueueEnd->arrSize = 0;
prmtQueueEnd->type = INT;
prmtQueueEnd->next = NULL;
prmtQueueEnd->lines = (LineList)malloc(sizeof(struct LineListRec));
prmtQueueEnd->lines->lineno = lineno;
prmtQueueEnd->lines->next = NULL;
}
static int insertNode(TreeNode *t){
BucketList found;
BucketList searchedPar = prmtQueueStart; //Only used for parameter case
static int mainFlag = 0;
switch (t->nodekind){
case DclK:
switch (t->kind.dcl){
case FuncK:
found = st_lookup(t->attr.name, 1);
if ( found ){
fprintf(listing, "ERROR in line %d : declaration of a duplicated\n", t->lineno);
fprintf(listing, "\t\t\tfirst declared at line %d\n", found->lines->lineno);
return -1;
}
if (mainFlag){
BucketList mainBucket = st_lookup("main", 0);
fprintf(listing, "ERROR in line %d : main should be the last function\n", mainBucket->lines->lineno);
return -1;
}
if (strcmp(t->attr.name, "main") == 0)
mainFlag = 1;
st_insert(t->attr.name, t->lineno, DclK, FuncK, 0, t->child[0]->attr.type, NULL);
break;
case VarK:
found = st_lookup(t->attr.name, 1);
if ( found ){
fprintf(listing, "ERROR in line %d : declaration of a duplicated\n", t->lineno);
fprintf(listing, "\t\t\tfirst declared at line %d\n", found->lines->lineno);
return -1;
}
st_insert(t->attr.name, t->lineno, DclK, VarK, 0, t->child[0]->attr.type, NULL);
break;
case ArrK:
found = st_lookup(t->attr.arrProp.name, 1);
if ( found ){
fprintf(listing, "ERROR in line %d : declaration of a duplicated\n", t->lineno);
fprintf(listing, "\t\t\tfirst declared at line %d\n", found->lines->lineno);
return -1;
}
st_insert(t->attr.arrProp.name, t->lineno, DclK, ArrK, t->attr.arrProp.size, t->child[0]->attr.type, NULL);
break;
}
break;
case PrmtK:
switch (t->kind.prmt){
case IntK:
case IntArrK:
while ((searchedPar != NULL) && (strcmp(searchedPar->name, t->attr.name) != 0)){
searchedPar = searchedPar->next;
}
if ( searchedPar != NULL ){
fprintf(listing, "ERROR in line %d : declaration of a duplicated\n", t->lineno);
fprintf(listing, "\t\t\tfirst declared at line %d\n", searchedPar->lines->lineno);
return -1;
}
insertPrmtQueue(t->attr.name, t->lineno, t->kind.prmt);
default: break;
}
break;
case ExpK:
switch (t->kind.exp){
case IdK:
case ArrIdK:
found = st_lookup(t->attr.name, 0);
if ( !found ){
fprintf(listing, "ERROR in line %d : variable %s not exist\n", t->lineno, t->attr.name);
return -1;
}
st_insert(NULL, t->lineno, 0, 0, 0, 0, found);
break;
case CallK:
found = st_lookup(t->attr.name, 0);
if ( !found ){
fprintf(listing, "ERROR in line %d : function %s not exist\n", t->lineno, t->attr.name);
return -1;
}
st_insert(NULL, t->lineno, 0, 0, 0, 0, found);
default: break;
}
default: break;
}
return 0;
}
int buildSymtabNode(TreeNode *t){
int compFlag = 0;
if (t != NULL){
if ((t->nodekind == StmtK) && (t->kind.stmt == CompK)){
compFlag = 1;
intoNewScope();
if (prmtQueueStart){
insertAllPrmt(prmtQueueStart);
prmtQueueEnd = prmtQueueStart = NULL;
}
}
if (insertNode(t) == -1){
Error = TRUE;
return -1;
}
{
int i;
for (i = 0; i < MAXCHILDREN; i++){
if (buildSymtabNode(t->child[i]) == -1)
return -1;
}
}
if (buildSymtabNode(t->sibling) == -1)
return -1;
if (compFlag){
closeScope();
}
}
return 0;
}
void buildSymtab(TreeNode *syntaxTree){
initSymbolTable();
if (buildSymtabNode(syntaxTree) == -1)
return;
fprintf(listing, "\nSymbol table:\n");
printSymTab(listing);
}
static TreeNode *root;
static ExpType curFuncRetType;
int checkNode(TreeNode *t){
BucketList found;
switch (t->nodekind){
case StmtK:
switch (t->kind.stmt){
case SelK:
case IterK:
if (t->child[0]->type != Integer){
fprintf(listing, "ERROR in line %d : condition must be integer type\n", t->child[0]->lineno);
return -1;
}
break;
case RetK:
if (t->child[0]){
if (curFuncRetType != t->child[0]->type){
fprintf(listing, "ERROR in line %d : wrong return type\n", t->child[0]->lineno);
return -1;
}
}
else{
if (curFuncRetType != Void){
fprintf(listing, "ERROR in line %d : there should be a return value\n", t->lineno);
return -1;
}
}
default: break;
}
break;
case ExpK:
switch (t->kind.exp){
case AssignK:
case OpK:
if ((t->child[0]->type != Integer) || (t->child[1]->type != Integer)){
fprintf(listing, "ERROR in line %d : invalid operand\n", t->lineno);
return -1;
}
t->type = Integer;
break;
case ConstK:
t->type = Integer;
break;
case IdK:
found = st_lookup(t->attr.name, 0);
if (found->subKind == 2)
t->type = Array;
else
t->type = Integer;
break;
case ArrIdK:
found = st_lookup(t->attr.name, 0);
if (found->subKind != 2){
fprintf(listing, "ERROR in line %d : array name is required in array expression\n", t->lineno);
return -1;
}
if (t->child[0]->type != Integer){
fprintf(listing, "ERROR in line %d : array index must be an integer\n", t->lineno);
return -1;
}
t->type = Integer;
break;
case CallK:
found = st_lookup(t->attr.name, 0);
if ((found->kind != DclK) || (found->subKind != FuncK)){
fprintf(listing, "ERROR in line %d : function name is required in function call\n", t->lineno);
return -1;
}
{
TreeNode *par = root, *arg = t->child[0];
while (strcmp(par->attr.name, t->attr.name) != 0){
par = par->sibling;
}
par = par->child[1];
if (!((par->kind.prmt == VoidK) && !arg)){
while ( par || arg ){
if (!(par && arg && (par->type == arg->type))){
fprintf(listing, "ERROR in line %d : argument type error\n", t->lineno);
return -1;
}
par = par->sibling;
arg = arg->sibling;
}
}
}
if (found->type == INT)
t->type = Integer;
else
t->type = Void;
default: break;
}
break;
case DclK:
switch (t->kind.dcl){
case VarK:
case ArrK:
if (t->child[0]->attr.type == VOID){
fprintf(listing, "ERROR in line %d : variable cannot have void type\n", t->lineno);
return -1;
}
break;
case FuncK:
if (strcmp(t->attr.name, "main") == 0){
if (t->child[0]->attr.type != VOID){
fprintf(listing, "ERROR in line %d : main fuction should return void\n", t->lineno);
return -1;
}
if (t->child[1]->kind.prmt != VoidK){
fprintf(listing, "ERROR in line %d : main must not have a parameter\n", t->lineno);
return -1;
}
}
default: break;
}
break;
case PrmtK:
switch (t->kind.prmt){
case IntK:
if (t->child[0]->attr.type == VOID){
fprintf(listing, "ERROR in line %d : parameter cannot have void type\n", t->lineno);
return -1;
}
t->type = Integer;
break;
case IntArrK:
if (t->child[0]->attr.type == VOID){
fprintf(listing, "ERROR in line %d : parameter cannot have void type\n", t->lineno);
return -1;
}
t->type = Array;
break;
default: break;
}
default: break;
}
return 0;
}
int checkTree(TreeNode *t){
int compFlag = 0;
if (t != NULL){
if ((t->nodekind == StmtK) && (t->kind.stmt == CompK)){
compFlag = 1;
inScope();
}
if ((t->nodekind == DclK) && (t->kind.dcl == FuncK)){
if (t->child[0]->attr.type == VOID)
curFuncRetType = Void;
else
curFuncRetType = Integer;
}
{
int i;
for (i = 0; i < MAXCHILDREN; i++){
if (checkTree(t->child[i]) == -1)
return -1;
}
}
if (checkNode(t) == -1){
Error = TRUE;
return -1;
}
if (checkTree(t->sibling) == -1)
return -1;
if (compFlag){
outScope();
}
}
return 0;
}
void typeCheck(TreeNode *syntaxTree){
root = syntaxTree;
checkTree(syntaxTree);
}