muhammad_nasif

symbolTable.cpp

Jun 6th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.17 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class SymbolInfo
  7. {
  8.     string name;
  9.     string type;
  10.     string name_type;       /// name_type -> int / float/ double / array ----- for variables
  11.     int arrLen;
  12.     string return_type;
  13.                    
  14.     ///POINTER OF THIS CLASS TO RESOLVE COLLISION DURING HASHING
  15.  
  16. public:
  17.    
  18.     vector<SymbolInfo*> func_param;
  19.     vector<string>  func_param_type;
  20.  
  21.  
  22.     SymbolInfo(string name, string type){
  23.         this->name = name;
  24.         this->type = type;
  25.         arrLen=-1;
  26.         name_type="";
  27.         return_type="";
  28.     }
  29.     SymbolInfo(){  
  30.     ///no code here
  31.     }
  32.    
  33.     void setName(string x)            ///SETTER
  34.     {
  35.         name = x;
  36.     }
  37.     void setArrLen(int x)
  38.     {
  39.         arrLen = x;
  40.     }
  41.     void setType(string x)
  42.     {
  43.         type = x;
  44.     }
  45.     void setNameType(string x)
  46.     {
  47.         name_type = x;
  48.     }
  49.     void setReturnType(string x)
  50.     {
  51.         return_type = x;
  52.     }
  53.    
  54.     string getName()                  ///GETTER
  55.     {
  56.         return name;
  57.     }
  58.     string getType()
  59.     {
  60.         return type;
  61.     }
  62.     string getNameType()
  63.     {
  64.         return name_type;
  65.     }
  66.     int getArrLen()
  67.     {
  68.         return arrLen;
  69.     }
  70.     string getReturnType()
  71.     {
  72.         return return_type;
  73.     }
  74. };
  75.  
  76. class ScopeTable
  77. {
  78.     ScopeTable *parentScope;
  79.     int bucket_size,tot_child;
  80.     int hash_index,pos_found;
  81.     vector<vector<SymbolInfo>> bucket;
  82.     string address = "";
  83. public:
  84.     ScopeTable(int x)
  85.     {
  86.         bucket_size = x;
  87.         tot_child=0;
  88.         parentScope=nullptr;
  89.         address="1";
  90.         bucket.resize(x);
  91.     }
  92.     ~ScopeTable(){
  93.         bucket.clear();
  94.         bucket.resize(0);
  95.     }
  96.     ScopeTable* getparentScope(){
  97.         return this->parentScope;
  98.     }
  99.     void setparentScope(ScopeTable* sc){
  100.         this->parentScope = sc;
  101.     }
  102.     int Hash_Function(string name, int x)
  103.     {
  104.         int sum = 0;
  105.         for(auto &i:name)
  106.         {
  107.             sum+=i;
  108.         }
  109.         return sum%x;
  110.     }
  111.     bool Insert(string name, string type)
  112.     {
  113.         int index = Hash_Function(name,bucket_size);
  114.         for(auto i=0; i<bucket[index].size(); ++i)
  115.         {
  116.             if( bucket[index][i].getName().compare(name) == 0 )                    ///CHECK IF TWO STRINGS ARE EQUAL
  117.             {
  118.                 return false;                                                      ///UNSUCCESSFUL INSERTION
  119.             }
  120.         }
  121.         SymbolInfo si;
  122.         si.setName(name);
  123.         si.setType(type);
  124.         bucket[index].push_back(si);
  125.         hash_index = index;
  126.         return true;                                                               ///SUCCESSFUL INSERTION
  127.     }
  128.     bool Insert(string name, string type, vector<SymbolInfo*> vect, string ret_Type, vector<string> param_type)
  129.     {
  130.         int index = Hash_Function(name,bucket_size);
  131.         for(auto i=0; i<bucket[index].size(); ++i)
  132.         {
  133.             if( bucket[index][i].getName().compare(name) == 0 )                    ///CHECK IF TWO STRINGS ARE EQUAL
  134.             {
  135.                 return false;                                                      ///UNSUCCESSFUL INSERTION
  136.             }
  137.         }
  138.         SymbolInfo si;
  139.         si.setName(name);
  140.         si.setType(type);
  141.         si.setReturnType(ret_Type);
  142.         si.func_param = vect;
  143.         si.func_param_type = param_type;
  144.         bucket[index].push_back(si);
  145.         hash_index = index;
  146.         return true;                                                               ///SUCCESSFUL INSERTION
  147.     }
  148.     void printScope(ofstream &outL)
  149.     {
  150.         outL<<"ScopeTable #"<<address<<"\n";
  151.         for(int i=0; i<bucket_size; i++)
  152.         {
  153.             if(!bucket[i].empty()){
  154.                 outL<<i<<" -->";
  155.  
  156.  
  157.                 for(auto it=bucket[i].begin(); it!=bucket[i].end(); it++)
  158.                 {
  159.                     outL<<" <"<<it->getName()<<" : "<<it->getType()<<">";
  160.                 }
  161.                 outL<<"\n";
  162.  
  163.             }
  164.  
  165.         }
  166.         outL<<endl;
  167.     }
  168.  
  169.     SymbolInfo* LookUp(string val)
  170.     {
  171.         int idx = Hash_Function(val,bucket_size);
  172.         for(int i=0; i<bucket[idx].size(); i++)
  173.         {
  174.             if( bucket[idx][i].getName().compare(val) == 0 )
  175.             {
  176.                 pos_found = i;
  177.                 //outL<<"Found in ScopeTable# "<<address<<" "<<" at position "<<idx<<", "<<pos_found<<endl<<endl;
  178.                 return &bucket[idx][i];
  179.             }
  180.         }
  181.  
  182.         return NULL;
  183.     }
  184.  
  185.     bool deleteSymbol(string sign)
  186.     {
  187.         int idx = Hash_Function(sign,bucket_size);
  188.         int counter = 0;
  189.         for(auto it=bucket[idx].begin() ; it!=bucket[idx].end() ; it++)
  190.         {
  191.             if(it->getName().compare(sign) == 0)
  192.             {
  193.                 pos_found=counter;
  194.                 bucket[idx].erase(it);
  195.                
  196.                 return true;
  197.             }
  198.             counter++;
  199.         }
  200.         return false;
  201.     }
  202.  
  203.  
  204.     string getScopeAddress()
  205.     {
  206.         string str = address;
  207.         return address;
  208.     }
  209.     void setScopeAddress(string parent_address,int x)   ///x / tot_child
  210.     {
  211.         string str = parent_address;
  212.         int q = x;
  213.         if(parent_address.compare("")==0){
  214.             address="1";
  215.         }
  216.         else{
  217.             address = parent_address + "." + to_string(x);
  218.         }
  219.  
  220.     }
  221.     void incChild()
  222.     {
  223.         tot_child++;
  224.     }
  225.     int getChild()
  226.     {
  227.         return tot_child;
  228.     }
  229.     string getHashIndex()
  230.     {
  231.         int len = bucket[hash_index].size() - 1;
  232.         string str = to_string(hash_index)+", "+to_string(len);
  233.         return str;
  234.     }
  235.  
  236. };
  237.  
  238.  
  239.  
  240. class SymbolTable
  241. {
  242.     ScopeTable *currentScope=nullptr;
  243.  
  244.     int bucketSize, hash_index;
  245.     vector<ScopeTable> scopes;
  246. public:
  247.     SymbolTable(int x)
  248.     {
  249.  
  250.         bucketSize = x;
  251.         currentScope = new ScopeTable(x);
  252.     }
  253.     SymbolTable(int x, string outputFile)
  254.     {
  255.     //outL(outputFile);
  256.         bucketSize = x;
  257.         currentScope = new ScopeTable(x);
  258.     }
  259.  
  260.     ~SymbolTable(){
  261.         delete currentScope;
  262.     }
  263.     void printCurrentScope(ofstream &outL)
  264.     {
  265.         currentScope->printScope(outL);
  266.     }
  267.  
  268.     void printAllScope(ofstream &outL)
  269.     {
  270.         ScopeTable* tempScope = currentScope;
  271.         while(tempScope != nullptr){
  272.             tempScope->printScope(outL);
  273.             tempScope = tempScope->getparentScope();
  274.         }
  275.     }
  276.     bool Insert(string x, string y)
  277.     {
  278.         if(currentScope->Insert(x,y))
  279.         {
  280.             //outL<<"Inserted in ScopeTable# "<<currentScope->getScopeAddress()<<" at position "<<currentScope->getHashIndex()<<"\n\n";
  281.             return true;
  282.         }
  283.         else
  284.         {
  285.             return false;
  286.         }
  287.  
  288.     }
  289.    
  290.    
  291.     bool Insert(string x, string y, vector<SymbolInfo*> vect, string ret_Type, vector<string> param_type)
  292.     {
  293.         if(currentScope->Insert(x,y,vect,ret_Type, param_type))
  294.         {
  295.             //outL<<"Inserted in ScopeTable# "<<currentScope->getScopeAddress()<<" at position "<<currentScope->getHashIndex()<<"\n\n";
  296.             return true;
  297.         }
  298.         else
  299.         {
  300.             return false;
  301.         }
  302.  
  303.     }
  304.     void EnterScope()
  305.     {
  306.  
  307.         ScopeTable* current = new ScopeTable(bucketSize);
  308.         currentScope->incChild();
  309.         int val = currentScope->getChild();
  310.         if(this->currentScope == nullptr){
  311.             this->currentScope = current;
  312.             currentScope->setScopeAddress(currentScope->getScopeAddress(),currentScope->getChild());
  313.  
  314.             currentScope->setparentScope(nullptr);
  315.         }
  316.         else{
  317.             current->setScopeAddress(currentScope->getScopeAddress(),currentScope->getChild());
  318.             current->setparentScope(currentScope);
  319.             this->currentScope=current;
  320.             //outL<<"ScopeTable with id "<<currentScope->getScopeAddress()<<" created\n\n";
  321.  
  322.  
  323.         }
  324.     }
  325.     void ExitScope()
  326.     {
  327.         int x = currentScope->getChild();
  328.         //outL<<"ScopeTable with id "<<currentScope->getScopeAddress()<<" removed\n\n";
  329.  
  330.         if(currentScope->getparentScope()!=nullptr){
  331.             currentScope = currentScope->getparentScope();
  332.         }
  333.     }
  334.  
  335.     bool removeSymbol(string sign)
  336.     {
  337.         SymbolInfo *temp = currentScope->LookUp(sign);
  338.         if(temp != NULL)
  339.         {
  340.             currentScope->deleteSymbol(sign);
  341.             return true;
  342.         }
  343.         else
  344.         {
  345.             return false;
  346.         }
  347.     }
  348.  
  349.     SymbolInfo* LookUp(string name)
  350.     {
  351.         SymbolInfo *temp =  currentScope->LookUp(name);
  352.  
  353.         if( temp == NULL )
  354.         {
  355.             int range = scopes.size();
  356.  
  357.             for(int i=range-1; i>=0; i--)
  358.             {
  359.                 temp = scopes[i].LookUp(name);
  360.                 if( temp != NULL )
  361.                 {
  362.                     return temp;
  363.                 }
  364.             }
  365.             return NULL;
  366.  
  367.         }
  368.         else{
  369.             return temp;
  370.         }
  371.     }
  372.     string getAddress()
  373.     {
  374.         return currentScope->getScopeAddress();
  375.     }
  376.     string getPosition()
  377.     {
  378.         return "";  ///HAVE TO WORK HERE
  379.     }
  380.  
  381. };
  382.  
  383.  
Advertisement
Add Comment
Please, Sign In to add comment