[antlr-interest] expression list in tree grammar

John B. Brodie jbb at acm.org
Wed Jan 28 20:12:16 PST 2009


Micheal Bedward asked:
> In my parser grammar:
>
> expr_list       : (expr (',' expr)* )? -> ^(EXPR_LIST expr*) ;
>
> In my tree grammar:
>
> expr_list returns [List<Double> values] : ^(EXPR_LIST expr*)
>                   { $values = new ArrayList<Double>();
>                     int n = $EXPR_LIST.getChildCount();
>                     for (int i = 0; i < n; i++) {
>                         $values.add($expr.value);
>                     }
>                   }
>                 ;
>
> Yuck !!  I can see I'm doing ANTLR's work here insstead of letting it
> work for me - but I haven't been able to see how to collect the values
> of 0 or more expressions. I understand that I can't use list label
> syntax here because I don't have output=AST for the tree grammar.  I
> can see by looking at the generated walker code that there is a loop
> iterating through the expr values, after which my ugly code does the
> same thing !

have you tried placing your action inside antlr's loop?

something like (untested):

expr_list returns [ List<Double> values ] : 
    { $values = new ArrayList<Double>(); }
    ^(EXPR_LIST ( e=expr {$values.add($e.value);} )*)
;

hope this helps
---
   -jbb

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


More information about the antlr-interest mailing list