# Prove that CFL is closed under union and star but not under intersection?

Data Structure AlgorithmsComputer ScienceComputers

CFL refers to Context Free Language in the theory of computation (TOC). Let us now understand how CFL is closed under Union.

CFL is closed under UNION

If L1 and L2 are CFL’s then L1 U L2 is also CFL.

Let L1 and L2 are generated by the Context Free Grammar (CFG).

G1=(V1,T1,P1,S1) and G2=(V2,T2,P2,S2) without loss of generality subscript each non terminal of G1 and a1 and each non terminal of G2 with a2 (so that V1∩V2=φ).

Subsequent steps are used production entirely from G1 or from G2.

Each word generated thus is either a word in L1 or L2.

Example

Let L1 be palindrome, defined by:

S->aSa|bSb|a|b|^

Let L2 be {anbn|n>=0} defined by −

S->aSb|^

Then the union language is defined as −

S->S1|S2

S1->aS1a|bS1b|a|b|^

S2->aS2b|^

CFG are closed under KLEENE star

Now, let us understand how CFG is closed under a star.

Proof

If L1 is a CFL then L1* is a CFL.

Let L1 be generated by CFG G1=(V1,T1,P1,S1) without loss of generality subscript each non-terminal of G1 with a1.

Define the CFG, G generates L1* as −

G=(v1 U {S}, T1, P1 U {S->S1S{^},S}

Each word generated either ^ or the same sequence of words in L1.

Every word in L1* can be generated by G.

Example

Let L1 be {anbn|n>=0} defined by S->aSb|^

Then L1* generated as:

S->S1S|^

S1->aS1b|^

CFGs are not closed under intersection

Let us now understand how CFG cannot be closed under intersection.

Proof

If L1 and L2 are CFL’s then L1∩L2 may not be a CFL.

L1={anbnam|n,m>=0} is generated by the CFG −

S->XA

X->aXb|^

A->Aa|^

L2-{anbmam|n,n>=0} is generated by CFG −

S->AX

X->aXb|^

A->Aa|^

L1∩L2 ={anbnan|n>=0} which is not to be a CFL.