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

John B. Brodie jbb at acm.org
Sun Aug 17 17:39:11 PDT 2008


Greetings!

Le Lann Jean-Christophe wrote (in part):
>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 :

... remainder snipped ...

i suspect that you have specified options { output=AST; } in your
problematic grammar.

i believe that ANTLR spawns those <rule_name>_return class definitions
for every rule in a grammar that generates an AST.  needed to capture
the possible multiple return values --- like you have specified.

Hope this helps...
   -jbb


More information about the antlr-interest mailing list