Pumping Lemma for Context-Free Languages CSCI 3130 Formal Languages - - PowerPoint PPT Presentation

pumping lemma for context free languages
SMART_READER_LITE
LIVE PREVIEW

Pumping Lemma for Context-Free Languages CSCI 3130 Formal Languages - - PowerPoint PPT Presentation

Pumping Lemma for Context-Free Languages CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Fall 2019 Chinese University of Hong Kong 1/18 regular context-free These languages are not regular Are they context-free? 2/18 L 1 = { a n


slide-1
SLIDE 1

Pumping Lemma for Context-Free Languages

CSCI 3130 Formal Languages and Automata Theory

Siu On CHAN Fall 2019

Chinese University of Hong Kong 1/18

slide-2
SLIDE 2

regular context-free L1 = {anbn | n 0} L2 = {z | z has the same number of a’s and b’s} L3 = {anbncn | n 0} L4 = {zzR | z ∈ {a, b}∗} L5 = {zz | z ∈ {a, b}∗} These languages are not regular Are they context-free?

2/18

slide-3
SLIDE 3

An attempt

L3 = {anbncn | n 0} Let’s try to design a CFG or PDA S → aBc | ε B → ??? read a / push x read b / pop x ???

3/18

slide-4
SLIDE 4

Suppose we could construct some CFG G for L3 e.g. S → CC | BC | a B → CS | b C → SB | c How does a long derivation look like? S ⇒ CC ⇒ SBC ⇒ SCSC ⇒ SSBSC ⇒ SSBBCC ⇒ aSBBCC ⇒ aaBBCC ⇒ aabBCC ⇒ aabbCC ⇒ aabbcC ⇒ aabbcc

4/18

slide-5
SLIDE 5

Repetition in long derivations

If a derivation is long enough, some variable must appear twice on the same root-to-leave path in a parse tree S ⇒ CC ⇒ SBC ⇒ SCSC ⇒ SSBSC ⇒ SSBBCC ⇒ aSBBCC ⇒ aaBBCC ⇒ aabBCC ⇒ aabbCC ⇒ aabbcC ⇒ aabbcc S C S a B C S a B b S B b C c C c

5/18

slide-6
SLIDE 6

Pumping example

Then we can “cut and paste” part of parse tree S C S a B C S a B b S B b C c C c aabbcc S C S a B C S a B C S a B b S B b C c S B b C c C c aaabbcbcc

6/18

slide-7
SLIDE 7

Pumping example

We can repeat this many times aabbcc ⇒ aaabbcbcc ⇒ aaaabbcbcbcc ⇒ . . . ⇒ (a)iab(bc)ic Every suffjciently large derivation will have a middle part that can be repeated indefjnitely

7/18

slide-8
SLIDE 8

Pumping in general

u A v A w x y uvwxy u A v A v A w x x y uv2wx2y u A v A v A v A w x x x y uv3wx3y u A w y uwy

8/18

slide-9
SLIDE 9

Example

L3 = {anbncn | n 0} If L3 has a context-free grammar G, then for any suffjciently long s ∈ L(G) s can be split into s = uvwxy such that L(G) also contains uv2wx2y, uv3wx3y, … What happens if s = ambmcm No matter how it is split, uv2wx2y / ∈ L3

9/18

slide-10
SLIDE 10

Pumping lemma for context-free languages

For every context-free language L There exists a number m such that for every long string s in L (|s| m), we can write s = uvwxy where

  • 1. |vwx| m
  • 2. |vx| 1
  • 3. For every i 0, the string uviwxiy is in L

u v w x y m At least

  • ne is

non-empty

10/18

slide-11
SLIDE 11

Pumping lemma for context-free languages

To prove L is not context-free, it is enough to show that For every m there is a long string s ∈ L, |s| m, such that for every way of writing s = uvwxy where

  • 1. |vwx| m
  • 2. |vx| 1

there is i 0 such that uviwxiy is not in L

11/18

slide-12
SLIDE 12

Using the pumping lemma

L3 = {anbncn | n 0}

  • 1. for every m
  • 2. there is s = ambmcm (at least m symbols)
  • 3. no matter how the pumping lemma splits s into uvwxy

(|vwx| m, |vx| 1)

  • 4. uv2wx2y /

∈ L3 (but why?)

12/18

slide-13
SLIDE 13

Using the pumping lemma

Case 1: v or x contains two kinds of symbols aa aabb

v

bbcccc Then uv2wx2y / ∈ L3 because the pattern is wrong Case 2: v and x both contain (at most) one kind of symbol aaa a

  • v

b bb

  • x

bcccc Then uv2wx2y does not have the same number of a’s, b’s and c’s Conclusion: uv2wx2y / ∈ L3

13/18

slide-14
SLIDE 14

Which is context-free? L1 = {anbn | n 0} ✓ L2 = {z | z has the same number of a’s and b’s} ✓ L3 = {anbncn | n 0} ✗ L4 = {zzR | z ∈ {a, b}∗} ✓ L5 = {zz | z ∈ {a, b}∗}

14/18

slide-15
SLIDE 15

Example

L5 = {zz | z ∈ {a, b}∗}

  • 1. for every m
  • 2. there is s = ambamb (at least m symbols)
  • 3. no matter how the pumping lemma splits s into uvwxy

(|vwx| m, |vx| 1)

  • 4. Is uv2wx2y /

∈ L5? aaa a

v

aba a

x

aaab

15/18

slide-16
SLIDE 16

Example

L5 = {zz | z ∈ {a, b}∗}

  • 1. for every m
  • 2. there is s = ambamb (at least m symbols)
  • 3. no matter how the pumping lemma splits s into uvwxy

(|vwx| m, |vx| 1)

  • 4. Is uv2wx2y /

∈ L5? aaa a

  • v

aba a

  • x

aaab

15/18

slide-17
SLIDE 17

Example

L5 = {zz | z ∈ {a, b}∗}

  • 1. for every m
  • 2. there is s = ambmambm (at least m symbols)
  • 3. no matter how the pumping lemma splits s into uvwxy

(|vwx| m, |vx| 1)

  • 4. Is uviwxiy /

∈ L5 for some i? Recall that |vwx| m

16/18

slide-18
SLIDE 18

Example

Three cases Case 1 aaa aabbb

vwx

bbaaaaabbbbb vwx is in the fjrst half of ambmambm Case 2 aaaaabb bbbaa

vwx

aaabbbbb vwx is in the middle part of ambmambm Case 3 aaaaabbbbbaaa aabbb

vwx

bb vwx is in the second half of ambmambm

17/18

slide-19
SLIDE 19

Example

Apply pumping lemma with i = 0 Case 1 aaa aabbb

vwx

bbaaaaabbbbb uwy becomes ajbkambm, where j < m or k < m Case 2 aaaaabb bbbaa

vwx

aaabbbbb uwy becomes ambjakbm, where j < m or k < m Case 3 aaaaabbbbbaaa aabbb

vwx

bb uwy becomes ambmajbk, where j < m or k < m Not of the form zz This covers all cases, so L5 is not context-free

18/18