def main(): """Driver code for Abstract syntax tree Generation""" #read source code provided by user arg_parser = argparse.ArgumentParser( description="Lexer for Source Language C") arg_parser.add_argument('source_code', help="source code file location") arg_parser.add_argument('-o', help="take the name of dot script", default="ast.dot") arg_parser.add_argument('-f', help="take the name of png file", default="ast.png") arg_parser.add_argument('-d', help="take the name of csv file", default="dump.csv") arg_parser.add_argument('-p', action='store_true', help="output dot script to console") arg_parser.add_argument('-l', action='store_true', help="output lexeme table") args = arg_parser.parse_args() try: # source_code = open(sys.argv[1],"r").read() source_code = open(args.source_code, "r").read() except FileNotFoundError: print( "source file cannot be open/read.\nCheck the file name or numbers of arguments!!" ) sys.exit(-1) if args.l: print_lexeme(source_code) parser = yacc.yacc(debug=1) lexer.lexer.filename = args.source_code result = parser.parse(source_code, lexer=lexer.lexer) #print(sym_table) if len(Errors.get_all_error()): for error in Errors.get_all_error(): print(error) return Graph = draw_ast(result) # print(args) if args.p: Graph.draw(args.f, format='png') print(Graph.string()) return Graph.draw(args.f, format='png') file = open(args.o, 'w') file.write(Graph.string()) file.close() # print(sym_table) # print(args.d) print_csv(sym_table=sym_table, filename=args.d)
def main(): """Driver code for Abstract syntax tree Generation""" #read source code provided by user arg_parser = argparse.ArgumentParser(description="C compiler for x86_64") arg_parser.add_argument('source_code', help="location of source code file") arg_parser.add_argument('-o', help="output file name, \{default a.out\}", default="a.out") arg_parser.add_argument( '-f', help="name of file for additional files, \{default a\}", default="a") arg_parser.add_argument('-c', action='store_true', help="output object file") arg_parser.add_argument('-d', action='store_true', help="output assembly") arg_parser.add_argument('-a', action='store_true', help="output ast") arg_parser.add_argument('-s', action='store_true', help="output symbol table") arg_parser.add_argument('-t', action='store_true', help="output 3 address code") arg_parser.add_argument('-l', action='store_true', help="output lexeme table") arg_parser.add_argument( '-stdc', action='store_true', help= "linker method, if specified it'll uses custom elf entry else from standard X86-64-linux.so" ) arg_parser.add_argument( '-n', action="store_false", help="only create till asm, , do not create executable") args = arg_parser.parse_args() try: source_code = open(args.source_code, "r").read() except FileNotFoundError: print( "source file cannot be open/read.\nCheck the file name or numbers of arguments!!" ) sys.exit(-1) source_file = args.source_code file_name = args.f grammar = get_grammar(source_file, source_code, debug=1) if len(Errors.get_all_error()): for error in Errors.get_all_error(): print(error) return tac_code = grammar.code tac_code = remove_none(tac_code) #to remove redundant labels tac_code = remove_label(tac_code) print_asm(tac_code, stdc=args.stdc) #assembly if args.d: os.system("cp temp.asm {}.asm".format(file_name)) if args.n: asm_file = "temp.asm" os.system('yasm -g dwarf2 -f elf64 temp.asm 2> temp') os.system('touch temp') if args.c: os.system("cp temp.o " + file_name + ".o") if args.stdc: os.system( "ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o {} temp.o -lc -lm 2> temp" .format(args.o)) else: os.system( "ld -o {} -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o -lc temp.o /usr/lib/x86_64-linux-gnu/crtn.o -lm 2> temp" .format(args.o)) os.system("rm -rf temp.asm temp.o temp") else: os.system("rm -rf temp.asm") # ast if args.a: Graph = draw_ast(grammar) Graph.draw(file_name + '.png', format='png') # symbol table if args.s: print_csv(sym_table=sym_table, filename=file_name + ".csv") # 3AC if args.t: print_code(tac_code, filename=file_name + ".3ac") # lexeme if args.l: print_lexeme(source_code, file_name + ".lex")