Lecture 12 Examples of CFL

advertisement

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

R

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

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 = w

1 w

2

···w n

.

Consider the following sequence:

#

0

(0) - #

1

(0) > 0,

#

0

(0w

1

) - #

1

(0w

1

),

…,

#

0

(0w

1

···w n

) - #

1

(0w

1

···w n

) < 0.

Note that in this sequence, two adjacent numbers

Have difference 1. Therefore, there exists I such that

#

0

(0w

1

···w i

) - #

1

(0w

1

···w i

) = 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(G

A

) and B = L(G

B

) where

G

A

= (V

A

, Σ

A

, R

A

, S

A

)

G

B

= (V

B

, Σ

B,

R

B

, S

B

)

Without loss of generality, assume V

A

∩ V

B

= Ǿ.

(Otherwise, we may change some nonterminal symbols.)

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

V = V

A

U V

B

U {S}

Σ = Σ

A

U Σ

B

R = R

A

U R

B

U {S → S

A

| S

B

}

m n m n

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

Hence, L = L(G) for G = ({S, S

A

, S

B

}, {0,1}, R, S) where

R = {S → S

S

A

A

| S

B

,

→ 0 | 0S

A

| 0S

A

1,

S

B

→1 | S

B

1 | 0S

B

1}

CFL is closed under concatenation.

Proof. Suppose A = L(G

A

) and B = L(G

B

) where

G

A

= (V

A

, Σ

A

, R

A

, S

A

)

G

B

= (V

B

, Σ

B,

R

B

, S

B

)

Without loss of generality, assume V

A

∩ V

B

= Ǿ.

(Otherwise, we may change some nonterminal symbols.)

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

V = V

A

U V

B

U {S}

Σ = Σ

A

U Σ

B

R = R

A

U R

B

U {S → S

A

S

B

}

+

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

+

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

L=L(G) for G = ({S, S

A

, S

B

}, {0, 1}, R, S) where

R = { S → S

A

S

B

,

S

A

→ 00 | 11 | 0S

A

0 | 1S

A

1,

S

B

→ ε | 0S

B

| 1S

B

}

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}.

S represents L and S * represents L * .

Then S *

 

| S * S .

So, S *

S .

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. m n p q

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

Let S represent L, n n p n p q

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

Then we can find relations

S → aSd | B | C,

B → aBc | A,

C → bCd | A,

A → bAc | ε.

Example 8

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 = x

1 x

2

… x m y

1 y

2

…y m

.

There exists i such that x i

≠ y i

.

Subcase 2.1 x = x

1

… x i-1

0x i+1

…x m y

1

…y i-1

1y i+1

…y m

.

x is in AB

Subcase 2.2 x = x

1

… x i-1

1x i+1

…x m y

1

…y i-1

0y i+1

…y m

.

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}.

Download