-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab.c
More file actions
217 lines (189 loc) · 5.05 KB
/
symtab.c
File metadata and controls
217 lines (189 loc) · 5.05 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
#include <stdio.h>
#include <string.h>
#include "symtab.h"
//Constants used to control hash table
#define SHIFT 4
// 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 hash table
static HashTableList hashTableList = NULL;
static HashTableList curHashTable = NULL;
// Hash table List initialiization
void initSymbolTable(void){
hashTableList = (HashTableList)malloc(sizeof(struct HashTableRec));
hashTableList->inner = NULL;
hashTableList->outer = NULL;
hashTableList->scope = 0;
hashTableList->location = 0;
hashTableList->visited = 0;
curHashTable = hashTableList;
}
// Come into new scope
void intoNewScope(void){
HashTableList temp = (HashTableList)malloc(sizeof(struct HashTableRec));
temp->outer = curHashTable;
temp->inner = curHashTable->inner;
if (temp->inner)
temp->inner->outer = temp;
curHashTable->inner = temp;
temp->scope = curHashTable->scope + 1;
temp->location = 0;
temp->visited = 0;
if (hashTableList == curHashTable)
hashTableList = temp;
curHashTable = temp;
}
// Go out the scope
void closeScope(void){
curHashTable = curHashTable->outer;
}
// Go into inner scope. Used in phase 2(checking)
void inScope(void){
HashTableList temp = curHashTable->inner;
while (temp->inner){
if (temp->inner->scope > curHashTable->scope)
temp = temp->inner;
else break;
}
while (temp != curHashTable){
if ((temp->scope == curHashTable->scope + 1) && (temp->visited == 0))
break;
temp = temp->outer;
}
curHashTable = temp;
curHashTable->visited = 1;
}
void outScope(void){
HashTableList temp = curHashTable->outer;
while (temp->scope >= curHashTable->scope){
temp = temp->outer;
}
curHashTable = temp;
}
void insertAllPrmt(BucketList prmtList){
while (prmtList){
int h = hash(prmtList->name);
BucketList l = curHashTable->hashTable[h];
BucketList temp = prmtList;
temp->loc = curHashTable->location++;
if ( l ){
while ( l->next ) l = l->next;
l->next = temp;
}
else
curHashTable->hashTable[h] = temp;
prmtList = prmtList->next;
temp->next = NULL;
}
}
// Insertion to hash table
void st_insert(char * name, int lineno, NodeKind kind, int subKind, int arrSize, TokenType type, BucketList found){
if (found == NULL){
int h = hash(name);
BucketList l = curHashTable->hashTable[h];
BucketList temp = (BucketList)malloc(sizeof(struct BucketListRec));
temp->name = name;
temp->loc = curHashTable->location++;
temp->kind = kind;
temp->subKind = subKind;
temp->arrSize = arrSize;
temp->type = type;
temp->next = NULL;
temp->lines = (LineList)malloc(sizeof(struct LineListRec));
temp->lines->lineno = lineno;
temp->lines->next = NULL;
if ( l ){
while ( l->next ) l = l->next;
l->next = temp;
}
else
curHashTable->hashTable[h] = temp;
}
else{
LineList t = found->lines;
while (t->next != NULL) t = t->next;
t->next = (LineList)malloc(sizeof(struct LineListRec));
t->next->lineno = lineno;
t->next->next = NULL;
}
}
BucketList st_lookup(char * name, int lookupMode){
int nextScope = curHashTable->scope;
HashTableList tableSearched = curHashTable;
int h = hash(name);
BucketList l;
if (lookupMode == 0){
while (nextScope >= 0){
while (tableSearched->scope != nextScope) tableSearched = tableSearched->outer;
nextScope--;
l = tableSearched->hashTable[h];
while((l != NULL) && (strcmp(name, l->name) != 0))
l = l->next;
if ( l )
return l;
}
}
else{
l = tableSearched->hashTable[h];
while((l != NULL) && (strcmp(name, l->name) != 0))
l = l->next;
if ( l )
return l;
}
return NULL;
}
void printSymTab(FILE *listing){
HashTableList cur = hashTableList;
int i;
BucketList l;
while (cur){
fprintf(listing, "\nName Scope Loc V/P/F Array? ArrSize Type Line Numbers\n");
fprintf(listing, "-----------------------------------------------------------------------\n");
for (i = 0; i < SIZE; i++){
BucketList l = cur->hashTable[i];
while ( l ){
fprintf(listing, "%-14s", l->name);
fprintf(listing, "%-7d", cur->scope);
fprintf(listing, "%-5d", l->loc);
if (l->kind == PrmtK){
fprintf(listing, "%-7s", "Par");
}
else{
if (l->subKind == FuncK)
fprintf(listing, "%-7s", "Func");
else
fprintf(listing, "%-7s", "Var");
}
if (l->subKind == 2){
fprintf(listing, "%-8s", "Array");
fprintf(listing, "%-9d", l->arrSize);
fprintf(listing, "%-6s", "array");
}
else{
fprintf(listing, "%-8s", "No");
fprintf(listing, "%-9s", "-");
if (l->type == VOID)
fprintf(listing, "%-6s", "void");
else
fprintf(listing, "%-6s", "int");
}
LineList t = l->lines;
while ( t ){
fprintf(listing, "%-5d", t->lineno);
t = t->next;
}
fprintf(listing, "\n");
l = l->next;
}
}
cur = cur->outer;
}
}