0% found this document useful (0 votes)
1K views26 pages

Lecture 3 Ambiguous and Unambiguous Grammar-1

The document discusses ambiguous and unambiguous grammars. It provides examples to illustrate: 1) Ambiguous grammars are those where a string can have more than one parse tree, leftmost derivation, or rightmost derivation. 2) Several grammars are checked for ambiguity by attempting to find a string with multiple parse trees. Grammars with such strings are ambiguous. 3) One example grammar is shown to be unambiguous, as the string tested has a single parse tree under leftmost and rightmost derivation.

Uploaded by

Snehal Poptani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views26 pages

Lecture 3 Ambiguous and Unambiguous Grammar-1

The document discusses ambiguous and unambiguous grammars. It provides examples to illustrate: 1) Ambiguous grammars are those where a string can have more than one parse tree, leftmost derivation, or rightmost derivation. 2) Several grammars are checked for ambiguity by attempting to find a string with multiple parse trees. Grammars with such strings are ambiguous. 3) One example grammar is shown to be unambiguous, as the string tested has a single parse tree under leftmost and rightmost derivation.

Uploaded by

Snehal Poptani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Ambiguous and

Unambiguous Grammar :

--Sakshi Surve
Ambiguity :
Ambiguous means Open to more than one
interpretation , Not having one obvious meaning

A grammar is said to ambiguous if for any string


generated by it, it produces more than one-
 Parse tree Or
 Leftmost Derivation Or
 Rightmost Derivation

If there exists at least one such string, then the


grammar is ambiguous otherwise unambiguous.
Ambiguous and Unambiguous Grammar :
Grammar Ambiguity-
1. There exists no general algorithm to remove
the ambiguity from grammar.

2. To check grammar ambiguity, we try finding a


string that has more than one parse tree.

3. If any such string exists, then


the grammar is ambiguous otherwise not.
Example 01-
Consider the following grammar-
E → E + E | E x E | id
Ambiguous Grammar

This grammar is an example of ambiguous grammar.

Any of the following reasons can be stated to prove the


grammar ambiguous-
Reason-01: Parse Tree E → E + E | E x E | id

Let us consider a string w generated by the grammar-


w = id + id x id
Now, let us draw the parse trees for this string w.

Since two parse trees exist for string w, the grammar is


ambiguous.
Reason-02: Leftmost Derivation :
E → E + E | E x E | id
Let us consider a string w generated by the grammar-
w = id + id x id
Now, let us write the leftmost derivations for this string w.

Since two leftmost derivations exist for string w, the


grammar is ambiguous.
 
Reason-03 : Rightmost Derivation
Let us consider a string w generated by the grammar-
E → E + E | E x E | id

w = id + id x id
Now, let us write the rightmost derivations for this string w.
 

Since two rightmost derivations exist for string w, the


grammar is ambiguous.
Example -02:
 
Check whether the given grammar is ambiguous or not-
S → A / B
A → aAb / ab
B → abB / ∈
 
Solution-
 
Let us consider a string w generated by the given grammar-
w = ab
Now, let us draw parse trees for this string w.
 
Since two different parse trees exist for string w, the
given grammar is ambiguous.
Example - 03:
Check whether the given grammar is ambiguous or not-
S → AB / C
A → aAb / ab
B → cBd / cd
C → aCd / aDd
D → bDc / bc
 
Solution-
 
Let us consider a string w generated by the given grammar-
w = aabbccdd

Now, let us draw parse trees for this string w.


Since two different parse trees exist for string w, the
given grammar is ambiguous.
Example - 04:
 
Check whether the given grammar is ambiguous or not-
S → aSbS / bSaS / ∈
 
Solution-
 
Let us consider a string w generated by the given grammar-
w = abab

Now, let us draw parse trees for this string w.


 
Since two different parse trees exist for string w, the
given grammar is ambiguous.
Example -05:
 
Check whether the given grammar is ambiguous or not-
S → SS
S → a
S → b

Solution-
 
Let us consider a string w generated by the given grammar-
w = abba
Now, let us draw parse trees for this string w.
Since two different parse trees exist for string w,
therefore the given grammar is ambiguous.
∑ = { a , b }
L = { w € L | w begins with a }
L = { a , aa, ab, aab, aba, aaa, ………}

S -> aA
A -> aA | bA | €
For ‘a’
Derivation of ‘a’

S aA
S a

a A


S -> aA
A -> aA | bA | €
For generating ‘aa’
 S

a A
Derivation of ‘aa’

S aA
a A aaA
aa


S -> aA Derivation of ‘aba’

A -> aA | bA | € S aA
abA
For generating ‘aba’ abaA
 S aba

a A In this grammar, the tuples are :

b A V = {S, A}
T = { a,b }
a A
P
S= S

This is an example of Unambiguous Grammar
Example 6 :
Consider the following example :
S → aB / bA
A → aS / bAA / a
B → bS / aBB / b

Let us consider a string


w = aaabbabbba

Now, let us derive the string w using leftmost derivation.


S → aB / bA
A → aS / bAA / a
Leftmost Derivation - B → bS / aBB / b
 S   → aB
aaabbabbba 
→  aaBB                   (Using B → aBB)
→ aaaBBB                (Using B → aBB)
→ aaabBB                (Using B → b)
→ aaabbB                (Using B → b)
→ aaabbaBB            (Using B → aBB)
→ aaabbabB            (Using B → b)
→ aaabbabbS          (Using B → bS)
→ aaabbabbbA        (Using S → bA)
→ aaabbabbba         (Using A → a)
S → aB / bA
A → aS / bAA / a
Rightmost Derivation- B → bS / aBB / b
 
aaabbabbba 
S   → aB
→  aaBB                    (Using B → aBB)
→ aaBaBB                 (Using B → aBB)
→ aaBaBbS               (Using B → bS)
→ aaBaBbbA             (Using S → bA)
→ aaBaBbba              (Using A → a)
→ aaBabbba              (Using B → b)
→ aaaBBabbba          (Using B → aBB)
→ aaaBbabbba          (Using B → b)
→ aaabbabbba           (Using B → b)
Since one parse tree exists for string w, the given
grammar is unambiguous.

You might also like