1. Free Ebooks Download
Mba Ebooks By Edhole
Mba ebooks
Free ebooks download
http://ebooks.edhole.com
2. Examples for Context-free Language and
Pumping Lemma
CSC3130 Tutorial 5
Xiao Linfu
lfxiao@cse.cuhk.edu.hk
Department of Computer Science &
Engineering
Fall 2009
http://ebooks.edhole.com
6. 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
http://ebooks.edhole.com
7. Example (I)
• Given the following CFG
S X | Y
X aXb | aX | a
Y aYb | Yb | b
• (1) L(G) = ?
• (2) Design an equivalent PDA for it.
Σ={a, b}
http://ebooks.edhole.com
8. Example (I) --- solution: L(S)
S X | Y
X aXb | aX | a
Y aYb | Yb | b
Try to write some strings generated by it:
SXaXbaaXbbaaaXbbaaaabb
SYaYbaYbbaaYbbbaabbbb
more a’s than b’s
more b’s than a’s
Observations:
• Start from S, we can enter two States X
& Y, and X, Y are “independent”;
• In X state, always more a are generated;
• In Y state, always more b are generated.
Ls = Lx U Ly
Lx = { ai
bj
; i>j }
Lx = { ai
bj
; i<j }
L(S) =
{ ai
bj
; i≠j }
http://ebooks.edhole.com
9. Example (I) --- solution: PDA
S X | Y
X aXb | aX | a
Y aYb | Yb | b
L(S) = { ai
bj
; i≠j }
PDA = NFA + a stack (infinite memory)
= { ai
bj
; i>j } U { ai
bj
; i<j }
A possible way: “divide and conquer”
Lx = { ai
bj
; i>j }
a,ε/A
q0
ε,ε/$ ε,ε/ε
q1
b,A/ε
b,$/$
q2
b,$/$
q3
ε,$/ε
LY = { ai
bj
; i<j }
q’0
ε,ε/$ ε,ε/ε
q’1
ε,A/ε
q’2
ε,A/ε
q’3
ε,$/ε
a,ε/A b,A/ε
ε, ε /ε
Combine both …
http://ebooks.edhole.com
10. Example (II)
• Given the following language:
• (1) design a CFG for it;
• (2) design a PDA for it.
L = {0i
1j
: i ≤ j ≤ 2i, i=0,1,…}, Σ = {0, 1}
http://ebooks.edhole.com
11. Example (II) -- solution: CFG
L = {0i
1j
: i ≤ j ≤ 2i, i=0,1,…}, Σ = {0, 1}
Consider two extreme cases:
(a). if j = i, then L1 = { 0i
1j
: i=j }; (b). if j = 2i, then L2 = { 0i
1j
:
2i=j }.
S 0S1
S ε
S 0S11
S ε
If i ≤ j ≤ 2i , then randomly choose “red-
rule” or “blue-rule” in the generation.
“red-rule” “blue-rule”
S 0S1
S 0S11
S ε http://ebooks.edhole.com
12. Example (II) -- solution: CFG
L = {0i
1j
: i ≤ j ≤ 2i, i=0,1,…}, Σ = {0, 1}
S 0S1 S 0S11 S ε
Need to verify L = L(G)
G =
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 = 0i
1j
, i ≤ j ≤ 2i, we use “red-rule” (2i - j) times and
then “blue-rule” ( j - i ) times, i.e.,
S =*=> 02i-j
S12i-j
=*=> 02i-j
0j-i
S12(j-i)
12i-j
==> 0i
1j
= whttp://ebooks.edhole.com
13. Example (II) -- solution: PDA
L = {0i
1j
: i ≤ j ≤ 2i, i=0,1,…}, Σ = {0, 1}
Similar idea: “randomly choose two extreme cases”
q0
ε,ε/$
0,ε/X
ε,ε/ε q1
q2
ε,$/ε
1,X/ε
1,X/X 1,X/ε
q3
http://ebooks.edhole.com
14. Example (III)
• Given the following language:
• (1). Design a CFG for it;
• (2). Design a PDA for it.
L = { ai
bj
ck
dl
: i, j, k, l=0,1,…; i+k=j+l },
where the alphabet Σ= {a, b, c, d}
http://ebooks.edhole.com
15. Example (III) – solution: CFG
L = { ai
bj
ck
dl
: 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 = ai
bj
ck
dl
= an
aj
bj
ck
dk
dn
w
an
dn
aj
bj
ck
dk
aj
bj
ck
dk
Three blocks come from
the same template:
N x N x
S aSd | XY
X aXb | ε
Y cYd | ε
S-->an
Sdn
--> an
XYdn
-->an
aj
bj
Ydn
-->an
aj
bj
ck
bk
dn
= an+j
bj
ck
bn+k
(n+j) + k = j + (n+k)
http://ebooks.edhole.com
16. Example (III) – solution: PDA
L = { ai
bj
ck
dl
: 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.
http://ebooks.edhole.com
17. Example (III) – solution: PDA
L = { ai
bj
ck
dl
: 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.
ε,ε/$
q5
q1
a,ε/X
ε,ε/ε
b,$/Y$
q2
ε,ε/ε
c,X/XX
q3
ε,ε/ε q4
ε, $ /ε
b,X/ε
b,Y/YY
c,$/X$
c,Y/ε
d,X/ε
d,$/Y$
d,Y/YY
http://ebooks.edhole.com
19. 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 uvi
wxi
y is in L.
wu yxv
http://ebooks.edhole.com
20. Example(4)
1
2
L1 = {0n
1n
0n
1n
| n 0≥ }
wu yxv
0 0 ... 0 0 1 1 …1 1 1 0 0 … 0 0 11 …11
choose n
write z = uvwxy
z = 0n
1n
0n
1n
i = 2
1. Neither v nor x can contain both 0 and 1
We try to argue that z = uv2
wx2
y is NOT in L1
http://ebooks.edhole.com
21. Example(5)
1
2
L2 = {0n
#02n
#3n
| n ≥ 0 }
wu yxv
0 0 ... 0 0 # 0 0 …0 0 0 # 0 0 0 0 … 0 0 0 0
choose n
write z = uvwxy
z = 0n
#02n
#03n
i = 2
1. Neither v nor x can contain #
2. 1:2:3 ratio can’t be maintained since at least
one segment NOT in v and x
We try to argue that z = uv2
wx2
y is NOT in L2
http://ebooks.edhole.com
22. End of this tutorial!
Thanks for coming!
http://ebooks.edhole.com
23. Free Ebooks Download
Mba Ebooks By Edhole
Mba ebooks
Free ebooks download
http://ebooks.edhole.com