Search code examples
compiler-errorsbnfjavacc

JAVACC ERROR choice conflict in (...)* construct


i am using javacc for the first time.

I keep getting this error :

Warning: Choice conflict in (...)* construct at line 35, column 43.
     Expansion nested within construct and expansion following construct
     have common prefixes, one of which is: <SPACE>
     Consider using a lookahead of 2 or more for nested expansion.

I've looked into this and found out it is due to a choice which the parser does not know which is the most appropriate.

void l_zero() : {} {
 TOKEN1 ( TOKEN2 TOKEN1)*  
}

The error arrises just before the ( in this line, could any one help me understand this properly please?


Solution

  • You have (I think)

    void l_zero() : {} {
        <TOKEN1> ( <SPACE> <TOKEN1> )*  
    }
    

    The problem is that a <SPACE> can follow an l_zero. For example, there might be a production like this somewhere

    void someNonterminal() : {} {
        l_zero() <SPACE> <YETANOTHERTOKEN>
    }
    

    Suppose the remaining input when l_zero starts is

    <SOMETOKEN> <SPACE> ...
    

    After consuming the <SOMETOKEN> the parser needs to decide whether to enter the loop or return from l_zero. It can't decide on the basis of the next token. This is what the error message is telling you.


    What to do?

    Is it the case that any space that follows a l_zero can not be followed by a <TOKEN1>? If so the parser can decide whether to enter the loop based on the next two tokens. You can resolve the conflict with either of the following:

    void l_zero() : {} {
        <TOKEN1> ( LOOKAHEAD(2) <SPACE> <TOKEN1> )*  
    }
    

    or

    void l_zero() : {} {
        <TOKEN1> ( LOOKAHEAD(<SPACE> <TOKEN1>) <SPACE> <TOKEN1> )*  
    }