I'm trying to do the if-then statement in bison. The problem is that im working on a solution that everyone says that is working, but it doesnt :( my code is:
statement : matchedstmt
| unmatchedstmt
;
matchedstmt : if '(' expression ')' matchedstmt else matchedstmt
| otherstmt
;
unmatchedstmt : if '(' expression ')' statement
| if '('expression ')' matchedstmt else unmatchedstmt
;
otherstmt : expressionstmt
| compoundstmt
| iterationstmt
| returnstmt
;
...
where "if" & "else" are %token
!! in terminal it says that i have one shift/reduce.
I also tried %nonassoc and %left
what can i do??
The following can be processed by bison with no conflicts of any kind:
That's unsurprising since you are using a standard mechanism for disambiguating the
if ... elsestatement.Presumably, the shift-reduce conflict is somewhere else in your grammar, possibly involving an interaction with this fragment. I suggest you add more statement types one at a time until you find the rule which causes the conflict. Unfortunately, LR grammars do not compose well: it is quite possible for two perfectly conflict-free fragments to produce a conflict when they are combined in a grammar.