[antlr-interest] Problem with Duplicating nodes/trees

Grzegorz Cieslewski cieslewski at hcs.ufl.edu
Fri Feb 8 07:58:07 PST 2008


I looked through the code that ANTLR generates.  It seems to me that
the function
org.antlr.runtime.tree.RewriteRuleElementStream.next() is not
following the spec.
It does not perform duplication correctly when the size()==1.


On Feb 8, 2008 9:38 AM, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:
> 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.
>



-- 
=====================================================
Grzegorz Cieslewski
Research Assistant
High-performance Computing & Simulation (HCS) Research Laboratory
University of Florida, Dept. of Electrical and Computer Engineering
330 Benton Hall, Gainesville, FL, 32611-6200
Phone: (352) 392-9041
Email: cieslewski at hcs.ufl.edu
Web: www.hcs.ufl.edu
=====================================================


More information about the antlr-interest mailing list