You are on page 1of 21

Fall 2009

Examples for Context-free


Language and Pumping Lemma
CSC3130 Tutorial 5
Xiao Linfu
lfxiao@cse.cuhk.edu.hk

Department of Computer Science &


Engineering

Outline
Context-free Languages, Context-free
grammars (CFG), Push-down Automata
(PDA)
Pumping lemma

Relations

Context free
language

regular
language

Relations

Context-free
Languages L

L = L(G)

Context-free
Grammars G
L(G) = L(M)

L = L(M)

Push-down
Automata M

What we should know?


Given a Language L
NOT context-free proof by pumping lemma
context-free design Pushdown Automata and
context-free grammar
NOT regular proof by pumping lemma
regular design DFA / NFA / RE

Example (I)
Given the following CFG

={a, b}

SX | Y
X aXb | aX

| a

Y aYb | Yb

| b

(1) L(G) = ?
(2) Design an equivalent PDA for it.

Example (I) --- solution: L(S)


SX|Y
X aXb | aX | a
Y aYb | Yb | b
Try to write some strings generated by it:
SXaXbaaXbbaaaXbbaaaabb

more as than bs

SYaYbaYbbaaYbbbaabbbb

more bs than as

Observations:
Start from S, we can enter two States X
& Y, and X, Y are independent;

Ls = Lx U Ly

In X state, always more a are generated;

Lx = { aibj; i>j }

In Y state, always more b are generated.

Lx = { aibj; i<j }

L(S) =
{ aibj; ij }

Example (I) --- solution: PDA


L(S) = { aibj; ij }

SX| Y
X aXb | aX
Y aYb | Yb

= { a b ; i>j } U { a b ; i<j }
i j

i j

| a
| b

PDA = NFA + a stack (infinite memory)


A possible way: divide and conquer

Lx = { aibj; i>j }
a,/A
,/$

q0

,/

b,A/
q1

,A/

LY = { aibj; i<j }

,A/
q2

a,/A

,$/

q3

,/$

, /
Combine both

q0

,/

b,A/

b,$/$

,$/
q1 b,$/$ q2
q3

Example (II)
Given the following language:
L = {0i1j: i j 2i, i=0,1,},
= {0, 1}
(1) design a CFG for it;
(2) design a PDA for it.

Example (II) -- solution: CFG


L = {0i1j: i j 2i, i=0,1,},

= {0, 1}

Consider two extreme cases:


(a). if j = i, then L1 = { 0i1j: i=j }; (b). if j = 2i, then L2 = { 0i1j:
2i=j }.
S 0S1

red-rule

S 0S11
S

If i j 2i , then randomly choose redrule or blue-rule in the generation.


S 0S1
S 0S11
S

blue-rule

Example (II) -- solution: CFG


L = {0i1j: i j 2i, i=0,1,},

= {0, 1}
Need to verify L = L(G)

G=

S 0S1

S 0S11

1). L(G) is a subset of L:


The red-rule and blue-rule guarantee that in each derivation,
the number of 1s generated is one or two times larger than that
of 0s. So, L(G) is a subset of L.

2). L is a subset of L(G):


For any w = 0i1j, i j 2i, we use red-rule (2i - j) times and
then blue-rule ( j - i ) times, i.e.,
S =*=> 02i-jS12i-j =*=> 02i-j0j-iS12(j-i)12i-j ==> 0i1j = w

Example (II) -- solution: PDA


L = {0i1j: i j 2i, i=0,1,},

= {0, 1}

Similar idea: randomly choose two extreme cases

1,X/

0,/X
,/$

,/
q0

q1

1,X/X

,$/
1,X/

q2

q3

Example (III)
Given the following language:
L = { aibjckdl: i, j, k, l=0,1,; i+k=j+l },
where the alphabet = {a, b, c, d}

(1). Design a CFG for it;


(2). Design a PDA for it.

Example (III) solution: CFG


L = { aibjckdl: i,j,k,l=0,1,; i+k=j+l },
Note that i + k = j + l ==> | i j | = | l k |.
Assume n = i j = l k > 0, then i = n + j, l = n + k, and

w = aibjckdl = anaj bj ckdkdn


w

Three blocks come from


the same template:

NxNx

S aSd | XY
X aXb |
Y cYd |

S-->anSdn--> anXYdn

ajbj

ckdk

-->anajbjYdn
-->anajbj ckbkdn

aj

bj ck dk

= an+jbj ckbn+k
(n+j) + k = j + (n+k)

Example (III) solution: PDA


L = { aibjckdl: i,j,k,l=0,1,; i+k=j+l },
Main idea:
(1) use X to record an a or c; use Y to record an b or d.
(2) Compare #X and #Y: by cancellation.
How to realize the comparison by cancellation?
Action1: Push an X, when a or c was read;
Action2: Pop an X (if any, otherwise push a Y), when b or d was read.
Action3: Pop an Y (if any, otherwise push an X), when a or c was read.

Example (III) solution: PDA


L = { aibjckdl: i,j,k,l=0,1,; i+k=j+l },
Action1: Push an X, when a or c was read;
Action2: Pop an X (if any, otherwise push a Y), when b or d was read.
Action3: Pop an Y (if any, otherwise push an X), when a or c was read.
b,X/

a,/X
,/$ q
1

c,$/X$
c,X/XX

,/

q2
b,$/Y$
b,Y/YY

,/

q3

c,Y/

,/

d,X/
q4
d,$/Y$
d,Y/YY

, $ / q
5

Outline
Context-free Languages, Context-free
grammars (CFG), Push-down Automata
(PDA)
Pumping Lemma

Pumping lemma for context-free


languages

Theorem: For every context-free language L

There exists a number n such that for


every string z in L, we can write z =
uvwxy where
|vwx| n
|vx| 1
For every i 0, the string uviwxiy
is in L.
u

Example(4)
L1 = {0n1n0n1n | n 0}
1
2

choose
n
write z = uvwxy

z=
n n n n
0
i =1 0 1
2

0 0 ... 0 0 1 1 1 1 1 0 0 0 0 11 11
u

We try to argue that z

= uv2wx2y is NOT in

L1 1. Neither v nor x can contain both 0 and 1

Example(5)
L2 = {0n#02n#3n | n 0 }
1
2

choose
n
write z = uvwxy

z=
n
2n
3n
0
i =#0 #0
2

0 0 ... 0 0 # 0 0 0 0 0 # 0 0 0 0 0 0 0 0
u

We try to argue that z

= uv2wx2y is NOT in

L2
1. Neither v nor x can contain #
2. 1:2:3 ratio cant be maintained since at least
one segment NOT in v and x

End of this tutorial!


Thanks for coming!

You might also like