[antlr-interest] tree grammar question...
pady
prabha_pady at comcast.net
Mon Dec 29 13:31:25 PST 2008
Thank you.
-- pady
----- Original Message -----
From: "Gavin Lambert" <antlr at mirality.co.nz>
To: "pady" <prabha_pady at comcast.net>; <antlr-interest at antlr.org>
Sent: Monday, December 29, 2008 4:28 PM
Subject: Re: [antlr-interest] tree grammar question...
> At 08:10 30/12/2008, pady wrote:
> >In my parser, I have rules like...
> >
> >statement
> > : assignment
> > | ifStatement
> > | compoundStatement
> > ;
> >
> >compoundStatement
> > : '{' statement* '}' -> statement*
> > ;
> >
> >In the equivalent tree grammar, I have
> >
> >statement
> > : assignment
> > | ifStatement
> > | compoundStatement
> > ;
> >
> >compoundStatement
> > : statement* // using the rewrite rule alone...
> > ;
> >
> >But when i build my tree grammar, I get a "mutually
> left-recursive"
> >rule error between statement/compoundStatement. I dont get this
> >error when building the parser grammar. I understand the issue,
> but
> >I am not sure how this can be resolved. I saw a similar rule in
> the
> >java tree grammar in the antlr grammar list. Not sure how the
> tree
> >grammar there will avoid this left-recursive error.
>
> You need to include a bit more structure in the AST, so that it
> doesn't get itself into an infinite loop trying to work out where
> the compoundStatements end and the statements begin. The simplest
> way to do that is to use an imaginary token:
>
> tokens { BLOCK; }
>
> compoundStatement
> : '{' statement* '}' -> ^(BLOCK statement*)
> ;
>
> ...
>
> compoundStatement
> : ^(BLOCK statement*)
> ;
>
More information about the antlr-interest
mailing list