Difference between Ambiguous and Unambiguous Grammar

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.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.