Formally, the reference context-free grammar of the language is ambiguous, meaning there is more than one correct parse tree.
In LR parsers, the dangling else is the archetypal example of a shift-reduce conflict.
The convention when dealing with the dangling else is to attach the else to the nearby if statement,[2] allowing for unambiguous context-free grammars, in particular.
In these cases alternative grouping is accomplished by explicit blocks, such as begin...end in Pascal[6] and {...} in C. Depending on the compiler construction approach, one may take different corrective actions to avoid ambiguity: The problem can also be solved by making explicit the link between an else and its if, within the syntax.
In C, the grammar reads, in part: Thus, without further rules, the statement could ambiguously be parsed as if it were either: or: The C standard clarifies that an else block is associated with the nearest if.