[antlr-interest] How to return my own objects ?

Jim Idle jimi at temporal-wave.com
Sun Aug 17 20:25:26 PDT 2008


On Mon, 2008-08-18 at 01:07 +0200, Le Lann Jean-Christophe wrote:

> Johannes Luber a écrit : 
> 
> > Le Lann Jean-Christophe schrieb:
> >   
> > 
> > > Hello,
> > > 
> > > I am struggling to return my own objects in the rules (v3.1). Normally
> > > this is obvious like :
> > > 
> > > expr returns [MyExpr expr] :
> > >    ^( '+' e1=expr e2=expr) { $expr = new MyExpr("+",e1,e2);}
> > >    | INT                   { $expr = new MyExpr(new String($INT.text));}
> > >        ;
> > > this perfectly generates (MyExpr returned) :
> > > // DumpModified.g:8:1: expr returns [MyExpr expr] : ( ^( '+' e1= expr
> > > e2= expr ) | INT );
> > >    public final MyExpr expr() throws RecognitionException {
> > > 
> > > but in a bigger grammar in which I have this rule :
> > > 
> > > architecture returns [Architecture arch] :
> > >                    ^(ARCHITECTURE ID <skipped>)            {$arch=new
> > > Architecture(new String(ID.text));}
> > > 
> > > the generated code is :
> > > 
> > > // Walker.g:23:1: architecture returns [Architecture arch] : ^(
> > > ARCHITECTURE ID <skipped>) ;
> > >    public final Walker.architecture_return architecture() throws
> > > RecognitionException {
> > > 
> > > ==> "Walker.architecture_return" returned while I expected "Architecture"
> > > 
> > > What I am missing ? Could you explain or give me a pointer (I have the
> > > book) ?
> > >     
> > 
> > 
> > architecture_return is a generated type, which encapsulates an
> > Architecture object. ANTLR hasn't been optimized yet to not to do that
> > if only one object is returned from a rule.


It has but if you are generating an AST, then you have your return value
and the tree to return. I think that the problem here is that both the
rule and the return value have been called expr. Change the name of the
return value to retExpr or something like that.

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080817/af2ed82b/attachment.html 


More information about the antlr-interest mailing list