Push Down Automata - Rice Universityyz2/COMP481/Chapt12.pdfAlternative Models of Computation...

29
Push Down Automata

Transcript of Push Down Automata - Rice Universityyz2/COMP481/Chapt12.pdfAlternative Models of Computation...

Push Down Automata

Non-Deterministic Finite State Automata

Non Deterministic Finite State Automata (NDFSA)

• M = {Q, Σ, δ , q0, F}

-- Σ = Symbols

-- Q = States

-- q0 = Initial State

-- F = Final (Accepting) States ⊆ Q

-- δ :Q × Σ∪{ε}( )→ P(Q) = Transition Functions

-- P(Q) = set of all subsets of Q

-- Transition to Next State is NOT Unique

-- Transitions on Empty String Permitted

Non-Deterministic Push Down Automata

Non Deterministic PDA

• M = {Q, Σ, Γ,Ω,δ , q0, F}

-- Σ = Input Symbols Γ = Stack Symbols

-- Q = States Ω = Stack

-- q0 = Initial State

-- F = Final (Accepting) States ⊆ Q

--

δ :Q × Σ∪ {ε}× Γ∗

pop → i (qi × Γi

push

)⎧⎨⎪

⎩⎪

⎫⎬⎪

⎭⎪ = Non-Deterministic Transition Functions

-- Transition to Next State and Next Stack is NOT Unique

-- ε ∈Γ∗ ⇒ No Pop ε ∈Γi∗ ⇒ No Push

-- Stack -- First In / Last Out

Alternative Models of Computation

Equivalent Machines

• Push 1 Symbol at a Time

• Pop 1 Symbol at a Time

Stronger Machines

• Replace 1 Stack by 2 Stacks

• Replace Stack by Queue

Configurations

Current Configuration -- (q,w,c)

• q = current state

• w = unread input symbols

• c = current contents of stack

Initial Configuration -- (q0,w,ε )

• initial state = q0

• input string = w ∈Σ∗

• initial contents of stack = ε

Transition Functions

• δ :one configuration→ many possible configurations

Accepting Computation

Accepting Conditions

• No More Input

• Empty Stack

• Accepting State

Accepting Computation

• M Accepts w ∈Σ∗ if M satisfies the Accepting Conditions for at least one choice of Transitions

• M Rejects w ∈Σ∗ if All possible choices of Transitions fail to accept

Example 1

s

b / a / ε

b / a / εf

a / ε / a

PDA for {anbn}

PDA’s can Count!

Example 2

s

b / a / ε

b / a / εf

a / ε / aa

PDA for {anb2n}

Push Two Symbols Onto the Stack

Example 3

a / b / εa / ε / a

b / ε / bb / a / ε

S

PDA for {a,b}* | number of  a 's = number of  b 's{ }Non-Determinism is Essential

Deterministic Finite State Automata

Deterministic Finite State Automata (DFSA)

• M = {Q, Σ, δ , q0, F}

-- Σ = Symbols

-- Q = States

-- q0 = Initial State

-- F = Final (Accepting) States ⊆ Q

-- δ :Q × Σ→Q = Transition Functions

-- Transition to Next State is Unique

-- Transitions on Empty String NOT Permitted

Deterministic Push Down Automata

Deterministic PDA

• M = {Q, Σ, Γ,δ, q0 , F}

-- Σ = Input Symbols Γ = Stack Symbols

-- Q = States

-- q0 = Initial State

-- F = Final (Accepting) States ⊆ Q

--

δ :Q × Σ∪ {ε}× Γ∗

pop →Q × Γ∗

push = Deterministic Transition Functions

-- Transition to Next State and Next Stack is Unique

-- All Transitions Out of an Accepting State Must Consume either an Input Symbol or Symbols from the Stack

⇒ Must Accept when Can Accept

Observations

Deterministic FSA’s are Equivalent to Non-Deterministic FSA’s.

Deterministic PDA’s are NOT Equivalent to Non-Deterministic PDA’s.

Reducing Non-Determinism

Special Symbols

• End of Stack -- #

• End of String -- $

Example 4

ε / a / ε

ε / a / ε 3

b / a / ε

2

a / ε / a

1

b / ε / ε

4

b / a / ε

b / ε / ε

PDA for {ambn |m ≠ n}

= {ambn | m > n}∪ {ambn| m < n}

Example 4a: Marking Bottom of Stack

ε / a / ε

ε / a / ε

b / a / ε

2

a / ε / a

1

b / ε / ε

4

b / a / ε

b / #/ ε

PDA for {ambn |m ≠ n}

= {ambn | m > n}∪ {ambn| m < n}

3

ε / #/ ε

Example 4b: Marking End of String

ε / a / ε

$ / a / ε

b / a / ε

2

a / ε / a

1

b / ε / ε

b / a / ε

b / #/ ε

PDA for {ambn |m ≠ n}

= {ambn | m > n}∪ {ambn| m < n}

3

ε / #/ ε

$ / ε / ε

4

Equivalence of Context Free Grammars and Push Down Automata

Theorem 1

For Every Context Free Grammar there Exists an Equivalent Push Down Automata.

Theorem 2

For Every Push Down Automata There Exist an Equivalent Context Free Grammar.

Theorem 3

Push Down Automata and Context Free Grammars Describe the Same Languages.

Theorem 1

For Every Context Free Grammar there Exists an Equivalent Push Down Automata

Construction 1 -- Top-Down Parser (Start with S and Apply Rules)

2 State Machine -- S = Start State and A = Accepting State

• Push S onto Stack and Move to Second State A (Accepting)

• Pop S off Stack and Replace S by an S Rule in the Grammar -- S →γ 1γ n

• Continue Popping Top Non-Terminal Symbols off Stack

and Replace by Rules in Grammar -- ε / T1 /γ 1γ n if T1 →γ 1γ n

• When a Terminal Symbol Appears

-- Pop off Stack

-- Compare to Current Input Symbol -- c / c / ε

• Continue Until Stack Empty (Accept) or Terminal Does Not Match Input Symbol

Push Down Automata for Context Free Grammar: Top Down Parser

S

ε /T / t1tn

ε / ε / S A c / c / ε

ε / S / s1sn

Example -- Top Down Parsing

L(G) = {anbn |n ≥ 0}

• Rules: S → aSb • S → ε

• String: w = a2b2

Top Down Parsing

• Start in State 1

• Push S onto Stack and go to State 2

• Do 2 Times-- Replace S by aSb-- Pop a Off Stack and Consume a in w

• Replace S by ε

• Do 2 Times-- Pop b Off Stack and Consume b in w

Theorem 1

For Every Context Free Grammar there Exists an Equivalent Push Down Automata

Construction 2 -- Bottom-Up Parser (Start with w and Apply Rules in Reverse)

2 State Machine -- S = Start State and A = Accepting State

• Push First Symbol(s) onto Stack (Stay in Start State) -- c /ε / c

• Pop Symbols off Stack and Replace Left Hand Side of a Rule in the Grammar

-- Rules on Right Hand Side Appear in Reverse Order

-- ε / ckc1 /T if T → c1ck

• Continue Pushing Terminal Symbols onto Stack

and Replacing when Possible by Left Hand Side of Rule in Grammar

-- ε / γ kγ k /T if T →γ 1γ k

• Continue Until Stack Contains S and/or All Symbols of Input are Used Up

-- If Stack Contains S. Then Pop S off Stack and Move to Accepting State A

Push Down Automata for Context Free Grammar: Bottom Up Parser

ε / tkt1 / T

S

c / ε / c

ε / S / ε A

Example -- Bottom Up Parsing

L(G) = {anbn |n ≥ 0}

• Rules: S → aSb • S → ε

• String: w = a2b2

Bottom Up Parsing

• Start in State 1

• Push w = ε a2 onto Stack and Stay in State 1

• Replace ε by S

• Do 2 Times-- Push b Onto Stack-- Pop bSa Off Stack and Push S onto Stack

• Pop S off Stack and Move to State 2

Restricted Normal Form

1. M has a new start state S#

• push # onto the stack• move to original start state S.

2. M has a single accepting state A#

• For each accepting state A:

-- add the transition (A, ε, #)→ (A#, ε )

-- Change A from an accepting to a non-accepting state

3. Every transition of M (except from S# ) pops exactly one symbol off the stack

• For every transition that pops k > 1 symbols-- replace the initial state with k new states that each pop a single symbol

• For every transition that does not pop any symbols-- replace with a transition that pops and pushes the same symbol

Theorem 2

For Every Push Down Automata in Restricted Normal Form

There Exist an Equivalent Context Free Grammar

Grammar

• Non-Terminal Symbols

-- < p,γ , q >

• Interpretation of < p,γ , q >

-- Traverse PDA from State p to State q Removing γ from Top of Stack

-- < S, #, A > ⇔ Traverse from Original Start State to an Accepting State

• Production Rules

-- Nothing Pushed onto Stack

-- One Symbol Pushed onto Stack

-- Many Symbols Pushed onto Stack

-- Start and Stop Rules

Production Rules

Nothing Pushed onto Stack• < q,c,γ >→ < r,ε > = Transition Function (Read c, Pop γ , No Push)• < q,γ ,w >→ c< r,ε,w > = Production Rules (For ALL States w)

One Symbol Pushed onto Stack• < q,c,γ >→ < r,α > = Transition Function• < q,γ ,w >→ c< r,α ,w > = Production Rules (For ALL States w)

Two Symbols Pushed onto Stack• < q,c,γ >→ < r,αβ > = Transition Function

• < q,γ ,w >→ c< r,α ,v > < v,β, w > = Production Rules (For ALL States w)

Start and Stop Rules

• S# → < S, #, A# > -- Start

• < q,ε,q >→ ε -- Stop (Eliminate Terminal Symbols)

Example from Text

String: abcba

PDA Grammar

< ′s ,ε, # >→ < s,ε > ′S → < S, #, A >

< s,a, # > → < s,a # > < S, #, A > → a < S,a, F > < F, #, A >

< s,b,a >→ < s,ab > < S, a, F > → b < S,b, F >

< s,c,b >→ < f ,b > < S,b, F > → c < F,b,F >

< f ,b,b >→ < f ,ε > < F,b, F > → b < F,ε, F >

< f ,a, a >→ < f ,ε > < F,a, F > → a < F,ε,F >

Decidable Questions for Regular Languages

1. Set Membership: w ∈L(M )?

• Run L(M ) on w.

2. Emptiness: L(M ) = φ ?

• Find minimal machine Min(M ) corresponding to M.

L(M ) = φ  ⇔ Min(M ) has only one (nonaccepting) state

• Test M on all strings of length < number of states of M

L(M ) = φ  ⇔ M does not accept any such string (Pumping Theorem)

3. Totality: L(M ) = Σ∗?

• L(M ) = Σ∗  ⇔ L(M )c = φ

Questions for Context Free Languages

1. Set Membership: w ∈L(M )?

• Does M Halt on w?

-- NOT Necessarily (Example)

• No Algorithm for Finding a Deterministic PDA

-- Deterministic PDA Might NOT Exist

2. Emptiness: L(M ) = φ ?

• There is NO Algorithm for Finding a Minimal PDA -- Undecidable

3. Totality: L(M ) = Σ∗?

• L(M ) = Σ∗  ⇔ L(M )c = φ