Vous êtes sur la page 1sur 17

# Lecture 12 Examples of CFL

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

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

gives rule
S 1S0

## Case 3. x=0w0. Suppose w = w1w2wn.

Consider the following sequence:
#0(0) - #1(0) > 0,
#0(0w1) - #1(0w1),
,
#0(0w1wn) - #1 (0w1wn) < 0.
Note that in this sequence, two adjacent numbers
Have difference 1. Therefore, there exists I such that
#0(0w1wi) - #1 (0w1wi) = 0
This means that x is the concatenation of two shorter
strings in L. So, we have a rule
S SS

## Based on the above analysis, we have CFG

G=(V, , R, S) where
V = {S}
= {0, 1}
R = {S | 0S1 | 1S0 | SS}

## 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

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

+

R

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

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

## Every nonterminal symbol A represents

a language which can be generated by
using A as start symbol.

Example 7.
m

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

Let S represent L,
A represent {b n c n| n > 0}
n

n

## 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
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 xmy1yi-11yi+1ym.

x is in AB
Subcase 2.2 x = x1 xi-11xi+1 xmy1yi-10yi+1ym.
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}.