Skip to content
Permalink
 
 
Cannot retrieve contributors at this time
from spi import Lexer, Parser, NodeVisitor, BuiltinTypeSymbol, VarSymbol
class SymbolTable(object):
def __init__(self):
self._symbols = {}
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__
def insert(self, symbol):
print('Insert: %s' % symbol.name)
self._symbols[symbol.name] = symbol
class SemanticAnalyzer(NodeVisitor):
def __init__(self):
self.symtab = SymbolTable()
def visit_Block(self, node):
for declaration in node.declarations:
self.visit(declaration)
self.visit(node.compound_statement)
def visit_Program(self, node):
self.visit(node.block)
def visit_Compound(self, node):
for child in node.children:
self.visit(child)
def visit_NoOp(self, node):
pass
def visit_VarDecl(self, node):
# For now, manually create a symbol for the INTEGER built-in type
# and insert the type symbol in the symbol table.
type_symbol = BuiltinTypeSymbol('INTEGER')
self.symtab.insert(type_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)
self.symtab.insert(var_symbol)
if __name__ == '__main__':
text = """
program SymTab2;
var x, y : integer;
begin
end.
"""
lexer = Lexer(text)
parser = Parser(lexer)
tree = parser.parse()
semantic_analyzer = SemanticAnalyzer()
semantic_analyzer.visit(tree)
print(semantic_analyzer.symtab)