Difference between Ambiguous and Unambiguous Grammar (original) (raw)
Last Updated : 15 Jul, 2025
Prerequisite - Context Free Grammars **1. Ambiguous Grammar :**A context-free grammar is called ambiguous grammar if there exists more than one derivation tree or parse tree.Example -
S -> S + S / S * S / S / a
**2. Unambiguous Grammar :**A context-free grammar is called unambiguous grammar if there exists one and only one derivation tree or parse tree.Example -
X -> AB A -> Aa / a B -> b
Difference between Ambiguous and Unambiguous Grammar :
| S.NO | Ambiguous Grammar | Unambiguous Grammar |
|---|---|---|
| 1. | In ambiguous grammar, the leftmost and rightmost derivations are not same. | In unambiguous grammar, the leftmost and rightmost derivations are same. |
| 2. | Amount of non-terminals in ambiguous grammar is less than in unambiguous grammar. | Amount of non-terminals in unambiguous grammar is more than in ambiguous grammar. |
| 3. | Length of the parse tree in ambiguous grammar is comparatively short. | Length of the parse tree in unambiguous grammar is comparatively large. |
| 4. | Speed of derivation of a tree in ambiguous grammar is faster than that of unambiguous grammar. | Speed of derivation of a tree in unambiguous grammar is slower than that of ambiguous grammar. |
| 5. | Ambiguous grammar generates more than one parse tree. | Unambiguous grammar generates only one parse tree. |
| 6. | Ambiguous grammar contains ambiguity. | Unambiguous grammar does not contain any ambiguity. |