def parse_file( filename, use_cpp=False, cpp_path='cpp', cpp_args=''): """ Parse a C file using pycparser. filename: Name of the file you want to parse. use_cpp: Set to True if you want to execute the C pre-processor on the file prior to parsing it. cpp_path: If use_cpp is True, this is the path to 'cpp' on your system. If no path is provided, it attempts to just execute 'cpp', so it must be in your PATH. cpp_args: If use_cpp is True, set this to the command line arguments strings to cpp. Be careful with quotes - it's best to pass a raw string (r'') here. For example: r'-I../utils/fake_libc_include' If several arguments are required, pass a list of strings. When successful, an AST is returned. ParseError can be thrown if the file doesn't parse successfully. Errors from cpp will be printed out. """ if use_cpp: path_list = [cpp_path] if isinstance(cpp_args, ListType): path_list += cpp_args elif cpp_args != '': path_list += [cpp_args] path_list += [filename] # Note the use of universal_newlines to treat all newlines # as \n for Python's purpose # pipe = Popen( path_list, stdout=PIPE, universal_newlines=True) text = pipe.communicate()[0] else: text = open(filename).read() parser = CParser() return parser.parse(text, filename)
def parse_file(filename, use_cpp=False, cpp_path='cpp', cpp_args=''): """ Parse a C file using pycparser. filename: Name of the file you want to parse. use_cpp: Set to True if you want to execute the C pre-processor on the file prior to parsing it. cpp_path: If use_cpp is True, this is the path to 'cpp' on your system. If no path is provided, it attempts to just execute 'cpp', so it must be in your PATH. cpp_args: If use_cpp is True, set this to the command line arguments strings to cpp. Be careful with quotes - it's best to pass a raw string (r'') here. For example: r'-I../utils/fake_libc_include' If several arguments are required, pass a list of strings. When successful, an AST is returned. ParseError can be thrown if the file doesn't parse successfully. Errors from cpp will be printed out. """ if use_cpp: path_list = [cpp_path] if isinstance(cpp_args, ListType): path_list += cpp_args elif cpp_args != '': path_list += [cpp_args] path_list += [filename] # Note the use of universal_newlines to treat all newlines # as \n for Python's purpose # pipe = Popen(path_list, stdout=PIPE, universal_newlines=True) text = pipe.communicate()[0] else: text = open(filename).read() parser = CParser() return parser.parse(text, filename)
def compile(self): parser = CParser() ast = parser.parse(self.textSrc.toPlainText()) self.textAST.setText(ast.toString(attrnames=True, nodenames=True)) translator = CTranslator() translator.visit(ast) codes = translator.get_codes() code_text = '' for code in codes: code_text += str(code) + '\n' self.textCode.setText(code_text) codes = translator.get_codes() tables = translator.get_tables() assembler = CAssembler(tables) asm = assembler.asm(codes) self.textAsm.setText(asm) with open('hello.s', 'w') as f: f.write(asm) os.system('gcc {filename} -o hello.exe'.format(filename='hello.s')) self.statusBar().showMessage('编译完成!请点击运行程序。')
def parse_file(filename, use_cpp=False, cpp_path='cpp', cpp_args='', parser=None): """ Parse a C file using pycparser. filename: Name of the file you want to parse. use_cpp: Set to True if you want to execute the C pre-processor on the file prior to parsing it. cpp_path: If use_cpp is True, this is the path to 'cpp' on your system. If no path is provided, it attempts to just execute 'cpp', so it must be in your PATH. cpp_args: If use_cpp is True, set this to the command line arguments strings to cpp. Be careful with quotes - it's best to pass a raw string (r'') here. For example: r'-I../utils/fake_libc_include' If several arguments are required, pass a list of strings. parser: Optional parser object to be used instead of the default CParser When successful, an AST is returned. ParseError can be thrown if the file doesn't parse successfully. Errors from cpp will be printed out. """ # if use_cpp: # text = preprocess_file(filename, cpp_path, cpp_args) # else: # with open(filename, 'rU') as f: # text = f.read() if parser is None: parser = CParser() return parser.parse(text, filename)
elif type(nodeObj) is If: handleIfElse(nodeObj) elif type(nodeObj) is For: handleForLoops(nodeObj) elif type(nodeObj) is Decl: handleDeclerations(nodeObj) elif type(nodeObj) is UnaryOp: handleUnaryOp(nodeObj) else: # are all other objs iterable ?? # handled recursively on children for child in nodeObj: dfs(child) ###################### ## exec starts here ###################### parser = CParser() with open("temp.c", 'rU') as f: text = f.read() syntaxTree = parser.parse(text, "temp.c") dfs(syntaxTree)
options = get_options() bounds_checked = options.bounds_checked null_on_free = options.null_on_free file_list = options.input while source_file_count < len(file_list): cur_file = open(file_list[source_file_count], 'r') token_list: Deque[CToken] = deque() tokenizer = Tokenizer() tokenizer.nexttok(cur_file) while (1): tok = tokenizer.nexttok(cur_file) token_list.append(tok) if tok.val == Tok.eof: break output = Output() parser = CParser(bounds_checked, null_on_free) parser.parse(output, token_list, source_file_count) # output to files output.output_to_file(source_file_count, cur_file) source_file_count += 1 print(source_file_count) cur_file.close() finish_time = time.time() print("Time taken: " + str(finish_time - init_time) + "s")
pass def dfs(nodeObj): if (nodeObj): if type(nodeObj) is Assignment: handleAssignmentOp(nodeObj) pass elif type(nodeObj) is While or type(nodeObj) is DoWhile: handleWhileLoops(nodeObj) elif type(nodeObj) is FuncCall: handleFunctionCalls(nodeObj) elif type(nodeObj) is If: handleIfElse(nodeObj) else: for child in nodeObj: dfs(child) parser = CParser() with open("testing_c_file.c", 'rU') as f: text = f.read() syntaxTree = parser.parse(text, "testing_c_file.c") dfs(syntaxTree)
from c_assembler import CAssembler import os data = ''' int main(){ for(int i = 1; i < 10; i++){ for(int j = 1; j<10; j++){ printf("%d*%d=%d\t", i, j, i*j); } printf("\n"); } } ''' parser = CParser() ast = parser.parse(data) # ast.show(attrnames=True, nodenames=True) print(ast.toString(attrnames=True, nodenames=True)) translator = CTranslator() translator.visit(ast) for code in translator.get_codes(): print(code) tables = translator.get_tables() print(tables['symbol_table']) codes = translator.get_codes() assembler = CAssembler(tables) asm = assembler.asm(codes) print(asm) with open('hello.s', 'w') as f: f.write(asm)
from c_parser import CParser data = ''' int main(double args){ char c = 'B'; char d[100]; for (int i = 0; i < 10; i++){ printf(i); } if (a>b) printf(">"); } void printf(int str){ return 0; } ''' parser = CParser() ast = parser.parse(data, '<none>') # ast.show(attrnames=True, nodenames=True) for decl in ast: decl.show(attrnames=True, nodenames=True)