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 test_missing_semicolon(self): with open("invalid/missing_semicolon.c", 'r') as infile: archivo = infile.read().replace('\n', '').strip().replace(" ", "") try: tokens = lexer.lexing(archivo, []) parser.parsing(tokens, "valid/missing_semicolon.c") except Exception as e: print("Error por: {}".format(e))
def test_lexing_le_true(self): with open("valid/le_true.c", 'r') as infile: archivo = infile.read().replace('\n', '').strip().replace(" ", "") self.assertEqual(lexer.lexing(archivo, []), [('intKeyword', 'int'), ('identifier', 'main'), ('openParen', '('), ('closeParen', ')'), ('openBrace', '{'), ('returnKeyword', 'return'), ('constant', '0'), ('lessthanequal', '<='), ('constant', '2'), ('semicolon', ';'), ('closeBrace', '}')])
def test_lexing_precedence2(self): with open("valid/precedence_2.c", 'r') as infile: archivo = infile.read().replace('\n', '').strip().replace(" ", "") self.assertEqual(lexer.lexing(archivo, []), [('intKeyword', 'int'), ('identifier', 'main'), ('openParen', '('), ('closeParen', ')'), ('openBrace', '{'), ('returnKeyword', 'return'), ('openParen', '('), ('constant', '1'), ('OR', '||'), ('constant', '0'), ('closeParen', ')'), ('AND', '&&'), ('constant', '0'), ('semicolon', ';'), ('closeBrace', '}')])
def test_salida_precedence_4(self): warnings.filterwarnings("ignore") assembly_file = os.path.splitext("valid/precedence_4.c")[0] + ".s" with open("valid/precedence_4.c", 'r') as infile: archivo = infile.read().replace('\n', '').strip().replace(" ", "") tokens = lexer.lexing(archivo, []) arbol_prueba = parser.parsing(tokens, "valid/precedence_4.c") generar(arbol_prueba, assembly_file) os.system("gcc -m32 {} -o {}".format(assembly_file, "precedence")) os.system("gcc -w valid/precedence_4.c") prueba = os.popen("./precedence_4;echo $?").read() verdadero = os.popen("./a.out;echo $?").read() print(prueba, verdadero) self.assertEqual(prueba, verdadero)
def parsing(code): if isinstance(code, basestring) : tokens = list(lexer.lexing(code)) else : tokens = list(code) goto = None for i, token in enumerate(tokens): if goto is not None: if i <= goto: continue goto = None type, value, code, offset = token if type == 'open': local_list = [] try : for element in parsing(tokens[i+1:]) : local_list.append(element) except exc.ParenthesisUnopenedError as e: goto = i + e.gotoindex else : raise exc.ParenthesisUnclosedError(code, offset, i, 'Parenthesis is not closed') yield local_list elif type == 'close': raise exc.ParenthesisUnopenedError(code, offset, i, 'Parenthesis is not opened') elif type == 'number': if '.' in value: yield float(value) else: yield int(value) elif type == 'symbol': yield environment.Symbol(value) elif type == 'string': yield eval(value) else: raise exc.ParsingError(code, offset, 'Unknown type error')
action='store_true', help='Desplegar lista de tokens') argparser.add_argument('-n', '--name', type=str, help='Definir el nombre del ejecutable') args = argparser.parse_args() # source_file = sys.argv[1] assembly_file = os.path.splitext(args.source_file)[0] + ".s" with open(args.source_file, 'r') as infile, open(assembly_file, 'w') as outfile: source = infile.read().replace('\n', '').strip().replace(" ", "") new_tokens = [] vacio = [] lexado = lexer.lexing(source, new_tokens) if args.ltokens: print("Tokens lexados: ") print(lexado) arbol = parser.parsing(new_tokens, args.source_file) if args.tree: arbol.printTree() generar(arbol, assembly_file) if args.assembly: print("Código Ensamblador") print(open(assembly_file).read()) if args.name: os.system("gcc {} -o {}".format(assembly_file, args.name)) else: os.system("gcc {} -o {}".format( assembly_file,