Complete the code to declare a symbol table as a data structure.
symbol_table = [1]()A symbol table is best represented as a dictionary (or hash map) because it allows fast lookup, insertion, and deletion of symbols by their names.
Complete the code to insert a symbol with its attributes into the symbol table.
symbol_table[[1]] = {'type': 'int', 'scope': 'global'}
The key in the symbol table dictionary should be the symbol's name as a string, for example, 'variable_name'.
Fix the error in the code to check if a symbol exists in the symbol table.
if [1] in symbol_table: print('Symbol found')
To check if a symbol exists, you check if its name as a string is a key in the symbol table dictionary.
Fill both blanks to create a symbol table comprehension that stores symbol names and their types only for global scope symbols.
{ [1]: symbol['type'] for [2], symbol in symbol_table.items() if symbol['scope'] == 'global' }The comprehension iterates over key-value pairs where the key is the symbol name (commonly called 'key') and the value is the symbol's attributes (called 'symbol'). Here, 'key' is used as the new key in the new dictionary and 'name' is used as the variable for the symbol name in the loop.
Fill all three blanks to create a function that returns the type of a symbol if it exists in the symbol table, otherwise returns 'undefined'.
def get_symbol_type([1]): return symbol_table.get([2], [3])
The function takes the symbol name as a parameter (symbol_name). The symbol_table.get method looks up the symbol by its name as a string key. If the symbol is not found, it returns the default string 'undefined'.
