Fundamentele Informatica 1 (I&E) najaar 2014 - - PowerPoint PPT Presentation

fundamentele informatica 1 i e
SMART_READER_LITE
LIVE PREVIEW

Fundamentele Informatica 1 (I&E) najaar 2014 - - PowerPoint PPT Presentation

Fundamentele Informatica 1 (I&E) najaar 2014 http://www.liacs.leidenuniv.nl/~vlietrvan1/fi1ie/ Rudy van Vliet rvvliet(at)liacs(dot)nl college 11, maandag 1 december 2014 6. Context-Free and Non-Context-Free Languages 6.1. The Pumping


slide-1
SLIDE 1

Fundamentele Informatica 1 (I&E)

najaar 2014 http://www.liacs.leidenuniv.nl/~vlietrvan1/fi1ie/ Rudy van Vliet rvvliet(at)liacs(dot)nl college 11, maandag 1 december 2014

  • 6. Context-Free and Non-Context-Free Languages

6.1. The Pumping Lemma for Context-Free Languages

1

slide-2
SLIDE 2
  • 6. Context-Free

and Non-Context-Free Languages 6.1. The Pumping Lemma for Context-Free Languages

  • reg. languages

FA

  • reg. grammar
  • reg. expression
  • determ. cf. languages

DPDA

  • cf. languages

PDA

  • cf. grammar
  • re. languages

TM

  • unrestr. grammar

2

slide-3
SLIDE 3

A slide from lecture 3:

2.4 The Pumping Lemma

Theorem 2.29. The Pumping Lemma for Regular Languages. Suppose L is a language over the alphabet Σ. If L is accepted by a finite automaton M = (Q, Σ, q0, A, δ), and if n is the number of states of M, then for every x ∈ L satisfying |x| ≥ n, there are three strings u, v, and w such that x = uvw and the following three conditions are true:

  • 1. |uv| ≤ n.
  • 2. |v| > 0 (i.e., v = Λ).
  • 3. For every i ≥ 0, the string uviw also belongs to L.

3

slide-4
SLIDE 4

A slide from lecture 3: Example 2.30. The language AnBn. Let L = {aibi | i ≥ 0}.

4

slide-5
SLIDE 5

Now, context-free languages. Intuitively clear that PDA cannot accept AnBnCn or XX. . .

5

slide-6
SLIDE 6

Now, context-free languages. Intuitively clear that PDA cannot accept AnBnCn or XX. . . Pumping lemma based on derivation in CFG (not on PDA): S ⇒∗ vAz ⇒∗ v wAy z ⇒∗ vw x yz S ⇒∗ vAz ⇒∗ v wAy z ⇒∗ vw wAy yz ⇒∗ vwmxymz

6

slide-7
SLIDE 7

Theorem 6.1. The Pumping Lemma for Context-Free Languages. Suppose L is a context-free language. Then there is an integer n so that for every u ∈ L with |u| ≥ n, u can be written as u = vwxyz, for some strings v, w, x, y and z satisfying

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L
  • Proof. . .

7

slide-8
SLIDE 8

A slide from lecture 7: Definition 4.29. Chomsky Normal Form A context-free grammar is said to be in Chomsky normal form if every production is of one of these two types: A → BC (where B and C are variables) A → σ (were σ is a terminal symbol)

8

slide-9
SLIDE 9

A slide from lecture 8: Theorem 4.30. (not Theorem 4.31!) For every context-free grammar G, there is another CFG G1 in Chomsky normal form such that L(G1) = L(G) − {Λ}. What if Λ / ∈ L(G) ?

9

slide-10
SLIDE 10

Number of leaf nodes in a binary tree of a given height

10

slide-11
SLIDE 11

Theorem 6.1. The Pumping Lemma for Context-Free Languages. Proof Let G be CFG in Chomsky normal form with L(G) = L − {Λ}. Derivation tree in G is binary tree (where each parent of a leaf node has only one child). Height of a tree is number of edges in longest path from root to leaf node. At most 2h leaf nodes in binary tree of height h: |u| ≤ 2h.

11

slide-12
SLIDE 12

At most 2h leaf nodes in binary tree of height h: |u| ≤ 2h. Let p be number of variables in G, let n = 2p and let u ∈ L(G) with |u| ≥ n. (Internal part of) derivation tree of u in G has height at least p. Hence, longest path in (internal part of) tree contains at least p + 1 (internal) nodes. Consider final portion of longest path in derivation tree. (leaf node + p + 1 internal nodes), with ≥ 2 occurrences of a variable A. Pump up derivation tree, and hence u.

12

slide-13
SLIDE 13

Pumping Lemma for CFLs

✖✕ ✗✔ ✁ ✁ ❅ ❅ ❅

S

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

v z

✖✕ ✗✔ ❆ ❆

  • A

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

w y

✖✕ ✗✔

A

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

x

13

slide-14
SLIDE 14

Pumping Lemma for CFLs

✖✕ ✗✔ ✁ ✁ ❅ ❅ ❅

S

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

v z

✖✕ ✗✔

A

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

x

✖✕ ✗✔ ✁ ✁ ❅ ❅ ❅

S

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

v z

✖✕ ✗✔ ❆ ❆

  • A

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

w y

✖✕ ✗✔ ❆ ❆

  • A

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

w y

✖✕ ✗✔

A

✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆

x

14

slide-15
SLIDE 15

Application of pumping lemma: mainly to prove that a language L cannot be generated by a context-free grammar. How? Find a string u ∈ L with |u| ≥ n that cannot be pumped up! What is n? What should u be? What can v, w, x, y and z be?

15

slide-16
SLIDE 16

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. Pumping lemma: For every u ∈ L with |u| ≥ n, there are five strings v, w, x, y and z such that u = vwxyz and the following three conditions are true:

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L

16

slide-17
SLIDE 17

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. We prove: NOT ( For every u ∈ L with |u| ≥ n, there are five strings v, w, x, y and z such that u = vwxyz and the following three conditions are true:

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L

)

17

slide-18
SLIDE 18

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. We prove: There exists u ∈ L with |u| ≥ n, such that NOT ( there are five strings v, w, x, y and z such that u = vwxyz and the following three conditions are true:

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L

)

18

slide-19
SLIDE 19

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. We prove: There exists u ∈ L with |u| ≥ n, such that for every five strings v, w, x, y and z such that u = vwxyz NOT ( the following three conditions are true:

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L

)

19

slide-20
SLIDE 20

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. We prove: There exists u ∈ L with |u| ≥ n, such that for every five strings v, w, x, y and z such that u = vwxyz NOT all of the following three conditions are true:

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L

)

20

slide-21
SLIDE 21

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. We prove: There exists u ∈ L with |u| ≥ n, such that for every five strings v, w, x, y and z such that u = vwxyz if

  • 1. |wy| > 0
  • 2. |wxy| ≤ n

then NOT (

  • 3. for every m ≥ 0, vwmxymz ∈ L

)

21

slide-22
SLIDE 22

Suppose that there exists context-free grammar G with L(G) = L. Let n be the integer from the pumping lemma. We prove: There exists u ∈ L with |u| ≥ n, such that for every five strings v, w, x, y and z such that u = vwxyz if

  • 1. |wy| > 0
  • 2. |wxy| ≤ n

then

  • 3. there exists m ≥ 0, such that vwmxymz does not belong to L

22

slide-23
SLIDE 23

Example 6.3. Applying the Pumping Lemma to AnBnCn AnBnCn = {aibici | i ≥ 0} Choose u = . . .

23

slide-24
SLIDE 24

Example 6.3. Applying the Pumping Lemma to AnBnCn AnBnCn = {aibici | i ≥ 0} Choose u = anbncn

24

slide-25
SLIDE 25

A slide from lecture 3:

2.4 The Pumping Lemma

Theorem 2.29. The Pumping Lemma for Regular Languages. Suppose L is a language over the alphabet Σ. If L is accepted by a finite automaton M = (Q, Σ, q0, A, δ), and if n is the number of states of M, then for every x ∈ L satisfying |x| ≥ n, there are three strings u, v, and w such that x = uvw and the following three conditions are true:

  • 1. |uv| ≤ n.
  • 2. |v| > 0 (i.e., v = Λ).
  • 3. For every i ≥ 0, the string uviw also belongs to L.

25

slide-26
SLIDE 26

Theorem 6.1. The Pumping Lemma for Context-Free Languages. Suppose L is a context-free language. Then there is an integer n so that for every u ∈ L with |u| ≥ n, u can be written as u = vwxyz, for some strings v, w, x, y and z satisfying

  • 1. |wy| > 0
  • 2. |wxy| ≤ n
  • 3. for every m ≥ 0, vwmxymz ∈ L
  • Proof. . .

26

slide-27
SLIDE 27

Example 6.5. Applying the Pumping Lemma to. . . {x ∈ {a, b, c}∗ | na(x) < nb(x) and na(x) < nc(x)} Choose u = . . .

27

slide-28
SLIDE 28

Example 6.5. Applying the Pumping Lemma to. . . {x ∈ {a, b, c}∗ | na(x) < nb(x) and na(x) < nc(x)} Choose u = anbn+1cn+1

28

slide-29
SLIDE 29

Example 6.6. The Set of Legal C Programs is Not a CFL Choose u = . . .

29

slide-30
SLIDE 30

Example 6.6. The Set of Legal C Programs is Not a CFL Choose u = main(){int aaa...a;aaa...a;aaa...a;} where aaa...a contains n + 1 a’s

30

slide-31
SLIDE 31

Example 6.4. Appplying the Pumping Lemma to XX XX = {xx | x ∈ {a, b}∗} Choose u = . . .

31

slide-32
SLIDE 32

Example 6.4. Appplying the Pumping Lemma to XX XX = {xx | x ∈ {a, b}∗} Choose u = anbnanbn

32