tags:

views:

106

answers:

1

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 : expression_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?

+1  A: 

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>
Arthur Reutenauer
hooo ! the good-old dangling else problem !
Adrien Plisson
Hey, I had to discover it by hand! I inspected the grammar for that.
Arthur Reutenauer
Yeah, I thought it was in there. Just needed to confirm. So how can it be fixed?
Phenom
if you thought it was there, why did you ask your question this way ???
Adrien Plisson
This person is amazing :-)
Arthur Reutenauer
@Phenom: you have asked this precise question in a number of different ways over the last few days. Just *read* the answers! Your solution is there.
Arthur Reutenauer