Pregunta

Suppose I have this string to parse: ABAA and the next grammar:

public void parse_X() :
{}
{
  (  
     LOOKAHEAD(parse_AA())
     parse_AA()
   |
     parse_AB()
   )*
 }

public void parse_AA() :
{}
{ <A> <A> }

public void parse_AB() :
{}
{ <A> <B> }

It is clear that there is no ambiguity nor choice conflicts, but I'm getting one warning that claims that there is a choice conflict at line 4. My guess is that JavaCC can't remember that the LOOKAHEAD(parse_AA()) failed and therefore, a parse_AA() can't follow.

Full warning message:

Warning: Choice conflict in (...)* construct at line 4. Expansion nested within construct and expansion following construct have common prefixes, one of which is: "A" Consider using a lookahead of 2 or more for nested expansion. Parser generated with 0 errors and 1 warnings.

How can I avoid this warning? Is there an alternative grammar? (Other than factorizing the common < A > )

¿Fue útil?

Solución

Try the following

public void parse_X() :
{}
{
  ((  
     LOOKAHEAD(parse_AA())
     parse_AA()
   |
     parse_AB()
   ))*
 }

If that doesn't work, the problem is that there is something that follows an X that can start with an <A>.


Edit.

Since that didn't work, try the following:

public void parse_X() :
{}
{
  ( LOOKAHEAD( <A> )
   (  
     LOOKAHEAD(parse_AA())
     parse_AA()
   |
     parse_AB()
   )
  )*
 }

Note that all this will do is suppress the warning. You'll have to to decided how to best deal with the ambiguity in your grammar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top