-
Notifications
You must be signed in to change notification settings - Fork 2
/
SymbolTable.c
322 lines (315 loc) · 8.33 KB
/
SymbolTable.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SymbolTable.h"
#include "SyntaxTree.h"
/* error handler*/
void throw_error(string s, TreeNode *p){
fprintf(stderr, "line %d: %s: %s\n", p->lineNo, s.c_str(), p->name);
}
/* hash function */
#define SIZE 256
#define SHIFT 4
int myhash(char *key){
int ret = 0;
int i = 0;
while (key[i]!='\0'){
ret = ((ret << SHIFT) + key[i]) % SIZE;
++i;
}
return ret;
}
/* Create Symbol Table*/
SymbolTable *CreateSymbolTable(char *path, int nestLevel){
int i;
SymbolTable *p = (SymbolTable *)malloc(sizeof(SymbolTable));
strcpy(p->path, path);
p->nestLevel = nestLevel;
p->sibling = NULL;
for (i=0; i<256; i++){
p->NameList[i] = NULL;
}
}
int ExistSymbolTable(char *path){
SymbolTable *p = SymTab;
while (p){
if (!strcmp(p->path, path)) return 1;
p = p->sibling;
}
return 0;
}
/* Create Symbol element */
void InsertSymbol(char *name, TreeNode *node, SymbolTable *symtab){
int idx = myhash(name);
Symbol *q = symtab->NameList[idx];
/* init p */
Symbol *p = (Symbol *)malloc(sizeof(Symbol));
strcpy(p->name, name);
p->content = node;
/* connect nodes */
if (q){
while (q->next) q = q->next;
q->next = p;
} else {
symtab->NameList[idx] = p;
}
}
/* Insert element */
void Insert(char *name, char *path, TreeNode *node){
SymbolTable *p = SymTab;
while (p){
if (!strcmp(p->path, path)){
InsertSymbol(name, node, p);
}
p = p->sibling;
}
}
/* lookup an element */
int sub_str(char *src, char *dst){
int i=0, j=0;
while (src[i] && (src[i]==dst[j])){ i++; j++; };
if (!src[i]) return 1;
else return 0;
}
/* lookup in a symbol table */
TreeNode *LookupSymbol(char *name, SymbolTable *symtab){
int idx = myhash(name);
Symbol *p = symtab->NameList[idx];
while (p){
if (!strcmp(p->name, name)){
return p->content;
}
p = p->next;
}
return NULL;
}
/* lookup in all symbol tables */
TreeNode *Lookup(char *name, char *path){
SymbolTable *p = SymTab;
int ansLevel = -1;
TreeNode *ret = NULL;
TreeNode *tmp = NULL;
// printf("lookup:%s, path:%s\n", name, path);
while (p){
if (sub_str(p->path, path)){
// cout<<"lookup symbol"<<endl;
tmp = LookupSymbol(name, p);
// cout<<"end"<<endl;
if (tmp){
// cout<<"find"<<endl;
if (ansLevel<p->nestLevel){
ret = tmp;
ansLevel = p->nestLevel;
}
}
}
if (!strcmp(p->path, path)) break;
p = p->sibling;
}
/* if (ret){
switch (ret->type){
case integer_type: printf("find %s: integer\n", name); break;
case real_type: printf("find %s: real\n", name); break;
case char_type: printf("find %s: char\n", name); break;
case boolean_type: printf("find %s: boolean\n", name); break;
case string_type: printf("find %s: string\n", name); break;
}
}*/
return ret;
}
void ConnectSymtabs(SymbolTable *p1, SymbolTable *p2){
while (p1->sibling)
p1 = p1->sibling;
p1->sibling = p2;
}
int DuplicateIdentifier(char *name, char *path){
SymbolTable *p = SymTab;
int ansLevel = -1;
TreeNode *tmp = NULL;
// printf("lookup:%s, path:%s\n", name, path);
while (p){
if (!strcmp(p->path, path)){
tmp = LookupSymbol(name, p);
// if (tmp) printf("duplicate %s\n", name);
break;
}
p = p->sibling;
}
if (tmp)
return 1;
else
return 0;
}
void SymtabTraverseTree(TreeNode *p, char *path, int nestLevel, SymbolTable *symtab){
char local_path[256];
if (!p) return ;
// printf("path:%s\n", path);
switch (p->node){
case routine_kind: {
SymtabTraverseTree(p->children[0], path, nestLevel, symtab);
SymtabTraverseTree(p->children[1], path, nestLevel, symtab);
break;
}
case const_kind: {
if (DuplicateIdentifier(p->name, path))
throw_error("duplicate identifier", p);
InsertSymbol(p->name, p, symtab);
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
break;
}
case type_kind: {
switch (p->type){
case decl_type:{
if (DuplicateIdentifier(p->name, path))
throw_error("duplicate identifier", p);
InsertSymbol(p->name, p, symtab);
if(p->dtype->type!=record_type){
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
} else {
sprintf(local_path, "%s/%s", path, p->name);
SymbolTable *new_symtab = CreateSymbolTable(local_path, nestLevel+1);
ConnectSymtabs(symtab, new_symtab);
SymtabTraverseTree(p->dtype, local_path, nestLevel+1, new_symtab);
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
}
break;
}
case record_type:{
SymtabTraverseTree(p->children[0], path, nestLevel, symtab);
break;
}
default:{ break; }
}
break;
}
case var_kind:{
TreeNode *q;
q = p->children[0];
while (q){
if (DuplicateIdentifier(q->name, path))
throw_error("duplicate identifier", q);
InsertSymbol(q->name, p, symtab);
q = q->sibling;
}
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
break;
}
case sub_kind: {
switch(p->sub){
case func_kind:{
// printf("enter function %s decl:\n", p->name);
InsertSymbol(p->name, p, symtab);
sprintf(local_path, "%s/%s", path, p->name);
SymbolTable * new_symtab = CreateSymbolTable(local_path, nestLevel+1);
ConnectSymtabs(symtab, new_symtab);
SymtabTraverseTree(p->children[0], local_path, nestLevel+1, new_symtab);
SymtabTraverseTree(p->children[1], local_path, nestLevel+1, new_symtab);
SymtabTraverseTree(p->children[2], local_path, nestLevel+1, new_symtab);
break;
}
case proc_kind:{
// printf("enter procedure %s decl:\n", p->name);
InsertSymbol(p->name, p, symtab);
sprintf(local_path, "%s/%s", path, p->name);
SymbolTable * new_symtab = CreateSymbolTable(local_path, nestLevel+1);
ConnectSymtabs(symtab, new_symtab);
SymtabTraverseTree(p->children[0], local_path, nestLevel+1, new_symtab);
SymtabTraverseTree(p->children[1], local_path, nestLevel+1, new_symtab);
SymtabTraverseTree(p->children[2], local_path, nestLevel+1, new_symtab);
break;
}
case param_var_kind:{
TreeNode *q;
q = p->children[0];
while (q){
if (DuplicateIdentifier(q->name, path))
throw_error("duplicate identifier", q);
InsertSymbol(q->name, p, symtab);
q = q->sibling;
}
break;
}
case param_val_kind:{
TreeNode *q;
q = p->children[0];
while (q){
if (DuplicateIdentifier(q->name, path))
throw_error("duplicate identifier", q);
InsertSymbol(q->name, p, symtab);
q = q->sibling;
}
break;
}
}
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
break;
};
case expr_kind:{
switch (p->expr){
case id_kind:{
// printf("look for identifier: %s\n", p->name);
if (!Lookup(p->name, path))
throw_error("unknown identifier", p);
break;
}
case fn_kind:{
// printf("look for function: %s\n", p->name);
if (!Lookup(p->name, path) && !p->system)
throw_error("unknown identifier", p);
}
default: break;
}
SymtabTraverseTree(p->children[0], path, nestLevel, symtab);
SymtabTraverseTree(p->children[1], path, nestLevel, symtab);
SymtabTraverseTree(p->children[2], path, nestLevel, symtab);
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
break;
}
case stmt_kind:{
switch (p->stmt){
case for_stmt:{
if (!Lookup(p->name, path))
throw_error("unknown identifier", p);
}
case proc_stmt:{
if (!Lookup(p->name, path) && !p->system)
throw_error("unknown identifier", p);
}
}
SymtabTraverseTree(p->children[0], path, nestLevel, symtab);
SymtabTraverseTree(p->children[1], path, nestLevel, symtab);
SymtabTraverseTree(p->children[2], path, nestLevel, symtab);
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
break;
}
default: {
SymtabTraverseTree(p->children[0], path, nestLevel, symtab);
SymtabTraverseTree(p->children[1], path, nestLevel, symtab);
SymtabTraverseTree(p->children[2], path, nestLevel, symtab);
SymtabTraverseTree(p->sibling, path, nestLevel, symtab);
break;
}
}
}
void print_symtab(SymbolTable *p){
int i;
Symbol *q;
while (p){
printf("%s:\n", p->path);
for (i=0; i<SIZE; i++){
q = p->NameList[i];
while (q){
if (q->content->dtype)
printf("%s\n", q->name);
q = q->next;
}
}
p = p->sibling;
}
}
void BuildSymTab(){
strcpy(path, "global");
nestLevel = 0;
SymTab = CreateSymbolTable(path, nestLevel);
SymtabTraverseTree(root, path, nestLevel, SymTab);
}