# Lecture 12 Examples of CFL - PowerPoint PPT Presentation

1 / 17

Lecture 12 Examples of CFL n n Example 1. L={0 1 | n > 0} is a CFL. To show that L is a CFL, we need to construct a CFG G generating L. Let G = (V, Σ , R, S) where V = {S} Σ = {0, 1} R ={S → ε |0S1 } Example 2. Construct CFG to generate L={xx | x is in (0+1)*}. R

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Lecture 12 Examples of CFL

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## Lecture 12 Examples of CFL

n

n

Example 1. L={0 1 | n > 0} is a CFL.

To show that L is a CFL, we need to construct a

CFG G generating L.

Let G = (V, Σ, R, S) where

V = {S}

Σ = {0, 1}

R ={S → ε|0S1 }

Example 2. Construct CFG to generate

L={xx | x is in (0+1)*}.

R

G = (V, Σ, R, S) where

V = {S}

Σ = {0, 1}

R = { S → ε | 0S0 | 1S1}

Example 3 L={ x (0+1)* | #0(x) = #1(x)}

Idea: what relations exist between longer strings

and shorter strings in this language?

Consider four cases:

Case 1. x = 0w1. Then x is in L iff w is in L.

If we use S to represent a string in L, then

the relation in Case 1 can be represented

As a rule

S → 0S1

Case 2. x=1w0. Similar to case 1. This case

gives rule

S → 1S0

Case 3. x=0w0. Suppose w = w1w2···wn.

Consider the following sequence:

#0(0) - #1(0) > 0,

#0(0w1) - #1(0w1),

…,

#0(0w1···wn) - #1 (0w1···wn) < 0.

Note that in this sequence, two adjacent numbers

Have difference 1. Therefore, there exists I such that

#0(0w1···wi) - #1 (0w1···wi)=0

This means that x is the concatenation of two shorter

strings in L. So, we have a rule

S → SS

Case 4. x=1w1. Similar to Case 3.

Based on the above analysis, we have CFG

G=(V, Σ, R, S) where

V = {S}

Σ = {0, 1}

R = {S → ε | 0S1 | 1S0 | SS}

Each nonterminal symbol represents a language.

Each rule represents a relationship between

languages represented by nonterminal symbols.

CFL is closed under union.

Proof. Suppose A = L(GA) and B = L(GB) where

GA = (VA, ΣA, RA, SA)

GB = (VB, ΣB, RB, SB)

Without loss of generality, assume VA ∩ VB= Ǿ.

(Otherwise, we may change some nonterminal

symbols.)

Then A U B = L(G) for G=(V, Σ, R, S) where

V = VA U VB U {S}

Σ = ΣA U ΣB

R = RA U RB U {S → SA | SB }

Example 4 L = { 0 1 | m ≠ n, m, n > 0}

m

n

m

n

n

m

L = {0 1 | m > n > 0} U {0 1 | n > m > 0}

Hence, L = L(G) for G = ({S, SA, SB}, {0,1}, R, S)

where

R = {S → SA | SB,

SA → 0 | 0SA | 0SA1,

SB →1 | SB1 | 0SB1}

CFL is closed under concatenation.

Proof. Suppose A = L(GA) and B = L(GB) where

GA = (VA, ΣA, RA, SA)

GB = (VB, ΣB, RB, SB)

Without loss of generality, assume VA ∩ VB= Ǿ.

(Otherwise, we may change some nonterminal

symbols.)

Then AB = L(G) for G=(V, Σ, R, S) where

V = VA U VB U {S}

Σ = ΣA U ΣB

R = RA U RB U {S → SASB }

+

Example 5 L = {xx w | x (0+1), w (0+1)*}

R

+

R

L = {xx | x (0+1) }{0,1}*

L=L(G) for G = ({S, SA, SB}, {0, 1}, R, S)

where

R = { S → SASB,

SA→ 00 | 11 | 0SA0 | 1SA1,

SB→ ε | 0SB | 1SB }

CFL is closed under star-closure.

Proof. Suppose L = (G) for G=(V, Σ, R, S).

Then L* = L(G*) for G* =(V, Σ, R*, S)

where R* = R U { S → ε | SS}.

Example 6 L=(0+1)*00

L=L(G) for G=({S, A}, {0,1}, R, S)

where

R={S → A00, A → ε | AA | 0 | 1 }

The role of nonterminat symbol.

Every nonterminal symbol A represents

a language which can be generated by

using A as start symbol.

Example 7.

L={a b c d | m+n = p+q, m, n, p, q > 0}

n

m

p

q

Let S represent L,

A represent {b c | n > 0}

B represent {a b c | m+n = p, m, n, p > 0}

C represent {b c d | n = p+q, n, p, q > 0}

n

n

n

m

p

n

p

q

Then we can find relations

S → aSd | B | C,

B → aBc | A,

C → bCd | A,

A → bAc | ε.

Example 8

L ={x (0+1)*| x ≠ ww for any w (0+1)*}

Let us analyze what would happens for x in L.

Case 1. |x| = odd.

In this case, x is either in language

A = {u0v | |u|=|v|, u, v (0+1)*}

or

B = {u1v | |u|=|v|, u, v (0+1)*}.

Case 2. |x| = even. Write

x = x1 x2 … xm y1 y2 …ym .

There exists i such that xi ≠ yi .

Subcase 2.1 x = x1… xi-10xi+1 …xmy1…yi-11yi+1…ym.

x is in AB

Subcase 2.2 x = x1… xi-11xi+1 …xmy1…yi-10yi+1…ym.

x is in BA

Thus, L = A+B+AB+BA.

So, L=L(G) for G=({L,A,B}, {0,1}, R, L)

where

R ={ L → A | B | AB | BA,

A → 0 | 0A0 | 0A1 | 1A0 | 1A1,

B → 1 | 0B0 | 0B1 | 1B0 | 1B1}.