C++ Program for implementation of Symbol Table using Hashing
#include <iostream>
#include <cstring>
using namespace std;
const int MAX=10;
class dictionary;
class node
{
string identifier,scope,type;
int lineNo;
node *next;
public:
friend class SymbolTable;
node()
{
next=NULL;
}
node(string key,string value,string type,int lineNo)
{
this->identifier=key;
this->scope=value;
this->type=type;
this->lineNo=lineNo;
next=NULL;
}
};
class SymbolTable
{
node *head[MAX];
public:
SymbolTable()
{
for(int i=0;i<MAX;i++)
head[i]=NULL;
}
int hashf(string id);
void insert(string id,string scope,string Type,int lineno);
void find(string id);
bool deleteRecord(string id);
void modify(string id);
void display();
};
int SymbolTable::hashf(string id)
{
int asciiSum=0;
for(int i=0;i<id.length();i++)
{
asciiSum=asciiSum+id[i];
}
return (asciiSum%10);
}
void SymbolTable::insert(string id,string scope,string Type,int lineno)
{
int index=hashf(id);
node *p=new node(id,scope,Type,lineno);
if(head[index]==NULL)
{
head[index]=p;
}
else
{
node *start=head[index];
while(start->next!=NULL)
start=start->next;
start->next=p;
}
cout<<endl<<id<<" inserted into Symbol Table.";
}
void SymbolTable:: display()
{
cout<<"\nIndex\tID\tScope\tType\tLineno";
for(int i=0;i<10;i++)
{
node *start=head[i];
if(start==NULL)
cout<<"\n";
while(start!=NULL)
{
cout<<"\n"<<i<<" :\t"<<start->identifier<<"\t"<<start->scope<<"\t"<<start->type<<"\t"<<start->lineNo;
start=start->next;
}
}
}
void SymbolTable::find(string id)
{
int index=hashf(id);
int flag=0;
node *start=head[index];
while(start!=NULL)
{
if(start->identifier==id)
{
//start->print();
flag=1;
break;
}
start=start->next;
}
if(flag==1)
cout<<"Id Is present.";
else
cout<<"Id Is not present.";
}
void SymbolTable::modify(string id)
{
int index=hashf(id);
node *start=head[index];
int flag=0;
while(start!=NULL)
{
if(start->identifier==id)
{
cout<<id<<" is present\n";
cout<<"Enter Scope: ";
cin>>(start->scope);
cout<<"Enter Type: ";
cin>>(start->type);
cout<<"Enter Line Number: ";
cin>>(start->lineNo);
flag=1;
}start=start->next;
}
if(flag==1)
cout<<"record modified";
else
cout<<"No record modified";
}
bool SymbolTable::deleteRecord(string id)
{
int index=hashf(id);
node *tmp=head[index];
node *par=head[index];
if(tmp==NULL) //if no identifier is present at that index
{
return false;
}
if(tmp->identifier==id && tmp->next==NULL)//only one identifier is present
{
head[index]=NULL;
delete tmp;
return true;
}
//tmp=tmp->next;
while(tmp->identifier!=id && tmp->next!=NULL)
{
par=tmp;
tmp=tmp->next;
}
if(tmp->identifier==id&&tmp->next!=NULL)
{
if(par-> identifier ==tmp-> identifier)
{
head[index]=tmp->next;
}
else
{
par->next=tmp->next;
tmp->next=NULL;
}
delete tmp;
return true;
}
else //delete at end
{
par->next=NULL;
tmp->next=NULL;
delete tmp;
return true;
}
return false;
}
int main() {
SymbolTable st;
int choice,lineno;
string id,scope,type;
do
{
cout<<"\n**** SYMBOL_TABLE ****\n"
<<"1.Insert Identifier\n"
<<"2.Find Identifier\n"
<<"3.Delete identifier\n"
<<"4.Modify attributes\n"
<<"5.Display\n"
<<"Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter Identifer's Name: ";
cin>>id;
cout<<"Enter Scope: ";
cin>>scope;
cout<<"Enter Type: ";
cin>>type;
cout<<"Enter Line Number: ";
cin>>lineno;
st.insert(id,scope,type,lineno);
break;
case 2:
cout<<"Enter Identifier to Search: ";
cin>>id;
st.find(id);
break;
case 3:
cout<<"Enter Identifier to Delete: ";
cin>>id;
if(st.deleteRecord(id))
cout<<" Identifier's Record is deleted.";
else
{
cout<<"\nFailed to delete "<<id;
}
break;
case 4:
cout<<"Enter Identifier to Modify: ";
cin>>id;
st.modify(id);
break;
case 5:
cout<<"***Symbol Table***";
st.display();
break;
default:
cout<<"\nWrong Choice.";
}
}while(choice!=0);
return 0;
}
output:
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :1
Enter Identifer's Name: abc
Enter Scope: a
Enter Type: a
Enter Line Number: 1
abc inserted into Symbol Table.
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :1
Enter Identifer's Name: bca
Enter Scope: b
Enter Type: b
Enter Line Number: 2
bca inserted into Symbol Table.
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :1
Enter Identifer's Name: cba
Enter Scope: c
Enter Type: c
Enter Line Number: 2
cba inserted into Symbol Table.
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :5
***Symbol Table***
Index ID Scope Type Lineno
4 : abc a a 1
4 : bca b b 2
4 : cba c c 2
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :3
Enter Identifier to Delete: bca
Identifier's Record is deleted.
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :5
***Symbol Table***
Index ID Scope Type Lineno
4 : abc a a 1
4 : cba c c 2
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :3
Enter Identifier to Delete: cba
Identifier's Record is deleted.
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :5
***Symbol Table***
Index ID Scope Type Lineno
4 : abc a a 1
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :4
Enter Identifier to Modify: abc
abc is present
Enter Scope: a
Enter Type: a
Enter Line Number: 3
record modified
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :5
***Symbol Table***
Index ID Scope Type Lineno
4 : abc a a 3
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :3
Enter Identifier to Delete: abc
Identifier's Record is deleted.
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :5
***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***Symbol Table***
Index ID Scope Type Lineno
**** SYMBOL_TABLE ****
1.Insert Identifier
2.Find Identifier
3.Delete identifier
4.Modify attributes
5.Display
Enter Your Choice :***S
No comments:
Post a Comment