本文整理汇总了Python中pycparser.parse_file函数的典型用法代码示例。如果您正苦于以下问题:Python parse_file函数的具体用法?Python parse_file怎么用?Python parse_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_with_cpp
def test_with_cpp(self):
ast = parse_file('c_files/memmgr.c', use_cpp=True,
cpp_path=CPPPATH,
cpp_args=r'-I../utils/fake_libc_include')
self.failUnless(isinstance(ast, c_ast.FileAST))
ast2 = parse_file('c_files/year.c', use_cpp=True,
cpp_path=CPPPATH,
cpp_args=r'-I../utils/fake_libc_include')
self.failUnless(isinstance(ast2, c_ast.FileAST))
示例2: test_with_cpp
def test_with_cpp(self):
c_files_path = os.path.join('tests', 'c_files')
ast = parse_file(self._find_file('memmgr.c'), use_cpp=True,
cpp_path=CPPPATH,
cpp_args='-I%s' % c_files_path)
self.failUnless(isinstance(ast, c_ast.FileAST))
ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
cpp_path=CPPPATH,
cpp_args=r'-Iutils/fake_libc_include')
self.failUnless(isinstance(ast2, c_ast.FileAST))
示例3: get_func_decls
def get_func_decls(filename, args):
cpp_args = s_cpp_args(args)
if args.cpp.lower() == "none":
ast = parse_file(filename)
else:
ast = parse_file(filename,
use_cpp=True,
cpp_path=os.path.join(os.path.dirname(__file__), "fake_cpp"),
cpp_args=cpp_args)
v = FuncDeclVisitor()
for idx, node in ast.children():
v.visit(node)
return v._ret
示例4: test_with_cpp
def test_with_cpp(self):
memmgr_path = self._find_file('memmgr.c')
c_files_path = os.path.dirname(memmgr_path)
ast = parse_file(memmgr_path, use_cpp=True,
cpp_path=CPPPATH,
cpp_args='-I%s' % c_files_path)
self.assertTrue(isinstance(ast, c_ast.FileAST))
fake_libc = os.path.join(c_files_path, '..', '..',
'utils', 'fake_libc_include')
ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
cpp_path=CPPPATH,
cpp_args=[r'-I%s' % fake_libc])
self.assertTrue(isinstance(ast2, c_ast.FileAST))
示例5: translate_to_c
def translate_to_c(filename):
""" Simply use the c_generator module to emit a parsed AST.
"""
ast = parse_file(filename, use_cpp=True)
generator = c_generator.CGenerator()
ast.show()
print(generator.visit(ast))
示例6: create_test_case
def create_test_case(ppcg_input_file, ppcg_output_files):
assert len(ppcg_output_files) == 1, "Currently support OpenCL only for VOBLA"
# Remove PENCIL qualifiers from C code otherwise it will not parse
remove_pencil_qualifiers_from_file(ppcg_input_file)
# Pre-process and parse the file
ast = pycparser.parse_file(ppcg_input_file, use_cpp=True)
gen = c_generator.CGenerator()
gen.visit(ast)
# Find PENCIL function declarations
pencil_info = FuncDeclVisitor()
pencil_info.visit(ast)
# Find struct definitions
struct_info = StructDefintionVisitor()
struct_info.visit(ast)
# We need the struct and function prototypes to dump in the test file
ast_new_file = copy_ast_declarations(pencil_info, struct_info)
# Analyse the types of parameters in each PENCIL function
for function_name in pencil_info.functions.keys():
for formal_param in pencil_info.get_formal_params(function_name):
decorate_formal_params(formal_param, struct_info)
# Create a main function that will call the PENCIL functions
main_func = create_main(pencil_info)
ast_new_file.ext.append(main_func)
# Write the program to a file
test_file = write_to_file(ast_new_file, ppcg_input_file)
# Compile the code
binary = compile_test_case(test_file, ppcg_output_files[0])
return binary
示例7: translate
def translate(filename, args, portion=None):
try:
ast = pycparser.parse_file(filename, use_cpp=True)
et_obj = EnumTranslator(ast, args.enum, args.output_class)
translate_params = {
'import_line': args.importline,
'header': args.header,
'comment': args.comment,
'hash_type': 'SHA-1',
'hash_value': sha1sum(args.header),
'portion': portion
}
et_obj.translate(translate_params, args.stop)
except pycparser.plyparser.ParseError as exc:
eprint('Translation error, try to use -p option with proper '
'boundaries')
eprint(exc)
return
except TypeError:
eprint('Translation error, try to use -s option with a stop '
'enumerator parameter')
return
if args.output:
with open(args.output, 'w') as ftw:
et_obj.write(ftw)
else:
et_obj.write()
示例8: generate_xml
def generate_xml(filename):
ast = parse_file(filename, use_cpp=True)
visitor = FuncDeclVisitor()
print '<?xml version="1.0" encoding="UTF-8"?>'
print "<blas_functions>"
visitor.visit(ast)
print "</blas_functions>"
示例9: show_decl_file
def show_decl_file(filename):
ast = parse_file(filename, use_cpp=True)
v = FuncDefVisitor()
v.visit(ast)
v = TypeDeclVisitor()
v.visit(ast)
printTypes()
示例10: generate_test_app
def generate_test_app():
c_file = os.path.join(test_app_dir, 'pycparser_main.c')
ast = parse_file(c_file,
use_cpp=True,
cpp_path=os.path.join(os.environ.get('XMOS_TOOL_PATH'),
'bin', 'xcc'),
cpp_args=['-E',
'{}{}'.format('-I',
os.path.join('..','lib_xcore_c','api')),
'{}{}'.format('-I',
os.path.join('..','lib_xcore_c','src')),
'{}{}'.format('-I',
os.path.join('..','..','lib_trycatch','lib_trycatch','api')),
'{}{}'.format('-I',
os.path.join('..','..','lib_trycatch','lib_trycatch','src')),
'{}{}'.format('-I',
os.path.join('..','..','lib_xassert','lib_xassert','api'))
]
)
functions = LibraryFunctionExtractor()
function_call_blocks = functions.get_function_call_blocks(ast)
with open(os.path.join(test_app_dir, 'test.c'), "w") as test_c_file:
test_c_file.writelines(file_top)
test_c_file.writelines(function_call_blocks)
test_c_file.writelines(file_bottom)
示例11: show_func_defs
def show_func_defs(filename):
# Note that cpp is used. Provide a path to your own cpp or
# make sure one exists in PATH.
ast = parse_file(filename, use_cpp=True)
v = FuncDefVisitor()
v.visit(ast)
示例12: translate_to_go
def translate_to_go(filename):
firstname = filename
if "." in filename:
firstname = filename.rsplit(".", 1)[0]
clearlog()
f = open(filename)
data = f.read()
f.close()
data = cleanup(data)
#filename = tempfile.mkstemp()[1]
filename2 = "/tmp/jeje"
f = open(filename2, "w")
f.write(data)
f.close()
try:
ast = parse_file(filename2, use_cpp=True)
except plyparser.ParseError as e:
print("Could not parse %s:" % (filename))
print("line " + "".join(str(e).split(":", 1)[1:]))
return
generator = GoGenerator()
s = generator.visit(ast)
s = generator.fix_int_to_bool(s)
s = generator.last_minute_replacements(s, firstname)
s = generator.make_header() + s
print(s)
示例13: parse_header
def parse_header(filename):
return parse_file(filename,
use_cpp=True,
cpp_args=[
r'-I{}'.format(os.path.dirname(filename)),
r'-I{}'.format(FAKE_LIBC_INCLUDE_DIR),
r'-D_DOXYGEN_ONLY_'])
示例14: show_func_defs
def show_func_defs(filename):
# Note that cpp is used. Provide a path to your own cpp or
# make sure one exists in PATH.
ast = parse_file(filename, use_cpp=True, cpp_args=r"-Iutils/fake_libc_include")
v = FuncDefVisitor()
v.visit(ast)
示例15: translate
def translate(filename):
ast = parse_file(filename,
use_cpp=True,
cpp_path='gcc',
cpp_args=[
"-E",
"-D__FBSDID(x)=", # FreeBSD identifier
"-D__attribute__(x)=", # attribute extension
"-D__builtin_va_list=void*", # include/x86/_types.h:154 typedef __builtin_va_list __va_list;
"-D__inline=",
"-D__asm(x)=",
"-D_RUNETYPE_H_=1", # skip include/runtype.h
"-D_RuneLocale=void*", # but it defines this type
"-D_Noreturn=",
"-U__BLOCKS__", # no (^) syntax: include/stdlib.h: int atexit_b(void (^)(void));
"-U__nonnull", # avoid __nonnull redefinition
"-nostdinc",
"-Ipycparser/utils/fake_libc_include",
"-Iinclude", # copy from /usr/include
])
generator = js_generator.JavaScriptGenerator()
print(generator.visit(ast))