0% found this document useful (0 votes)
7 views

CFG Properties

Uploaded by

Chintu Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CFG Properties

Uploaded by

Chintu Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Properties of Context-free

Languages
By,
Mr.R.Karunamoorthi, AP / CSE

1
Closure Property Results
 CFLs are closed under:
 Union
 Concatenation
 Kleene closure operator
 Substitution
 Homomorphism, inverse homomorphism
 reversal
 CFLs are not closed under:
Note: Reg languages
 Intersection are closed
 Difference under
 Complementation these
operators
2
Strategy for Closure Property
Proofs
 First prove “closure under substitution”
 Using the above result, prove other closure properties
 CFLs are closed under:
 Union
 Concatenation
 Kleene closure operator
Prove  Substitution
this first  Homomorphism, inverse homomorphism
 Reversal

3
Note: s(L) can use
a different alphabet

The Substitution operation


For each a  ∑, then let s(a) be a language
If w=a1a2…an  L, then:

s(w) = { x1x2 … }  s(L), s.t., xi  s(ai)
Example:
 Let ∑={0,1}
 Let: s(0) = {anbn | n ≥1}, s(1) = {aa,bb}
 If w=01, s(w)=s(0).s(1)
 E.g., s(w) contains a1 b1 aa, a1 b1bb,
a2 b2 aa, a2 b2bb,
… and so on.

4
CFLs are closed under
Substitution
IF L is a CFL and a substititution defined
on L, s(L), is s.t., s(a) is a CFL for every
symbol a, THEN:
 s(L) is also a CFL

What is s(L)?
L s(L)
w1 s(w1) Note: each s(w)
w2 s(L) s(w2) is itself a set of strings
w3 s(w3)
w4 s(w4)
5
CFLs are closed under
Substitution
 G=(V,T,P,S) : CFG for L
 Because every s(a) is a CFL, there is a CFG for each s(a)
 Let Ga = (Va,Ta,Pa,Sa)
 Construct G’=(V’,T’,P’,S) for s(L);
 V’ has V and all V for a  ∑; T’ has T and all a  ∑
 P’ consists of:
 The productions of P, but with every occurrence of terminal “a” in
their bodies replaced by Sa.
 All productions in any Pa, for any a  ∑
Parse tree for G’: S

Sa1 Sa2 San


As w = …
x1x2..xn; CFL
x1 x2 xn 6
Substitution of a CFL:
example
 Let L = language of binary palindromes s.t., substitutions for 0
and 1 are defined as follows:
 s(0) = {anbn | n ≥1}, s(1) = {xx,yy}
 Prove that s(L) is also a CFL.

CFG for L: CFG for s(0): CFG for s(1):

S=> 0S0|1S1| S0=> aS0b | ab S1=> xx | yy

Therefore, CFG for s(L):

S=> S0SS0 | S1 S S1 |
S0=> aS0b | ab
S1=> xx | yy 7
CFLs are closed under union
Let L1 and L2 be CFLs
To show: L2 U L2 is also a CFL
Let us show by using the result of Substitution
 Make a new language:
 Lnew = {a,b} s.t., s(a) = L1 and s(b) = L2
==> s(Lnew) == same as == L1 U L2

 A more direct, alternative proof


 Let S1 and S2 be the starting variables of the grammars for L1
and L2

Then, Snew => S1 | S2

8
CFLs are closed under
concatenation
 Let L1 and L2 be CFLs
Let us show by using the result of Substitution
 Make Lnew= {ab} s.t.,
s(a) = L1 and s(b)= L2
==> L1 L2 = s(Lnew)

 A more direct, alternative proof


 Let S1 and S2 be the starting variables of the grammars for L1
and L2

Snew => S1 S2

9
CFLs are closed under
Kleene Closure
 Let L be a CFL

 Let Lnew = {a}* and s(a) = L1

 Then, L* = s(Lnew)

10
We won’t use substitution to prove this result

CFLs are closed under


Reversal
 Let L be a CFL, with grammar
G=(V,T,P,S)
 For LR, construct GR=(V,T,PR,S) s.t.,
 If A==>  is in P, then:
 A==> R is in PR

 (that is, reverse every production)


 Sentential forms of G are reversal of sentential
forms of GR
11
homorphism
 L is in CFL over Σ and h is a
homomorphism on Σ. Let ‘s’ be
substitution that replaces very symbol
‘a’ in Σ by a lang of one string h(a).
s(a)=h(a). Then h(L)=s(L)

12
Some negative closure results

CFLs are not closed under


Intersection
 Existential proof:
 L = {0n1n2i | n≥1,i≥1}
1
 L = {0i1n2n | n≥1,i≥1}
2
 Both L1 and L1 are CFLs
 S AB; A 0A1 | 01; B  2B | 2 ( same no of 0’s and 1’s)

 S AB ; A  0A | 0; B  1B2 | 12 (same no of 1’s and

2’s)
 But L1  L2 cannot be a CFL
 Why?  same no of 0,1,2  not CFL

 We have an example, where intersection is not closed.


 Therefore, CFLs are not closed under intersection

13
Some negative closure results

CFLs are not closed under


complementation
 Follows from the fact that CFLs are not
closed under intersection

 L1  L2 = L1 U L2
Logic: if CFLs were to be closed under complementation
 the whole right hand side becomes a CFL (because
CFL is closed for union)
 the left hand side (intersection) is also a CFL
 but we just showed CFLs are
NOT closed under intersection!
 CFLs cannot be closed under complementation.
14
Some negative closure results

CFLs are not closed under


difference
 Follows from the fact that CFLs are not
closed under complementation

 Because, if CFLs are closed under


difference, then:
 L = ∑* - L
 So L has to be a CFL too
 Contradiction

15

You might also like