2

I've this code in my JavaCC parser:

< VARIABILE : "§" < LETTERA > ( < CIFRA > | < LETTERA > )* >

< TERMINE: ( < NUM_SEGNO > | < VARIABILE > | "(-" < VARIABILE > ")" ) >

I get this error when compiling

Regular Expression choice : VARIABILE can never be matched as : TERMINE

How can I fix this?

walter4991
  • 45
  • 1
  • 6
  • Any help here: http://stackoverflow.com/questions/791591/explanation-and-solution-for-javaccs-warning-regular-expression-choice-foo-c?rq=1 – Mike Jul 21 '14 at 17:46

1 Answers1

1

In your production for TERMINE, the second alternative is useless; you might as well write

< TERMINE: ( < NUM_SEGNO > | "(-" < VARIABILE > ")" ) >

That's what the error message is telling you. Why is it useless? JavaCC's regular expressions obey the three rules explained in FAQ 3.3. Go read about them, before you read further. ... Ok you're back. You now should understand that, if the longest prefix of the input that matches any rule matches the rule for < VARIABILE > (and therefore also your rule for <TERMINE>), then the rule for < VARIABILE > will beat the rule for < TERMINE > by virtue of being first in the .jj file.

What to do to fix this depends on what you want to achieve. My guess is that you should move the choice up to the parser level. I.e. delete the rule for TERMINE and replace it with a syntactic rule

void Termine() : {} {
    <NUM_SEGNO>
|
    <VARIABILE>
|
    "(-" <VARIABLE> ")"
}

For other possibilities, see FAQ 3.6 and FAQ 4.19.

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45