following: [antlr-interest] learning pitfall....

John P N Pybus john-yahoo at pybus.org
Wed Apr 3 06:33:51 PST 2002


On Wednesday 03 Apr 2002 12:25 am, you (Lloyd Dupont) wrote:
> > > But if I add this production in the grammar "java.g":
> > > // ------- added in java.g, in JavaRecognizer class ----------
> > > script : (statement | importDefinition) *  ;
> > > //---------------------------------------------------------------------
> > > the grammar (in examples/java/java) no longer work
> >
> > The problem is that this rule can be empty (zero or more times
> > something). Which will not work (antlr needs lookahead to decide things
> > if a rule can be empty there's nothing to decide with...) Rewriting to
> > something like ....
>
> after more reflexion I am not really satisfyied.
> It appear that this help me find solution but:
>
> 1st/
> the script is not EMPTY, there is at least "EOF" (implictly added), you
> could have blank script (file empty) and it should work !
>
> 2n/
> // --------------------------
> script: ( statement ) * ;
> // --------------------------
> work perfectly without problem. but I do want to have import !
>
> .....
> so anyidea about what's wrong with importDefinition rule ?
> (examples/java/java/java.g grammar)

As the error says it's the importDefinition rule antlr is worried about.

identifierStar
	:	IDENT
		( DOT^ IDENT )*
		( DOT^ STAR  )?
	;

The appearance of 'statement' (which may start with IDENT) as an alt in the 
'script' rule puts IDENT into the k=2 lookahead set for the (DOT IDENT) 
alternative.  It now thinks it sees non-determinism between another go at 
(DOT IDENT) and moving on (even though it isn't actually any!).  This is the 
limitation of linear approximate LL(k) rather than the real thing (I'm sure 
there's lots more on this in the FAQ).

Now that you know the non-det warning is false alarm (the grammer does 
actually match statements and imports as you expected) you can disable the 
warning for the identifierStar rule, and all is well.

John

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list