Module-5
Module-5
NORMAL FORMS,
Properties of CFL AND
PUMPING LEMMA
Contents:
Normal forms for CFGs:
Chomsky Normal Form and
Griebach Normal Form
Closure properties of CFLs
Decision Properties of CFLs:
Emptiness, Finiteness and Membership
Pumping lemma for CFLs.
THE CHOMSKY NORMAL FORM
A context-free grammar is in Chomsky
normal form if every rule is of the form:
A → BC A,B, C are variables
A→a a is a terminal
Operations needed:
Eliminate ε productions
Eliminate unit productions
Eliminate useless symbols
Convert the following into Chomsky normal form:
A → BAB | B | ε
B → 00 | ε
Eliminate Epsilon Production
A → BAB | B | ε A → BAB | B | BB | AB | BA
B → 00 | ε B → 00
C → AB,
A → BC | DD | BB | AB | BA , B → DD, D → 0
Convert to CNF
S→bA|aB
A →bAA|aS|a
B →aBB|bS|b
There are no ε productions
There are no unit productions
There are no useless symbols.
Add new Variables:
S→CbA|CaB Cb →b, Ca →a
A →CbD1| Ca S|a D1 → AA
B → Ca D2| Cb S|b D2 → BB
CNF
1. S aB | bA 4. S AB | CA
A aS| bAA |a B BC| AB
B bS| aBB |b A a
C aB|b
2. S aAbB
5. S aAa | bBb|ε
A aA | a
A C| a
B bB | b
B C|b
3. S →aSaA|A C CDE| ε
A →abA|b D A|B|ab
GNF(Greibach Normal Form)
• Every CFL without ε can be generated by a
grammar for which every production is of
the form
A→aα
where A is a variable,
a is terminal and
α is a string of variables.
Steps for constructing GNF
• Let G=(V,T,P,S) be a chomsky normal form
grammar generating the CFL L.
• Assume that V={A1,A2,A3,…. Am}
• Modify the production which is of the form
• AiAjγ where j < i
– If Ai Aj γ where j< i then substitute for Aj with its
productions.
suppose Aj AK | AL then the new set of productions are
Aj AK γ | AL γ
– If we get the productions like Ai Aj γ where j = i , introduce
a new variable Bi.
GNF Cntd
• Bi→ γ | γBi
and remove the production Ai Ai γ
- For each production Ai β where β does not begin
with Ai then add the production
Ai βBi
After converting all these things, we have only
productions of the forms
1. Ai Ajγ , j>i
2. Ai a γ aT
3. Bi γ γ (V{B1, B2 .....Bi-1. }α
GNF
• SAB A1 A2A3 i<j
• A BS|b A2 A3A1|b i<j
• B SA|a A3 A1A2|a i>j
Substitute the string A2A3 for A1 (i>j)
A3 A2A3 A2 | a
Substitute the string A3A1|b for A2 (i>j)
A3 A3A1 A3 A2 |b A3 A2 | a
(i=j) B3 A1 A3 A2 | A1 A3 A2 B3 (Bi→ γ | γBi)
A3 b A3 A2 | a | b A3 A2 B3 | a B3 (Ai→ β | βBi)
The resultant productions are:
• A1 A2A3
• A2 A3A1|b
• A3 b A 3 A2 | a | b A 3 A2 B3 | a B3
• B3 A 1 A 3 A2 | A 1 A 3 A2 B3
S S
A A
A A
R
A
u v w x y u v x y
v w x
Idea of Proof: If Z is short, then any parse tree
for z must not have v and x.
u y
Proving Languages not to be regular
To show that a language L is not context free, We need
to use the following steps:
1. Assume L is context-free. Let n be the
natural number obtained by using the pumping
lemma.
2. Choose z є L, so that |z|≥n. Write
z=uvwxy using the lemma.
3.Find a suitable integer k such that
uvkwxk y L. This is a contradiction and so L is
not context free.
Show that L={anbncn|n≥1} is not CFL
• Assume L is context-free. Let n be the natural
number obtained by using the pumping
lemma.
• Let z = |anbncn |= 3n>n
• uvwxy =anbncn
• As 1≤|vx| ≤n, v or x cannot contain all the three
symbols a,b,c.
• So v or x is of the form:
CASE 1: only a’s , only b’s , only c’s
CASE 2: aibj for some i,j, bicj for some i,j
CASE 1:
• vx is formed by the repetition of single symbol.
• vx=ai |vx|=i
uvkwxk y =? when k=0
uv0wx0 y = uwy = an-ibncn L
• vx=bi |vx|=i
uvkwxk y =? when k=0
uv0wx0 y = uwy = anbn-icn L
• vx=ci |vx|=i
uvkwxk y =? when k=0
uv0wx0 y = uwy = anbncn-i L
CASE 2:
• vx is formed by two symbols.
• vx= aibj
uvkwxk y =? when k=0
uv0wx0 y = uwy = an-ibn-jcn L
• vx=bi cj
• uvkwxk y =? when k=0
• uv0wx0 y = uwy = anbn-icn-j L
• L = L(0*)
• = (s(0))*
• = {an bn }*
Union
If L1 and L2 are context free languages. Then
L1 L2 is the language S(L), where L is the
language {1,2} and S is the substitution
defined by S(1) = L1 and S(2) = L2
Concatenation
If L1 and L2 are context free languages. Then
L1L2 is the language S(L), where L is the
language {12} and S is the substitution defined
by S(1) = L1 and S(2) = L2
Star Closure and +closure
If L is a context free language. Then L is the
language {1}* and S is the substitution defined by
S(1) = L1 and L1*=S(L).
If L is a context free language. Then L is the
language {1}+ and S is the substitution defined by
S(1) = L1 and L1+=S(L).
Homomorphism
Suppose L is a context free language over ∑ and h is a
homomorphism on ∑. Let S be the substitution that
replaces each symbol a in ∑ by the language consisting
of the one string that is h(a). Then S(a)= {h(a)} for all a in
∑. Then h(L)=S(L).
Reversal
• CFL s are closed under reversal. We cannot use
substitution theorem.
• Theorem: If L is a CFL, then So is LR.
• Proof: Let L=L(G) for some CFL G=(V,T,P,S).
Construct GR=(V,T,PR,S). Where PR is the
reverse of each production.
• i.e. If A→α is a production of G, then
• A→αR is a production of GR
• Example: G= S →0S1|01
GR= S →1S0|10
The CFLs are not closed under
intersection
• L={0n1n2n | n ≥ 1} is not a CFL. However the
following two languages are context-free.
L1={0n1n2i | n, i ≥ 1}-equal no. of 0’s and 1’s
L2={0i1n2n | n ,i ≥ 1}- equal no. of 1’s and 2’s
L3={0n1i2n | n ,i ≥ 1}=?
The grammar for L1 The grammar for L2
is: is:
S →AB S →AB
A → 0A1|01 A → 0A|0
B →2B|2 B →1B2|12
L= L1∩ L2
L=L ∩ L2-A string in both languages must have
1
Stack
L∩R
• FA = (QA, Σ, A, qA, FA)
• PDA = (QP, Σ, Γ, P, qP, Z0, FP)
• Resultant PDA:
p q r
ε ,X0 /ZX0 ε ,X0 / ε
i
e L(FA)= i*e*
s t
e
(PDA)
L(FA) ∩L(PDA) =inen+1
δP (p,ε ,X0)= (q,ZX0 )
δ P (q,e ,Z)= (q, ε ) δ ((p,s),ε ,X0)= {((q,s),ZX0 )}
δ P (q,i ,Z)= (q,ZZ ) δ ((q,s),e ,Z)= {(q,t), ε )}
δ P (q,ε ,X0)= (r, ε ) δ ((q,s)i ,Z)= {( (q,s),ZZ )}
(FA) δ (q,s),ε ,X0)= {((r,s), ε )}
δA(s,i)= s S→iSe | e
δ (q,t),e ,Z)= {((q,t), ε )}
δA (s,e)= t δ (q,t),ε ,X0)={ ((r,t), ε )}
δA (t,e)= t ε ,X0 / ε
r,s
i , Z /ZZ
e, Z /ε ε ,X0 / ε
p,s q,s q,t r,t
ε ,X0 /ZX0 e, Z /ε
Find L(FA) ∩L(PDA)
• L1-R= L1∩R‘
• R‘ is complement of R –It is RL.
• But L1∩R’is definitely CFL.
Inverse Homomorphism
a h
Input h(a)
Buffer
Accept/
PDA State
Reject
Stack
III Internal Test Portions
UNIT III Push Down Automata (PDA):
Description and definition – Instantaneous
Description - Language of PDA: Acceptance by
Final state, Acceptance by empty stack -
Deterministic PDA – Equivalence of PDA and CFG:
CFG to PDA and PDA to CFG
UNIT IV PROPERTIES OF CONTEXT FREE
GRAMMAR
Normal forms for CFGs: Chomsky Normal Form
and Griebach Normal Form - Closure properties of
CFLs – Decision Properties of CFLs: Emptiness,
Finiteness and Membership -Pumping lemma for