/* File: else3b.y Another way to resolve the dangling else is to rewrite the grammar with "matched" versus "open" if statements (p. 212 in the purple dragon book). Here also, mid-rule actions can cause problems. Again, this can be resolved by factoring out the common parts of the rules causing the reduce/reduce conflicts. Note that we have to do this twice using an "if_part" and an "if_part_then_matched_stmt". */ %{ %} %token IF THEN ELSE %token FOO %token EXPR %% program : statement_list ; statement_list : statement_list statement | statement ; statement : if_stmt ; stmt_block : '{' statement_list '}' ; other_stmt : FOO ';' | stmt_block ; if_stmt : matched_if_stmt | open_if_stmt ; if_part : IF EXPR { /* do something */ } ; if_part_then_matched_stmt : if_part THEN matched_if_stmt { /* do something */ } ; matched_if_stmt : if_part_then_matched_stmt ELSE matched_if_stmt { /* */ } | other_stmt ; open_if_stmt : if_part THEN if_stmt { /* */ } | if_part_then_matched_stmt ELSE open_if_stmt { /* */ } ; %%