cbisondangling-else

Bison Flex reduce/reduce conflict on dangling else with mid action


I am currently moving a fun-project of mine over to bison/flex as parser and have trouble solving a reduce/reduce conflict:

// https://github.com/X39/yaoosl/blob/master/code-gen/yaoosl.y#L761-L766
ifthen: YST_IF YST_ROUNDO expression code_ifstart YST_ROUNDC codebody code_ifendnoelse
        | YST_IF YST_ROUNDO expression code_ifstart YST_ROUNDC ifthen_clsd YST_ELSE code_ifelse ifthen code_ifendelse
        ;
ifthen_clsd: codebody
           | YST_IF YST_ROUNDO expression code_ifstart YST_ROUNDC ifthen_clsd code_ifelse YST_ELSE ifthen_clsd code_ifendelse
           ;

Note: stuff prefixed with code_ are the mid-actions

Could somebody explain to me how to solve this properly and why the "go-to" solution is either wrong or did not worked? Thanks, X39


Solution

  • Since the two rules are identical up to the code_ifelse (and assuming code_ifelse is an empty rule, like an in-rule action), it can't tell whether to reduce code_ifelse before or after the YST_ELSE. You might be able to fix it by making the two rules consistent with the order of code_ifelse and YST_ELSE.

    Some rules-of-thumb for grammars: