简体   繁体   中英

How To Avoid False Choice Conflict Warning in JavaCC?

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 > )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM