Search code examples
compiler-constructionlexerjavacc

How to resolve conflict in choice in multiple choices case that have the same token


I'm trying to write a compiler for some specific format of messages. My problem now since i'm building different formats of specific messages is described like this

< WORD : ([LETTER]){2,5}>
< ANOTHER_WORD : (<LETTER>|<DIGIT>){1,5}>
< SPECIAL_WORLD_EN : "START">
< SPECIAL_WORLD_FR : "COMMENCER">
< SPECIAL_END_WORLD_EN : "END">
< SPECIAL_END_WORLD_FR : "FIN">
< LEFT_BRACKET : "[">
< RIGHT_BRACKET : "]">
void grammar():
{
}
{ 
LOOKAHEAD(2)
 <LEFT_BRACKET><SPECIAL_WORLD_EN>< WORD ><RIGHT_PAREN>
| <LEFT_BRACKET><SPECIAL_WORLD_FR>< WORD >< ANOTHER_WORD ><RIGHT_BRACKET>
| <LEFT_BRACKET><SPECIAL_END_WORLD_EN>(< WORD >)?<RIGHT_BRACKET>
| <LEFT_BRACKET><SPECIAL_END_WORLD_FR>(< WORD >)+<RIGHT_BRACKET>
}

So the LOOKAHEAD(2) solves the conflict for the first two choices. How can I solve the conflict for the other choices ( in the real grammar I have more than 4 choices)

UPDATE I managed to remove the choice conflict warning by using a LOOKAHEAD(2) in each choice

void grammar():
{
}
{ 
LOOKAHEAD(2)
 <LEFT_BRACKET><SPECIAL_WORLD_EN>< WORD ><RIGHT_PAREN>
| LOOKAHEAD(2) <LEFT_BRACKET><SPECIAL_WORLD_FR>< WORD >< ANOTHER_WORD ><RIGHT_BRACKET>
| LOOKAHEAD(2) <LEFT_BRACKET><SPECIAL_END_WORLD_EN>(< WORD >)?<RIGHT_BRACKET>
| LOOKAHEAD(2) <LEFT_BRACKET><SPECIAL_END_WORLD_FR>(< WORD >)+<RIGHT_BRACKET>
}

I'm not sure it's the best solution, is there any other better/correct way to resolve it?


Solution

  • Left factor:

    void grammar():
    { }
    { 
        <LEFT_BRACKET>
        ( <SPECIAL_WORLD_EN>< WORD ><RIGHT_PAREN>
        | <SPECIAL_WORLD_FR>< WORD >< ANOTHER_WORD ><RIGHT_BRACKET>
        | <SPECIAL_END_WORLD_EN>(< WORD >)?<RIGHT_BRACKET>
        | <SPECIAL_END_WORLD_FR>(< WORD >)+<RIGHT_BRACKET> )
     }