examples for context-free language and pumping lemma csc3130 tutorial 5 xiao linfu...

21
Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu [email protected] Department of Computer Science & Engineering Fall 2009

Post on 19-Dec-2015

236 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

Examples for Context-free Language and Pumping Lemma

CSC3130 Tutorial 5

Xiao Linfu

[email protected]

Department of Computer Science & Engineering

Fall 2009

Page 2: 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

Page 3: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

Relations

Context free language

regular language

Page 4: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

Relations

Context-free Languages L

Context-free Grammars G

Push-down Automata M

L = L(G)

L = L(M) L(G) = L(M)

Page 5: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 6: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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}

Page 7: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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 }

Page 8: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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 …

Page 9: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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}

Page 10: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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 ε

Page 11: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 12: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 13: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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}

Page 14: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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)

Page 15: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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.

Page 16: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 17: 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

Page 18: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 19: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 20: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

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

Page 21: Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu lfxiao@cse.cuhk.edu.hk Department of Computer Science & Engineering

End of this tutorial!Thanks for coming!