[antlr-interest] Changing the base node type

Sam Harwell sharwell at pixelminegames.com
Fri Feb 6 08:06:20 PST 2009


You'll also see to call setTreeAdaptor (java target) in the @init section of the entry rule(s) to your grammar. Your implementation of TreeAdaptor should include implementations of at least the following 2 functions (example here is the C# target just because I had it ):

class GrammarASTAdaptor : CommonTreeAdaptor
{
    public override object create( IToken payload )
    {
        GrammarAST t = new GrammarAST( payload );
        return t;
    }
    public override object errorNode( ITokenStream input, IToken start, IToken stop, RecognitionException e )
    {
        GrammarAST t = new GrammarASTErrorNode( input, start, stop, e );
        return t;
    }
}

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
Sent: Friday, February 06, 2009 9:56 AM
To: Bruno Marc-Aurele
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Changing the base node type

Hi. There should be a faq entry on this or an example.  Implement  
TreeAdaptor :)
Ter
On Feb 6, 2009, at 6:17 AM, Bruno Marc-Aurele wrote:

> Hello all,
>
> I am trying to do a grammar with a new base type for nodes. I can  
> see that I am
> provided with ASTLabelType, whose value I set to Node, the name of my
> CommonTree subclass. The only difference between the two classes is  
> the
> presence of a "Accept" method. The parser, however, still creates  
> CommonTree
> nodes (of course, the ITreeAdaptor is still a CommonTreeAdaptor).
>
> What would be the suggested method to make my parser create Node  
> objects instead
> of CommonTree objects?
>
> Thanks a lot,
> Bruno M-A.
>
> P.S.: I am using the C#2 target.
> --
> Bruno Marc-Aurèle
> courriel Poly: bruno.marc-aurele at polymtl.ca
> courriel perso: bruno.m-a at hotmail.com
>
>
>
>
>
> 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