Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

BCSE307P

COMPILER DESIGN LAB - 4


SYMBOL TABLE CONSTRUCTOR
Name: Krishna Prasaad
Reg no: 21BRS1714

AIM:

As part of this exercise, you may use any programming language of your choice to
construct a symbol table. The basic operations that should be supported by your
symbol table should include

A) Adding symbols
b) Deleting symbols
c) Searching if a symbol already exists in the table

CODE:

#include <iostream>
using namespace std;
const int MAXIMUM = 100;
class Node
{
string identifier, scope, type;
int Line_Number;
Node *next_Node;

public:
Node()
{
next_Node = NULL;
}
Node(string key, string value, string type, int Line_Number)
{
this->identifier = key;
this->scope = value;
this->type = type;
this->Line_Number = Line_Number;
next_Node = NULL;
}
void print()
{
cout << "Identifier's Name:" << identifier << "\nType:" << type << "\nScope: " << scope << "\nLine
Number: " << Line_Number << endl;
}
friend class SymbolTable;
};
class SymbolTable
{
Node *header[MAXIMUM];

public:
SymbolTable()
{
for (int i = 0; i < MAXIMUM; i++)
header[i] = NULL;
}
int hashf(string id);
bool insertion(string id, string scope, string Type, int Line_Number);
string search(string id);
bool deleteRecord(string id);
bool modify(string id, string scope, string Type, int Line_Number);
};
bool SymbolTable::modify(string id, string s, string t, int l)
{
int index = hashf(id);
Node *start = header[index];
if (start == NULL)
return "-1";
while (start != NULL)
{
if (start->identifier == id)
{
start->scope = s;
start->type = t;
start->Line_Number = l;
return true;
}
start = start->next_Node;
}
return false;
}
bool SymbolTable::deleteRecord(string id)
{
int index = hashf(id);
Node *Temporary_Variable = header[index];
Node *par = header[index];
if (Temporary_Variable == NULL)
{
return false;
}
if (Temporary_Variable->identifier == id && Temporary_Variable->next_Node == NULL)
{
Temporary_Variable->next_Node = NULL;
delete Temporary_Variable;
return true;
}
while (Temporary_Variable->identifier != id && Temporary_Variable->next_Node != NULL)
{
par = Temporary_Variable;
Temporary_Variable = Temporary_Variable->next_Node;
}
if (Temporary_Variable->identifier == id && Temporary_Variable->next_Node != NULL)
{
par->next_Node = Temporary_Variable->next_Node;
Temporary_Variable->next_Node = NULL;
delete Temporary_Variable;
return true;
}
else
{
par->next_Node = NULL;
Temporary_Variable->next_Node = NULL;
delete Temporary_Variable;
return true;
}
return false;
}
string SymbolTable::search(string id)
{
int index = hashf(id);
Node *start = header[index];
if (start == NULL)
return "-1";
while (start != NULL)
{
if (start->identifier == id)
{
start->print();
return start->scope;
}
start = start->next_Node;
}
return "- 1";
}
bool SymbolTable::insertion(string id, string scope, string Type, int Line_Number)
{
int index = hashf(id);
Node *p = new Node(id, scope, Type, Line_Number);
if (header[index] == NULL)
{
header[index] = p;
cout << "\n"
<< id << " insertion";
return true;
}
else
{
Node *start = header[index];
while (start->next_Node != NULL)
start = start->next_Node;
start->next_Node = p;
cout << "\n"
<< id << " insertion";
return true;
}
return false;
}
int SymbolTable::hashf(string id)
{
int asciiSum = 0;
for (int i = 0; i < id.length(); i++)
{
asciiSum = asciiSum + id[i];
}
return (asciiSum % 100);
}
int main()
{
SymbolTable st;
string check;
cout << "**** SYMBOL_TABLE ****\n";
int ch = 1;
cout << "1.Insert \n2.Search\n3.Delete\n4.Exit\n";
while (ch != 0)
{
cout << "\nSelect Choice:";
cin >> ch;
if (ch == 1)
{
string x;
cout << "\nGive local keyword name to insert:";
cin >> x;
if (st.insertion(x, "local", "keyword", 4))
cout << " -successfully";
else
cout << "\nFailed to insertion.\n";
}
else if (ch == 2)
{
string x;
cout << "\nGive local keyword name to Search:";
cin >> x;
check = st.search(x);
if (check != "-1")
cout << "Identifier Is present\n";
else
cout << "\nIdentifier Not Present\n";
}
else if (ch == 3)
{
string x;
cout << "\nGive local keyword name to be deleted:";
cin >> x;
if (st.deleteRecord(x))
cout << "if Identifier is deleted\n";
else
cout << "\nFailed to delete\n";
}
else
{
cout << "INVALID / END OP";
break;
}
}
return 0;
}

Output:

You might also like