Question: This is the C + + code to implement a Symbol Table of a file called scope _ table.h: #include symbol _ info.h #include
This is the C code to implement a Symbol Table of a file called scopetable.h:
#include "symbolinfo.h
#include
#include
#include
#include
class scopetable
private:
int bucketcount;
int uniqueid;
scopetable parentscope nullptr;
std::vector table;
int hashfunctionstd::string name
A simple hash function: sum of ASCII values modulo bucketcount
int sum ;
for char c : name
sum c;
return sum bucketcount;
public:
scopetable
scopetableint bucketcount, int uniqueid scopetable parentscope nullptr
: bucketcountbucketcount uniqueiduniqueid parentscopeparentscope
table.resizebucketcount, nullptr;
scopetable getparentscope
return parentscope;
int getuniqueid
return uniqueid;
symbolinfo lookupinscopesymbolinfo symbol
int index hashfunctionsymbolname;
if tableindex nullptr && tableindexname symbolname
return tableindex;
else
return nullptr;
bool insertinscopesymbolinfo symbol
if lookupinscopesymbol nullptr
return false; Symbol already exists in this scope
int index hashfunctionsymbolname;
tableindex symbol;
return true;
bool deletefromscopesymbolinfo symbol
int index hashfunctionsymbolname;
if tableindex nullptr && tableindexname symbolname
tableindex nullptr;
return true;
else
return false; Symbol not found in this scope
void printscopetablestd::ofstream& outlog
outlog "ScopeTable # std::tostringuniqueid std::endl;
for int i ; i bucketcount; i
if tablei nullptr
outlog "Bucket i : tableiname tableitype std::endl;
~scopetable
Free dynamically allocated memory
for auto& symbol : table
delete symbol;
;
The code has been completed based on these requirements:
Please complete the code based on these requirements:
scope
table: This class implements a hash table. You may need an array
array of pointers of symbol
info type
and a hash function. The hash function will determine the corresponding bucket no
the sumbol
info object will go into. The hash function would be any suitable hash function to your choice. The hash function will take the name of the symbol as input. Don
t forget to keep the hash value in the range of the bucket size.
You will also need a pointer of scope
table type object named parent
scope as a member variable so that you can maintain a list of scope tables in the symbol table. Also give each table a unique id
You also need to add the following functionalities to your Scope Table:
Insert: Insert into symbol table if already not inserted in this scope table. Return type of this function should be boolean indicating whether insertion is successful or not.
Look up: Search the hash table for a particular symbol. Return a symbol
info pointer.
Delete: Delete an entry from the scope table. Return true in case of a successful deletion and false otherwise.
Print: Print the scope table in the logfile
You should also write a constructor that takes an integer n as a parameter and allocates n buckets for the hash table. You should also write a destructor to deallocate memory.
I need your help with two more incomplete code snippets:
Please complete the following incomplete C code in a file called symbolinfo.h:
#include
using namespace std;
class symbolinfo
private:
string name;
string type;
Write necessary attributes to store what type of symbol it is variablearrayfunction
Write necessary attributes to store the typereturn type of the symbol intfloatvoid
Write necessary attributes to store the parameters of a function
Write necessary attributes to store the array size if the symbol is an array
public:
symbolinfostring name, string type
thisname name;
thistype type;
string getname
return name;
string gettype
return type;
void setnamestring name
thisname name;
void settypestring type
thistype type;
Write necessary functions to set and get the attributes
~symbolinfo
Write necessary code to deallocate memory, if necessary
;
NOTE: Will post the other snippet another post.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
