def__str__(self): symtab_header = 'Symbol table contents' lines = ['\n', symtab_header, '_' * len(symtab_header)] lines.extend( ('%7s: %r' % (key, value)) for key, value in self._symbols.items() ) lines.append('\n') s = '\n'.join(lines) return s
__repr__ = __str__
definsert(self, symbol): print('Insert: %s' % symbol.name) self._symbols[symbol.name] = symbol
deflookup(self, name): print('Lookup: %s' % name) symbol = self._symbols.get(name) # 'symbol' is either an instance of the Symbol class or None return symbol
# We have all the information we need to create a variable symbol. # Create the symbol and insert it into the symbol table. var_name = node.var_node.value var_symbol = VarSymbol(var_name, type_symbol)
# Signal an error if the table alrady has a symbol with the same name if self.symtab.lookup(var_name) isnotNone: raise Exception("Error: Duplicate identifier '%s' found" % var_name) self.symtab.insert(var_symbol)
defvisit_Assign(self, node): # right-hand side self.visit(node.right) # left-hand side self.visit(node.left)