View
237
Download
2
Embed Size (px)
Citation preview
Examples for Context-free Language and Pumping Lemma
CSC3130 Tutorial 5
Xiao Linfu
Department of Computer Science & Engineering
Fall 2009
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
Context-free Grammars G
Push-down Automata M
L = L(G)
L = L(M) L(G) = L(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 S X | Y X aXb | aX | a Y aYb | Yb | b• (1) L(G) = ?• (2) Design an equivalent PDA for it.
Σ={a, b}
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 = { aibj; i>j }
Lx = { aibj; i<j }
L(S) =
{ aibj; i≠j }
Example (I) --- solution: PDA
S X | Y
X aXb | aX | a
Y aYb | Yb | b
L(S) = { aibj; i≠j }
PDA = NFA + a stack (infinite memory)
= { aibj; i>j } U { aibj; i<j }
A possible way: “divide and conquer”
Lx = { aibj; i>j }
a,/A
q0,/$ ,/
q1
b,A/
b,$/$q2
b,$/$
q3
,$/
LY = { aibj; i<j }
q’0,/$ ,/
q’1,A/
q’2
,A/
q’3
,$/
a,/A b,A/
, /
Combine both …
Example (II)
• Given the following language:
• (1) design a CFG for it;
• (2) design a PDA for it.
L = {0i1j: i ≤ j ≤ 2i, i=0,1,…}, = {0, 1}
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
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 ε
Example (II) -- solution: CFG
L = {0i1j: 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 = 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”
q0
,/$
0,/X,/ q1
q2
,$/1,X/
1,X/X 1,X/q3
Example (III)
• Given the following language:
• (1). Design a CFG for it;
• (2). Design a PDA for it.
L = { aibjckdl: i, j, k, l=0,1,…; i+k=j+l },
where the alphabet Σ= {a, b, c, d}
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
an dn
ajbj ckdk
aj bj ck dk
Three blocks come from the same template:
N x N x
S aSd | XY
X aXb | ε
Y cYd | ε
S-->anSdn--> anXYdn
-->anajbjYdn
-->anajbj ckbkdn
= 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.
,/$ q5q1
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
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.
wu yxv
Example(4)
1
2
L1 = {0n1n0n1n | n ≥ 0}
wu yxv0 0 ... 0 0 1 1 …1 1 1 0 0 … 0 0 11 …11
choose nwrite z = uvwxy
z = 0n1n0n1ni = 2
1. Neither v nor x can contain both 0 and 1
We try to argue that z = uv2wx2y is NOT in L1
Example(5)
1
2
L2 = {0n#02n#3n | n ≥ 0 }
wu yxv0 0 ... 0 0 # 0 0 …0 0 0 # 0 0 0 0 … 0 0 0 0
choose nwrite z = uvwxy
z = 0n#02n#03ni = 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 = uv2wx2y is NOT in L2
End of this tutorial!Thanks for coming!