Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class SymbolInfo
- {
- string name;
- string type;
- string name_type; /// name_type -> int / float/ double / array ----- for variables
- int arrLen;
- string return_type;
- ///POINTER OF THIS CLASS TO RESOLVE COLLISION DURING HASHING
- public:
- vector<SymbolInfo*> func_param;
- vector<string> func_param_type;
- SymbolInfo(string name, string type){
- this->name = name;
- this->type = type;
- arrLen=-1;
- name_type="";
- return_type="";
- }
- SymbolInfo(){
- ///no code here
- }
- void setName(string x) ///SETTER
- {
- name = x;
- }
- void setArrLen(int x)
- {
- arrLen = x;
- }
- void setType(string x)
- {
- type = x;
- }
- void setNameType(string x)
- {
- name_type = x;
- }
- void setReturnType(string x)
- {
- return_type = x;
- }
- string getName() ///GETTER
- {
- return name;
- }
- string getType()
- {
- return type;
- }
- string getNameType()
- {
- return name_type;
- }
- int getArrLen()
- {
- return arrLen;
- }
- string getReturnType()
- {
- return return_type;
- }
- };
- class ScopeTable
- {
- ScopeTable *parentScope;
- int bucket_size,tot_child;
- int hash_index,pos_found;
- vector<vector<SymbolInfo>> bucket;
- string address = "";
- public:
- ScopeTable(int x)
- {
- bucket_size = x;
- tot_child=0;
- parentScope=nullptr;
- address="1";
- bucket.resize(x);
- }
- ~ScopeTable(){
- bucket.clear();
- bucket.resize(0);
- }
- ScopeTable* getparentScope(){
- return this->parentScope;
- }
- void setparentScope(ScopeTable* sc){
- this->parentScope = sc;
- }
- int Hash_Function(string name, int x)
- {
- int sum = 0;
- for(auto &i:name)
- {
- sum+=i;
- }
- return sum%x;
- }
- bool Insert(string name, string type)
- {
- int index = Hash_Function(name,bucket_size);
- for(auto i=0; i<bucket[index].size(); ++i)
- {
- if( bucket[index][i].getName().compare(name) == 0 ) ///CHECK IF TWO STRINGS ARE EQUAL
- {
- return false; ///UNSUCCESSFUL INSERTION
- }
- }
- SymbolInfo si;
- si.setName(name);
- si.setType(type);
- bucket[index].push_back(si);
- hash_index = index;
- return true; ///SUCCESSFUL INSERTION
- }
- bool Insert(string name, string type, vector<SymbolInfo*> vect, string ret_Type, vector<string> param_type)
- {
- int index = Hash_Function(name,bucket_size);
- for(auto i=0; i<bucket[index].size(); ++i)
- {
- if( bucket[index][i].getName().compare(name) == 0 ) ///CHECK IF TWO STRINGS ARE EQUAL
- {
- return false; ///UNSUCCESSFUL INSERTION
- }
- }
- SymbolInfo si;
- si.setName(name);
- si.setType(type);
- si.setReturnType(ret_Type);
- si.func_param = vect;
- si.func_param_type = param_type;
- bucket[index].push_back(si);
- hash_index = index;
- return true; ///SUCCESSFUL INSERTION
- }
- void printScope(ofstream &outL)
- {
- outL<<"ScopeTable #"<<address<<"\n";
- for(int i=0; i<bucket_size; i++)
- {
- if(!bucket[i].empty()){
- outL<<i<<" -->";
- for(auto it=bucket[i].begin(); it!=bucket[i].end(); it++)
- {
- outL<<" <"<<it->getName()<<" : "<<it->getType()<<">";
- }
- outL<<"\n";
- }
- }
- outL<<endl;
- }
- SymbolInfo* LookUp(string val)
- {
- int idx = Hash_Function(val,bucket_size);
- for(int i=0; i<bucket[idx].size(); i++)
- {
- if( bucket[idx][i].getName().compare(val) == 0 )
- {
- pos_found = i;
- //outL<<"Found in ScopeTable# "<<address<<" "<<" at position "<<idx<<", "<<pos_found<<endl<<endl;
- return &bucket[idx][i];
- }
- }
- return NULL;
- }
- bool deleteSymbol(string sign)
- {
- int idx = Hash_Function(sign,bucket_size);
- int counter = 0;
- for(auto it=bucket[idx].begin() ; it!=bucket[idx].end() ; it++)
- {
- if(it->getName().compare(sign) == 0)
- {
- pos_found=counter;
- bucket[idx].erase(it);
- return true;
- }
- counter++;
- }
- return false;
- }
- string getScopeAddress()
- {
- string str = address;
- return address;
- }
- void setScopeAddress(string parent_address,int x) ///x / tot_child
- {
- string str = parent_address;
- int q = x;
- if(parent_address.compare("")==0){
- address="1";
- }
- else{
- address = parent_address + "." + to_string(x);
- }
- }
- void incChild()
- {
- tot_child++;
- }
- int getChild()
- {
- return tot_child;
- }
- string getHashIndex()
- {
- int len = bucket[hash_index].size() - 1;
- string str = to_string(hash_index)+", "+to_string(len);
- return str;
- }
- };
- class SymbolTable
- {
- ScopeTable *currentScope=nullptr;
- int bucketSize, hash_index;
- vector<ScopeTable> scopes;
- public:
- SymbolTable(int x)
- {
- bucketSize = x;
- currentScope = new ScopeTable(x);
- }
- SymbolTable(int x, string outputFile)
- {
- //outL(outputFile);
- bucketSize = x;
- currentScope = new ScopeTable(x);
- }
- ~SymbolTable(){
- delete currentScope;
- }
- void printCurrentScope(ofstream &outL)
- {
- currentScope->printScope(outL);
- }
- void printAllScope(ofstream &outL)
- {
- ScopeTable* tempScope = currentScope;
- while(tempScope != nullptr){
- tempScope->printScope(outL);
- tempScope = tempScope->getparentScope();
- }
- }
- bool Insert(string x, string y)
- {
- if(currentScope->Insert(x,y))
- {
- //outL<<"Inserted in ScopeTable# "<<currentScope->getScopeAddress()<<" at position "<<currentScope->getHashIndex()<<"\n\n";
- return true;
- }
- else
- {
- return false;
- }
- }
- bool Insert(string x, string y, vector<SymbolInfo*> vect, string ret_Type, vector<string> param_type)
- {
- if(currentScope->Insert(x,y,vect,ret_Type, param_type))
- {
- //outL<<"Inserted in ScopeTable# "<<currentScope->getScopeAddress()<<" at position "<<currentScope->getHashIndex()<<"\n\n";
- return true;
- }
- else
- {
- return false;
- }
- }
- void EnterScope()
- {
- ScopeTable* current = new ScopeTable(bucketSize);
- currentScope->incChild();
- int val = currentScope->getChild();
- if(this->currentScope == nullptr){
- this->currentScope = current;
- currentScope->setScopeAddress(currentScope->getScopeAddress(),currentScope->getChild());
- currentScope->setparentScope(nullptr);
- }
- else{
- current->setScopeAddress(currentScope->getScopeAddress(),currentScope->getChild());
- current->setparentScope(currentScope);
- this->currentScope=current;
- //outL<<"ScopeTable with id "<<currentScope->getScopeAddress()<<" created\n\n";
- }
- }
- void ExitScope()
- {
- int x = currentScope->getChild();
- //outL<<"ScopeTable with id "<<currentScope->getScopeAddress()<<" removed\n\n";
- if(currentScope->getparentScope()!=nullptr){
- currentScope = currentScope->getparentScope();
- }
- }
- bool removeSymbol(string sign)
- {
- SymbolInfo *temp = currentScope->LookUp(sign);
- if(temp != NULL)
- {
- currentScope->deleteSymbol(sign);
- return true;
- }
- else
- {
- return false;
- }
- }
- SymbolInfo* LookUp(string name)
- {
- SymbolInfo *temp = currentScope->LookUp(name);
- if( temp == NULL )
- {
- int range = scopes.size();
- for(int i=range-1; i>=0; i--)
- {
- temp = scopes[i].LookUp(name);
- if( temp != NULL )
- {
- return temp;
- }
- }
- return NULL;
- }
- else{
- return temp;
- }
- }
- string getAddress()
- {
- return currentScope->getScopeAddress();
- }
- string getPosition()
- {
- return ""; ///HAVE TO WORK HERE
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment