Is there a shift/reduce error in this yacc code?
I'm getting a message from yacc saying that there is a shift/reduce conflict. I think it's coming from this part of the yacc file.
statement : expre开发者_如何学Pythonssion_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt ;
selection_stmt : IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement ;
expression : var '=' expression | simple_expression ;
Can you see a conflict? How can it be fixed?
Yes, I'm seeing a conflict. The selection_statement
rule matches expressions like
IF(<expression 1>)
THEN
IF(<expression 2>)
THEN <expression statement 1>
ELSE <expression statement 2>
But that's ambiguous. It could also be
IF(<expression 1>)
THEN
IF(<expression 2>)
THEN <expression statement 1>
ELSE <expression statement 2>
精彩评论