Symbol Table
Symbol Table
Name: Harisankar R N R
Reg No: 21BRS1524
Symbol Table Implementation using C++ with Hashtable / Unordered Map data
structure to represent the symbol Table.
struct Symbol {
string type;
int value;
};
class SymbolTable {
private:
unordered_map<string, Symbol> table;
public:
void add_symbol(const string& symbol, const string& symbol_type, int value
= 0) {
if (table.find(symbol) == table.end()) {
Symbol new_symbol = {symbol_type, value};
table[symbol] = new_symbol;
cout << "Symbol '" << symbol << "' added to the table.\n";
} else {
cout << "Symbol '" << symbol << "' already exists in the table.
Use update_symbol() to modify it.\n";
}
}
void display_table() {
cout << "\nSymbol Table:\n";
for (const auto& entry : table) {
cout << entry.first << " | Type: " << entry.second.type << " |
Value: " << entry.second.value << "\n";
}
cout << "\n";
}
};
int main() {
SymbolTable symbol_table;
symbol_table.display_table();
symbol_table.search_symbol("y");
symbol_table.update_symbol("y", "int", 5);
symbol_table.display_table();
symbol_table.delete_symbol("x");
symbol_table.display_table();
symbol_table.search_symbol("x");
return 0;
}