0% found this document useful (0 votes)
99 views23 pages

TCS Lect 24-25 Derivation Tree

TCS Lect 24-25 Derivation Tree
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)
99 views23 pages

TCS Lect 24-25 Derivation Tree

TCS Lect 24-25 Derivation Tree
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/ 23

DERIVATION Module 3.

TREE
Derivation
Definition:
The production rules are used to derive certain strings
If G ( V, T, S, P ) be some context free grammar then generation of
some language using specific rule is called Derivation

Leftmost Derivation:
It is the derivation in which the leftmost non terminal is replaced first
from sentential form

Rightmost Derivation:
It is the derivation in which the rightmost non terminal is replaced first
from sentential form
Example:
For production rules, Derivation
S→XYX
X→a
Y→b

Leftmost Derivation:
S⇒XYX
⇒aYX
⇒abX
⇒aba

Rightmost Derivation
S⇒XYX
⇒XYa
⇒Xba
⇒aba
Examples
Ex. 1 Derive the string “a a b b a b b a” for leftmost derivation and rightmost
derivation using CFG given below
S→aB|bA
A→a|aS|bAA
B→b|bS|aBB
Solution:
Leftmost Derivation:
S⇒aB Rule 1
⇒aaBB Rule 8
⇒aabB Rule 6
⇒aabbS Rule 7
⇒aabbaB Rule 1
⇒aabbabS Rule 7
⇒aabbabbA Rule 2
⇒aabbabba Rule 3
Examples
Ex. 1 Derive the string “a a b b a b b a” for leftmost derivation and rightmost
derivation using CFG given below
S→aB|bA
A→a|aS|bAA
B→b|bS|aBB
Solution:
Rightmost Derivation:
S⇒aB Rule 1
⇒aaBB Rule 8
⇒aaBbS Rule 7
⇒aaBbbA Rule 2
⇒aaBbba Rule 3
⇒aabSbba Rule 7
⇒aabbAbba Rule 2
⇒aabbabba Rule 3
Derivation Tree/ Parse Tree
Definition:
Derivation Tree is graphical representation for the derivation of the
given production rules of given CFG

Properties of any Derivation Tree:


1. The root node is always indicating start symbol
2. The derivation is read from left to right
3. The leaf nodes are always terminal nodes
4. The interior nodes are always non – terminal nodes
Derivation Tree

Example:
For production rules,
S→bSb
S→a
S→b
S

Leftmost Derivation:
b S b
S⇒bSb
⇒bbSbb
b S b
⇒bbabb

a
Examples
Ex. 1 Construct the derivation tree for the string “a a b b a b b a “ where CFG given
by
S→aB|bA
A→a|aS|bAA S
B→b|bS|aBB
Solution:
Leftmost Derivation:
a B

S⇒aB Rule 1
⇒aaBB Rule 8 a B
B
⇒aabSB Rule 7
⇒aabbAB Rule 2 b
⇒aabbaB Rule 3 S b S
⇒aabbabS Rule 7
⇒aabbabbA Rule 2 b
A b A
⇒aabbabba Rule 3

a a
Examples
Ex. 2 Derive the string “0 0 1 1 0 1 0 1” for leftmost derivation and rightmost
derivation using CFG given below. Draw derivation Tree as well
S→0B|1A
A→0|0S|1AA
B→1|1S|0BB
Solution:
Leftmost Derivation:
S⇒0B Rule 1
⇒00BB Rule 8
⇒001SB Rule 7
⇒0011AB Rule 2
⇒00110B Rule 3
⇒001101S Rule 7
⇒0011010B Rule 1
⇒00110101 Rule 6
Examples
Ex. 2 Derive the string “0 0 1 1 0 1 0 1” for leftmost derivation and rightmost
derivation using CFG given below. Draw derivation Tree as well
S→0B|1A
A→0|0S|1AA
B→1|1S|0BB
Solution:
Rightmost Derivation:
S⇒0B Rule 1
⇒00BB Rule 8
⇒00B1S Rule 7
⇒00B10B Rule 1
⇒00B101 Rule 6
⇒001S101 Rule 7
⇒0011A101 Rule 2
⇒00110101 Rule 3
Examples
Ex. 2 Derive the string “0 0 1 1 0 1 0 1” for leftmost derivation and rightmost
derivation using CFG given below. Draw derivation Tree as well
S→0B|1A
A→0|0S|1AA S
B→1|1S|0BB
Solution:
Leftmost Derivation:
0 B

S⇒0B Rule 1
⇒00BB Rule 8 0 B
B
⇒001SB Rule 7
⇒0011AB Rule 2 1
⇒00110B Rule 3 S 1 S
⇒001101S Rule 7
⇒0011010B Rule 1 1
A 0 B
⇒00110101 Rule 6

0 1
Ambiguity
Definition:

The Grammar can be derived in either Leftmost derivation or

Rightmost derivation.

Parse tree must be unique for both the derivation

If there exists more than one Parse Tree for any string which is derived

from given grammar then such grammar is ambiguous


Ambiguity
Example:
The CFG given by G ( V, T, S, P )
Where V = { E }, T = { id }, S = E and P is
given by
E→E+E
E→E*E
E
E → id

E + E
Derivation of the string id * id + id
Leftmost Derivation:
E * E
E⇒E+E
id
⇒E*E+E
⇒ id * E + E id id
⇒ id * id + E
⇒ id * id + id
Ambiguity
Example:
The CFG given by G ( V, T, S, P )
Where V = { E }, T = { id }, S = E and P is
given by
E→E+E
E→E*E
E
E → id

E * E
Derivation of the string id * id + id
Rightmost Derivation:
E⇒E*E E + E
id
⇒E*E+E
⇒ E * E + id
id id
⇒ E * id + id
⇒ id * id + id
Example:
The CFG given by G ( V, T, S, P ) Ambiguity
Where V = { E }, T = { id }, S = E and P is
given by
E→E+E
E→E*E
E → id
For Derivation of the string id * id + id
We have two different parse trees. Hence the
grammar is ambiguous.

E E

E + E * E
E

E * E E + E
id
id

id id id id
Inherent Ambiguity
If grammar that generates L is ambiguous then the language is called
Inherently ambiguous
Example:
S→XYZ|aaYbb
X→aaY|aa
Y→baZ|ba
Y→bZb|bb
Derivation of string: a a b a b b
S S

X Y Z a a Y b b

a a b a b b
b a
Ambiguous to Unambiguous Grammar

Definition:

For removing ambiguity we will apply rule as:

• If the grammar has Left Associative operator ( +, -, *, / ) then induce

left recursion

• If the grammar has Right Associative operator ( Exponentiation )

then induce right recursion


Examples
Example: Consider the grammar
E→E+E
E→E*E
E → id
Convert above ambiguous grammar to unambiguous
E
Solution:
Unambiguous grammar can be written as
E→E+T|T E + T
T→T*F|F
F → id T
Derivation of the string id * id + id F
E⇒E+T Rule 1
T * F
⇒T+T Rule 2
id
⇒T*F+T Rule 3
⇒F*F+T Rule 4 F id
⇒ id * F + T Rule 5
⇒ id * id + T Rule 5
⇒ id * id + F Rule 4 id
Examples
Example 2: Design an unambiguous grammar to accept arithmetic
statement having terminals as { + , - , * , / , = , ^ , ; , ( , ) , id }
E→E+E
E→E*E
E → id
Solution:
By applying rule, Unambiguous can be
Ambiguous grammar can be written as
written as
S→S=EZ
S→S=EZ
Z→;
Z→;
E→(E)
E→(T)
E→E+E
T→T+F|F
E→E-E
F → F – G| G
E→E*E
G→G*H|H
E→E/E
H→H/I|I
E→E^E
I→J^I|J
E → id
J → id
Examples
Question 3: Show that the following grammar is ambiguous
S→aSbS|bSaS|ε
What is the language generated by the grammar?
Solution:
Consider the string a b a b
Leftmost Derivation: S
S⇒aSbS Rule 1
⇒abSaSbS Rule 2 a S b S
⇒abεaSbS Rule 3
⇒abaSbS
⇒abaεbS Rule 3 b S a S ε
⇒ababS
⇒ababε Rule 3
⇒abab ε ε
Examples
Question 3: Show that the following grammar is ambiguous
S→aSbS|bSaS|ε
What is the language generated by the grammar?
Solution:
Consider the string a b a b
Rightmost Derivation:
S
S⇒aSbS Rule 1
⇒aSbaSbS Rule 1 a S b S
⇒aSbaSbε Rule 3
⇒aSbaSb
⇒aSbaεb Rule 3 ε b S a S
⇒aSbab
⇒aεbab Rule 3
⇒abab
ε ε

There are more than one parse tree for the given string which is derived
from the given grammar.
Hence it is ambiguous grammar
Examples
Question 3: Show that the following grammar is ambiguous
S→aSbS|bSaS|ε
What is the language generated by the grammar?
Solution:
Derivation of Strings
S⇒aSbS
S⇒aSbS
⇒ aaSbSbS
⇒ aεbε
⇒ aaεbεbε
⇒ab
⇒ aabb

S⇒bSaS
⇒bεaε We can describe the language as
⇒ba L(G) consists of strings with equal number of a’s and
equal number of b’s
S⇒bSaS
⇒baSbSaS
⇒baεbεaε
⇒baba

You might also like