Beispiel #1
0
    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)
Beispiel #2
0
 def _init_builtins(self):
     self.insert(BuiltinTypeSymbol('INTEGER'))
     self.insert(BuiltinTypeSymbol('REAL'))
Beispiel #3
0
        return s

    __repr__ = __str__

    def insert(self, symbol):
        print('Insert: %s' % symbol.name)
        self._symbols[symbol.name] = symbol


if __name__ == '__main__':
    text = """
program SymTab1;
   var x, y : integer;
begin
end.
"""
    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()

    symtab = SymbolTable()
    int_type = BuiltinTypeSymbol('INTEGER')
    symtab.insert(int_type)

    var_x_symbol = VarSymbol('x', int_type)
    symtab.insert(var_x_symbol)

    var_y_symbol = VarSymbol('y', int_type)
    symtab.insert(var_y_symbol)
    print(symtab)