This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScopeTable.h
415 lines (362 loc) · 13.6 KB
/
ScopeTable.h
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Created by rng70
#ifndef COMPILER_SCOPETABLE_H
#define COMPILER_SCOPETABLE_H
class ScopeTable {
int size, Id, tableIdTracker;
int TableID;
/// Declare parent Scope to Store parent Scope of current Scope
ScopeTable *parentScope;
// HashTable to store information of symbol(s)
SymbolInfo **HashTable;
public:
ScopeTable() {
this->size = -1;
this->tableIdTracker = -1;
this->Id = this->tableIdTracker;
}
explicit ScopeTable(int tableSize,int counter = 0, int p = 0) {
this->size = tableSize;
this->tableIdTracker = 0;
this->Id = p + 1;
this->TableID = counter;
// Set initial parent scope to 0 not null or nullptr
this->parentScope = 0;
// Allocate memory for hashtable
HashTable = new SymbolInfo *[tableSize];
for (int i = 0; i < tableSize; i++) {
HashTable[i] = new SymbolInfo();
}
}
void setTableIdTracker() {
tableIdTracker++;
}
int getTableIdTracker() {
return this->tableIdTracker;
}
std::string getStringifyID() {
std::string sID = std::to_string(this->Id);
ScopeTable *tempParent = this->parentScope;
while (tempParent != 0) {
sID += "." + std::to_string(tempParent->getID());
tempParent = tempParent->getParentScope();
}
reverse(sID.begin(), sID.end());
return sID;
}
bool Insert(std::string s, std::string type) {
int idx = HashFunc(s);
// Case-1: Table is empty and so no-collision occurred
if (HashTable[idx]->getName() == "") {
HashTable[idx]->setName(s);
HashTable[idx]->setType(type);
// Successfully inserted
return true;
}
// Case-2: Table is not empty and collision occurred
else {
SymbolInfo *root = HashTable[idx];
while (root->getNextPointer() != 0) {
// next pointer is not null, it is 0
if (root->getName() == s) {
if(root->getType()=="ID"){
std::string foundMsg = s + " already exists in current ScopeTable\n";
//fprintf(logs, "%s\n", foundMsg.c_str());
}
return false;
}
root = root->getNextPointer();
}
// if root is at level-0
if (root->getNextPointer() == 0 && root->getName() == s) {
// duplicate found at level 0
if(root->getType()=="ID"){
std::string foundMsg = s + " already exists in current ScopeTable\n";
// fprintf(logs, "%s\n", foundMsg.c_str());
}
return false;
}
// at that point we did not find any instance of s in symbol table
// redundant declaration
root = HashTable[idx];
int positionTracker = 0;
SymbolInfo *newSymbolInfo = new SymbolInfo(s, type, 0);
// set the appropriate data
// newSymbolInfo->setName(s);
// newSymbolInfo->setType(type);
// newSymbolInfo->setNextPointer(0);
while (root->getNextPointer() != 0) {
root = root->getNextPointer();
positionTracker++;
}
root->setNextPointer(newSymbolInfo);
return true;
}
// At that point it is ensured that we must have inserted the item
return false;
}
bool InsertModified(SymbolInfo* s)
{
std::string key = s->getName();
std::string value = s->getType();
int index = HashFunc(key);
///if the indexed node is empty
if(HashTable[index]->getName() == "" ){
HashTable[index] = s;
return true;
}else ///collision occurred
{
///checking here for duplicates
SymbolInfo* head = HashTable[index];
///if initial node is the duplicate one
if(head->getNextPointer() == 0 && head->getName() == key){
return false;
}else{
///The duplicate one may be further down the chain
while(head!=0){
if(head->getName() == key){
return false;
}
head = head->getNextPointer();
}
}
///if duplicate is not found, we insert in the chain to resolve collision
int j = 0;
head = HashTable[index];
while(head->getNextPointer() != 0){
head = head->getNextPointer();
j++;
}
s->setNextPointer(0);
head->setNextPointer(s);
return true;
}
}
int getID() {
return this->TableID;
}
ScopeTable *getParentScope() {
return this->parentScope;
}
void setParentScope(ScopeTable *parent) {
this->parentScope = parent;
}
int HashFunc(std::string s) const {
int sumOfAsciiValue = 0;
for (auto si:s) {
sumOfAsciiValue += si;
}
return abs(sumOfAsciiValue % size);
}
// Search the hashtable and return a symbol-info pointer
SymbolInfo *LookUP(std::string s) {
int index = HashFunc(s);
SymbolInfo *head = HashTable[index];
int positionTracker = 0;
// Search for the string
if (head == 0) {
return 0;
}
while (head != 0) {
if (head->getName() == s) {
return head;
}
positionTracker++;
head = head->getNextPointer();
}
return head;
}
bool Delete(std::string s) {
// Case to Consider:
/*
* 1. Table is empty
* 2. Table has one entity and that does not match with the string
* 3. Table has one entity and that is the element want to remove
* 4. Table has a chain and it's first element is the element want to remove
* 5. Table has a chain and it's middle element is the element want to remove
* 6. Table has a chain but does not contain the element
* 7. Last element of the chain is the element
*/
// Calculate the index to look for
int index = HashFunc(s);
// Initialize a symbol-info pointer to search for the keys
SymbolInfo *root = HashTable[index];
SymbolInfo *temp, *p, *q;
// Case-1: return false if the index of the hashtable is empty
// Things broke sometimes here but don't know why
if (root == 0) {
return false;
}
// Case-2: if only one index found and that does not contain the string
if (root->getName() != s && root->getNextPointer() == 0) {
return false;
}
// Case-3: Table has one entity and that is the element want to remove
else if (root->getName() == s && root->getNextPointer() == 0) {
root->setName("");
root->setType("");
// No need to set next pointer because it is already 0
return true;
}
// Case-4: Table has a chain and it's first element is the element want to remove
else if (HashTable[index]->getName() == s && HashTable[index]->getNextPointer() != 0) {
temp = HashTable[index];
HashTable[index] = HashTable[index]->getNextPointer();
// show delete message here
temp->setNextPointer(0);
delete temp->getNextPointer();
delete temp;
// at that point return true
return true;
}
// Case-5-7: element may be anywhere in the chain [middle or end]
else {
// keep track for a index and it's next index
p = root;
q = root->getNextPointer();
int positionTracker = 0;
// Check until we reached at the end of the chain
while (q != 0 && q->getName() != s) {
p = q;
q = q->getNextPointer();
positionTracker++;
}
// if the element is found in the middle
if (q != 0) {
p->setNextPointer(q->getNextPointer());
q->setNextPointer(0);
delete q->getNextPointer();
delete q;
// Show delete message and return
return true;
}
// Show message here that value not found at that point
return false;
}
}
void print() {
std::cout << std::endl << "ScopeTable # " << this->getStringifyID() << std::endl;
for (int i = 0; i < size; i++) {
std::cout << i << " --> ";
if (HashTable[i]->getNextPointer() == 0) {
if (HashTable[i]->getName() != "")
std::cout << " < " << HashTable[i]->getName() << " : " << HashTable[i]->getType() << "> ";
} else {
SymbolInfo *temp = HashTable[i];
while (temp != 0) {
std::cout << " < " << temp->getName() << " : " << temp->getType() << "> ";
temp = temp->getNextPointer();
}
}
std::cout << " " << std::endl;
}
std::cout << std::endl;
}
void printModified(FILE *logs) {
fprintf(logs,"ScopeTable # %s\n",this->getStringifyID().c_str());
for (int i = 0; i < size; i++) {
if (HashTable[i]->getNextPointer() == 0) {
if (HashTable[i]->getName() != ""){
fprintf(logs," %d --> ",i);
fprintf(logs,"< %s : %s> ",HashTable[i]->getName().c_str(),HashTable[i]->getType().c_str());
fprintf(logs, "\n");
}
} else {
SymbolInfo *temp = HashTable[i];
fprintf(logs, " %d --> ", i);
while (temp != 0) {
//fprintf(logs," %d --> ",i);
fprintf(logs,"< %s : %s> ",temp->getName().c_str(),temp->getType().c_str());
temp = temp->getNextPointer();
}
fprintf(logs,"\n");
}
}
fprintf(logs,"\n");
}
bool copyOfDeleteFunction(std::string s) {
// Case to Consider:
/*
* 1. Table is empty
* 2. Table has one entity and that does not match with the string
* 3. Table has one entity and that is the element want to remove
* 4. Table has a chain and it's first element is the element want to remove
* 5. Table has a chain and it's middle element is the element want to remove
* 6. Table has a chain but does not contain the element
* 7. Last element of the chain is the element
*/
// Calculate the index to look for
int index = HashFunc(s);
// Initialize a symbol-info pointer to search for the keys
SymbolInfo *root = HashTable[index];
SymbolInfo *temp, *p, *q;
// Case-1: return false if the index of the hashtable is empty
// Things broke sometimes here but don't know why
if (root == 0) {
return false;
}
// Case-2: if only one index found and that does not contain the string
if (root->getName() != s && root->getNextPointer() == 0) {
return false;
}
// Case-3: Table has one entity and that is the element want to remove
else if (root->getName() == s && root->getNextPointer() == 0) {
root->setName("");
root->setType("");
// No need to set next pointer because it is already 0
return true;
}
// Case-4: Table has a chain and it's first element is the element want to remove
else if (HashTable[index]->getName() == s && HashTable[index]->getNextPointer() != 0) {
temp = HashTable[index];
HashTable[index] = HashTable[index]->getNextPointer();
// show delete message
temp->setNextPointer(0);
delete temp->getNextPointer();
delete temp;
// at that point return true
return true;
}
// Case-5-7: element may be anywhere in the chain [middle or end]
else {
// keep track for a index and it's next index
p = root;
q = root->getNextPointer();
int positionTracker = 0;
// Check until we reached at the end of the chain
while (q != 0 && q->getName() != s) {
p = q;
q = q->getNextPointer();
positionTracker++;
}
// if the element is found in the middle
if (q != 0) {
p->setNextPointer(q->getNextPointer());
q->setNextPointer(0);
delete q->getNextPointer();
delete q;
// Show message and return
return true;
}
// Show message that value not found at that point
return false;
}
}
void OutOfScope() {
for (int i = 0; i < size; i++) {
SymbolInfo *temp = HashTable[i];
while (temp->getName() != "") {
copyOfDeleteFunction(temp->getName());
temp = HashTable[i];
}
}
}
~ScopeTable() {
this->OutOfScope();
for (int i = 0; i < size; i++) {
delete HashTable[i]->getNextPointer();
delete HashTable[i];
}
delete[] HashTable;
}
};
#endif //COMPILER_SCOPETABLE_H