Aakash Anuj
Aakash Anuj

Reputation: 3871

Reduce -reduce conflict in grammar

I am getting a reduce/reduce conflict for the following grammar in bison because n is also an epsilon production.

m   :                       {$$=line_no;}
    ;
n   :                       {   

                            $$.nl=makelist(line_no);
                            codelines[line_no].opcode=GOTO;
                            codelines[line_no].result=0;
                            line_no++;
                            }
    ;
selection_statement
    : IF '(' expression ')' m statement         %prec THAN
    | IF '(' expression ')' m statement n ELSE m statement
    | SWITCH '(' expression ')' statement
    ;

How do I modify this to remove this conflict?

Upvotes: 0

Views: 180

Answers (2)

rici
rici

Reputation: 241911

As you say, the problem is the epsilon reduction of n, which needs to be associated with the shift of the ELSE. The problem will occur in exactly the same circumstance as the "ambiguous" else, since it will not be clear to which if statement the n belongs.

The obvious and easy solution is to move the n after the ELSE since it does not make any difference whether the reduction action happens before or after the shift, and after the shift it is clear even to an LALR parser.

Upvotes: 1

Himanshu
Himanshu

Reputation: 2454

Your grammar doesn't seem to use the value of m. So you might as well remove all references to m and its rule.

Upvotes: 0

Related Questions