Writing a Grammar
Eliminating Ambiguity
The ambiguity from a grammar can be eliminated by rewriting the production rules.
In general, the ambiguity from the grammars of the form
A → α A β A Ƴ | α1 | α2 | …. | αn can be eliminated by rewriting the productions as follows:
A → α A β A Ƴ | A’
A → α1 | α2 | …. | αn
If more than one production of a grammar is ambiguous, then it is modified by applying transformations repetitively. For example, consider the context free grammar G, for arithmetic expression given as
E → E + E | E * E | (E) | id
This grammar is ambiguous because non-terminal E appears twice at the right hand side of a production. Each arithmetic operator has precedence. Therefore the precedence of operators must be preserved while writing the grammar rules for expressions involving these operators.
To eliminate the ambiguity from the above grammar, we introduce a new non-terminal (T) to move the rightmost of these non-terminals further down the parse Tree, so we get,
E → E + T | T
T → E * E | (E) | id
Reference Link
