Tag: Difference between Ambiguous and Unambiguous Grammar

Difference between Ambiguous and Unambiguous Grammar

Difference Between Ambiguous and Unambiguous Grammar-

 

Before you go through this article, make sure that you have gone through the previous article on Ambiguous Grammar.

 

Some of the important differences between ambiguous grammar and unambiguous grammar are-

 

Ambiguous Grammar Unambiguous Grammar
A grammar is said to be ambiguous if for at least one string generated by it, it produces more than one-
  • parse tree
  • or derivation tree
  • or syntax tree
  • or leftmost derivation
  • or rightmost derivation
A grammar is said to be unambiguous if for all the strings generated by it, it produces exactly one-
  • parse tree
  • or derivation tree
  • or syntax tree
  • or leftmost derivation
  • or rightmost derivation
For ambiguous grammar, leftmost derivation and rightmost derivation represents different parse trees. For unambiguous grammar, leftmost derivation and rightmost derivation represents the same parse tree.
Ambiguous grammar contains less number of non-terminals. Unambiguous grammar contains more number of non-terminals.
For ambiguous grammar, length of parse tree is less. For unambiguous grammar, length of parse tree is large.
Ambiguous grammar is faster than unambiguous grammar in the derivation of a tree.

(Reason is above 2 points)

Unambiguous grammar is slower than ambiguous grammar in the derivation of a tree.
Example-

 

E → E + E / E x E / id

(Ambiguous Grammar)

 

Example-

E → E + T / T

T → T x F / F

F → id

(Unambiguous Grammar)

 

Next Article- Algorithm To Check Grammar Ambiguity

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.