[antlr-interest] Problem writing a Grammar

Gavin Lambert antlr at mirality.co.nz
Fri May 4 05:23:23 PDT 2007


At 22:53 4/05/2007, lists at mainiero.de wrote:
 >Aborting because the following rules are mutually 
left-recursive:
 >    [functioncall, prefixexp, var]
 >
 >Perhaps anyone can have a look at it to give me hints or 
solutions
 >how to get this grammar correct. I'm willing to donate this
 >grammar, to make it available on the ANTLR side.

With a bit of rearranging and some assistance from ANTLRworks' 
automatic left-recursion-remover, I've come up with this (not 
totally sure it matches the original, but I think it does):

varOrExp: var | '(' exp ')';
nameAndArgs: (':' NAME)? args;
varSuffix: nameAndArgs* ('[' exp ']' | '.' NAME);

var: (NAME | '(' exp ')' varSuffix) varSuffix*;
prefixexp: varOrExp nameAndArgs*;
functioncall: varOrExp nameAndArgs+;

(You can probably come up with better names for the first three 
rules.) :)

Basically I did this by taking each of the last three rules in 
isolation, then inlining any references it made to one of the 
other problem rules, rearranging and extracting subrules out until 
I got a simple left-recursion, then using ANTLRworks' "Remove Left 
Recursion" function, and finally tidying up the result a bit.



More information about the antlr-interest mailing list