-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtab.c
244 lines (223 loc) · 6.67 KB
/
symtab.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
/****************************************************/
/* File: symtab.c */
/* Symbol table implementation for the TINY compiler*/
/* (allows only one symbol table) */
/* Symbol table is implemented as a chained */
/* hash table */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtab.h"
#include "globals.h"
/* SHIFT is the power of two used as multiplier
in hash function */
#define SHIFT 4
/* the hash function */
static int hash ( char * key )
{ int temp = 0;
int i = 0;
while (key[i] != '\0')
{ temp = ((temp << SHIFT) key[i]) % SIZE;
i;
}
return temp;
}
/* the scope table */
static ScopeList scopeHashTable[SIZE];
ScopeList getParentScope(char * scope)
{ int i;
char *tmp;
ScopeList l;
if (strcmp(scope, "") == 0)
return NULL;
tmp = (char *)malloc(sizeof(char) *(strlen(scope) 1));
strcpy(tmp, scope);
for(i = strlen(tmp); 0 <= i; i--)
{ if (tmp[i] == ':')
{ tmp[i] = '\0';
break; }
else
tmp[i] = '\0';
}
l = scopeHashTable[hash(tmp)];
while ((l != NULL) && (strcmp(tmp,l->name) != 0))
l = l->next;
if (l == NULL)
l = getParentScope(tmp);
free(tmp);
return l;
}
ScopeList getScope(char * scope)
{ ScopeList l = scopeHashTable[hash(scope)];
while ((l != NULL) && (strcmp(scope, l->name) != 0))
l = l->next;
return l;
}
/* Procedure st_insert_ inserts line numbers and
* memory locations into the symbol table
* loc = memory location is inserted only the
* first time, otherwise ignored
*/
void st_insert_( ScopeList scope, char * name, ExpType type, int lineno, int loc, int isArray )
{ int h = hash(name);
BucketList l = scope->bucket[h];
while ((l != NULL) && (strcmp(name,l->name) != 0))
l = l->next;
if (l == NULL) /* variable not yet in table */
{ l = (BucketList) malloc(sizeof(struct BucketListRec));
l->name = name;
l->lines = (LineList) malloc(sizeof(struct LineListRec));
l->type = type;
l->lines->lineno = lineno;
l->memloc = loc;
l->lines->next = NULL;
l->next = scope->bucket[h];
l->isArray = isArray;
scope->bucket[h] = l; }
else /* found in table, so just add line number */
{ LineList t = l->lines;
while (t->next != NULL) t = t->next;
t->next = (LineList) malloc(sizeof(struct LineListRec));
t->next->lineno = lineno;
t->next->next = NULL;
}
}
void st_insert( char * scope, char * name, ExpType type, int lineno, int loc, int isArray )
{ int h = hash(scope);
ScopeList l = scopeHashTable[h];
while ((l != NULL) && (strcmp(scope,l->name) != 0))
l = l->next;
if (l == NULL) /* scope not yet in table */
{ l = (ScopeList) malloc(sizeof(struct ScopeListRec));
memset(l, 0, sizeof(struct ScopeListRec));
l->parent = getParentScope(scope);
l->name = (char *)malloc(sizeof(char) * (strlen(scope) 1));
strcpy(l->name, scope);
l->next = scopeHashTable[h];
scopeHashTable[h] = l; }
st_insert_(l, name, type, lineno, loc, isArray);
}/* st_insert */
/* Function st_lookup returns the memory
* location of a variable or -1 if not found
*/
BucketList st_lookup_ ( ScopeList scope, char * name )
{ int h = hash(name);
if (scope == NULL)
return NULL;
BucketList l = scope->bucket[h];
while ((l != NULL) && (strcmp(name,l->name) != 0))
l = l->next;
if (l == NULL) return NULL;
else return l;
}
BucketList st_lookup ( char * scope, char * name )
{ BucketList result;
int h = hash(scope);
ScopeList l = scopeHashTable[h];
while ((l != NULL) && (strcmp(scope,l->name) != 0))
l = l->next;
if (l == NULL)
{ ScopeList parent = getParentScope(scope);
if (parent == NULL)
return NULL;
return st_lookup(parent->name, name);
}
result = st_lookup_(l, name);
if (result == NULL) //lookup in parent
{ ScopeList parent = getParentScope(scope);
if (parent == NULL) //if there is no parent
return NULL;
return st_lookup(parent->name, name);
}
else return result;
}
int st_get_location(char *scope, char *name)
{ BucketList bucketList = NULL;
bucketList = st_lookup(scope, name);
if(bucketList == NULL)
return -1;
return bucketList->memloc;
}
BucketList st_lookup_excluding_parent ( char * scope, char * name )
{ int result;
int h = hash(scope);
ScopeList l = scopeHashTable[h];
while ((l != NULL) && (strcmp(scope,l->name) != 0))
l = l->next;
if (l == NULL)
return NULL;
return st_lookup_(l, name);
}
int checkArray(char *scope, char *name)
{ BucketList bucketList = st_lookup(scope, name);
if(bucketList == NULL)
return -1;
return bucketList->isArray;
}
void addline( char * scope, char * name, int lineno )
{ BucketList bucket;
LineList lineList;
ScopeList l = scopeHashTable[hash(scope)];
while ((l != NULL) && (strcmp(scope, l->name)))
l = l->next;
if (l == NULL)
l = getParentScope(scope);
while (l)
{ bucket = l->bucket[hash(name)];
while ((bucket != NULL) && (strcmp(name, bucket->name) != 0))
bucket = bucket->next;
if (bucket == NULL)
l = l->parent;
else
break;
}
lineList = bucket->lines;
while(lineList->next != NULL) lineList = lineList->next;
lineList->next = (LineList) malloc(sizeof(struct LineListRec));
lineList->next->lineno = lineno;
lineList->next->next = NULL;
}
/* Procedure printSymTab prints a formatted
* listing of the symbol table contents
* to the listing file
*/
void printSymTab_(FILE * listing, ScopeList scope)
{ int i;
fprintf(listing,"Variable Name Variable Type Location Line Numbers\n");
fprintf(listing,"------------- ------------- -------- ------------\n");
for (i=0;i<SIZE; i)
{ if (scope->bucket[i] != NULL)
{ BucketList l = scope->bucket[i];
while (l != NULL)
{ LineList t = l->lines;
fprintf(listing,"%-14s ",l->name);
fprintf(listing,"%-13s ",l->type == Integer? "Integer" : "Void");
fprintf(listing,"%-8d ",l->memloc);
while (t != NULL)
{ fprintf(listing,"M ",t->lineno);
t = t->next;
}
fprintf(listing,"\n");
l = l->next;
}
}
}
}
void printSymTab(FILE * listing)
{ int i;
for (i=0;i<SIZE; i)
{ if (scopeHashTable[i] != NULL)
{ ScopeList l = scopeHashTable[i];
while ( l != NULL)
{ fprintf(listing,"Scope name : %s\n", l->name);
fprintf(listing,"----------------------------------\n");
printSymTab_(listing, l);
fprintf(listing,"----------------------------------\n\n\n\n");
l = l->next;
}
}
}
} /* printSymTab */