Question: Give: #include SymbolTable.h #include #include using namespace std; #define NUM_BUCK 500 struct Node { Node(const string& id, int lineNum, int scope) { m_id = id;

Give:

#include "SymbolTable.h" #include #include using namespace std; #define NUM_BUCK 500

struct Node { Node(const string& id, int lineNum, int scope) { m_id = id; m_line = lineNum; m_scope = scope; } string m_id; int m_line; int m_scope; };

int hashFunc(const string &id) { int total = 0; for (int i = 0; i

int index = (total % NUM_BUCK); return(index); }

class SymbolTableImpl { public: SymbolTableImpl() { m_AtScope = 0; } ~SymbolTableImpl(); void enterScope(); bool exitScope(); bool declare(const string& id, int lineNum); int find(const string& id); private: list m_buckets[NUM_BUCK]; int m_AtScope; };

SymbolTableImpl::~SymbolTableImpl() { }

void SymbolTableImpl::enterScope() { m_AtScope++; }

bool SymbolTableImpl::exitScope() { if (m_AtScope == 0) return false;

for (int j = 0; j::iterator n = m_buckets[j].begin(); n != m_buckets[j].end();) { if (n->m_scope == m_AtScope) n = m_buckets[j].erase(n);

else n++; } } m_AtScope--; return true; }

bool SymbolTableImpl::declare(const string& id, int lineNum) { int slot = hashFunc(id);

list* b = &m_buckets[slot];

Node* n = new Node(id, lineNum, m_AtScope);

if (b->empty()) { b->push_front(*n); return true; }

for (list::iterator p = b->begin(); p != b->end(); p++) { if (n->m_id == p->m_id && p->m_scope == m_AtScope) return false; }

b->push_front(*n); return true; }

int SymbolTableImpl::find(const string& id) { if (id.empty()) return -1;

int slot = hashFunc(id);

list* b = &m_buckets[slot];

for (list::iterator p = b->begin(); p != b->end(); p++) { if (p->m_id == id && p->m_scope <= m_AtScope) return p->m_line; }

return -1; }

//*********** SymbolTable functions **************

// For the most part, these functions simply delegate to SymbolTableImpl's // functions.

SymbolTable::SymbolTable() { m_impl = new SymbolTableImpl; }

SymbolTable::~SymbolTable() { delete m_impl; }

void SymbolTable::enterScope() { m_impl->enterScope(); }

bool SymbolTable::exitScope() { return m_impl->exitScope(); }

bool SymbolTable::declare(const string& id, int lineNum) { return m_impl->declare(id, lineNum); }

int SymbolTable::find(const string& id) const { return m_impl->find(id); }

How do I write a distructor for SymbolTableImpl

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!