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 scope_table.h:
#include "symbol_info.h"
#include
#include
#include
#include
class scope_table {
private:
int bucket_count;
int unique_id;
scope_table* parent_scope = nullptr;
std::vector table;
int hash_function(std::string name){
// A simple hash function: sum of ASCII values modulo bucket_count
int sum =0;
for (char c : name){
sum += c;
}
return sum % bucket_count;
}
public:
scope_table(){}
scope_table(int bucket_count, int unique_id, scope_table* parent_scope = nullptr)
: bucket_count(bucket_count), unique_id(unique_id), parent_scope(parent_scope){
table.resize(bucket_count, nullptr);
}
scope_table* get_parent_scope(){
return parent_scope;
}
int get_unique_id(){
return unique_id;
}
symbol_info* lookup_in_scope(symbol_info* symbol){
int index = hash_function(symbol->name);
if (table[index]!= nullptr && table[index]->name == symbol->name){
return table[index];
} else {
return nullptr;
}
}
bool insert_in_scope(symbol_info* symbol){
if (lookup_in_scope(symbol)!= nullptr){
return false; // Symbol already exists in this scope
}
int index = hash_function(symbol->name);
table[index]= symbol;
return true;
}
bool delete_from_scope(symbol_info* symbol){
int index = hash_function(symbol->name);
if (table[index]!= nullptr && table[index]->name == symbol->name){
table[index]= nullptr;
return true;
} else {
return false; // Symbol not found in this scope
}
}
void print_scope_table(std::ofstream& outlog){
outlog << "ScopeTable # "<< std::to_string(unique_id)<< std::endl;
for (int i =0; i < bucket_count; ++i){
if (table[i]!= nullptr){
outlog << "Bucket "<< i <<": "<< table[i]->name <<"->"<< table[i]->type << std::endl;
}
}
}
~scope_table(){
// 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:
1. Please complete the following incomplete C++ code in a file called symbol_info.h:
#include
using namespace std;
class symbol_info
{
private:
string name;
string type;
// Write necessary attributes to store what type of symbol it is (variable/array/function)
// Write necessary attributes to store the type/return type of the symbol (int/float/void/...)
// 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:
symbol_info(string name, string type)
{
this->name = name;
this->type = type;
}
string get_name()
{
return name;
}
string get_type()
{
return type;
}
void set_name(string name)
{
this->name = name;
}
void set_type(string type)
{
this->type = type;
}
// Write necessary functions to set and get the attributes
~symbol_info()
{
// Write necessary code to deallocate memory, if necessary
}
};
NOTE: Will post the other snippet in another post.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!