-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.cpp
626 lines (523 loc) · 14.4 KB
/
compiler.cpp
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/**
* @file compiler.cpp
* @author Zahidul Islam (link)
* @brief
* @version 0.1
* @date 2022-05-29
*
* @copyright Copyright (c) 2022
*
* # for reading from file provide filename as command line argument
*
* # Instructions
* # I for Insert
* # L for Lookup
* # D for Delete
* # P for Print
* # S for New Scope
* # E for Exit Current Scope
* # A for All
* # C for Current
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include "LineParser.h"
#include "Utility.h"
using namespace std;
int idTracker[32] = {0}; // let's assume that the doubly linked list size will not exceed 32!
// ==========================================
// class containing actual symbole/token info
// ==========================================
class SymbolInfo
{
private:
string name;
string type;
public:
SymbolInfo *next; // SymbolInfo next pointer for chaining
SymbolInfo()
{
// pass
}
SymbolInfo(string name, string type)
{
setName(name);
setType(type);
this->next = NULL;
}
void setName(string name)
{
this->name = name;
}
void setType(string type)
{
this->type = type;
}
string getName()
{
return this->name;
}
string getType()
{
return this->type;
}
};
// ============================================
// hashmap of multiple SymbolInfo in same scope
// ============================================
class ScopeTable
{
public:
static int tableCount;
string id;
SymbolInfo **hashTable;
ScopeTable *child; // for maintaining child, like next link
ScopeTable *parent; // for maintaining parent, like prev link
int TABLE_SIZE;
ScopeTable(int n)
{
TABLE_SIZE = n;
hashTable = new SymbolInfo*[TABLE_SIZE];
for (int i = 0; i < n; i++)
{
hashTable[i] = NULL;
}
child = NULL;
parent = NULL;
id = to_string(++tableCount);
}
ScopeTable *getRoot()
{
ScopeTable *cursor;
cursor = this;
while (cursor->parent != NULL)
{
cursor = cursor->parent;
}
return cursor;
}
string getId()
{
string longId;
if (tableCount == 1)
{
return to_string(tableCount);
}
else if (tableCount == 2)
{
return to_string(1) + "." + to_string(1);
}
else if (tableCount == 3)
{
return to_string(1) + "." + to_string(1) + "." + to_string(1);
}
int i=0;
while (true)
{
if(idTracker[i+1] == 0)
{
longId += to_string(idTracker[i]);
break;
}
longId += to_string(idTracker[i]) + ".";
i++;
}
return longId;
}
bool insert(SymbolInfo *symbolInfo)
{
string symbolName = symbolInfo->getName();
int index = hashf(symbolName, TABLE_SIZE);
if(hashTable[index] == NULL)
{
symbolInfo->next = hashTable[index];
hashTable[index] = symbolInfo;
string scopeNo = getId();
cout<<"Inserted in ScopeTable #"<< scopeNo <<" at position "<< index <<", 0"<<endl;
return true;
}
else
{
int chainNo = 0;
SymbolInfo *cursor;
cursor = hashTable[index]; // getting the head of the current chain
while (cursor->next != NULL)
{
string currentName = cursor->getName();
if(currentName.compare(symbolName) == 0)
{
cout<< symbolName <<" already exists in current ScopeTable"<<endl;
return false;
}
cursor = cursor->next;
chainNo++;
}
cursor->next = symbolInfo;
string scopeNo = getId();
cout<<"Inserted in ScopeTable #"<< scopeNo <<" at position "<< index <<", "<< chainNo <<endl;
return true;
}
}
SymbolInfo *lookup(string name)
{
int index = hashf(name, TABLE_SIZE);
string scopeNo = getId();
if(hashTable[index] == NULL)
{
cout<<"Not Found in ScopeTable #"<< scopeNo <<endl;
return NULL;
}
SymbolInfo *cursor;
cursor = hashTable[index];
int chainNo = 0;
while (cursor != NULL)
{
string currentName = cursor->getName();
if(currentName.compare(name) == 0)
{
cout<<"Found in ScopeTable #"<< scopeNo <<" at position "<< index <<", "<<chainNo<<endl;
return cursor;
}
cursor = cursor->next;
chainNo++;
}
return NULL;
}
bool remove(string name)
{
if(lookup(name) == NULL)
{
cout<<name<<" could not be deleted from current ScopeTable"<<endl;
return false;
}
int index = hashf(name, TABLE_SIZE);
// for matching of 1st node
SymbolInfo *cursor;
cursor = hashTable[index];
string cursorName = cursor->getName();
if(cursorName.compare(name) == 0)
{
hashTable[index] = cursor->next;
cout<<"Deleted Entry "<< index <<", 0" <<" from current ScopeTable"<<endl;
cursor = NULL;
return true;
}
// for matching from 2nd node to last
SymbolInfo *current, *prev;
current = hashTable[index];
prev = hashTable[index];
int chainNo = 0;
while (current->next != NULL)
{
prev = current;
current = current->next;
chainNo++;
string currentName = current->getName();
if(currentName.compare(name) == 0)
{
prev->next = current->next;
current->next = NULL;
cout<<"Deleted Entry "<< index <<", 0"<< chainNo <<" from current ScopeTable"<<endl;
return true;
}
}
return true;
}
void print()
{
cout<<endl;
cout<<"# scope table id = "<< this->getId() <<endl;
for(int i=0; i<TABLE_SIZE; i++)
{
if (hashTable[i] == NULL)
{
printf("\t%i\t---\n", i);
}
else
{
printf("\t%i\t",i);
SymbolInfo *cursor = hashTable[i];
while (cursor != NULL)
{
string name = cursor->getName();
string type = cursor->getType();
printf("<%s,%s> ", name.c_str(), type.c_str());
cursor = cursor->next;
}
printf("\n");
}
}
printf("\n");
}
};
int ScopeTable::tableCount = 0;
// ============================================================
// linked list of multiple ScopeTable, using doubly linked list
// ============================================================
class SymbolTable
{
public:
ScopeTable *head;
SymbolTable()
{
head = NULL;
}
int getSymbolTableLength()
{
int length = 0;
ScopeTable *cursor;
cursor = head;
if (cursor == NULL)
{
return 1;
}
while (cursor->child != NULL)
{
cursor = cursor->child;
length++;
}
return length;
}
ScopeTable *getHead() // head of doubly linked list
{
return head;
}
ScopeTable *getTail() // tail of doubly linked list
{
ScopeTable *cursor;
cursor = head;
while (cursor->child != NULL)
{
cursor = cursor->child;
}
return cursor;
}
void append(ScopeTable *newNode) // appedning new ScopeTable at the tail of doubly linked list (SymbolTable)
{
int length;
int count = 0;
if(head == NULL)
{
head = newNode;
string scopeNo = head->getId();
cout<<"New ScopeTable with id "<< scopeNo <<" created"<<endl;
length = getSymbolTableLength();
idTracker[0] = 1;
return;
}
ScopeTable *cursor;
cursor = head;
while (cursor->child != NULL)
{
cursor = cursor->child;
}
cursor->child = newNode;
newNode->parent = cursor;
string scopeNo = newNode->getId();
cout<<"New ScopeTable with id "<< scopeNo <<" created"<<endl;
length = getSymbolTableLength();
idTracker[length] += 1;
}
SymbolInfo *lookup(string name)
{
SymbolInfo *temp = NULL;
ScopeTable *cursor;
cursor = head;
// searching in every ScopeTable
while(cursor != NULL)
{
temp = cursor->lookup(name);
if(temp != NULL)
{
return temp;
}
cursor = cursor->child;
}
return NULL;
}
bool remove(string name)
{
bool removed = false;
ScopeTable *cursor;
cursor = head;
while(cursor != NULL)
{
removed = cursor->remove(name);
if(removed == true)
{
break;
}
cursor = cursor->child;
}
return removed;
}
void printCurrentScopeTable()
{
ScopeTable *currentScopeTable = this->getTail();
currentScopeTable->print();
}
void printAllScopeTable()
{
if(head == NULL)
{
return;
}
ScopeTable *cursor;
cursor = head;
while (cursor != NULL)
{
cursor->print();
cursor = cursor->child;
}
}
ScopeTable *deleteCurrentScopeTable(ScopeTable *currentScopeTable)
{
ScopeTable *root;
ScopeTable *cursor;
root = cursor = this->getHead();
// of current scope table is the only remaining scope table, then just delete it
if(currentScopeTable == root)
{
// currentScopeTable = NULL; // deletion causing segmentation fault
return currentScopeTable;
}
while (cursor->child != currentScopeTable)
{
cursor = cursor->child;
}
string scopeNo = currentScopeTable->getId();
cout<<"ScopeTable with id "<< scopeNo <<" removed"<<endl;
currentScopeTable = cursor;
delete(cursor->child);
cursor->child = NULL;
return currentScopeTable;
}
ScopeTable *resetCurrentScopeTable()
{
// pass
return NULL;
}
};
// =======================================================
// ==================== Main Function ====================
// =======================================================
// accepts command line argument
// no command line argument = console i/o
// command line argument = file i/o
int main(int argc, char const *argv[])
{
bool console = true;
string fileName;
int m; // bucket size
fstream fin;
string text;
int choice;
if(argc == 1)
{
cin>>m;
cin.ignore();
}
else if(argc == 2)
{
fileName = argv[1];
fin.open(fileName); // opening file
getline(fin, text); // reading first line
// m = atoi(text.c_str()); // extracting m
m = stoi(text); // extracting m
console = false;
}
SymbolInfo *symbolInfo = NULL;
ScopeTable *currentScopeTable = NULL;
SymbolTable *symbolTable = NULL;
vector<string> words;
currentScopeTable = new ScopeTable(m);
symbolTable = new SymbolTable();
symbolTable->append(currentScopeTable); // appending as 1st node of doubly linked list
while(true)
{
if(console == true)
{
getline(cin, text);
LineParser<string> parser(text, ' ');
words = parser.parseLineToString();
choice = to_ascii(words.front());
}
else if(console == false)
{
getline(fin, text);
words = tokenizeText(text);
choice = to_ascii(words.front());
if(fin.eof() != 0) break;
}
switch (choice)
{
case I: // ascii value of I, insert
{
string name = words.at(1);
string type = words.at(2);
if(currentScopeTable->lookup(name) != NULL) break;
symbolInfo = new SymbolInfo(name, type);
currentScopeTable->insert(symbolInfo);
break;
}
case L: // ascii value of L, lookup
{
string name;
name = words.at(1);
symbolTable->lookup(name);
currentScopeTable = symbolTable->getTail();
break;
}
case D: // ascii value of D, delete
{
string name;
name = words.at(1);
symbolTable->remove(name);
currentScopeTable = symbolTable->getTail();
break;
}
case P: // ascii value of P, print symbol table
{
string instruction = words.at(1);
if (instruction.compare("A") == 0)
{
symbolTable->printAllScopeTable();
}
else if (instruction.compare("C") == 0)
{
// currentScopeTable->print();
symbolTable->printCurrentScopeTable();
}
break;
}
case S: // ascii value of S, entering new scope
{
currentScopeTable = new ScopeTable(m);
symbolTable->append(currentScopeTable);
break;
}
case E: // ascii value of E, entering current scope
{
currentScopeTable = symbolTable->deleteCurrentScopeTable(currentScopeTable);
break;
}
case Q: // ascii value of Q, quit
cout<<"# Quit Program" <<endl;
cout<< "~TERMINATED" <<endl;
exit(0);
break;
case NEWLINE:
case SPACE:
continue;
default:
break;
}
}
fin.close();
return 0;
}