[antlr-interest] Custom AST nodes - attempt #2

Martijn Reuvers martijn.reuvers at gmail.com
Fri Jul 23 06:13:01 PDT 2010


Hey Bill,

It seems so, but its not hard. Roughly you should create the adaptor
below this email, where you implement your own ASTNodes. And then set
it on the parser you are using, e.g:

    SimpleParser parser = new SimpleParser(new
CommonTokenStream(createLexer(value)));
    parser.setTreeAdaptor(new CustomTreeAdaptor());
    return parser

Martijn


----------------

public class CustomTreeAdaptor extends CommonTreeAdaptor  {

  @Override
  public Object create(Token payload) {
    return new ModelASTNode(payload);
  }

  @Override
  public Object dupNode(Object t) {
    if(t == null) {
      return null;
    }
    return create(((ModelASTNode)t).token);
  }

  @Override
  public Object errorNode(TokenStream input, Token start, Token stop,
RecognitionException e) {
    return new CustomErrorNode(input, start, stop, e);
  }

}



On Fri, Jul 23, 2010 at 2:33 AM, Bill Andersen <bill.andersen at mac.com> wrote:
> Hi Ter
>
> I set ASTLabelType = ASNode in options on the tree grammar
>
> Got runtime cast error
>
> Exception in thread "main" java.lang.ClassCastException: org.antlr.runtime.tree.CommonTree cannot be cast to com.hf.lang.eclif.ltt.ASNode
>
>  in generated code:
>
>            // /Users/bill/Documents/Eclipse Workspaces/workspace2/OWExperimental/LTT2/com/hf/lang/eclif/ltt/ASPass1.g:21:4: ^( AS_SPEC ( rule )+ )
>            {
>            root_0 = (ASNode)adaptor.nil();
>
> I get what's going on in ANTLR - the default adaptor is spitting out CommonTree instances.  This seems to imply that one has to supply a custom adaptor.  Is this true?
>
> WIth no adaptor and with ASTLabelType = CommonTree the tree grammar works just fine - with no actions.
>
> Now, when I try to access a node in an action in a tree grammar as an ASNode, I can only get it to work with an explicit cast in the semantic action
>
> simple_alt
>        : AS_TYPE
>        { System.out.println( ">>> " + ((ASNode) $AS_TYPE).nodeType ); }
>        | ^(AS_SUBRULE ID)
>        ;
>
> Is this the verdict?  Either write an adaptor that does the right thing or live with the casts?
>
> On Jul 22, 2010, at 16:34 , Terence Parr wrote:
>
>> Hi Bill. try setting ASTLabelType in tree grammar.
>> T
>> On Jul 22, 2010, at 12:26 PM, Bill Andersen wrote:
>>
>>> Hi Folks,
>>>
>>> This is my second attempt at asking this question.  Hopefully it is clear.  If not, please let me know.
>>>
>>> I'm interested in using a custom AST node class to carry type information.
>>>
>>> I do not want to build a custom TreeAdaptor to determine node type based on token id because only a few AST nodes will have to be of the custom class.
>>>
>>> Here's a sample production from the tree generating grammar.
>>>
>>> rule
>>>      : ... match something ...
>>>      -> ^(NODE<CustomType>[param] ...)
>>>      ;
>>>
>>> So far so good.  I know how to do this.
>>>
>>> Let's say CustomType looks like this
>>>
>>> class CustomType extends CommonTree {
>>>      public T param;
>>>      public CustomType(int ttype, T param) { ... }
>>>      public T getParam() { ... }
>>>      ...
>>> }
>>>
>>> Now I have another grammar that consumes ASTs from the first grammar.  I want to do this:
>>>
>>> rule
>>>      : ^(NODE ...)
>>>        { ... do something with this information ... }
>>>      ;
>>>
>>> So, what syntax do I need in the action to get a T-typed param value from this node?
>>>
>>> Any help appreciated
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list