VNW Ce Toc Un 03
VNW Ce Toc Un 03
VNW Ce Toc Un 03
Class: TE
Course: Theory of Computation
Unit 03: Context Free Grammars & Languages
Generation of Derivation Tree / Parse Tree
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
Example: Construct a derivation tree for the string “aabbabba” for the CFG
given by, S → aB | bA , A → a | aS | bAA, B → b | bS | aBB.
❑ To draw a tree, we will first try to obtain derivation for the string
“aabbabba”.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
❑ The derivation or the yield of a parse tree is the final string obtained
by concatenating the labels of the leaves of the tree from left to right,
ignoring the Nulls. However, if all the leaves are Null, derivation is
Null.
❑ There are two different approaches to draw a derivation tree:
1. Top-down Approach
▪ Starts with the starting symbol S.
▪ Goes down to tree leaves using productions.
2. Bottom-up Approach
▪ Starts from tree leaves.
▪ Proceeds upward to the root which is the starting symbol S.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
❑ Types of Derivation
❑ Two options to decide which non-terminal to be placed with production
rule.
1. Leftmost Derivation
2. Rightmost Derivation.
▪ A rightmost derivation is obtained by applying production to
the rightmost variable in each step.
▪ Rightmost derivation, read the input string from right to left.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
Example: Let any set of production rules in a CFG be E = E + E | E = E - E |
E = a | b over an alphabet {a, b}, obtain the left & right most derivation
for "a - b + a“.
❑ The derivation for the string "a - b + a" may be:
Leftmost Rightmost
E=E+E E=E-E
E=E-E+E E=E-E+E
E=a-E+E E=E-E+a
E=a-b+E E=E-b+a
E=a-b+a E=a-b+a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
Example: Derive the string "aabbabba" for leftmost derivation and rightmost
derivation using a CFG given by, S → aB | bA, S → a | aS | bAA, S → b |
aS | aBB.
❑ The derivation for the string " aabbabba " may be:
S
aB S → aB S → aB
aaBB B → aBB B → aBB
aabB B→b B → bS
aabbS B → bS S → bA
aabbaB S → aB A→ a
aabbabS B → bS B → bS
aabbabbA S → bA S → bA
aabbabba A→ a A→ a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
Example: Let any set of production rules in a CFG be X → X+X | X*X |X| a
over an alphabet {a}, obtain the left & right most derivation tree for "a+a*a“.
❑ The leftmost derivation for the string "a+a*a" may be:
X → X+X
X → a+X
X → a + X*X
X → a+a*X
X → a+a*a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Generation of Derivation Tree / Parse Tree 14
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Ambiguity in Context-Free Grammar
Derivation 1 Derivation 2
❑ If a context free grammar G has more than one derivation tree for
some string w ∈ L(G), it is called an ambiguous grammar. S → SS S → aSb
❑ A grammar is said to be ambiguous if there exists more than one S → εaSb S → aaSbb
leftmost derivation or more than one rightmost derivation or more S → aaSbb S → aaεbb
than one parse tree for the given input string.
S → aaεbb S → aabb
Example: Check whether the given grammar G is ambiguous or not.
S → aabb
S → aSb | SS, S → ε, for the string “aabb”.
❑ For the string "aabb" the above grammar can generate two parse
trees.
❑ Since there are two parse trees
for a single string "aabb", the
grammar G is an ambiguous.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Ambiguity in Context-Free Grammar
Example: Check whether the grammar G with production rules, X → X+X | Derivation 1 Derivation 2
X*X |X| a, for the string "a+a*a“. X → X+X X → X*X
Derivation / Parse Tree 1 Derivation / Parse Tree 2 X → a+X X → X*a
X → a + X*X X → X+X*a
X → a+a*X X→ X+a*a
X → a+a*a X → a+a*a
❑ Since there are two parse trees for a single string "a+a*a", the grammar
G is an ambiguous.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Ambiguity in Context-Free Grammars
Example: Consider a grammar G with the production rule, E → I, E → E Derivation 1 Derivation 2
+ E, E → E * E, I → ε | 0 | 1 | 2 | ... | 9 , to generate the string “3 * 2 +
5”. E→E*E E→E+E
❑ For the string "3 * 2 + 5", the above grammar can generate two parse E→I*E E→E+I
trees by leftmost derivation: E → I * E+E E → E*E + I
E→I*I+I E→I*I+I
E → 3 * 2+ 5 E → 3 * 2+5
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Unambiguous Grammar
❑ A grammar can be unambiguous if the grammar does not contain ambiguity that means
if it does not contain more than one leftmost derivation or more than one rightmost
derivation or more than one parse tree for the given input string.
❑ To convert ambiguous grammar to unambiguous grammar, we will apply the following
rules:
1. If the left associative operators (+, -, *, /) are used in the production rule, then apply
left recursion in the production rule. Left recursion means that the leftmost symbol
on the right side is the same as the non-terminal on the left side.
Example: X → Xa
2. If the right associative operates (^) is used in the production rule then apply right
recursion in the production rule. Right recursion means that the rightmost symbol on
the left side is the same as the non-terminal on the right side.
Example: X → aX
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Unambiguous Grammar
P = {S → AB
S -> aaB
❑ Unambiguous grammar will be: A→a
S → AB A → Aa
A → Aa | a B → b}
B→b
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Unambiguous Grammar
Example: Show that the given grammar is ambiguous for the string “aa”. Derivation 1 Derivation 2
Also, find an equivalent unambiguous grammar for S → ABA, A → aA S → ABA S → ABA
| ε, B → bB | ε. S → aεA S → aABA
❑ Let us derive the string “aa“. S → aεa S → aAεA
❑ As there are two different parse S → aa S → aaAεε
tree for deriving the same string, S → aaεεε
the given grammar is ambiguous. S → aa
B→ b | ε
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Unambiguous Grammar
Example: Show that the given grammar is ambiguous. Also, find an Derivation 2 Derivation 1
equivalent unambiguous grammar, E → E + E, E → E * E, E → id for E→E*E E→E+E
the string "id + id * id“. E → id * E E → E + id
❑ Let us derive the string "id + id * id“. E → id *E+E E → E*E + id
❑ As there are two different parse E → id*id+id E → id*id+id
tree for deriving the same string,
the given grammar is ambiguous.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Simplification of CFG
Simplification of CFF’s
1. Removal of Useless Symbols
It mainly consist of
❑ A symbol can be useless if it does not appear on the right-hand side
of the production rule and does not take part in the derivation of any 1. Removal Useless Symbols
string. That symbol is known as a useless symbol. 2. Removal Unit Production
❑ Similarly, a variable can be useless if it does not take part in the 3. Elimination of ε Production
derivation of any string. That variable is known as a useless variable.
Example: T → aaB | abA | aaT , A → aA , B → ab | b, C → ad
In the above example, the variable 'C' will never occur in the
derivation of any string, so the production C → ad is useless. So
eliminate it, and the other production A → aA is also useless because
there is no way to terminate it. If it never terminates, then it can never
produce a string. Hence this production can never take part in any
derivation.
So Simplified CFG T → aaB | aaT, B → ab | b
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Simplification of CFG
2. Removal of Unit Production Simplification of CFF’s
❑ The unit productions are the productions in which one non-terminal It mainly consist of
gives another non-terminal.
1. Removal Useless Symbols
❑ Use the following steps to remove unit production:
2. Removal Unit Production
Step 1: To remove X → Y, add production X → a to the grammar
rule whenever Y → a occurs in the grammar. 3. Elimination of ε Production
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Simplification of CFG
Simplification of CFF’s
3. Elimination of ε Production
It mainly consist of
❑ The productions of type S → ε called ε Productions. These type of
productions can only be removed from those grammars that do not 1. Removal Useless Symbols
generate ε. 2. Removal Unit Production
❑ Use the following steps to Elimination of ε Production: 3. Elimination of ε Production
Step 1: First find out all null able non-terminal variable which
derives ε.
Step 2: For each production A → a, construct all production A → x,
where x is obtained from a by removing one or more non-terminal
from step 1.
Step 3: Now combine the result of step 2 with the original production
and remove ε productions.
Example: Remove the production from the following CFG by preserving
the meaning of it. S → XYX, X → 0X | ε, Y → 1Y | ε.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Simplification of CFG
Simplification of CFF’s
3. Elimination of ε Production
It mainly consist of
Example: Remove the production from the following CFG by preserving
the meaning of it. S → XYX, X → 0X | ε, Y → 1Y | ε. 1. Removal Useless Symbols
❑ Now, while removing ε production, we are deleting the rule X → ε 2. Removal Unit Production
and Y → ε. To preserve the meaning of CFG we are actually placing 3. Elimination of ε Production
ε at the right-hand side whenever X and Y have appeared. Then
S → XYX
❑ If the first X at right-hand side is ε. Then
S → YX
❑ Similarly if the last X in R.H.S. = ε. Then
S → XY
❑ If Y = ε then
S → XX
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Simplification of CFG
3. Elimination of ε Production Simplification of CFF’s
Example: Remove the production from the following CFG by preserving It mainly consist of
the meaning of it. S → XYX, X → 0X | ε, Y → 1Y | ε.
1. Removal Useless Symbols
❑ If Y and X are ε then,
2. Removal Unit Production
S→X
3. Elimination of ε Production
❑ If both X are replaced by ε
S→Y
Now,
S → XYX | XY | YX | XX | X | Y
❑ Now let us consider
X → 0X If Y = ε then
❑ If we place ε at right-hand side for X then,
X→0
X → 0X | 0
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Simplification of CFG
Simplification of CFF’s
3. Elimination of ε Production
It mainly consist of
Example: Remove the production from the following CFG by preserving
the meaning of it. S → XYX, X → 0X | ε, Y → 1Y | ε. 1. Removal Useless Symbols
❑ Similarly 2. Removal Unit Production
Y → 1Y | 1 3. Elimination of ε Production
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Normal Form
Normal Form
❑ CFG always should be in some standard form.
It mainly of two types
❑ There are mainly two normal forms:
1. Chomsky's Normal Form (CNF) 1. Chomsky's Normal Form
2. Greibach Normal Form (GNF) 2. Greibach Normal Form
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky's Normal Form (CNF)
1. Chomsky's Normal Form (CNF) Normal Form
❑ Steps for converting CFG into CNF: It mainly of two types
Step 1: Eliminate start symbol from the RHS. If the start symbol T is 1. Chomsky's Normal Form
at the right-hand side of any production, create a new production as:
2. Greibach Normal Form
S1 → S (Where S1 is the new start symbol)
S1 → S
S → a | aA | B
A → aBB | ε
B → Aa | b
❑ Step 2: As grammar G1 contains A → ε null production, its removal
from the grammar yields:
S1 → S
S → a | aA | B
A → aBB
B → Aa | b | a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky's Normal Form (CNF)
Normal Form
❑ Now, as grammar G1 contains Unit production S → B, its removal
yield: It mainly of two types
S1 → S 1. Chomsky's Normal Form
S → a | aA | Aa | b 2. Greibach Normal Form
A → aBB
B → Aa | b | a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky's Normal Form (CNF)
Normal Form
❑ Step 3: In the production rule S0 → aA | Aa, S → aA | Aa, A → aBB
and B → Aa, terminal a exists on RHS with non-terminals. So we It mainly of two types
will replace terminal a with X. 1. Chomsky's Normal Form
S0 → a | XA | AX | b 2. Greibach Normal Form
S → a | XA | AX | b
A → XBB
B → AX | b | a
X→a
❑ Step 4: In the production rule A → XBB, RHS has more than two
symbols, removing it from grammar yield:
S0 → a | XA | AX | b
S → a | XA | AX | b
A → RB, B → AX | b | a
X → a, R → XB
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Greibach Normal Form (GNF)
2. Greibach Normal Form (GNF) Normal Form
❑ GNF stands for Greibach normal form. A CFG (Context Free It mainly of two types
Grammar) is in GNF (Greibach Normal Form) if all the
production rules satisfy one of the following conditions: 1. Chomsky's Normal Form
2. Greibach Normal Form
▪ Start symbol generating ε.
Example: S → ε
▪ A non-terminal generating a terminal.
Example: S → a
▪ A non-terminal generating a terminal which is followed by any
number of non-terminals.
Example: S → aASB
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky's Normal Form (CNF)
2. Greibach Normal Form (GNF) Normal Form
Example: G1 = {S → aAB | aB, A → aA| a, B → bB | b} It mainly of two types
G2 = {S → aAB | aB, A → aA | ε, B → bB | ε} 1. Chomsky's Normal Form
❑ The production rules of Grammar G1 satisfy the rules specified for 2. Greibach Normal Form
GNF, so the grammar G1 is in GNF.
G1 = { S → aAB | aB,
A → aA | a
B → bb | b } ……….. all are in GNF
❑ However, the production rule of Grammar G2 does not satisfy the
rules specified for GNF as A → ε and B → ε contains ε(only start
symbol can generate ε). So the grammar G2 is not in GNF.
G2 = { S → aAB | aB,
A → aA | ε,
B → bB | ε} ……….. all are not in GNF
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Greibach Normal Form (GNF)
Normal Form
2. Greibach Normal Form (GNF)
It mainly of two types
❑ Steps for converting CFG into GNF: 1. Chomsky's Normal Form
Step 1: Convert the grammar into CNF. 2. Greibach Normal Form
Step 3: In the grammar, convert the given production rule into GNF
form.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Greibach Normal Form (GNF)
Normal Form
2. Greibach Normal Form (GNF)
It mainly of two types
Example: Convert the given CFG to GNF. Consider the given grammar,
S → XB | AA, A → a | SA, B → b, X → a. 1. Chomsky's Normal Form
❑ As the given grammar G is already in CNF and there is no left 2. Greibach Normal Form
recursion, so we can skip step 1 and step 2 and directly go to step 3.
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Greibach Normal Form (GNF)
Normal Form
❑ The production rule S → XB and B → XBA is not in GNF, so we
substitute X → a in the production rule S → XB and B → XBA as: It mainly of two types
S → aB | AA 1. Chomsky's Normal Form
A → a | aBA | AAA 2. Greibach Normal Form
B→b
X→a
❑ Now we will remove left recursion (A → AAA), we get:
S → aB | AA
A → aC | aBAC
C → AAC | ε
B→b
X→a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Greibach Normal Form (GNF)
❑ Now we will remove null production C → ε, we get: Normal Form
S → aB | AA It mainly of two types
A → aC | aBAC | a | aBA 1. Chomsky's Normal Form
C → AAC | AA 2. Greibach Normal Form
B→b
X→a
❑ The production rule S → AA is not in GNF, so we substitute
A → aC | aBAC | a | aBA in production rule S → AA as:
S → aB | aCA | aBACA | aA | aBAA
A → aC | aBAC | a | aBA
C → AAC
C → aCA | aBACA | aA | aBAA
B→b
X→a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Greibach Normal Form (GNF)
Normal Form
❑ The production rule C → AAC is not in GNF, so we substitute A →
aC | aBAC | a | aBA in production rule C → AAC as: It mainly of two types
1. Chomsky's Normal Form
S → aB | aCA | aBACA | aA | aBAA
2. Greibach Normal Form
A → aC | aBAC | a | aBA
C → aCAC | aBACAC | aAC | aBAAC ….. is in GNF
C → aCA | aBACA | aA | aBAA
B→b
X→a
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky Hierarchy
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky Hierarchy
Example: A → xy
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky Hierarchy
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org
Chomsky Hierarchy
www.sandipuniversity.edu.in
Department of Computer Engineering & Information Technology, SITRC, Nashik www.sandipfoundation.org