[antlr-interest] rewrite rule element cardinality

joestelmach at gmail.com joestelmach at gmail.com
Tue Feb 23 11:40:27 PST 2010


I'm having some trouble understanding the behavior of AST rewrite rule element cardinality in certain cases. Consider the following grammar:

grammar Test;

options {
output=AST;
}

tokens {
GROUP;
FOO;
BAR;
BAZ;
}

search 
: c (SPACE option)+ -> ^(GROUP c option)+
;

c
: C -> FOO BAR
;

option 
: (A | B) -> BAZ
;

A : 'a';
B : 'b';
C : 'c';
SPACE : ' ';

If we feed the parser the input string: "c a b a", I'm expecting to see the following AST:

(GROUP FOO BAR BAZ) (GROUP FOO BAR BAZ) (GROUP FOO BAR BAZ)

but instead, I get the following:

(GROUP FOO BAR BAZ) (GROUP FOO BAR BAZ BAZ) (GROUP FOO BAR BAZ BAZ)

Notice the extra BAZ in each child after the first? Can someone explain why this is happening? I'm using ANTLR 3.2 with the java target.

Thanks

- Joe


More information about the antlr-interest mailing list