[antlr-interest] expression list in tree grammar

Michael Bedward michael.bedward at gmail.com
Wed Jan 28 18:27:13 PST 2009


Hello all,

I think I should probably apologize in advance for this question
because (a) it's probably really obvious and (b) I know it's been
asked before but as a newbie I've had trouble understanding the
previous posts or finding the solution in the book.

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 !

I'd be pathetically grateful if someone could point me in the right
direction here :-)

Michael


More information about the antlr-interest mailing list