-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyze.c
More file actions
319 lines (304 loc) · 9.92 KB
/
analyze.c
File metadata and controls
319 lines (304 loc) · 9.92 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
/********************************************/
/* File: analyze.c */
/* Semantic analyzer implementation */
/* for the C- compiler */
/* tallate */
/********************************************/
#include "analyze.h"
#include "symtab.h"
#include "util.h"
#include "globals.h"
/* 变量的内存地址的计数器 */
// static int location = 0;
static int nestNum = 0;
static TreeNode *enclosingFunction = NULL;
/* typeError 打印类型错误信息
*/
static void typeError(TreeNode *t, char *message) {
fprintf(listing, "Type error at line %d: %s\n", t->lineno, message);
printf("%s\n", t->attr.name);
Error = TRUE;
}
static int isArrType(TreeNode *decl) {
return decl->nodeKind == DeclK && decl->kind.decl == ArrD;
}
static int isFunType(TreeNode *decl) {
return decl->nodeKind == DeclK && decl->kind.decl == FunD;
}
static int typeEqual(ExpType target, ExpType cur) {
return target == cur;
}
/* 检查函数调用的实参列表是否与其定义的形参列表一致 */
static int checkFunCall(TreeNode *funDecl, TreeNode *funCall) {
if(! funDecl || ! funCall) {
/* 传入了空指针 */
return 0;
}
TreeNode *declArgList = funDecl->child[0];
TreeNode *callArgList = funCall->child[0];
while(declArgList && callArgList) {
if(! typeEqual(declArgList->varType, callArgList->expType)) {
return 0;
}
declArgList = declArgList->sibling;
callArgList = callArgList->sibling;
}
if(declArgList || callArgList) {
return 0;
} else {
return 1;
}
}
/* traverse 语法树递归遍历的子程序
* 以前序应用preProc,以后序应用postProc
*/
static void traverse(TreeNode *t,
void (* preProc) (TreeNode *),
void (* postProc) (TreeNode *)) {
if(t) {
preProc(t);
/* 遍历所有子节点 */
for(int i = 0; i < MAXCHILDREN; i++) {
traverse(t->child[i], preProc, postProc);
}
postProc(t);
traverse(t->sibling, preProc, postProc);
}
}
/* insertNode 向符号表的当前scope插入一个标志符
*/
static void insertNode(TreeNode *t) {
switch(t->nodeKind) {
case DeclK:
switch(t->kind.decl) {
/* 在当前scope中插入一个标志符 */
case VarD:
/*****************************************/
if(st_lookup(t->attr.name, 1)) {
typeError(t, "Redeclaration");
symbolTable->err = TRUE;
}
st_insert(t->attr.name, t, t->lineno, symbolTable->location++);
break;
case ArrD:
/*****************************************/
if(st_lookup(t->attr.name, 1)) {
typeError(t, "Redeclaration");
symbolTable->err = TRUE;
}
st_insert(t->attr.name, t, t->lineno, symbolTable->location);
if(t->child[0]) {
symbolTable->location += t->child[0]->attr.val;
} else {
/* 数组参数,传入的是地址 */
symbolTable->location++;
}
break;
case FunD:
/*****************************************/
if (st_lookup(t->attr.name, 1)) {
typeError(t, "Redeclaration");
symbolTable->err = TRUE;
}
st_insert(t->attr.name, t, t->lineno, symbolTable->location++);
printSymTab(listing);
st_augment();
enclosingFunction = t;
break;
default:
break;
}
break;
case StmtK:
switch(t->kind.stmt) {
case ComS:
printf("line: %d\n", t->lineno);
printSymTab(listing);
/* 不为0表示当前scope不是函数定义的复合语句中 */
if(nestNum != 0) {
st_augment();
/* 初始地址继承自外部的符号表 */
symbolTable->location = symbolTable->next->location;
}
nestNum++;
break;
default:
break;
}
default:
break;
}
} /* insertNode */
/* checkNode 对当前树节点进行type checking
*/
static void checkNode(TreeNode *t) {
BucketList *tmp;
switch(t->nodeKind) {
case DeclK:
switch(t->kind.decl) {
case VarD:
// if(! symbolTable->err) {
if (t->varType != Integer) {
typeError(t, "Var declared with non-integer type");
symbolTable->err = TRUE;
}
// }
break;
case ArrD:
// if(! symbolTable->err) {
if(t->varType != Integer) {
typeError(t, "Array declared with non-integer type");
symbolTable->err = TRUE;
}
// }
break;
case FunD:
// if(! symbolTable->err) {
// }
break;
default:
break;
}
break;
case StmtK:
switch(t->kind.stmt) {
case ExpS:
break;
case ComS:
t->scope = st_abrade(); /* 保存对应scope用于代码生成 */
/* 记录AR的最大偏移量 */
if(t->scope->location > t->scope->maxOffset) {
t->scope->maxOffset = t->scope->location;
}
if(symbolTable->next) { /* 说明不是函数域 */
if(t->scope->location > symbolTable->maxOffset) {
symbolTable->maxOffset = t->scope->location;
}
}
nestNum--;
if(nestNum == 0) {
enclosingFunction = NULL;
}
break;
case SelS:
break;
case IteS:
break;
case RetS:
if(t->child[0]) {
if(enclosingFunction->funcReturnType == Void) {
typeError(t, "function with void return type returned non-void value");
symbolTable->err = TRUE;
}
} else {
if(enclosingFunction->funcReturnType != Void) {
typeError(t, "function with non-void return type returned void");
symbolTable->err = TRUE;
}
}
break;
default:
break;
}
break;
case ExpK:
switch(t->kind.exp) {
case AssignE:
if(t->child[0]->kind.exp != VarE && t->child[0]->kind.exp != ArrE) {
typeError(t, "l-value is not var or array");
symbolTable->err = 1;
}
t->attr.val = t->child[1]->attr.val;
break;
case OpE:
if((t->child[0]->expType != Integer) ||
(t->child[1]->expType != Integer)) {
typeError(t, "Op applied to non-integer");
}
switch(t->attr.op) {
/* relop(关系表达式) */
case LT: case LTET: case MT:
case MTET: case EQ: case NEQ:
t->expType = Boolean;
break;
default:
t->expType = Integer;
break;
}
break;
case ConstE:
t->expType = Integer;
break;
case VarE:
tmp = st_lookup(t->attr.name, -1);
if(! tmp) {
typeError(t, "Var Used without declaration");
t->expType = Void;
} else {
t->expType = Integer;
}
break;
case ArrE:
tmp = st_lookup(t->attr.name, -1);
if(! tmp) {
typeError(t, "Array used without declaration");
t->expType = Void;
} else if(!isArrType(tmp->decl)) {
typeError(t, "Subscripted value is not array");
t->expType = Void;
} else {
t->expType = Integer;
}
break;
case CallE:
tmp = st_lookup(t->attr.name, -1);
if(! tmp) {
typeError(t, "Call withoud declaration");
t->expType = Void;
} else if(! isFunType(tmp->decl)) {
typeError(t, "Called object is not a function");
t->expType = Void;
} else if(! checkFunCall(tmp->decl, t)) {
typeError(t, "Call with false arguments");
t->expType = Void;
} else {
t->expType = Integer;
}
break;
default:
break;
}
break;
default:
break;
}
} /* checkNode */
void initSymbolTable() {
st_augment();
/* 预定义的输入函数 */
TreeNode *inputFun = newDeclNode(FunD);
inputFun->attr.name = copyString("input");
inputFun->funcReturnType = Integer;
inputFun->child[1] = newStmtNode(ComS);
inputFun->child[1]->scope = (SymbolTable*) malloc(sizeof(SymbolTable));
inputFun->child[1]->scope->location = inputFun->child[1]->scope->maxOffset = 0;
st_insert("input", inputFun, -1, symbolTable->location++);
/* 预定义的输出函数 */
TreeNode *outputFun = newDeclNode(FunD);
outputFun->attr.name = copyString("output");
outputFun->funcReturnType = Void;
outputFun->child[0] = newDeclNode(VarD);
outputFun->child[0]->varType = Integer;
outputFun->child[1] = newStmtNode(ComS);
outputFun->child[1]->scope = (SymbolTable*) malloc(sizeof(SymbolTable));
outputFun->child[1]->scope->location = outputFun->child[1]->scope->maxOffset = 1;
st_insert("output", outputFun, -1, symbolTable->location++);
}
void analyze(TreeNode *syntaxTree) {
initSymbolTable();
traverse(syntaxTree, insertNode, checkNode);
if(TraceAnalyze) {
fprintf(listing, "\nSymbol table:\n");
printSymTab(listing);
}
}