Intro to Theory of Computation

21
Sofya Raskhodnikova Intro to Theory of Computation LECTURE 8 Theory of Computation Converting a PDA to a CFG Pumping Lemma for CFLs 10/31/2022 Sofya Raskhodnikova; based on slides by Nick Hopper L8.1

description

L ECTURE 8 Theory of Computation Converting a PDA to a CFG Pumping Lemma for CFLs. Intro to Theory of Computation. CS 464. Sofya Raskhodnikova. A language is generated by a CFG It is recognized by a PDA. . Converting a PDA to a CFG. Given PDA P = (Q, Σ , Γ , , q, F). - PowerPoint PPT Presentation

Transcript of Intro to Theory of Computation

Page 1: Intro to Theory of Computation

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova

Intro to Theory of Computation

LECTURE 8 Theory of Computation• Converting a PDA to a CFG• Pumping Lemma for CFLs

L8.1

Page 2: Intro to Theory of Computation

A language is generated by a CFG

It is recognized by a PDA

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.2

Page 3: Intro to Theory of Computation

Given PDA P = (Q, Σ, Γ, , q, F)

Construct a CFG G = (V, Σ, R, S) with L(G)=L(P)

First, simplify P so that:

(1) It has a single accept state, qaccept

(2) It empties the stack before accepting

(3) Each transition either pushes a symbol or pops a symbol, but not both

Converting a PDA to a CFG

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.3

Page 4: Intro to Theory of Computation

For each pair of states p and q in P, add a variable Apq to CFGthat generates all strings that that can take P from p to q without changing the stack*

V = {Apq | p,qQ }

S = Aq0qaccept

*Starting from any stack S in p, including empty stack,P has stack S at q.

From PDA to CFG: main idea

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.5

Page 5: Intro to Theory of Computation

ε,ε → $0,ε → 0

1,0 → ε

1,0 → εε,$ → ε

q0 q1

q2q3

ε,0 → ε

ε,ε → 0

ε,ε → 0

q4

q5

What strings does Aq0q1 generate?

What strings does Aq1q2 generate?

What strings does Aq1q3 generate?

none

{0n1n | n > 0}

none

Example

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.6

Page 6: Intro to Theory of Computation

Apq generates all strings that take P from p to q without changing the stack

Let x be such a string• P’s first move on x must be a push

• P’s last move on x must be a pop

Consider the stack while reading x. Either:1. New portion of the stack first empties only at the end of x2. New portion empties before the end of x

From PDA to CFG: main idea

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.7

Page 7: Intro to Theory of Computation

stackheight

inputstring p q

Apq → aArsb

r sba

1. New portion of the stack first empties only at the end of x

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.8

Page 8: Intro to Theory of Computation

stackheight

inputstring p r q

Apq → AprArq

2. New portion empties before the end of x

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.9

Page 9: Intro to Theory of Computation

V = {Apq | p,qQ }

S = Aq0qaccept

For each p,q,r,s Q, t Γ and a,b Σε

If (r,t) (p,a,ε) and (q, ε) (s,b,t)

Then add the rule Apq → aArsb

For each p,q,r Q,add the rule Apq → AprArq

For each p Q,add the rule App → ε

CFG construction

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.10

Page 10: Intro to Theory of Computation

ε,ε → $0,ε → 0

1,0 → ε

1,0 → εε,$ → ε

q0 q1

q2q3

ε,0 → ε

ε,ε → 0

ε,ε → 0

q4

q5

What strings does Aq0q1 generate?

What strings does Aq1q2 generate?

What strings does Aq1q3 generate?

Aq0q3 → εAq1q2ε

Aqq → ε

Apq → AprArq

Aq1q2 → 0Aq1q21

Aq1q2 → 0Aq1q11

none

{0n1n | n > 0}

none

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper

Page 11: Intro to Theory of Computation

A language is generated by a CFG

It is recognized by a PDA

Equivalence of CFGs & PDAs

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper L8.12

Page 12: Intro to Theory of Computation

04/19/2023 Sofya Raskhodnikova; based on slides by Nick Hopper

Read on your own

1. Ambiguous CFGs.2. Chomsky normal form for CFGs3. (Skipping Chapter 2.4 in Sipser).

L8.13

Page 13: Intro to Theory of Computation

L₁ = { xy | x,y {0,1}* and x=y}∈

L₂ = {xy | x,y {0,1}*, |x|=|y| and x ≠ y}∈

NOT

YES

Context-free or not?

L8.14

Page 14: Intro to Theory of Computation

Let L be a context-free language

Then there exists P such that For every w L with |w| ≥ P

1. |vy| > 0

there exist uvxyz=w, where:

3. uvixyiz L for all i ≥ 0

2. |vxy| ≤ P

Pumping lemma for CFLs

L8.15

Page 15: Intro to Theory of Computation

1. |vy| > 0

2. |vxy| ≤ P

3. uvixyiz L for all i ≥ 0

there exist uvxyz=w, where:

Example: L = { w 2 {0,1}* | w = wR}. w = 0; u,v,x,y,z = ?w = 010; u,v,x,y,z=?

Example: L = { w 2 {a,b}* | #a > #b in w}. w = a; u,v,x,y,z = ?w = aab; u,v,x,y,z=?

Examples

L8.16

Page 16: Intro to Theory of Computation

If string w is long enough, then every parse tree for w must have a path that contains a variable more than once.

T

R

R

u v x zy

T

u z

R

R

v y

R

R

v x y

Pumping lemma: proof idea

L8.17

Page 17: Intro to Theory of Computation

• If the height of a parse tree is h, the length of the string generated is at most:

• Let b be the maximum number of symbols on the right-hand side of a rule.

bh

• Let |V| be the number of variables in G.

• Define p = b|V|+2 .• Let w be a string of length at least p.• Let T be the parse tree for w with the

smallest number of nodes. • T must have height at least |V|+2.

Pumping lemma: proof

L8.18

Page 18: Intro to Theory of Computation

The longest path in T must have ≥ |V|+1 variablesSelect R to be the variable that repeats among the lowest |V|+1 variables

1. |vy| > 0

2. |vxy| ≤ P

T

R

R

u v x zy

T

u z

R

R

v y

R

R

v x y

Pumping lemma: proof

L8.19

Page 19: Intro to Theory of Computation

Let L be a context-free language.

Then there exists P such that For every w L with |w| ≥ P

1. |vy| > 0

there exist uvxyz=w, where:

3. For every i ≥ 0, uvixyiz L

2. |vxy| ≤ P

for every P,There is a

For every

There is an

If

L is not context-free

Negating the pumping lemma

L8.20

Page 20: Intro to Theory of Computation

Prove L = {ww | w 2 {0,1}*} is not context-freeAssume L is context-free.

Then there is a pumping length P.No matter what P is, the string s = 0P1P0P1P has

|s| ¸ P and s 2 L.

So there should be uvxyz=s with:1) |vy| > 0, 2) |vxy| · P, 3) 8 i, uvixyiz 2 L.

s = 00…0011…1100…0011…11

P P P P

Using the pumping lemma

L8.21

Page 21: Intro to Theory of Computation

No matter what P is, the string s = 0P1P0P1P has|s| ¸ P and s 2 L.

So there should be uvxyz=s with:1) |vy| > 0, 2) |vxy| · P, 3) 8 i, uvixyiz 2 L.

s = 00…0011…1100…0011…11

P P P P

vxy cannot be only in the first half, since pumping down would move a 0 to the end of the first half.

vxy cannot be only in the last half, since pumping up would move a 0 to the end of the first half.

vxy

Then pumping down must remove at least one 1or one zero, e.g. uxz = 0P1i0j1P where

i p or j p. So uxz L, no matter howthey are chosen; so L cannot be context-free!

{ww | w 2 {0,1}*} is not a CFL: proof

L8.22