def test_parsing_precedence_4(self): with open("valid/precedence_4.c", 'r') as infile: archivo = infile.read().replace('\n', '').strip().replace(" ", "") raiz = AST.Node('program', 'valid/precedence_4.c', None) funcion_n = AST.Node('identifier', 'main', None) raiz.insertIzq(funcion_n) return_n = AST.Node('return', 'return', None) funcion_n.insertIzq(return_n) operador_bi_n = AST.Node('binaryop', '||', None) return_n.insertIzq(operador_bi_n) constante_izq = AST.Node('constant', '0', None) operador_bi_n.insertDer(constante_izq) operador_bi_der = AST.Node('binaryop', '==', None) operador_bi_n.insertIzq(operador_bi_der) constante_izq = AST.Node('constant', '2', None) operador_bi_der.insertIzq(constante_izq) constante_der = AST.Node('constant', '2', None) operador_bi_der.insertDer(constante_der) arbol = AST.AST(raiz) tokens = lexer.lexing(archivo, []) arbol_prueba = parser.parsing(tokens, "valid/precedence_4.c") l_arbol = [] l_arbol_prueba = [] print() self.assertEqual(postorder(arbol.raiz, l_arbol), postorder(arbol_prueba.raiz, l_arbol_prueba))
def Analiza(self, analex): self.alex=analex self.siguiente() if self.analizaPrograma(): print 'Programa analizado con exito' print '\n- Tabla de simbolos:\n\n', [str(i) for i in AST.AST().ids.values()] print '\n\n- Arbol de sintaxis abstracta:\n\n', self.ast
def __init__(self, filename: str, string_stream=None): # Some info about the traversing will be recorded self._ast = AST.AST(filename, string_stream) # Will help us determine if we are in the global scope. self._scope_counter = 0 # Function def helper self._func_def_node = None self._prev_node = None
def analizar(self): print("current limit:" + str(sys.getrecursionlimit())) print("Iniciando analisis") ts_global = TS.TablaDeSimbolos() ms_global = MS.Mensajes() parser = GR.Gramatica(ms_global) GLO.gramatica = {} GLO.isdesc = False input = self.ToAnalize.get("1.0", 'end-1c') input += " \n exit;" instrucciones = parser.parse(input) #SinLex = parser.Errors() if instrucciones is not None: self.procesar_instrucciones(instrucciones, ts_global, ms_global) salida = "" if (ms_global.correcto): print("Analisis correcto") showinfo("Notepad", "Analisis correcto") prints = ms_global.GetMensajes() for Mensaje in prints: print(Mensaje.constructMensaje()) salida += Mensaje.constructMensaje() arparser = AR.AST() raiz = arparser.parse(input) graf = TreeMaker(raiz) try: graf.crearArbol() except: print("No se genero el arbol") #self.ToTS.config(state='normal') self.ToERR.config(state='disabled') else: print("Se encontraron errores") showinfo("Notepad", "Se encontraron errores") self.errores = ms_global.GetErrores() '''if (SinLex is not None): for Mensaje in SinLex.mensajes: print(Mensaje.constructError()) salida+=Mensaje.constructError()''' for Mensaje in self.errores: print(Mensaje.constructError()) salida += Mensaje.constructError() #self.ToTS.config(state='disabled') self.ToERR.config(state='normal') self.setText(salida) self.ts_global = ts_global self.ms_global = ms_global self.ts_global.printts()
def analizalista_id(self): if self.c.cat == 'Identif': # guardo el identificador en el diccionario de la tabla de simbolos self.ids[self.c.valor]=self.c # actualizo la tabla de simbolos en la clase AST if AST.AST().actualiza_ids(self.c.valor, self.c): self.listavar.append(self.c.valor) self.actual=self.c.valor self.siguiente() return self.analizaresto_listaid() else: self.error('Identif')
def function_declare_statement(self): """ sub declaration statement, builds a whole other RootNode which make a sub its own AST to be added to a hash inside the VM to switch to whenever the program calls this sub """ self.eat(TokenType.FUNCTION_DECLARE) name = str(self.current_token.value) if (name not in AST.func_table): AST.func_table[name] = None self.eat(TokenType.ID) self.eat(TokenType.LCURLY) func_body = [] while (self.current_token.type != TokenType.RCURLY): func_body.append(self.statement()) self.eat_end_of_statement() self.eat(TokenType.RCURLY) root = RootNode(func_body) AST.func_table[name] = root return AST()
argParser = argparse.ArgumentParser() argParser.add_argument('testFileName') args = argParser.parse_args() testFileName = args.testFileName try: with open(testFileName, 'r') as testFile: testFileData = testFile.readlines() except FileNotFoundError: print('Error: test file {} does not exist'.format(testFileName)) sys.exit() lexer = Lexer() tokens = lexer.lex(testFileData) ast = AST() verbose = True parser = Parser(ast, verbose) visitor = Visitor(ast) parser.parse(tokens) print(ast) c = Context(ast) print(c) pretty = PrettyPrinter(ast)
def generate_individual(args): return AST(args[0], args[1])()
def parsing(tokens, nombre): program = parse_program(tokens, nombre) ast = AST.AST(program) return ast
import AST as AR from TreeMaker import * parser = AR.AST() f = open("./entrada.txt", "r") input = f.read() raiz = parser.parse(input) graf = TreeMaker(raiz) try: graf.crearArbol() except: print("No se genero el arbol")
def parse_to_AST(self, tokens: List[Token]) -> Union[AST.AST, None]: if tokens == []: return [] head, *tail = tokens try: if head.name == 'COMMENT': return AST.AST(f=AST.empty) #just ignore varaible token it's not used if head.name == 'VARIABLE': return self.parse_to_AST(tail) # interger: Number if head.name == 'INTERGER': interger = AST.AST(value=head.value) return interger # identifier / string if head.name == 'IDENTIFIER': if not tail or tail[0].name == 'RPAREN' or tail[ 0].name == 'PRINT_WO_NL': identifier = AST.AST(value=head.value) return identifier # call a vrariable if head.name == 'CALL' and tail[0].name == 'IDENTIFIER': if tail[1].name == 'CALL': call = AST.AST(f=AST.call) call.right = AST.AST(name=tail[0].value) return call return None # input elif head.name == 'INPUT': return AST.AST(f=AST.my_input) # run_once elif head.name == 'RUN_ONCE': run_once = AST.AST(f=AST.run_once) run_once.right = self.parse_to_AST(tail) return run_once # assign elif head.name == 'IDENTIFIER' and tail[0].name == 'ASSIGN': opperator = AST.AST(f=AST.assign) opperator.left = AST.AST(name=head.value) opperator.right = self.parse_to_AST(tail[1:]) return opperator # add elif head.name == 'IDENTIFIER' and tail[0].name == 'ADD': opperator = AST.AST(f=AST.add) opperator.left = AST.AST(f=AST.call) opperator.left.right = AST.AST(name=head.value) opperator.right = self.parse_to_AST(tail[1:]) return opperator # min elif head.name == 'IDENTIFIER' and tail[0].name == 'MIN': opperator = AST.AST(f=AST.min) opperator.left = AST.AST(f=AST.call) opperator.left.right = AST.AST(name=head.value) opperator.right = self.parse_to_AST(tail[1:]) return opperator # print elif head.name == "PRINT": func = AST.my_print_ln if not self.name_in_list( tail, 'PRINT_WO_NL') else AST.my_print pri = AST.AST(f=func) pri.right = self.parse_to_AST(tail) return pri # if statement elif head.name == 'LPAREN': if self.name_in_list(tail, 'NOT_EQUAL'): left = self.list_until_name(tail, 'NOT_EQUAL') func = AST.not_equal else: left = self.list_until_name(tail, 'NOT_MOD') func = AST.modus right = tail[len(left) + 1:] sign = AST.AST(f=func) sign.left = self.parse_to_AST(left) sign.right = self.parse_to_AST(right) return sign # end elif head.name == 'END': return AST.AST(f=AST.end) # line down elif head.name == 'LINE_DOWN': return AST.AST(f=AST.line_down) # debug elif head.name == 'DEBUG': return AST.AST(f=AST.debug) else: return None except Exception as e: return None
def statement(self): """ Parses a language statement """ if self.current_token.type == TokenType.COMMENT: self.eat(TokenType.COMMENT) return AST() elif self.current_token.type in [ TokenType.SCALAR, TokenType.LIST, TokenType.LPAREN ]: return self.assignment() elif self.current_token.type == TokenType.FUNCTION_DECLARE: return self.function_declare_statement() elif self.current_token.type in [TokenType.IF, TokenType.UNLESS]: return self.if_statement() elif self.current_token.type in [TokenType.WHILE, TokenType.UNTIL]: return self.while_statement() elif self.current_token.type == TokenType.FOR: return self.for_statement() elif self.current_token.type == TokenType.LCURLY: self.eat(TokenType.LCURLY) stmts = [] while (self.current_token.type != TokenType.RCURLY): stmts.append(self.statement()) self.eat_end_of_statement() self.eat(TokenType.RCURLY) return RootNode(stmts) elif self.current_token.type == TokenType.LABEL: name = str(self.current_token.value) self.eat(TokenType.LABEL) if self.current_token.type in [ TokenType.WHILE, TokenType.UNTIL, TokenType.FOR ]: # no nothing for a labeled loop, we'll use the name later self.last_label_name = name return AST() # generic label statement return LabelNode(name) elif self.current_token.type == TokenType.LAST: self.eat(TokenType.LAST) label = None if self.current_token.type == TokenType.ID: label = str(self.current_token.value) self.eat(TokenType.ID) return self.check_for_conditional(LastNode(label)) elif self.current_token.type == TokenType.NEXT: self.eat(TokenType.NEXT) label = None if self.current_token.type == TokenType.ID: label = str(self.current_token.value) self.eat(TokenType.ID) return self.check_for_conditional(NextNode(label)) elif self.current_token.type == TokenType.REDO: self.eat(TokenType.REDO) label = None if self.current_token.type == TokenType.ID: label = str(self.current_token.value) self.eat(TokenType.ID) return self.check_for_conditional(RedoNode(label)) elif (self.current_token.type == TokenType.GOTO): self.eat(TokenType.GOTO) label = None if self.current_token.type == TokenType.ID: label = str(self.current_token.value) self.eat(TokenType.ID) return self.check_for_conditional(GotoNode(label)) # if nothing else, then treat it as an expression else: return self.check_for_conditional(self.expression()) raise Exception("Invalid statement, line number: " + str(self.lex.line_number))