CS 301 Lecture 12 Pushdown automata Stephen Checkoway February - - PowerPoint PPT Presentation

cs 301
SMART_READER_LITE
LIVE PREVIEW

CS 301 Lecture 12 Pushdown automata Stephen Checkoway February - - PowerPoint PPT Presentation

CS 301 Lecture 12 Pushdown automata Stephen Checkoway February 28, 2018 1 / 14 A new type of machine DFAs and NFAs are finite and that turns out to be too limiting Even simple languages like { a n b n n 0 } are too complicated What


slide-1
SLIDE 1

CS 301

Lecture 12 – Pushdown automata Stephen Checkoway February 28, 2018

1 / 14

slide-2
SLIDE 2

A new type of machine

DFAs and NFAs are finite and that turns out to be too limiting Even simple languages like {anbn ∣ n ≥ 0} are too complicated What we want is some way to remember things about the input that we’ve seen so far which can be arbitrarily long

2 / 14

slide-3
SLIDE 3

A new type of machine

DFAs and NFAs are finite and that turns out to be too limiting Even simple languages like {anbn ∣ n ≥ 0} are too complicated What we want is some way to remember things about the input that we’ve seen so far which can be arbitrarily long So let’s add a stack to an NFA!

2 / 14

slide-4
SLIDE 4

Pushdown automaton (PDA)

Like an NFA, it has

  • A finite set of states Q
  • An input alphabet Σ
  • A transition function δ
  • A start state q0
  • A set of accepting states F

New to the PDA is a stack and a corresponding stack alphabet Γ The transition function is modified to handle the stack

3 / 14

slide-5
SLIDE 5

PDA transition function

A PDA can examine both the next symbol in the input and the top of the stack to decide what actions to take with respect to the stack There are four stack actions. The PDA can

1 ignore the stack entirely;

4 / 14

slide-6
SLIDE 6

PDA transition function

A PDA can examine both the next symbol in the input and the top of the stack to decide what actions to take with respect to the stack There are four stack actions. The PDA can

1 ignore the stack entirely; 2 push a symbol onto the stack without examining the top of the stack;

4 / 14

slide-7
SLIDE 7

PDA transition function

A PDA can examine both the next symbol in the input and the top of the stack to decide what actions to take with respect to the stack There are four stack actions. The PDA can

1 ignore the stack entirely; 2 push a symbol onto the stack without examining the top of the stack; 3 pop a symbol off of the stack; or

4 / 14

slide-8
SLIDE 8

PDA transition function

A PDA can examine both the next symbol in the input and the top of the stack to decide what actions to take with respect to the stack There are four stack actions. The PDA can

1 ignore the stack entirely; 2 push a symbol onto the stack without examining the top of the stack; 3 pop a symbol off of the stack; or 4 replace the symbol on the top of the stack with a new (or the same) symbol

4 / 14

slide-9
SLIDE 9

PDA transition function

A PDA can examine both the next symbol in the input and the top of the stack to decide what actions to take with respect to the stack There are four stack actions. The PDA can

1 ignore the stack entirely; 2 push a symbol onto the stack without examining the top of the stack; 3 pop a symbol off of the stack; or 4 replace the symbol on the top of the stack with a new (or the same) symbol

In cases 1 and 2, the PDA makes its decision without looking at the symbol on the top

  • f the stack

In cases 3 and 4, the PDA explicitly examines the symbol at the top of the stack and either removes it or replaces it

4 / 14

slide-10
SLIDE 10

PDAs are nondeterministic

At each step, the PDA has multiple options:

  • It can move to one of several possible states
  • It can perform one of the four stack actions
  • It can transition without examining the next input symbol

5 / 14

slide-11
SLIDE 11

Transitions

There are four possible transitions from state q to state r on input a

1

q r a, ε → ε ignore the stack

2

q r a, ε → c push c onto the stack

3

q r a, b → ε pop b from the top of the stack

4

q r a, b → c replace b on the top of the stack with c

6 / 14

slide-12
SLIDE 12

Transitions

There are four possible transitions from state q to state r on input a

1

q r a, ε → ε ignore the stack

2

q r a, ε → c push c onto the stack

3

q r a, b → ε pop b from the top of the stack

4

q r a, b → c replace b on the top of the stack with c There are four possible transitions from state q to state r on no input (ε-transition)

1

q r ε, ε → ε ignore the stack

2

q r ε, ε → c push c onto the stack

3

q r ε, b → ε pop b from the top of the stack

4

q r ε, b → c replace b on the top of the stack with c

6 / 14

slide-13
SLIDE 13

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0}

Informal description

1 While the next input symbol is a, push it onto the stack 2 Once all of the as have been read, transition to a new state 3 While the next input symbol is b and the top of the stack is a, pop the a 4 At the end of the input, if the stack is empty, accept

7 / 14

slide-14
SLIDE 14

How do we know if the stack is empty?

The stack alphabet Γ doesn’t need to be the same as the input alphabet Σ Let’s add a end-of-stack marker $ as the first step q0 q1 ε, ε → $

8 / 14

slide-15
SLIDE 15

How do we know if the stack is empty?

The stack alphabet Γ doesn’t need to be the same as the input alphabet Σ Let’s add a end-of-stack marker $ as the first step q0 q1 ε, ε → $ Before we accept, we can ensure the stack is empty by popping the $ q2 q3 ε, $ → ε

8 / 14

slide-16
SLIDE 16

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

9 / 14

slide-17
SLIDE 17

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0;

9 / 14

slide-18
SLIDE 18

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0; 2 pushes $ onto the stack and moves to q1;

9 / 14

slide-19
SLIDE 19

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0; 2 pushes $ onto the stack and moves to q1; 3 remains in q1 reading as and pushing them on the stack;

9 / 14

slide-20
SLIDE 20

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0; 2 pushes $ onto the stack and moves to q1; 3 remains in q1 reading as and pushing them on the stack; 4 moves to q2

9 / 14

slide-21
SLIDE 21

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0; 2 pushes $ onto the stack and moves to q1; 3 remains in q1 reading as and pushing them on the stack; 4 moves to q2 5 remains in q2 reading bs and popping as off the stack;

9 / 14

slide-22
SLIDE 22

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0; 2 pushes $ onto the stack and moves to q1; 3 remains in q1 reading as and pushing them on the stack; 4 moves to q2 5 remains in q2 reading bs and popping as off the stack; 6 once $ is on the top of the stack, it moves to q3; and

9 / 14

slide-23
SLIDE 23

Example

Build a PDA to recognize A = {anbn ∣ n ≥ 0} The input alphabet is Σ = {a, b}; let’s use a stack alphabet Γ = {a, $} q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε When run on some input, the PDA

1 starts in q0; 2 pushes $ onto the stack and moves to q1; 3 remains in q1 reading as and pushing them on the stack; 4 moves to q2 5 remains in q2 reading bs and popping as off the stack; 6 once $ is on the top of the stack, it moves to q3; and 7 if there’s no more input, it accepts

9 / 14

slide-24
SLIDE 24

Formal definition

A PDA is a 6-tuple M = (Q, Σ, Γ, δ, q0, F) with Q – finite set of states Σ – input alphabet Γ – stack alphabet δ – transition function q0 – start state F – set of accepting states

10 / 14

slide-25
SLIDE 25

Formal definition

A PDA is a 6-tuple M = (Q, Σ, Γ, δ, q0, F) with Q – finite set of states Σ – input alphabet Γ – stack alphabet δ – transition function q0 – start state F – set of accepting states The transition function is complicated δ ∶ Q × Σε × Γε → P(Q × Γε) It takes as input a state, an input symbol or ε, a stack symbol or ε It returns 0 or more pairs of a state and a stack symbol or ε

10 / 14

slide-26
SLIDE 26

Example’s transition function

q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε δ(q0, t, s) = {{(q1, ε)} if t = ε and s = ε ∅

  • therwise

δ(q1, t, s) = {{(q1, a)} if t = a and s = ε {(q2, ε)} if t = ε and s = ε δ(q2, t, s) = {{(q2, ε)} if t = b and s = a {(q3, ε)} if t = ε and s = $ δ(q3, t, s) = ∅

11 / 14

slide-27
SLIDE 27

Example’s transition function in tabular form

q0 q1 q2 q3 ε, ε → $ a, ε → a ε, ε → ε b, a → ε ε, $ → ε δ(q, t, s) ∶ t = a t = b t = ε s = a s = $ s = ε s = a s = $ s = ε s = a s = $ s = ε q0 {(q1, $)} q1 {(q1, a)} {(q2, ε)} q2 {(q2, ε)} {(q3, ε)} q3 All blank entries are ∅

12 / 14

slide-28
SLIDE 28

PDA acceptance

A PDA M = (Q, Σ, Γ, δ, q0, F) accepts a string w = w1w2⋯wn for wi ∈ Σε if there exist

  • states r0, r1, . . . , rn ∈ Q and
  • strings s0, s1, . . . , sn ∈ Γ∗ (representing the stacks)

such that

13 / 14

slide-29
SLIDE 29

PDA acceptance

A PDA M = (Q, Σ, Γ, δ, q0, F) accepts a string w = w1w2⋯wn for wi ∈ Σε if there exist

  • states r0, r1, . . . , rn ∈ Q and
  • strings s0, s1, . . . , sn ∈ Γ∗ (representing the stacks)

such that

1 r0 = q0 and s0 = ε

(i.e., M starts in the start state with an empty stack);

13 / 14

slide-30
SLIDE 30

PDA acceptance

A PDA M = (Q, Σ, Γ, δ, q0, F) accepts a string w = w1w2⋯wn for wi ∈ Σε if there exist

  • states r0, r1, . . . , rn ∈ Q and
  • strings s0, s1, . . . , sn ∈ Γ∗ (representing the stacks)

such that

1 r0 = q0 and s0 = ε

(i.e., M starts in the start state with an empty stack);

2 xu = si−1 for some x ∈ Γε and u ∈ Γ∗, (ri, y) ∈ δ(ri−1, wi, x), and si = yu

(i.e., M moves from state ri−1 with stack si−1 to state ri with stack si according to δ); and

13 / 14

slide-31
SLIDE 31

PDA acceptance

A PDA M = (Q, Σ, Γ, δ, q0, F) accepts a string w = w1w2⋯wn for wi ∈ Σε if there exist

  • states r0, r1, . . . , rn ∈ Q and
  • strings s0, s1, . . . , sn ∈ Γ∗ (representing the stacks)

such that

1 r0 = q0 and s0 = ε

(i.e., M starts in the start state with an empty stack);

2 xu = si−1 for some x ∈ Γε and u ∈ Γ∗, (ri, y) ∈ δ(ri−1, wi, x), and si = yu

(i.e., M moves from state ri−1 with stack si−1 to state ri with stack si according to δ); and

3 rn ∈ F

(i.e., M ends in an accept state)

13 / 14

slide-32
SLIDE 32

More PDAs!

Build a PDA to recognize the languages

  • B = {w ∣ w ∈ {a, b}∗ and w has the same number of as as bs}
  • C = {w#wR ∣ w ∈ {a, b}∗}
  • D = {ak#w ∣ k > 0, w ∈ {a, b}∗, and ∣w∣ = k}
  • E = {aibjck ∣ i = j or j = k}
  • F = {wwR ∣ w ∈ {a, b}∗}
  • G = {w ∣ w ∈ {a, b}∗ and w = wR}
  • H is given by the CFG

S → SS ∣ (S) ∣ [S] ∣ ε

  • I is given by the CFG

E → E+E ∣ E-E ∣ (E) ∣ BN N → BN ∣ ε B → 0 ∣ 1

14 / 14