[antlr-interest] AST rewrite infinite loop

Gerald Rosenberg gerald at certiv.net
Wed Oct 14 16:21:34 PDT 2009


At 03:46 PM 10/14/2009, Bill Andersen wrote:

>On Oct 14, 2009, at 16:38 , John B. Brodie wrote:
>
> > Greetings!
> >
> > On Wed, 2009-10-14 at 16:15 -0400, Bill Andersen wrote:
> >> FYI, all.  The problem described does NOT occur without rewrite =
> >> true
> >>
> > Isn't rewrite = true for Tree grammars only? -- your rule below looks
> > like a Parser rule to me - altho can't really tell for sure...
>
>Nah, it's a tree grammar, 'sentence' bundles up a bunch of different
>AST patterns for sentences, like
>
>sentence
>         : ^(ATOM term+)
>         | ^(IMPL sentence sentence)
>         | ...
>         ;

So, the "assertion" tree grammar rule will match on every instance of 
a "sentence" node in the AST.

If the rewrite is being performed in place, the tree pattern match 
will recurse infinitely on the rewritten nodes.  This would match 
your reported results, including the consequence of turning rewrite 
off.  Have to check the generated tree parser code to verify that 
this is what is going on.

If the rewrite is unique -- the source AST does not contain ASSERTION 
rooted sentence nodes that you want to rewrite -- you can add a 
predicate to prevent the recursion.

assertion : { !hasParent(ASSERTION) }? sentence -> ^(ASSERTION 
NAME["foo"] sentence)





> >> On Oct 14, 2009, at 11:20 , Bill Andersen wrote:
> >>
> >>> Hi folks
> >>>
> >>> I have this production
> >>>
> >>> assertion
> >>>     : sentence -> ^(ASSERTION NAME["foo"] sentence)
> >>>     ;
> >
> > Appended below is a COMPLETE Parsing example using just this rule.
> >
> > And it WORKS just fine.
> >
> > Could you please modify this complete example so that it will
> > demonstrate the error you are seeing. And then maybe someone will be
> > better able to figure out what is the cause.
> >
> > Thank You
> >   -jbb
> >
> >>>
> >>> in a tree grammar with rewrite = true.  This is meant to tack a name
> >>> onto the results of parsing a 'sentence'.  Thing is when I parse a
> >>> sample input I get an infinite tree that looks like this
> >>>
> >>> (ASSERTION foo
> >>>     (ASSERTION foo
> >>>             (ASSERTION foo
> >>>                     (ASSERTION foo
> >>>                             (ASSERTION foo
> >>>                                     ....
> >>>
> >>> Thing is that the sentence production does not call assertion,
> >>> otherwise I could understand why my rewrite might get itself
> >>> reparsed
> >>> or something.
> >>>
> >>> Any idea what's happening here and how to stop it?
> >



More information about the antlr-interest mailing list