/* File: else2b.y One way to fix the dangling else is to require the programmer to always use braces after THEN and ELSE. Note that statement has been replaced with stmt_block. Using mid-rule actions causes problems, though. One fix for this problem is to "factor out" the if part of both forms of the if statement. This only works if you can manage to use the same action for both forms of the if statement. */ %{ %} %token IF THEN ELSE %token FOO %token EXPR %% program : statement_list ; statement_list : statement_list statement | statement ; statement : if_stmt | other_stmt | stmt_block ; stmt_block : '{' statement_list '}' ; other_stmt : FOO ';' ; if_part : IF EXPR { /* do something */} ; if_stmt : if_part THEN stmt_block ELSE stmt_block { /* do something else */ } | if_part THEN stmt_block {/* do something */} ; %%