0

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?

neuromancer
  • 53,769
  • 78
  • 166
  • 223
  • No, I don't see a conflict, but your second question seems to imply that you do. Anyway, this can't be your full grammar (you're missing the rules for `expression_stmt`, `compound_stmt`, etc.). If you're having an actual conflict with your grammar, please post all the relevant bits so that we can try and reproduce it. – Arthur Reutenauer Nov 18 '09 at 09:49
  • @arthur: the full grammar is here: http://stackoverflow.com/questions/1752185/whats-wrong-with-my-grammar – Adrien Plisson Nov 18 '09 at 10:03
  • @Adrien: OK, thanks :-) So it seems it's a duplicate. – Arthur Reutenauer Nov 18 '09 at 10:10
  • @arthur: no it's just that our OP is not able to do his homework himself. each error pointed in an answer gives way to a new question where he wants us to correct his mistakes... using this iterative approach, he might be able to answer his assignment in some weeks, and even manage to get a pretty decent mark without doing anything... – Adrien Plisson Nov 18 '09 at 10:16
  • No, I am learning dialectically. Dialectical learning is one of the best ways to learn. – neuromancer Nov 18 '09 at 10:22
  • @Adrien: Or he will paste the answers in the wrong order, to his teacher's puzzlement! – Arthur Reutenauer Nov 18 '09 at 10:22
  • @phenom: asking "what is the problem ?" and pasting a lot of code is not learning dialectically. we would like you to think by yourself and perform a bit of research. can you show us that you made some research ? did you ask google what a shift/reduce conflict is ? what did you learn from your search ? – Adrien Plisson Nov 18 '09 at 10:31
  • What does “learning dialectically” mean anyway? But what the heck, I'm teaching dialectically. I answer “I don't see a conflict” as a comment and “I'm seeing a conflict” as an answer. – Arthur Reutenauer Nov 18 '09 at 10:37
  • There was actually a lot of code, and I narrowed it down to where I thought the problem was. I learned from what someone else said that a shift/reduce conflict happens with ambiguous grammar and this is an instance of the famous "dangling else" problem. I learned that all from other peoplee not from google. That's dialectical learning! – neuromancer Nov 18 '09 at 10:38
  • @Adrien: Actually, I think his grammar is seriously screwed up. There's no way any language would have ambiguities such as the one I outlined in my answer. – Arthur Reutenauer Nov 18 '09 at 10:41
  • @arthur: you think so ? then what about C ? iirc, early versions of pascal also had the same problem. – Adrien Plisson Nov 18 '09 at 10:50
  • @Adrien: Right, I never thought about that. Then again, I never had to write a C parser :-) – Arthur Reutenauer Nov 18 '09 at 10:57
  • @arthur: i always found C was clunky... now that we laughed a bit and you took the time to find a perfectly valid answer (thanks for the effort), can we close it as duplicate of http://stackoverflow.com/questions/196179/shift-reduce-conflict ? – Adrien Plisson Nov 18 '09 at 10:59
  • @Adrien: In retrospect I'm not a little proud of myself. I never took any classes in CS, it only took me one hour to understand a common problem in compiler techniques :-) But I can't vote to close, I have only 927 rep as of time of writing. You need 3k apparently. – Arthur Reutenauer Nov 18 '09 at 11:04

1 Answers1

1

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
  • 2,622
  • 1
  • 17
  • 15