[antlr-interest] Problem with Duplicating nodes/trees

Mark Volkmann r.mark.volkmann at gmail.com
Fri Feb 8 06:38:17 PST 2008


On Feb 8, 2008 8:00 AM, Grzegorz Cieslewski <cieslewski at hcs.ufl.edu> wrote:
> Hello All,
>
> I have encountered a problem with tree duplication during AST
> construction.  I boiled my problem to following grammar.
>
> grammar T2;
> options {output=AST;}
> tokens
> {
>         TEST;
> }
> a : c (',' c)* b -> ^(TEST c b)+
> ;
> b       :       INT;
> c       :       ID;
>
> ID : 'a'..'z'+ ;
> INT : '0'..'9'+;
> WS : (' '|'\n') {$channel=HIDDEN;} ;
>
> For input like "x, y, z 1", I was expecting to get a tree looking like
>  ^(TEST x 1) ^(TEST y 1) ^(TEST z 1),
> but the antlr seems not to duplicate the b rule and I get ^(TEST x 1)
> ^(TEST y) ^(TEST z).
>
> Does any one know what am I doing wrong?  Is this a bug or a "feature".

Based on section 7.5 of the book, in the subsection titled
"Duplicating Nodes and Trees", what you have looks correct. The
relevant statement is this.

"In a rewrite rule, ANTLR duplicates any element with cardinality one
when referenced more than once or encountered more than once because
of an EBNF * or + suffice operator."

b is your element with a cardinality of one and it is encountered more
than once in your rewrite rule with a + operator, but it isn't being
duplicated.

I tried your grammar in the latest intermediate build and it doesn't
work. I hope someone else has an idea why. I think it's possible
you've found a bug.

-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list