TCS Lect 24-25 Derivation Tree
TCS Lect 24-25 Derivation Tree
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
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:
Rightmost derivation.
If there exists more than one Parse Tree for any string which is derived
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:
left recursion
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