How do I get the generated AST from lemon?

241 views Asked by At

How do I get the root node of the AST (abstract syntax tree) from lemon? I have tried using %extra_argument { Node *rootNode } and using the following code to return the root node object.

program ::= statements(A). { rootNode = A; }

But root node the root node in the main parse function stays empty.

Here is the main parse function.

Node parse()
{
    void* parser = ParseAlloc(malloc);
    int token;
    Node astRoot;

    while (token = yylex())
    {
        Parse(parser, token, yytext, &astRoot);
    }

    Parse(parser, 0, NULL, &astRoot);
    ParseFree(parser, free);

    return astRoot;
}

Can anyone help? Thanks in advance.

1

There are 1 answers

1
Kelvin Sherlock On

rootNode is a pointer. You're updating the local variable rootNode. Try dereferencing it when you copy:

program ::= statements(A). { *rootNode = *A; }