pushdown automata (pda) - swarthmore collegesindhu/cs46/s16/pdadesign.pdf · pushdown automata...

33
Pushdown Automata (PDA) state control 00011111000 a a b a stack input

Upload: dangnhu

Post on 07-Jul-2019

258 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

Pushdown Automata (PDA)

state control

00011111000

a

a

b

a

stack

input

Page 2: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

Pushdown Automata (PDA)If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push y, advance read head

q2a, x → y

q1

If a = ℇ do not advance read headIf x = ℇ do not read from stackIf y = ℇ do not write to stack

Page 3: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

When does a PDA accept a string?

r1w1q0 r2

w2 qfwn

input: w1w2…wn

…w3

accept if any branch accepts

Page 4: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

Pushdown Automata (PDA)

(Q, Σ, Γ, δ, q0, F) δ: Q × Σℇ × Γℇ → !(Q × Γℇ)

q2a, x → y

q1

q3

a, x → z

Page 5: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

TheoremsNot every nondeterministic PDA has an equivalent deterministic PDA

A language is context-free iff some PDA recognizes it

Page 6: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

CFL vs.

Regular Languages

Page 7: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

CFL vs. Regular Languages

PDA to NFA

Page 8: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

CFL vs. Regular Languages

NFA to PDA

q2q1a

Page 9: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

CFL vs. Regular Languages

NFA to PDA

q2a, ℇ → ℇ

q1

Page 10: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

PDA Design Examples

Page 11: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

PDA Design {0n1n | n ≥ 0}

Page 12: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

stack

input: 000111

{0n1n | n ≥ 0}

Page 13: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q20, ℇ → 0

stack

input: 000111

{0n1n | n ≥ 0}

Page 14: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q20, ℇ → 0

stack

input: 000111

0

{0n1n | n ≥ 0}

Page 15: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q20, ℇ → 0

stack

0

0

input: 000111

{0n1n | n ≥ 0}

Page 16: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q20, ℇ → 0

stack

0

0

0

input: 000111

{0n1n | n ≥ 0}

Page 17: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

1, 0 → ℇ

1, 0 → ℇ

0, ℇ → 0

stack

0

0

0

input: 000111

{0n1n | n ≥ 0}

Page 18: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

1, 0 → ℇ

1, 0 → ℇ

0, ℇ → 0

stack

0

0

input: 000111

{0n1n | n ≥ 0}

Page 19: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

1, 0 → ℇ

1, 0 → ℇ

0, ℇ → 0

stack

0

input: 000111

{0n1n | n ≥ 0}

Page 20: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

1, 0 → ℇ

1, 0 → ℇ

0, ℇ → 0

stack

input: 000111

{0n1n | n ≥ 0}

Page 21: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

1, 0 → ℇ

1, 0 → ℇ

0, ℇ → 0

stack

input: 000111_

{0n1n | n ≥ 0}

Page 22: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

1, 0 → ℇ

1, 0 → ℇ

0, ℇ → 0

Does this work?

{0n1n | n ≥ 0}

Page 23: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3q4

ℇ, ℇ → $

1, 0 → ℇ

ℇ, $ → ℇ 1, 0 → ℇ

q10, ℇ → 0

{0n1n | n ≥ 0}

Page 24: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

PDA Design {wwR | w ∈ {0, 1}*}

Page 25: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3q4

ℇ, ℇ → $

ℇ, ℇ → ℇ

ℇ, $ → ℇ

q1

0, ℇ → 0 1, ℇ → 1

0, 0 → ℇ 1, 1 → ℇ

{wwR | w ∈ {0, 1}*}

Page 26: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

PDA Design {aibjck | i, j, k ≥ 0 and i = j or j = k}

Page 27: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

ℇ, ℇ → ℇ

a, ℇ → a

b, a → ℇ

{aibjck | i, j, k ≥ 0 and i = j}

q4

ℇ, ℇ → $

ℇ, $ → ℇ

q1

c, ℇ → ℇ

Page 28: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

r2

r3

ℇ, ℇ → ℇ

a, ℇ → ℇ

b, ℇ → b

{aibjck | i, j, k ≥ 0 and j = k}

r5

ℇ, ℇ → $

ℇ, $ → ℇ

r1

r4

ℇ, ℇ → ℇ

c, b → ℇ

Page 29: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

r2

r3

ℇ, ℇ → ℇ

a, ℇ → ℇ

b, ℇ → b

{aibjck | i, j, k ≥ 0 and i=j or j = k}

r5

ℇ, ℇ → $

ℇ, $ → ℇ

q1

r4

ℇ, ℇ → ℇ

c, b → ℇ

q2

q3

ℇ, ℇ → ℇ

a, ℇ → a

b, a → ℇ

q4

ℇ, $ → ℇ

c, ℇ → ℇ

ℇ, ℇ → $r1

Page 30: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

q2

q3

ℇ, ℇ → ℇ

a, ℇ → ℇ

b, ℇ → b

{aibjck | i, j, k ≥ 0 and i=j or j = k}

q4

ℇ, ℇ → $

ℇ, $ → ℇ

q1

q3

ℇ, ℇ → ℇ

c, b → ℇ

q2

q3

ℇ, ℇ → ℇ

a, ℇ → a

b, a → ℇ

q4

ℇ, $ → ℇ

c, ℇ → ℇ

ℇ, ℇ → $

Page 31: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

Pumping Lemma: Regular Languages

If A is a regular language,then there is a pumping length p stif s ∈ A with |s| ≥ p then we can write s = xyz so that

• ∀i ≥ 0 xyiz ∈ A• |y| > 0 • |xy| ≤ p

Page 32: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

To prove {0n1n | n ≥ 0} is not regular using the Pumping Lemma

1. Suppose {0n1n | n ≥ 0} is regular2. Call its pumping length p3. Find string s ∈ A with |s| ≥ p. Let s = 0p1p

4. The pumping lemma says that for some split 0p1p = xyz all the following conditions hold

• ∀i ≥ 0 xyiz ∈ A • |y| > 0 • |xy| ≤ p

⇒ y is a non-empty string of 0s

Page 33: Pushdown Automata (PDA) - Swarthmore Collegesindhu/cs46/s16/PDADesign.pdf · Pushdown Automata (PDA) If the input symbol is a and the top stack symbol is x then q1 to q2, pop x, push

To prove A is not regular using the Pumping Lemma

1. Suppose A is regular 2. Call its pumping length p 3. Find string s ∈ A with |s| ≥ p 4. The pumping lemma says that for some split

s = xyz all the following conditions hold • ∀i ≥ 0 xyiz ∈ A • |y| > 0 • |xy| ≤ p