[antlr-interest] Unexpected behavior while using += in a tree grammar

Luis Pureza pureza at gmail.com
Sat Jan 9 09:03:33 PST 2010


Hi,

I've started using antlr a few days ago, so let me begin by thanking
everyone that contributed for creating this fantastic project.

Unfortunately, I think I ran into a bug and I'm hoping you might help me.

I'm using a tree grammar where I have the following rule:

expr returns [Expr value]
   | ID                                                { $value = new
Var($ID.text); }
   | ^(APP fn=expr (args+=expr)+ { $value = new App($fn.value, $args); }
   ...

Surprisingly, $args is a list of CommonTrees, and not a list of Expr
as I was hoping it would be. Is this a bug or a feature? If it's the
latter, is there any way to "convert" the tree into an Expr?

For now, I'm collecting args manually, with the following workaround:

expr returns [Expr value]
@init {
 List<Expr> ops = new ArrayList<Expr>();
}
   | ^(APP fn=expr (op=expr { ops.add($op.value); })+) { ... }
   | ID
                   { ... }

Thanks!

Luís Pureza


More information about the antlr-interest mailing list