[antlr-interest] tree grammar question...

Gavin Lambert antlr at mirality.co.nz
Mon Dec 29 13:28:08 PST 2008


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