/* File: else3a.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. */ %{ %} %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 ; matched_if_stmt : IF EXPR { /* do something */ } THEN matched_if_stmt ELSE { /* */ } matched_if_stmt { /* */ } | other_stmt ; open_if_stmt : IF EXPR { /* */ } THEN if_stmt { /* */ } | IF EXPR { /* */ } THEN matched_if_stmt ELSE { /* */ } open_if_stmt { /* */ } ; %%