CFG With unambiguous if-else statement I am writting a small compiler for a compilers' class. The algorithm is having a shift/reduce conflict when I try to enclose if-else with braces '{', '}'.
 The non-ambiguous if-else statement I took from the dragon book, which turns to be ambiguous when I add the braces. To build the parser and test it I am using the Ply library written in Python.

The grammar terminals/nonterminals specification is backwards here - Uppercase strings are the terminals and Lowercase are the non-terminals. Also the grammar below is not ambiguous untill you try to enclose the if-else with {} which is tokenized as LBRACE and RBRACE.

Below is the grammar specification:

Rule 0     S' -&gt; program
Rule 1     program -&gt; function_definition program
Rule 2     program -&gt; empty
Rule 3     function_definition -&gt; DEFINE data_type ID LPAREN function_definition_args RPAREN block
Rule 4     function_definition_args -&gt; var_list
Rule 5     function_definition_args -&gt; empty
Rule 6     var_list -&gt; var_declaration COMMA var_list
Rule 7     var_list -&gt; var_declaration
Rule 8     var_declaration -&gt; data_type ID
Rule 9     data_type -&gt; VOID
Rule 10    data_type -&gt; INT
Rule 11    data_type -&gt; FLOAT
Rule 12    data_type -&gt; STRING
Rule 13    data_type -&gt; CHAR
Rule 14    block -&gt; LBRACE statements RBRACE
Rule 15    statements -&gt; statement statements
Rule 16    statements -&gt; empty
Rule 17    statement -&gt; matched_statement
Rule 18    statement -&gt; unmatched_statement
Rule 19    matched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE matched_statement RBRACE ELSE LBRACE matched_statement RBRACE
Rule 20    matched_statement -&gt; command
Rule 21    unmatched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE command RBRACE
Rule 22    unmatched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE matched_statement RBRACE ELSE LBRACE unmatched_statement RBRACE
Rule 23    logical_expression -&gt; logical_term OR logical_term
Rule 24    logical_expression -&gt; logical_term
Rule 25    logical_term -&gt; logical_factor AND logical_factor
Rule 26    logical_term -&gt; logical_factor
Rule 27    logical_factor -&gt; boolean_statement
Rule 28    logical_factor -&gt; NOT logical_factor
Rule 29    logical_factor -&gt; LPAREN logical_expression RPAREN
Rule 30    boolean_statement -&gt; value comparison_op value
Rule 31    boolean_statement -&gt; value
Rule 32    value -&gt; ID
Rule 33    value -&gt; NUMBER
Rule 34    comparison_op -&gt; LESS_THAN
Rule 35    comparison_op -&gt; LESS_EQUAL
Rule 36    comparison_op -&gt; GREATER_THAN
Rule 37    comparison_op -&gt; GREATER_EQUAL
Rule 38    comparison_op -&gt; EQUAL
Rule 39    command -&gt; compound_statement
Rule 40    command -&gt; simple_statement
Rule 41    compound_statement -&gt; WHILE LPAREN logical_expression RPAREN block
Rule 42    simple_statement -&gt; expression
Rule 43    expression -&gt; expression PLUS term
Rule 44    expression -&gt; expression MINUS term
Rule 45    expression -&gt; term
Rule 46    term -&gt; term TIMES factor
Rule 47    term -&gt; term DIVIDE factor
Rule 48    term -&gt; factor
Rule 49    factor -&gt; NUMBER
Rule 50    factor -&gt; ID
Rule 51    factor -&gt; LPAREN expression RPAREN
Rule 52    uminus_expression -&gt; MINUS LPAREN expression RPAREN
Rule 53    empty -&gt; &lt;empty&gt;
Rule 54    function_definition -&gt; DEFINE data_type ID LPAREN function_definition_args RPAREN error


The following change bellow is the one which causes the shif/reduce conflicts

Rule 21    unmatched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE command RBRACE
Rule 22    unmatched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE matched_statement RBRACE ELSE LBRACE unmatched_statement RBRACE
If you insist that the alternatives in a conditional statement be blocks, then the dangling-else ambiguity vanishes.

The dangling else ambiguity is something like this, where the else could be attached to either if:

if (a &gt; 3) if (b &gt; 4) do_something else do_something_else


However, if the statements in the conditional must be surrounded by braces, that is a syntax error. You would have to write either:

if (a &gt; 3) { if (b &gt; 4) { do_something } else { do_something_else } }


or

if (a &gt; 3) { if (b &gt; 4) { do_something } } else { do_something_else }


and both of those are quite explicit about which if the else belongs to.

In effect, a braced statement list (or block, as you refer to it in the context of a while statement) is a matched_statement, and you could just add it to the set of possibilities for that non-terminal. But that's only necessary if you want the braces to be optional; if you're going to require them, then you can scrap all that matched- and unmatched- statement stuff, and just use block in your if statement, just as with your while statement.

In any case, the problem is

Rule 20    matched_statement -&gt; command
Rule 21    unmatched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE command RBRACE
Rule 22    unmatched_statement -&gt; IF LPAREN logical_expression RPAREN LBRACE matched_statement RBRACE ELSE LBRACE unmatched_statement RBRACE


Since matched_statement -&gt; command (rule 20), the RBRACE in rule 21 induces a shift-reduce conflict. Assuming a command has just been reduced, the parser has to decide whether rule 20 should be applied (a reduce), which is necessary if the input is the start of rule 22, or whether the RBRACE should just be shifted, necessary if the input is the start of rule 21. And it can't know that by just looking at the RBRACE, since that is the same in both rules.

However, as I said, it doesn't matter. You don't need two different non-terminals for a block, and just using block will avoid the problem.