def visit_Program(self, node): #print("ENTER scope: " + self.program_name) builtin_scope = SymbolTable( scope_name=self.program_name, scope_level=0, enclosing_scope=None ) self.current_scope = builtin_scope #print("ENTER scope: global") global_scope = SymbolTable( scope_name="global", scope_level=1, enclosing_scope=builtin_scope # None ) self.current_scope.children[global_scope.scope_name] = global_scope self.current_scope = global_scope if(len(node.children) == 0): pass for child in node.children: self.visit(child) #print(global_scope) self.current_scope = self.current_scope.enclosing_scope
def visit_IfElse(self, node): node.condition.Scope = node.Scope node.condition.accept(self) node.instruction1.Scope = SymbolTable(node.Scope) node.instruction1.accept(self) node.instruction2.Scope = SymbolTable(node.Scope) node.instruction2.accept(self)
def visit_IfNode(self, node): self.current_scope.current_if += 1 #print("ENTER scope: if" + str(self.current_scope.current_if)) if_scope = SymbolTable( scope_name="if" + str(self.current_scope.current_if), scope_level=self.current_scope.scope_level + 1, enclosing_scope=self.current_scope ) self.current_scope.children[if_scope.scope_name] = if_scope self.current_scope = if_scope for child in node.true_block: self.visit(child) #print(if_scope) #print("LEAVE scope: if") self.current_scope = self.current_scope.enclosing_scope if(node.false_block): self.current_scope.current_else += 1 #print("ENTER scope: else" + str(self.current_scope.current_else)) else_scope = SymbolTable( scope_name="else" + str(self.current_scope.current_else), scope_level=self.current_scope.scope_level + 1, enclosing_scope=self.current_scope ) self.current_scope.children[else_scope.scope_name] = else_scope self.current_scope = else_scope for child in node.false_block: self.visit(child) #print(else_scope) #print("LEAVE scope: else") self.current_scope = self.current_scope.enclosing_scope
def __init__(self, tokenizer, output_file): self.tokenizer = tokenizer self.output_file = output_file self.class_symbol_table = SymbolTable() self.subroutine_symbol_table = SymbolTable() self.vm_writer = VMWriter(output_file) self.label_counter = LabelCounter(labels=self.TOKENS_THAT_NEED_LABELS) self.class_name = None
def __init__(self, path): with open(path) as f: text = f.read() self.tokens = tokenize(text) self.count = 0 self.ClassScope = SymbolTable() self.SubroutineScope = SymbolTable() self.label_count = {"if": 0, "while": 0} self.vmcode = ""
def pass1(self): symTable = SymbolTable() tokenTable = TokenTable(self.instTable) tk = "" pre_sect_index = 0 for i in range(len(self.lineList)): #linelist의 한 요소씩 접근한다. line = self.lineList[i] token = Token(line) if token.operator != "" and token.operator in ( "END", "CSECT"): # operator가 END 또는 CSECT면 if token.operator == "END": #END일 때 현재 라인을 토큰화하여 저장한다. tokenTable.putToken(line) self.instLoaction(tokenTable, token, symTable) self.symtabList.append( symTable) #현재까지의 symTable을 symtabList에 저장한다. tokenTable.setSymTable( symTable) #현재의 tokenTable에 symtable을 넣는다. self.TokenList.append( tokenTable) #TokenList에 현재의 toknenTable을 저장한다. del symTable symTable = SymbolTable() # symtable을 초기화한다. tokenTable = TokenTable(self.instTable) #새로운 TokenTable을 생성한다. self.locator = 0 symTable.putSymbol(token.label, 0) #새로 만든 symTable에 현재의 라벨을 저장 if token.operator == "CSECT": tokenTable.putToken(line) else: #operator가 END , CSECT 이외의 token일 경우 tokenTable.putToken(line) #tokenTable에 명령어를 넣어 토큰화한다. if token.label != "" and token.label != '.': #label이 있으면 symtable에 넣는다. symTable.putSymbol(token.label, 0) if len(token.operand ) != 0 and token.operator != "RSUB" and token.operand[ 0][0] == '=': #literal일 경우 literalTable에 저장한다. tokenTable.literTab.putLiteral(token.operand[0], 0) if token.operator != "" and token.operator == "EXTDEF": #EXTDEF일 경우 해당 tokenTable의 extdef 리스트에 저장한다. for k in range(len(token.operand)): tokenTable.putExtdef(token.operand[k]) elif token.operator != "" and token.operator == "EXTREF": #EXTREF일 경우 해당 tokenTable의 extref 리스트에 저장한다 for k in range(len(token.operand)): tokenTable.putExtref(token.operand[k]) self.instLoaction(tokenTable, token, symTable) #해당 명령어의 location값을 계산한다. self.outputSymbolTable("symtab_20160333") #symtable을 파일에 저장한다.
def __init__(self, filepath): self._tokenizer = JackTokenizer(filepath) self._writer = VMWriter(filepath) self._classVariables = SymbolTable() self._subroutineVariables = SymbolTable() self._currentToken = None self._preserveCurrentToken = False self._className = '' self._currentCompilingFunction = {'kind': '', 'name': ''} self._numberConditionalsStatementsCurrentFunction = 0
def visit_IfElseInstruction(self, node): self.visit(node.condition) inner_scope = SymbolTable(self.symbol_table, "if") self.symbol_table = inner_scope self.visit(node.instruction) self.symbol_table = self.symbol_table.getParentScope() inner_scope = SymbolTable(self.symbol_table, "else") self.symbol_table = inner_scope self.visit(node.else_instruction) self.symbol_table = self.symbol_table.getParentScope()
def test_push_new_scope(): ST = SymbolTable("") ST2 = SymbolTable("") ST.PushNewScope() ST.PushNewScope() ST.PushNewScope() assert(len(ST2.Table) < len(ST.Table)) assert(len(ST.Table) == 3)
def test_variable_aliasing_fails(self): source = """x = "hello world"; x = 5; """ AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() ast.symbolTable = SymbolTable() self.assertRaises(Exception, ast.assignments[0].expr.fillSymbolTable())
def __init__(self, tokenizer): self.tokenizer = tokenizer self.indent = 0 self.class_symbol_table = SymbolTable() self.routine_symbol_table = SymbolTable() self.class_name = "" self.function_name = "" self.items_pushed_on_stack = 0 self.label_counter = 0 self.constructor = False self.method = False
def visit_Choice_instr_with_else(self, node): node.condition.Functions = node.Functions node.condition.Variables = node.Variables node.condition.accept2(self) node.instruction.Functions = FunctionsTable(node.Functions, "Functions") node.instruction.Variables = SymbolTable(node.Variables, "Variables") node.instruction.accept2(self) node.elseinstruction.Functions = FunctionsTable( node.Functions, "Functions") node.elseinstruction.Variables = SymbolTable(node.Variables, "Variables") node.elseinstruction.accept2(self)
def __init__(self, problem): self.__symbolTable = SymbolTable(10) self.__programInternalForm = [] self.__listOfTokens = [] self.readFromFile("tokens.txt") self.__input = "" self.readProgram(problem)
def visit_Function(self, node): if self.table.get(node.name) is None: fun = FunctionSymbol(node.name, node.type, SymbolTable(self.table, node.name)) self.table.put(node.name, fun) self.actualFun = fun self.table = fun.table numOfParameter = 0 for arg in node.parameters.children: self.visit( arg ) #Wojtku, nie musisz pisac explicite visit_Parameter. Program sam wnioskuje, ktora funkcje odwiedzic, a jak nie to odwiedza defaultowa if self.table.get(arg.name) != None: print "Error: Variable '{}' already declared: line {}".format( arg.name, node.line) varSym = VariableSymbol(arg.name, self.visit(arg)) self.table.put(arg.name, varSym) fun.put(str(numOfParameter), varSym) numOfParameter = numOfParameter + 1 #Zamiast nazywac parametry funkcji bedziemy je numerowac self.visit(node.body) if self.actualFun.returnExists == False: print "Error: Missing return statement in function '{}' returning int: line {}".format( node.name, node.body.endline) self.table = self.table.getParentScope() self.actualFun = None else: print "Error: Redefinition of function '{}': line {}".format( node.name, node.line)
def __init__(self, input_file, output_file): self.tokenizer = input_file self.vm_writer = output_file self.symbol_table = SymbolTable() # cause tokenizer advance is irreversible # need a buffer to save token which need to use more than one time self.buffer = []
def __init__(self): self.table = SymbolTable(None, "root") self.type = "" self.function = "" self.varDict = {} self.rowsLengths = {} self.is_in_loop = False
def __init__(self, arrayToken): self.arrayToken = arrayToken self.token = self.arrayToken[0] self.indexToken = 0 self.actualTable = SymbolTable() self.symbolTableTree = SymbolTableTree(self.actualTable) self.no = AST('AST')
def __init__(self, filepath): jt = JackTokenizer(filepath) ce = CompilationEngine(filepath, jt.tokens) self.vmwriter = VMWriter(filepath) self.symbol_table = SymbolTable() self.class_name = None self.generateClass(ce.root)
def test_function_declaration_in_symbol_table(self): source = """function special_loop(group, transition_list, number_loops) { group.takeOff(5); group.loop(transition_list, number_loops); group.land(); }""" AST.symbolTable = SymbolTable() lexer = Lexer(source) tokens = lexer.getAllTokens() parser = Parser(source, tokens) ast = parser.Program() symTable = ast.fillSymbolTable() table_items = symTable.getTable() assert 'special_loop' in table_items for key, value in symTable.getTable().items(): assert type(key) is str assert type(value) is SymbolTableEntry assert table_items['special_loop'].identifier == 'special_loop' assert table_items['special_loop'].type == 'FUNCTION' assert type(table_items['special_loop'].value) == dict assert len(table_items['special_loop'].value['args']) == 3 assert table_items['special_loop'].value['args'] == [ 'group', 'transition_list', 'number_loops' ] assert len(table_items['special_loop'].value['asssignments']) == 3 assert type(table_items['special_loop'].value['asssignments'] [0].cmd) == TakeOffCmd assert type(table_items['special_loop'].value['asssignments'] [1].cmd) == LoopCmd assert type(table_items['special_loop'].value['asssignments'] [2].cmd) == LandCmd
def __init__(self, tokenizer, vmWriter): self.symbolTable = SymbolTable() self.tkn = tokenizer self.vmWriter = vmWriter self.whileCount = -1 self.ifCount = -1 self.fieldsCount = 0
def __init__(self, cfg, INIT='prg'): # Load the syntax parser and the first known state self.syntax = Syntax(cfg) self.startState = INIT self.started = False self.symboltable = SymbolTable("conf/symboltable.cb")
def test_add_symbol(): ST = SymbolTable("") T = FastRBTree() Content = {'Type': "int" , 'Attribute': None , 'TokenLocation': (10,2) } ST.InsertSymbol("age", Content) T.insert("age", Content) Content = {'Type': "float" , 'Attribute': 'static' , 'TokenLocation': (11,2) } ST.InsertSymbol("temperature", Content) T.insert("temperature", Content) Content = {'Type': "char" , 'Attribute': 'const' , 'TokenLocation': (12,2) } ST.InsertSymbol("letter", Content) T.insert("letter", Content) keys = ST.TopScope.keys() for key in keys: assert(T.__contains__(key)) assert(T.get(key) == ST.TopScope.get(key)) assert(T.get(key) is not ST.TopScope.get(key)) #write test to prove that the returned item is a pointer return
def __init__(self, tokens, outfile): self.tokens = tokens self.symbol_table = SymbolTable() self.current_token = next(self.tokens) self.outfile = outfile self.while_counter = 0 self.if_counter = 0
def visit_CompoundInstruction(self, node): innerScope = SymbolTable(self.table, "innerScope") self.table = innerScope if node.declarations is not None: self.visit(node.declarations) self.visit(node.instructions) self.table = self.table.getParentScope()
def test_constants(): #basic token test for constants ST = SymbolTable("") LA = LexicalAnalizer(ST) LA.BuildLexer() data = '''\'a\'''' LA.Lexer.input(data) for Tok in LA.Lexer: assert (Tok.type == 'CHARACTER_CONSTANT') data = '''//words''' LA.Lexer.input(data) for Tok in LA.Lexer: assert (Tok.type == 'SINGLE_LINE_COMMENT') data = '''-456.89''' LA.Lexer.input(data) for Tok in LA.Lexer: assert (Tok.type == 'FLOATING_CONSTANT') data = '''\"this is string\"''' LA.Lexer.input(data) for Tok in LA.Lexer: assert (Tok.type == 'STRING_LITERAL')
def __init__(self, inputFile): self._tac = [] self._st = SymbolTable() with open(inputFile, "r") as f: for line in f: self.parseLine(line) f.close()
def visit_CompoundInstr(self, node): innerTable = SymbolTable(self.table, "CompoundScope") prevTable = self.table self.table = innerTable self.visit(node.decls) self.visit(node.instrs) self.table = prevTable
def __init__(self, codificationFile, sourceCodeFile): self.__codification = Codification(codificationFile) self.__st = SymbolTable() self.__pif = ProgramInternalForm() self.__sourceCodeFD = open(sourceCodeFile, 'r') self.__lineNo = 0 self.__currentLine = ''
def test_find_in_table(): ST = SymbolTable("") Content1 = {'Type': "int" , 'Attribute': 'static' , 'TokenLocation': (10,2) } ST.InsertSymbol("age", Content1) ST.PushNewScope(); Content2 = {'Type': "float" , 'Attribute': None , 'TokenLocation': (11,2) } ST.InsertSymbol("temperature", Content2) ST.PushNewScope(); Content3 = {'Type': "char" , 'Attribute': None , 'TokenLocation': (13,2) } ST.InsertSymbol("letter", Content3) ST.PushNewScope(); assert( ST.FindSymbolInTable("letter")[0] == {1: Content3} ) assert( ST.FindSymbolInTable("temperature")[0] == {2: Content2} ) assert( (ST.FindSymbolInTable("age")[0] is {3: Content1}) == False ) #nonexistent symbol case assert( ST.FindSymbolInTable("foo") == False ) #searcing in current scope case assert( ST.FindSymbolInCurrentScope("age") == False ) return
def __init__(self): self.module = ir.Module('Main') self.symbolTable = SymbolTable() self.builder = ir.IRBuilder() self.globalNameGen = self.globalNameGenerator() self.flags = {'bind': False}