[antlr-interest] Best way to construct informative AST nodes?

Jared Klumpp jklumpp at harmonia.com
Thu Oct 22 12:16:55 PDT 2009


My goal is to create the AST in a way that I can quickly access information about *important* child nodes - for example if I had:

proc : PROCEDURE ident formalPartOpt body -> ^(PROCEDURE_DECL ident formalPartOpt body);

I would like this to create an AST node that would have the identifier and parameters easily accessible, with something like ASTProc.getIdent() or ASTProc.getParams();

I've found three ways of doing this, all seem like they blend the code and grammar too much:

1. Creating heterogeneous tree nodes like on this page: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction - and passing the identifier and formalPartOpt trees as parameters

2. Using custom AST nodes, and whenever a child is added to a custom ASTProc node, testing if it is of a type I care about - i.e., if child.getType() == Lexer.IDENT, store that as the name of the procedure.

3. Wait until the information is requested from the node and traverse the tree to find the name or parameters.

Are there other ways of doing this that are more efficient/decouple the AST creation from the grammar a little better?

Thanks in advance,
Jared
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091022/24e94d99/attachment.html 


More information about the antlr-interest mailing list