# Generating Code by building a Syntax Tree¶

The last, most structured alternative for generating code is to construct the tree data structure representing the syntax, and then transforming this back into source code form.

This approach to code generation is perhaps the most applicable to the programmatic generation of code, and less the generation directly by a user.

In [1]:

    from cgen import *

In [2]:

    func = FunctionBody(
        FunctionDeclaration(Const(Pointer(Value("char", "greet"))), []),
        Block([Statement('return "hello world"')])
        )

In [3]:

    print(func)

    char const *greet()
    {
      return "hello world";
    }

In [ ]: