Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Established as per the Section 2(f) of the UGC Act, 1956

Approved by AICTE, COA and BCI, New Delhi

Lecture 25.1
Ambiguity in Grammars and Languages
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

Topic of the Lecture

Objective and Outcome of Lecture

Lecture Discussion

• Problems on Ambiguity in Grammars and Languages


Ambiguity in Grammars and Languages R e c a p
of Previous Lecture
RECAP OF PREVIOUS LECTURE

Introduction to Ambiguous grammar

Definition of Ambiguous grammar

Definition of Unambiguous grammar

Examples-ambiguous grammar
Differences between ambiguous and unambiguous grammar
Ambiguity in Grammars and Languages
Topic for the lecture
TOPIC OF THE LECTURE

Languages

Ambiguity in
and Grammars

Grammars and
in Ambiguity
on Examples •
Languages
Ambiguity in Grammars and Languages
Objective and Outcome of Lecture
construct
Ambigui
ty in
Gramma
rs and
Languag Outcome
es to Lecture
solve the
• given
Illustrat
problem
e the
by
constru
applying
ction of
LMD or
Ambigui
RMD Objective
ty
techniquin Lecture
Gramm
es.
ars and
Languag
es for a
given
gramma
OBJECTIVE AND OUTCOME OF LECTURE
r.
Ambiguity in Grammars and Languages
Problems on Ambiguity in Grammars and Languages
EXAMPLE-1

Is the grammar ambiguous?


S → aS | X
XaX|a
is ambiguous or not?
Solution
Consider the two leftmost derivations for the string aaaa.
Derivation-1 Derivation-2

SaS given [ S  a S] SX given [ S X]


aaS [ S  a S] aX [ S  a X]
aaaS [S  a S] aaX [XaX]
aaaX [S  X] aaaX [ X  a X]
aaaa [Xa] aaaa [Xa]
EXAMPLE-1 CONTD…
S
Parse tree-1 Parse tree-2
S X

a S a X

a S
a X
a X
a X
a
a
Since there are two leftmost derivations for the same sentence aaaa, the
grammar G is ambiguous.
EXAMPLE-2

• A CFG is ambiguous if there is a string in the language that is the yield of two or
more parse trees.
• Example: S -> SS | (S) | ()
• Two parse trees for ()()() on next slide.
EXAMPLE 2 – CONTINUED

Parse tree-1 Parse tree-2

Since there are two leftmost derivations for the same sentence ( )( )( ) the
grammar G is ambiguous.
DISCUSSION
5 MINUTES

You might also like