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_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_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 create(name): cfg = util.load_config_from_file(name) l=[ obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')] cfg_new = node.gen_graph(l) node.show_node(cfg_new,debug=True) device = model.DEVICE() for i in cfg_new: if i.TYPE==ast.AST.hostname: device.hostname = i.RGX[1]; elif i.TYPE==ast.AST.ip_default_gateway: device.default_gateway = i.RGX[1] elif i.TYPE==ast.AST.svi: mng = i.Node[0] device.mng_ip, device.mng_mask = mng.RGX[2], mng.RGX[3] device.mng_int_vlan = i.RGX[1] elif i.TYPE==ast.AST.interface: port = device.getport(int(i.RGX[1])) switchport_allowed_tagged, switchport_allowed_untagged, switchport_native, description = None, None, None, None for j in i.Node: if j.TYPE ==ast.AST.description: port.setdescription(j.RGX[1]) elif j.TYPE==ast.AST.switchport_allowed_tagged: port.addtagged( j.VLAN ) elif j.TYPE==ast.AST.switchport_allowed_untagged: port.adduntagged( j.VLAN ) elif j.TYPE==ast.AST.description: port.setdescription( j.RGX[1] ) elif j.TYPE==ast.AST.shutdown: port.setdown(); elif i.TYPE==ast.AST.vlan: if i.RGX[1] not in ['1','4093']: device.addvlan(model.VLAN(i.RGX[4],i.RGX[1])) elif i.TYPE==ast.AST.ip_igmp_snooping: if i.RGX[4] not in device.igmp_snooping: device.igmp_snooping.append(i.RGX[4]); elif i.TYPE==ast.AST.ip_dhcp_snooping: if i.RGX[4] not in device.dhcp_snooping: device.dhcp_snooping.append(i.RGX[4]);
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, args.source_file.split("/")[-1][:-2])) # -O optimizador # -t Impirmir Arbol AST
import parser import exc import environment if __name__ == '__main__': import sys code = sys.stdin.read() try: rst = list(parser.parsing(code)) env = environment.Environment(environment.INITIAL_ENV, None) env['env'] = env ret_value = map( lambda rst:environment.evaluate(env, rst), rst) except exc.SyntaxError as e: e.display_error() else: print 'implement result : ', ret_value
def create(name): device = model.DEVICE() cfg = util.load_config_from_file(name) l = [obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')] cfg_new = node.gen_graph(l) node.show_node(cfg_new) for i in cfg_new: if i.TYPE == ast.AST.hostname: device.hostname = i.RGX[1] elif i.TYPE == ast.AST.interface: for j in i.Node: if j.TYPE == ast.AST.name: port = device.getport(int(i.RGX[2])) port.setdescription(j.RGX[1]) if j.TYPE == ast.AST.inactive: port = device.getport(int(i.RGX[2])) port.setdown() elif i.TYPE == ast.AST.mvr: source_port, receiver_port, name, tagged = None, None, None, None vlan = model.VLAN(name, i.RGX[1]) device.addvlan(vlan) for j in i.Node: if j.TYPE == ast.AST.source_port: source_port = j.PORT elif j.TYPE == ast.AST.receiver_port: receiver_port = j.PORT elif j.TYPE == ast.AST.name: name = j.RGX[1] elif j.TYPE == ast.AST.tagged: tagged = j.PORT device.setmvr(model.MVR(i.RGX[1], name)) for v in source_port: port = device.getport(v) port.mvr.tag = i.RGX[1] port.mvr.receiver_port = True for v in receiver_port: port = device.getport(v) port.mvr.tag = i.RGX[1] port.mvr.source_port = True for v in tagged: port = device.getport(v) port.addtagged(i.RGX[1]) elif i.TYPE == ast.AST.vlan: fixed, forbidden, untagged, name = None, None, None, None for j in i.Node: if j.TYPE == ast.AST.fixed: fixed = j.PORT elif j.TYPE == ast.AST.forbidden: forbidden = j.PORT elif j.TYPE == ast.AST.untagged: untagged = j.PORT elif j.TYPE == ast.AST.name: name = j.RGX[1] elif j.TYPE == ast.AST.ip: if ast.default_management in j.RGX: device.mng_ip, device.mng_mask = j.RGX[3], j.RGX[4] device.mng_int_vlan = i.RGX[1] elif ast.default_gateway in j.RGX: device.default_gateway = j.RGX[3] device.addvlan(model.VLAN(name, i.RGX[1])) for k, _ in fixed.items(): port = device.getport(k) if k in untagged: port.adduntagged(i.RGX[1]) else: port.addtagged(i.RGX[1]) print("*" * 45) return device
def create(name): device = model.DEVICE() cfg = util.load_config_from_file(name) l = [obj.Struct(**node.parsing(i.strip())) for i in cfg.split('\n')] cfg = node.gen_graph(l) # node.show_node(cfg,debug=True); for i in cfg: if i.TYPE == ast.AST.hostname: device.hostname = i.RGX[1] elif i.TYPE == ast.AST.interface_range_ethernet: ports = node.parsing_interface(i.RGX[1]).PORT if len(i.Node) > 0: for j in i.Node: if j.TYPE != None: if j.TYPE == ast.AST.description: for h in ports: port = device.getport(int(h)) port.setdescription(j.RGX[1]) elif j.TYPE == ast.AST.switchport_trunk_allowed_vlan_add: for h in ports: port = device.getport(int(h)) port.addtagged(j.RGX[1]) elif j.TYPE == ast.AST.switchport_access: for h in ports: port = device.getport(int(h)) port.adduntagged(j.RGX[2]) elif j.TYPE == ast.AST.switchport_trunk_native_vlan: for h in ports: port = device.getport(int(h)) port.adduntagged(j.RGX[1]) elif j.TYPE == ast.AST.switchport_general_pvid: for h in ports: # print(" port",h," switchport_general_pvid",j.RGX); port = device.getport(int(h)) port.adduntagged(j.RGX[1]) port.addgeneral_pid(j.RGX[1]) elif j.TYPE == ast.AST.switchport_general_allowed_vlan_add: for h in ports: # print(" port",h," ast.AST.switchport_general",j.RGX); port = device.getport(int(h)) if len(j.RGX) == 2: port.addtagged(j.RGX[1]) port.addgeneral_tag(j.RGX[1]) elif len(j.RGX) == 3: port.adduntagged(j.RGX[1]) port.addgeneral_untag(j.RGX[1]) else: print( " port range", h, " ast.AST.switchport_general_allowed_vlan_add", j.RGX) elif i.TYPE == ast.AST.interface_ethernet: if len(i.Node) > 0: for j in i.Node: if j.TYPE != None: port = device.getport(int(i.RGX[2])) if j.TYPE == ast.AST.description: port.setdescription(j.RGX[1]) elif j.TYPE == ast.AST.switchport_general_allowed_vlan_add: if len(j.RGX) == 2: port = device.getport(int(i.RGX[2])) port.addtagged(j.RGX[1]) else: port = device.getport(int(i.RGX[2])) port.addtagged(j.RGX[2]) elif j.TYPE == ast.AST.switchport_general_pvid: port.adduntagged(j.RGX[1]) port.addgeneral_pid(j.RGX[1]) # print(" port",i.RGX[2]," switchport_general_pvid",j.RGX); elif j.TYPE == ast.AST.switchport_general: port = device.getport(int(i.RGX[2])) # print(" port",i.RGX[2]," ast.AST.switchport_general",j.RGX); if len(j.RGX) == 2: port = device.getport(int(i.RGX[2])) port.addtagged(j.RGX[1]) port.addgeneral_tag(j.RGX[1]) elif len(j.RGX) == 3: port.adduntagged(j.RGX[1]) port.addgeneral_tag(j.RGX[1]) else: print(" port", i.RGX[2], " ast.AST.switchport_general", j.RGX) elif i.TYPE == ast.AST.vlan_database: if len(i.Node) > 0: for j in i.Node: if j.TYPE == ast.AST.vlan: p = node.parsing_port(j.RGX[1]).VLAN for _, i in p.items(): device.addvlan(model.VLAN("", i)) elif i.TYPE == ast.AST.interface_vlan: if len(i.Node) > 0: for j in i.Node: if j.TYPE != None: if j.TYPE == ast.AST.name: device.setvlanname(i.RGX[1], j.RGX[1]) elif j.TYPE == ast.AST.ip_address: device.mng_ip, device.mng_mask = j.RGX[1], j.RGX[2] device.mng_int_vlan = i.RGX[1] elif j.TYPE == ast.AST.ip_igmp_snooping: if i.RGX[1] not in device.igmp_snooping: device.igmp_snooping.append(i.RGX[1]) else: print(" ", j.__dict__) elif i.TYPE == ast.AST.ip_dhcp_snooping: if i.RGX[2] not in device.dhcp_snooping: device.dhcp_snooping.append(i.RGX[2]) elif i.TYPE == ast.AST.ip_default_gateway: device.default_gateway = i.RGX[1] return device
def parse(): for i in range(1, 12): for k in data.lessons: url = f'https://12baliv.com.ua/{i}-klas/{k}' # forming URL parser.parsing(url, i, k) # starting the parse process