Module 4 [Autosaved]1
Module 4 [Autosaved]1
Theory of Computation
MODULE - 4
Dr. T.M. Navamani
Associate Professor
School of Computer Science and Engineering (SCOPE)
Module -4
Context Free Grammars:
• Context-Free Grammar (CFG)
• Derivations - Parse Trees
• Ambiguity in CFG
• CYK algorithm
• Simplification of CFG
• Elimination of Useless symbols, Unit productions, Null productions
• Normal forms for CFG: CNF and GNF
• Pumping Lemma for CFL
• Closure Properties of CFL
• Context-sensitive grammars: Definition and Examples
Context Free Grammar (CFG)
A grammar G = (N, T, P, S) is said to be context-free if all productions in
P have the form
A →
S → abB
A → aaBb
B → bbAa
A → ε
L = {ab(bbaa) bba(ba) / n 0}
n n
Examples
4) The language L = {a n b m / m n} is context-free.
E + E E * E
a E * E E E c
+
b c a b
a=2
b=3 a + b * c = 14 a + b * c = 20
c=4 yield of the tree a + b * c
A tree that has properties 3, 4 and 5, but in which 1 does not
necessarily hold and in which property 2 is replaced by
2a) Every leaf has label from NUTU{ε}.
is said to be a Partial derivation tree.
E
E + E
a
yield of the tree is a + E
Ambiguity in CFG
Let G = (N, T, P, S) be a context-free grammar (CFG). A word w ∈ L(G) is
said to be ambiguously derivable if there exists more than one
derivation tree (or more than one left most derivation or more than
one right most derivation) for w in L(G).
S a S b S S a S b S
ε ε
ε
S a S b S S a S b S S a S b S
ε ε
ε
ε ε
ε ε ε
S b S a S
S S S S
ε ε
a S b a S b
a S b a S b
ε ε
Examples S
3) Consider the grammar G,
G = ({S}, {a, b}, P, S), where
a S b
P: S → aSb
S → ab
a S b
G is unambiguous grammar.
a b
A CFL L can be generated by many grammars G1 , G2 , G3 , . . .
L is said to be unambiguous if there is an unambiguous grammar
generating L.
a S a S
a a S
a S
a S
a
a S
a
unambiguous grammar.
a
L(G) = { an / n ≥ 1 }
S → SS
S→a
S S
S S
a S S S S
S S
a a S S a a S S
a a a a
ambiguous grammar
Example: (Inherently ambiguous)
L(G) = { anbn cm / n, m ≥ 1 } U { anbm cm / n, m ≥ 1 } = L1 U L2
G= ({S, S1, S2 , A, B, C, D}, {a, b, c}, P, S), where
P: S → S1 / S2
S1 → AB S2 → CD
A → aAb / ab C → aC / a
B → cB / c D → bDc / bc
L1∩L2 = { anbn cn / n ≥ 1 }
L1 = { anbn cm / n, m ≥ 1 }
abc , abc2 , abc3 , abc4 , abc5 , abc6 , . . . .
a2b2c , a2b2c2 , a2b2c3 , a2b2c4 , a2b2c5 , a2b2c6 , . . . .
a3b3c , a3b3c2 , a3b3c3 , a3b3c4 , a3b3c5 , a3b3c6 , . . . .
................................................
L2 = { anbm cm / n, m ≥ 1 }
abc , ab2c2 , ab3c3 , ab4c4 , ab5c5 , ab6c6 , . . . .
a2bc , a2b2c2 , a2b3 c3 , a2b4c4 , a2b5c5 , a2b6c6 , . . . .
a3bc , a3b2c2 , a3b3 c3 , a3b4c4 , a3b5c5 , a3b6c6 , . . . .
................................................
L1∩L2 = { anbn cn / n ≥ 1 }
S
S
S2
S1
C D
A B
a b c
a b c
Theorem: The algorithm described above correctly computes Xij for all
i and j. Hence w L(G), for a CFL L = L(G) if and only if S X1n
Problems
Consider the CFG G with the following productions:
S0 → AB / SA
S → AB / SA / a
A → AB / SA / a / b
B → SA
Test the membership of aba in L(G).
S a A a
A b
Problems
ab ba
S0 SA aA ab S0 AB bB
S SA aA ab S AB bB
A SA aA ab A AB bB
B SA aA ab
aba
S0 SA SAA aAA abA aba
S SA SAA aAA abA aba
A SA SAA aAA abA aba
B SA SAA aAA abA aba
Problems
Apply Lemma 1 S → a , A→ a
Apply Lemma 2 S → a
Elimination of Null Productions
Def: Any production of a CFG G = (N, T, S, P) is of the form
A→ε
is called a ε-production or null production. A non-terminal A for which
a derivation
*
A ε
is possible is called nullable or nullable non-terminal.
Theorem: If L=L(G) for some CFG G = (N, T, S, P), then L – {ε} is L(G’) for
a CFG G’ with no useless symbols or ε-productions (or null production).
G = (N, T, S, P) G’ = (N, T, S, P’)
Step 1: Find all nullable non-terminals
Theorem: Every CFL without ε can be generated by a CFG without unit productions.
Theorem: Every CFL without ε can be generated by a CFG without
ε-productions, unit productions and useless symbols.
G = (N, T, S, P) G’ = (N, T, S, P’)
Step 1: Find all pairs (A, B) such that A B
*
*
P’ = P2 U For every pair (A, B) such that A B
add rules A→ β if B→ β P2
Example:
S → aSb
S→A L(G)= { ancmdmbn / n ≥ 0, m ≥ 1 }
A → cAd
A → cd
*
Step 1: Find all pairs (A, B) such that A B
(S, A)
Step 2: P1 = {S → A }
P2 = {S → aSb , A → cAd , A → cd}
P’ = P2 U {S → cAd , S → cd }
= {S → aSb , A → cAd , A → cd , S → cAd , S → cd }
G = (N, T, S, P)
⇓ Eli𝑚𝑖𝑛𝑎𝑡𝑒 ε−productions
G1 = (N, T, S, P1)
⇓ Eli𝑚𝑖𝑛𝑎𝑡𝑒 unit productions
G2 = (N, T, S, P2)
⇓ Apply Lemma 1
G3 = (N3, T, S, P3) Elimination of
⇓ Apply Lemma 2 useless symbols
G4 = (N4, T4, S, P4)
(Reduced Grammar)
Chomsky Normal Form
Steps to convert CFG to Chomsky Normal
Form
Problems
Chomsky Normal Form (CNF)
A context-free grammar is in Chomsky normal form if all productions
are of the form
A → BC
or
A→a
Where A, B, C ∈ N and a ∈ T.
Note: Every CFL without ε can be generated by a CFG with rules in CNF.
Examples
The grammar with productions
S → AS / a
A → SA/ b
is in Chomsky normal form. But the grammar with productions
S → AS / AAS
A → S A/ aa
is not in Chomsky normal form.
Convert CFG into CNF
Consider the grammar G = ({S}, {a, b}, P, S), where
P: S → aSb
S → ab
Step :1 For every terminal symbol introduce a new non-terminal.
S → ASB A→a
S → AB B→b
Convert CFG into CNF
Step :2 If A→ B1B2. . . Bm is a rule, then it is replaced by
A→ B1D1
D1→ B2D2
D2→ B3D3
.....
Dm-3→ Bm-2Dm-2
Dm-2→ Bm-1Bm
S → AD A→a
D → SB
S → AB B→b
Problems
2) Convert the grammar in CNF
E → E+E
E → E*E
E → (E)
E→a
Step :1 For every terminal symbol introduce a new non-terminal.
E → EPE P→+
E → EME M→*
E → LER L→( R→)
E→a
Problems (Continued..)
Step :2
E → ED1 P→+
D1 → PE
E → E D2 M→*
D2 → ME
E → L D3 L→( R→)
D3 → ER
E→a
Problems
3) Convert the grammar in CNF
S → aAD
A → aB
A → bAB
B→a
D→d
4) Convert the grammar in CNF
S → aSa
S → bSb
S→c
Greibach Normal Form (GNF)
A context-free grammar is in Greibach normal form if all productions
are of the form
A → ax
Where A ∈ N , a ∈ T and x ∈ N*.
Note: Every CFL without ε can be generated by a CFG with rules in GNF.
Lemma 1:
Define an A-production to be a production with non-terminal A on the
left. Let G = (N, T, P, S) be a context-free grammar. Let A → α1Bα2 be a
production in P and B → β1 / β2 /. . . / βr be the set of all B-productions.
Let G1 = (N, T, P1, S) be obtained from G by deleting the production
A → α1Bα2
from P and adding the productions
A → α1 β1 α2 / α1 β2 α2 / . . . / α1 βr α2
Then L(G) = L(G1).
Lemma 2:
Let G = (N, T, P, S) be a context-free grammar. Let A → Aα1 / Aα2 /. . . / Aαr
be the set of all A-productions for which A is the leftmost symbol of the
RHS. Let A → β1 / β2 /. . . / βs be the remaining A-productions.
Let G1 = (NU{Z}, T, P1, S) be the CFG formed by adding the non-terminal Z
to N and replacing all the A-productions
1) A → βi 2) Z → αi
A → βi Z , 1≤ i ≤ s Z → αi Z , 1≤ i ≤ r
A1 → a A1 A3 A2 → a
A1 → a A3 A3 → b
Step :5 Convert the Z rules into GNF
Example:2
S → SS
S → aSb
S → ab
Step :1 For every terminal symbol introduce a new non-terminal.
S → SS
S → ASB A→a
S → AB B→b
Step :2 Introduce order among non-terminals by renaming them.
S = A1 A1 → A1 A1
A = A2 A1 → A2 A1 A3 A2 → a
B = A3 A1 → A2 A3 A3 → b
Step :3
A1 → A1 A1
A1 → A2 A1 A3 / A 2 A3
A2 → a
A3 → b
Apply Lemma 2 for A1 productions, we get
A1 → A2 A1 A3 / A 2 A3
A 1 → A 2 A 1 A 3 Z / A 2 A 3Z
Z → A1 / A1 Z
A2 → a
A3 → b
Step :4 Convert the Ai rules into GNF by back substitution
A1 → aA1 A3 / aA3
A1 → aA1 A3 Z / aA3Z
Z → A1 / A1 Z
A2 → a
A3 → b
Step :5 Convert the Z rules into GNF
A1 → aA1 A3 / aA3 / aA1 A3 Z / aA3Z
Z → aA1 A3 / aA3 / aA1 A3 Z / aA3Z /
aA1 A3 Z/ aA3 Z / aA1 A3 Z Z/ aA3ZZ
A2 → a
A3 → b
Example:3
S → AS / a
A → SA / b
3. L ={ wwRw / w { a, b}* }
4. L ={ anbm cn dm / n, m ≥ 1 }
5. L = { anbn cm / n ≠ m }
Closure Properties of CFL
1. A context-free language is closed under the following operations:
i) Union
ii) Concatenation
iii) Kleene closure
2. A context-free language is not closed under
i) Intersection
ii) Complementation
3. A context-free language is closed under reversal.
4. If L is a context-free language and R is a regular language, then
L∩R is context-free language.
1. If L1 and L2 are context-free languages, then L1UL2 is a context-free
language.
Proof: L1 is a CFL there exist a CFG G1 = (N1, T1, S1, P1) such that G1
generates L1
L2 is a CFL there exist a CFG G2 = (N2, T2, S2, P2) such that G2
generates L2
Now, Construct a CFG G by combining G1 and G2 that will generate L1UL2
G = (N1U N2U{S} , T1UT2 , S , P1UP2U{S→S1 , S→S2} )
PR = { A → αR / A → α P}
7. If L is a context-free language and R is a regular language, then
L∩R is context-free language.
Proof: L is a CFL there exists a PDA M1 =(Q1, Σ1, Γ1, δ1, q1, Z1, F1)
such that M1 accepts L
R is regular there exists a DFA M2 =(Q2, Σ2, δ2, q2, F2)
such that M2 accepts R
We can combine M1 and M2 into a single PDA
M=(Q, Σ, Γ, δ, [q1, q2 ], Z, F) , where
Q = Q1 x Q2 , Σ = Σ1 U Σ2 , Γ = Γ1 , Z = Z1 , F = F1 x F2
L(G) = { anbnCn/ n ≥ 1 }
Grammars and Languages
6) G= ({S, A, B}, {a, b}, P, S), where
P: S → AB (rule 1) S AB aAbB aaB aa = aa
A → aAb (rule 2) S AB aAbB aAbbbB aAbbb
bB → bbbB (rule 3) aabb
aAb →aa (rule 4) S AB aAbB aAbbbB aAbbb
B → (rule 5) aaAbbbb
aaabbb
L(G) = { an+1bn+k/ n ≥ 1, k = -1, 1, 3, 5, . . .}
Greibach Normal Form
Convert the following grammars into GNF:
1) S → aSa
S → bSb
S→a
S→b
2) S → ABa /a
A → aaA / B
B → bAb
3) S → ab
S → aS
S → aaS