Professional Documents
Culture Documents
FAFL Final Lecture 24.2 CMH
FAFL Final Lecture 24.2 CMH
Lecture 24.2
Ambiguous Grammar
S c h o o l o f C o m p u t i n g a n d I n f o r m a t i o n Te c h n o l o g y
Chaithra M H
AY:2020-2021
OUTLINE
Recap of Previous Lecture
Lecture Discussion
• Examples-ambiguous grammar
• Differences between ambiguous and unambiguous grammar
Ambiguous Grammar
Recap of Previous Lecture
RECAP OF PREVIOUS LECTURE
S a S b S given [ S a S b S] S a S b S given [ S a S b S]
aaSbSbS [ S a S b S] aaSbSbS [ S a S b S]
aabSaSbSbS [S b S a S ] aabSbS [S Ԑ]-
aabaSbSbS [ S Ԑ] aabaSbSbS [SaSbS]
aababSbS [S Ԑ ] aababSbS [S Ԑ ]
aababbS [S Ԑ ] aababbS [S Ԑ ]
aababb [S Ԑ ] aababb [S Ԑ ]
EXAMPLE-4 CONTD…
The corresponding derivation trees for the two leftmost derivations are shown below
Parse tree - 1 Parse tree - 2
S S
a S b S
a S b S
a S b S ԑ ԑ
Since the two parse a S b
S
trees are different for
b S a S ԑ the same sentence
a S b S
aababb by applying ԑ
ԑ ԑ leftmost derivation,
the grammar is ԑ
ԑ
ambiguous.
EXAMPLE-5 CONTD…
The corresponding derivation trees for the two leftmost derivations are shown below
Parse tree - 1 Parse tree - 2
B B
a B b B
a B b B
a B b B ԑ ԑ
Since the two parse a B b
B
trees are different for
b B a B ԑ the same sentence
a B b B
aababb by applying ԑ
ԑ ԑ leftmost derivation,
the grammar is ԑ
ԑ
ambiguous.
DIFFERENCES BETWEEN AMBIGUOUS GRAMMAR AND
UNAMBIGUOUS GRAMMAR
AMBIGUOUS GRAMMAR UNAMBIGUOUS GRAMMAR
A grammar is said to be ambiguous if for at least A grammar is said to be unambiguous if for all
one string generated by it, it produces more the strings generated by it, it produces exactly
than one-parse tree or derivation tree or syntax one-parse tree or derivation tree or syntax tree
tree or leftmost derivation or rightmost or leftmost derivation or rightmost derivation.
derivation.
For ambiguous grammar, leftmost derivation For unambiguous grammar, leftmost derivation
and rightmost derivation represents differentand rightmost derivation represents the same
parse trees. parse tree.
Ambiguous grammar contains less number of Unambiguous grammar contains more number
non-terminals and length of parse tree is less.
of non-terminals and length of parse tree is
large.
Ambiguous grammar is faster than Unambiguous grammar is slower than
unambiguous grammar in the derivation of a ambiguous grammar in the derivation of a tree.
tree. (Reason is above 2 points) (Reason is above 2 points)
Differences
between
Ambiguous
ambiguous
grammar with
grammar and
examples
unambiguous
grammar
SUMMARY OF THE LECTURE
QUIZ TIME
10 MINUTES
1. A CFG is ambiguous if
A) No parse tree can be generated for B) It has more than one leftmost derivations
the CFG
A) true B) false
4. Grammar that produce more than one Parse tree for same sentence is:
A) Ambiguous B) Unambiguous
QUIZ
Questions from Lecture 24.1 & 24.2