File size: 1,822 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -----------------------------------------------------------------
# pycparser: construct_ast_from_scratch.py
#
# Tiny example of writing an AST from scratch to C code.
#
# Andre Ribeiro [https://github.com/Andree37]
# License: BSD
# -----------------------------------------------------------------
import sys

# This is not required if you've installed pycparser into
# your site-packages/ with setup.py
sys.path.extend(['.', '..'])

from pycparser import c_ast, c_generator


# target C code:
# int main() {
#     return 0;
# }


def empty_main_function_ast():
    constant_zero = c_ast.Constant(type='int', value='0')
    return_node = c_ast.Return(expr=constant_zero)
    compound_node = c_ast.Compound(block_items=[return_node])
    type_decl_node = c_ast.TypeDecl(declname='main', quals=[],
                                    type=c_ast.IdentifierType(names=['int']),
                                    align=[])
    func_decl_node = c_ast.FuncDecl(args=c_ast.ParamList([]),
                                    type=type_decl_node)
    func_def_node = c_ast.Decl(name='main', quals=[], storage=[], funcspec=[],
                               type=func_decl_node, init=None,
                               bitsize=None, align=[])
    main_func_node = c_ast.FuncDef(decl=func_def_node, param_decls=None,
                                   body=compound_node)

    return main_func_node


def generate_c_code(my_ast):
    generator = c_generator.CGenerator()
    return generator.visit(my_ast)


if __name__ == '__main__':
    main_function_ast = empty_main_function_ast()
    print("|----------------------------------------|")
    main_function_ast.show(offset=2)
    print("|----------------------------------------|")
    main_c_code = generate_c_code(main_function_ast)
    print("C code: \n%s" % main_c_code)