parsinglr-grammarshift-reduce-conflictreduce-reduce-conflict

Resolving reduce/reduce conflicts


We have a CFG grammar and we construct LR(1) Parsing Table. We see that one cell on the parsing table have a reduce - reduce conflict. Is it possible to solve this conflict by using more input symbols of lookahead at each step? I am asking this beacuse I think that by increasing lookahead symbols, we can(not always) only resolve shift - reduce conflicts.I mean the extra lookaheads in a reduce-reduce conflict doesn't help us. Am I right ?


Solution

  • It might be possible to solve a reduce/reduce conflict with more lookahead. It might also be possible to solve it by refactoring.

    It really depends on the nature of the conflict. There is no general procedure.

    An example of a reduce/reduce conflict which can be solved by extra lookahead:

    A → something
    B → A
    C → A
    D → B u v
    D → C u w
    

    Here, the last two productions of D are unambiguous, but the decision about reducing A to B or to C cannot be made when the u is seen. One more symbol of lookahead would do it, though, because the second next symbol determines the reduction.

    The refactoring solution:

    Au → A u
    Bu → Au
    Cu → Au
    D  → Bu v
    D  → Cu w
    

    By deferring the B/C choice by one token, we've succeeded in removing the reduce/reduce conflict. Note that this solution will work even if u is not a single token; it could, for example, by a non-terminal. So this model may work in cases where simply increasing lookahead is not sufficient.