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
Eliminate Unit Production
A → BAB | 00 | BB | AB | BA
B → 00 Add new variables
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
According to GNF right hand side of production
should start with terminals
GNF Form
A3 b A 3 A2 | a | b A 3 A2 B3 | a B 3
A2 b A3 A2 A1| aA1 | b A3 A2 B3A1 | a B3 A1|b
A1 b A3 A2 A1A3| aA1A3 | b A3 A2 B3A1A3 | a B3
A1A3|b A3
B3 b A3 A2 A1A3 A3 A2 | aA1A3 A3 A2 | b A3 A2
B3A1A3 A3 A2 | a B3 A1A3 A3 A2 |b A3 A3 A2 | b A3 A2
A1A3 A3 A2 B3 | aA1A3 A3 A2 B3 | b A3 A2 B3A1A3 A3
A2 B3 | a B3 A1A3 A3 A2 B3 |b A3 A3 A2 B3
Home work
1 S → AA | a
A →SS | b
2.S → AB
C →AB | b
A→a
B → CA
3. S →aSb
S →ab
THE PUMPING LEMMA FOR CFGs
Let L be a context-free language
Then there is a constant n such that
if z L and |z| ≥ n
then can write z = uvwxy, where:
1. |vx| > 0
2. |vwx| ≤ n
3. For every k ≥ 0, uvkwxk y L
Idea of Proof: If z is long enough, then any
parse tree for z must have a path that contains
a variable more than once
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.
[Link] 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
Therefore L={ anbncn| n≥1} is not context free
Assignment
• Show that the following languages are not
CFL:
• 1. L={ambncmdn | n≥1}
• 2. L={ambncm | m≥n}
• 3. L={0P | p is a prime}
Closure Properties of CFL
(Application of Substitution theorem)
• CFL’s are closed under the following operations :
• 1. Union
• 2. Concatenation
• 3. Closure(*,+)
• 4. homomorphisms, inverse homomorphisms
• 5. Reversal
CFL’s are not closed under
[Link]
[Link]
substitution theorem
• Let ∑ be an alphabet, and suppose that for every
a in ∑, we choose a language La These
languages define a function s on ∑, which is
called substitution.
• If w=a1a2…an is a string in ∑* then S(w) is the
language of all strings x1x2…xn such that string
xi is in the language S(ai).
• S(w)=S(a1 )S(a2 )…..S(an ).
• S(L)=union of S(w) for all strings w in L.
Substitution example
• S(0)={an bn | n≥1} S(1)={aa, bb}
• ∑={0,1}
Let w=01
S(w)=S(0).S(1)
={an bn aa, an bn bb | n≥1}
• 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
equal number of all three symbols and thus
be in L= {0n1n2n | n ≥ 1}.
Since ={0n1n2n | n ≥ 1} is not CFL ,
L ∩ L2 is not CFL .
1
If L is CFL &R is RL then L∩R
is a CFL.
FA
Input AND Accept
PDA
Stack
L∩R
• FA = (QA, Σ, A, qA, FA)
• PDA = (QP, Σ, Γ, P, qP, Z0, FP)
• Resultant PDA:
PDAR=(QP XQA ,Σ, Γ, , (qP, qA ), Z0, FPXFA)
((q,p),a,X)=((r,s),γ) such that
– 1. s= A (p,a)
– 2. pair (r, γ) is in P (q,a,X)
Given PDA e, Z /ε L(PDA) =One more else than if
i,Z /ZZ Final state 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)
Given PDA L(PDA) =0n1n
0, Z0 /0Z0 1,0 /ε
0,0 / 00
p q r
1,0 /ε
ε ,Z0 / Z0
Given FA
1 1
L(FA)= odd number of 0’s
s 0 t
0
Difference
• L1-L2 is not necessarily CFL.
• L1-L2= L1∩L2‘.
• L2‘ is complement of L2 –It is CFL.
• But L1∩L2’is not necessarily CFL.
• 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