How to get AST from YACC?

I know how to get YACC to generate AST, but how do you do it? I mean, how do you actaully get the root value of a node from YACC?

+5
source share
4 answers

Yacc returns only one node at a time, and this is always what you just gave yacc at an earlier time, that is, whatever you would like to return from shortened production or that you would like to return a character from the terminal. (Sorry, you said you knew this, but some people reading this may not do this.)

So, take what you returned from the root or top rule, and save it (in the attached abbreviation C) in whatever way you like.

+6
source

What Yacc gives you is a parse tree that is different from AST. You will need to build your AST yourself, going through each node of the parsing tree (via yacc).

+2
source

, , , , (, ) , yacc (aka $$) . . , , , , .

0

:

yacc (your_grammar.y):

%parse-param {AstNode **pproot}
%parse-param {char **errmsg}

(your_program.c):

res = yyparse(&pAst, &errmsg);

AST nodes are assigned and linked like a tree inside yyparse () (you do the logic), and the root node address is passed back in the pAst pointer.

0
source

All Articles