[antlr-interest] How to access member of CommonTree subclass

Kaleb Pederson kaleb.pederson at gmail.com
Mon Oct 26 15:55:04 PDT 2009


I'm working with a tree parser that is creating ASTs of a custom type. This CommonTree subclass contains a member named elementType that identifies the type represented by the tree.  How do I set the elementType dynamically within an action?

I tried in the following way:

id:
  ^(AST_ID tok+=ID+)
  {
    $tree.setElementType( typeResolver.resolve(StringUtils.join($tok, ".")) );
  }
  ;

This, however, doesn't work since the tree has not yet been set:


                //...     
                ((ElementTypeTree)retval.tree).setElementType( typeResolver.resolve(StringUtils.join(list_tok, ".")) );
            }
            retval.tree = (ElementTypeTree)adaptor.rulePostProcessing(root_0);

In this context, I need semantic information in order to resolve the type, so I can't statically tell ANTLR to create a different tree type (as I do in other contexts).

Thanks for the suggestions.

--
Kaleb Pederson

Twitter - http://twitter.com/kalebpederson
Blog - http://kalebpederson.com


More information about the antlr-interest mailing list