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.
rootNodeis a pointer. You're updating the local variablerootNode. Try dereferencing it when you copy: