[antlr-interest] Problems overriding lookahead

Andreas Bartho andreas.bartho at inf.tu-dresden.de
Mon Aug 20 11:00:26 PDT 2007


Hi,

as far as I know it is allowed to set the k option on a per-rule basis.
I'm currently dealing with a huge grammar (C#) for which I cannot use
LL(*). (Parser creation takes ages, I cancelled it after 30 minutes or
so). Using k=2 the parser creation takes only 2 minutes or so.

There are, however, some nasty rules that require LL(*).

I would like to override k for those rules, but I don't get it to work.
I made a test grammar that contain only the LL(*) rules, the resulting
compiler works as it should. Then, as a test I tried to set global k=2
and for each rule k=*.

The resulting parser behaves differently, but still not the same as if
k=* was set globally. This little example illustrates the point:

grammar csharpstatement_snip;
options {output=AST; k=2; backtrack=true;}


expr
options {k=*;}
	:	expra
	|	exprb
	;
	

expra
options {k=*;}
	:	rec '(' ')'
	;

exprb
options {k=*;}
	:	rec '++'
	;

//rule has originally been introduced to remove left recursion
//therefore name rec
rec
options {k=*;}
	: ( '(' ')' | '++' )*
	;

The resulting parser returns for input "()" the following error message:
recoverFromMismatchedToken
BR.recoverFromMismatchedToken
line 0:-1 mismatched input '<EOF>' expecting '('

When the rule options are removed, the following error message appears:

recoverFromMismatchedToken
BR.recoverFromMismatchedToken
line 0:-1 mismatched input '<EOF>' expecting '++'

The parse trees shown by ANTLRWorks are also quite different from each
other.

Setting k=* in the global options works fine

What is my mistake here?

Andreas





More information about the antlr-interest mailing list