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

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

Approved by AICTE, COA and BCI, New Delhi

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

Topic of the Lecture

Objective and Outcome of Lecture

Lecture Discussion

• Examples-ambiguous grammar
• Differences between ambiguous and unambiguous grammar
Ambiguous Grammar
Recap of Previous Lecture
RECAP OF PREVIOUS LECTURE

Introduction to Ambiguous grammar


Course Description

Definition of Ambiguous grammar- with examples

Definition of Unambiguous grammar


Ambiguous grammar
Topic for the lecture
• Examples-
ambiguous
grammar Grammar

Ambiguous
Differences
between ambiguous
and unambiguous
grammar
TOPIC OF THE LECTURE
Ambiguous Grammar
Objective and Outcome of Lecture
Identify
and
construct
ambiguo
us
grammar
to solve Outcome
the given Lecture
problem
by
• applying
Illustrat
LMD
e or
the
RMD
constru
techniqu
ction of
es.
Objective
ambigu Lecture
ous
gramma
r for a
given
gramma
r.
OBJECTIVE AND OUTCOME OF LECTURE
Ambiguous Grammars
Ambiguous Grammar-Examples
EXAMPLE-4

Show that the following grammar is ambiguous : S  a S b S | b S a S | Ԑ


Solution
Consider the leftmost derivation for the string “aababb”.
Derivation -1 Derivation -2

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 [SaSbS]
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…

Show that the following grammar is ambiguous : B  aBbB | bBaB | Ԑ


Solution
Consider the leftmost derivation for the string “aababb”.
Derivation -1 Derivation -2

B a B b B given [ B a B b B] BaBbB given [ B a B b B]


aaBbBbB [B  a B b B] aaBbBbB [ B  a B b B]
aabBaBbBbB [B  b B a B] aabBbB [B  Ԑ]-
aabaBbBbB [B Ԑ] aabaBbBbB [BaBbB]
aababBbB [B  Ԑ ] aababBbB [B  Ԑ ]
aababbB [B  Ԑ ] aababbB [B  Ԑ ]
aababb [B  Ԑ ] aababb [B  Ԑ ]
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

“All of you have to give quiz where there


will be 8 questions covering part1 and
part 2 of lecture. It is compulsory as it is
used for assessment and attendance”
QUIZ
Questions from Lecture 24.1 & 24.2

1. A CFG is ambiguous if

A) No parse tree can be generated for B) It has more than one leftmost derivations
the CFG

2. Which of the following are always unambiguous?

A) Deterministic Context free B) Non-Deterministic Regular


grammars grammars
QUIZ
Questions from Lecture 24.1 & 24.2

3. State true or false:


Statement: R->R|T T->ε is an ambiguous grammar

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

5. In a Derivation, if a variable is replaced at every LHS then it is called________


representation of a grammar.
A) LMD B) RMD
THANK YOU
DISCUSSION
5 MINUTES

Obtain a CFG to generate a string of balanced parentheses

You might also like