Yulfy
Yulfy

Reputation: 395

JavaCC choice conflict warning

I'm having a singular issue with JavaCC at the moment. I have my grammar defined, the language is LL(1) and all Left Recursion has been removed. I'm getting a choice conflict error (dumped below). I believe it is caused by the line expr2() line calling itself and causing a conflict. I'm honestly completely stumped. I don't even know how to go about fixing the problem.

Here's the snippet of code generating the warning:

void expr() : {}
{
  <LBRAC> arg_list() <RBRAC> expression2()
}

void expr2() : {}
{
  section() (<SUB>|<ADD>|<MUL>|<DIV>|<MOD> section())* expr2()
  | {}
}

void section() : {}
{
  <IDENTIFIER> | <TRUE> | <FALSE> | <REAL> | (<ADD> | <SUB>) section() | expr()
}

And the warning is:

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

Where line 233 corrisponds to first line in expr2()

Upvotes: 2

Views: 630

Answers (1)

@Juan Lopes wrote:

Consider "2 + 2". The "+" is the <ADD> at expr2() or<ADD> at section()?

The OP replied:

I can remove the Add/Sub commands from section() and it still causes the same issue. I understand what you mean though, but that doesn't appear to be causing the problem.

@Theodore Norvell wrote:

What Juan said is entirely correct. But changing (<ADD> | <SUB>) section() to section() introduces a new problem. Try changing the first line in the body of expr2 to either section() (<MUL>|<DIV>|<MOD> section())* expr2() or section() ((<MUL>|<DIV>|<MOD>) section())* expr2() depending on what you actually want. If that doesn't work, ask a new question. Juan has answered this one.

Upvotes: 1

Related Questions