Top Banner
Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 2014 COMP 2600 — Pushdown Automata 1
27

Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Jul 07, 2019

Download

Documents

ngohanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Pushdown Automata

COMP2600 — Formal Methods for Software Engineering

Katya Lebedeva

Australian National University

Semester 2, 2014

COMP 2600 — Pushdown Automata 1

Page 2: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Pushdown Automata — PDA

stack

memoryz2

z1

zk

Finite

Control

input tapereadhead

a0 a1 a2 ... an. . . .

State

COMP 2600 — Pushdown Automata 2

Page 3: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Intuition

A finite state control reads the string, one symbol at a time (the input symbol).

Informally, a PDA determines its transition by observing

• the input symbol

• its current state

• the symbol on the top of stack

Alternatively, it may make a spontaneous transition using ε as its input instead

of an input symbol!

COMP 2600 — Pushdown Automata 3

Page 4: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

A Transition of a PDA

In one transition the PDA does the following:

1. Consumes the input symbol from the input string. If ε is the input symbol,

then no input symbol is consumed.

2. Goes to a new state. It may be the same state.

3. Replaces the symbol at the top of the stack by any string (see next slide!!!)

COMP 2600 — Pushdown Automata 4

Page 5: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Replacing the top symbol A of the stack by a string γ

Let Γ be the stack alphabet (i.e. the set of stack symbols).

Let A ∈ Γ be the top symbol on the stack.

Let B ∈ Γ and B 6= A.

Let w = X1X2 . . .Xn, w ∈ Γ∗.

γ Operation on stack

ε Popping: removal of the most recently added element (stack’s top symbol)

A Popping stack’s top symbol. Pushing A. Hence, no change is made.

B Popping stack’s top symbol. Pushing B.

w Popping stack’s top symbol. Pushing X1X2 . . .Xn onto the stack (X1 is on the top).

COMP 2600 — Pushdown Automata 5

Page 6: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Example

{anbn | n≥ 1}

Recall that this language cannot be recognised by a FSA.

It can be recognised by a PDA!

Intuition:

• begin in state q0 with the start symbol Z on the stack

• phase 1: (state q1) push a’s, one by one, from the input onto the stack

• phase 2: (state q2) when seeing b on the tape pop a from the stack

• finalise: if the stack is empty and the input is exhausted in the final state

(q3), accept the string.

COMP 2600 — Pushdown Automata 6

Page 7: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

The stack starts off containing only the initial symbol Z.

The transition function of the PDA to recognise anbn is as follows:

δ(q0,a,Z) = {(q1,aZ)} · · · push first a

δ(q1,a,a) = {(q1,aa)} · · · push further a’s

δ(q1,b,a) = {(q2,ε)} · · · start popping a’s

δ(q2,b,a) = {(q2,ε)} · · · pop further a’s

δ(q2,ε,Z) = {(q3,ε)} · · · accept

where q0 is the start state and q3 is the final state (underlined).

COMP 2600 — Pushdown Automata 7

Page 8: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

A Transition Diagram for PDA’s

• The nodes correspond to the states of the PDA.

• An arrow labelled start indicates the start state.

Doubly circled states are accepting.

• If δ(q,a,X) contains a pair (p,α),then there is an arc from q to p labeled a,X/α

q pa,X/α

The only thing that the digram does not tell us is which stack symbol is the

start symbol. Conventionally, it is Z.

COMP 2600 — Pushdown Automata 8

Page 9: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

The diagramm to our PDA for {anbn | n≥ 1}

δ(q0,a,Z) = {(q1,aZ)} · · · push first a

δ(q1,a,a) = {(q1,aa)} · · · push further a’s

δ(q1,b,a) = {(q2,ε)} · · · start popping a’s

δ(q2,b,a) = {(q2,ε)} · · · pop further a’s

δ(q2,ε,Z) = {(q3,ε)} · · · accept

q0start q1 q2 q3a,Z/aZ

a,a/aa

b,a/ε

b,a/ε

ε,Z/ε

COMP 2600 — Pushdown Automata 9

Page 10: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Transition function

Transition function of a deterministic PDA:

δD : Q× (Σ∪{ε})×Γ → Q×Γ∗

δD : (state, input symbol or ε, top-of-stack) → (new state,string of stack’s symbols)

Note that δD can be partial.

Transition function of a non-deterministic PDA:

δN : Q× (Σ∪{ε})×Γ → 2Q×Γ∗

δN : (state, input symbol or ε, top-of-stack) → set of (new state,string of stack’s symbols)

COMP 2600 — Pushdown Automata 10

Page 11: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Definition of a Nondeterministic PDA

A nondeterministic PDA has the form (Q,q0,F, Σ, Γ,Z, δ), where

• Q is the set of states

q0 ∈ Q is the initial state and F ⊆ Q is the set of the final states

• Σ is the set of input symbols (the alphabet)

• Γ is the set of stack symbols(the stack alphabet)

Z ∈ Γ is the initial stack symbol

• δ is a transition function

δ : Q× (Σ∪{ε})×Γ → 2Q×Γ∗

COMP 2600 — Pushdown Automata 11

Page 12: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Definition of a Deterministic PDA

A deterministic PDA has the form (Q,q0,F, Σ, Γ,Z, δ), where

• Q is the set of states

q0 ∈ Q is the initial state and F ⊆ Q is the set of the final states

• Σ is the set of input symbols (the alphabet)

• Γ is the set of stack symbols(the stack alphabet)

Z ∈ Γ is the initial stack symbol

• δ is a (partial) transition function

δ : Q× (Σ∪{ε})×Γ → Q×Γ∗

such that for all q ∈ Q and s ∈ Γ, δ(q,ε,s) is defined iff δ(q,a,s) is undefined

for all a ∈ Σ.

COMP 2600 — Pushdown Automata 12

Page 13: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

PDA execution: reading a symbol x ∈ Σ or ε /∈ Σ

• (q2,σ) ∈ δ(q1,x,Y )

state tape’s head stack

the PDA can go from q1 points at x Y on top-of-stack

to q2 advances σ replaces Y on stack

• (q2,σ) ∈ δ(q1,ε,Y )

state tape’s head stack

the PDA can go from q1 points any symbol from Σ Y on top-of-stack

to q2 does not advance σ replaces Y on stack

ε-transitions go from one state to another spontaneously, leaving the in-

put symbol intact (the tape head is not advanced).

COMP 2600 — Pushdown Automata 13

Page 14: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Instantaneous Descriptions of a PDA

PDA goes from configuration to configuration in response to input symbols.

PDA’s configuration involves both state and content of the stack.

It is also useful to see the portion of the input string that remains.

We shall represent the configuration of a PDA as a triple (q,w,γ), called an

instantaneous description :

• q is the state

• w is the remaining input

• γ is the stack contents

Convention: show the top of the stack at the left end of γ and the bottom at

the right end

COMP 2600 — Pushdown Automata 14

Page 15: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Representing one computation step of a PDA

Let P = (Q,q0,F, Σ, Γ,Z, δ) be a PDA.

Suppose δ(q,a,X) contains (p,α).Then for all w ∈ Σ∗ and β ∈ Γ∗

(q,aw,Xβ) ` (p,w,αβ)

Consuming a (which may be ε) from the input and replacing X on top of the

stack by α, we can go from state q to state p.

Note that what remains on the input, i.e. w, and what is below the top of the

stack, i.e. β, do not influence the action of the PDA in this computation step.

They are merely carried along and can influence later steps.

COMP 2600 — Pushdown Automata 15

Page 16: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Example

Consider the input string aaabbb.

(q0,aaabbb,Z) ` (q1,aabbb,aZ) (push first a)

` (q1,abbb,aaZ) (push further a’s)

` (q1,bbb,aaaZ) (push further a’s)

` (q2,bb,aaZ) (start popping a’s)

` (q2,b,aZ) (pop further a’s)

` (q2,ε,Z) (pop further a’s)

` (q3,ε,ε) (accept)

The machine halts in the final state with input exhausted and an empty stack.

The string is accepted.

COMP 2600 — Pushdown Automata 16

Page 17: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Computation of a PDA(Q,q0,F, Σ, Γ,Z, δ)

Start configuration:

• PDA is in state q0

• the tape head is on the leftmost symbol of the input string

• the stack contains only Z

Computation and termination: Starting at the start configuration, the PDAperforms a sequence of computation steps (moves). It terminates when thestack becomes empty.

Acceptance by final state: The PDA consumes the input and enters a finalstate.

Acceptance by empty stack: The PDA consumes the input and empties thestack.

COMP 2600 — Pushdown Automata 17

Page 18: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Example: Palindromes with ‘Centre Mark’

Consider the language

{wcwR | w ∈ {a,b}∗ ∧ wR is w reversed}

This language is context-free, and we can design a deterministic PDA to ac-cept it:

• Push a’s and b’s onto the stack as we see them

• When we see c, change state

• Now try to match the symbols we are reading with the symbols on top ofthe stack, popping as we go

• If the top of the stack has symbol Z, pop it and enter the final state via anε-transition. Hopefully our input has been used up too!

Full formal details are left as an exercise.

COMP 2600 — Pushdown Automata 18

Page 19: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Languages of Nondeterministic and Deterministic PDAs.

Nondeterministic PDAs recognise context free languages.

Deterministic PDAs recognise all the regular languages, but only a proper

subset of context free languages!

L(DPDA)⊂ L(NPDA)

COMP 2600 — Pushdown Automata 19

Page 20: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Example: Even-Length Palindromes

Consider the context free language of even length palindromes

{wwR | w ∈ {a,b}∗ ∧ wR is w reversed}

It cannot be recognised by a deterministic PDA, because without a centre

mark it cannot know whether it is in the first half of a sentence (and should

continue pushing into memory) or the second half (and should be matching

input and stack, and popping).

But a non-deterministic PDA can recognise this language.

COMP 2600 — Pushdown Automata 20

Page 21: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

L = {wwR | w ∈ {a,b}∗ ∧ wR is w reversed}

The following transitions are necessary to recognize L

(r,Z) ∈ δ(q,ε,Z)

(r,a) ∈ δ(q,ε,a)

(r,b) ∈ δ(q,ε,b)

q is the ‘push’ state, and r the ‘match and pop’ state.

In other words, we continually ‘guess’ which job we should be doing!

Task: Define other transitions!

COMP 2600 — Pushdown Automata 21

Page 22: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Grammars and PDAs

Theorem

The class of languages recognised by non-deterministic PDA’s is exactly the

class of context-free languages.

We will only justify this result in one direction: for any CFG, there is a corre-

sponding PDA.

This direction is the basis of automatically deriving parsers from grammars.

COMP 2600 — Pushdown Automata 22

Page 23: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

From CFG to PDA

Given a CFG

G = (Σ,N,S,P)

We define a PDA

P = ({q0,q1,q2},q0,{q2},Σ, N∪Σ, Z,δ)

Note that P has three states: q0 (initial), q1 (processing), and q2 (final). Its

alphabet the alphabet (i.e. the set of terminals) of G, and the set of its stack

symbols consists of G’s terminals and non-terminals.

P is a non-deterministic PDA since there may be several productions for

each non-terminal.

COMP 2600 — Pushdown Automata 23

Page 24: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

1. Initialise the process by pushing the start symbol S onto the stack, andentering state q1:

δ(q0,ε,Z) = {(q1,SZ)}

2. For each production A→ α define

(q1,α) ∈ δ(q1,ε,A)

Thus, if a non-terminal is on top of stack, it is replaced it with the righthand side of the production.

3. For each terminal symbol t, pop the stack if it matches the input:

δ(q1, t, t) = {(q1,ε)}

4. For termination, add the transition, with final state q2:

δ(q1,ε,Z) = {(q2,ε)}

COMP 2600 — Pushdown Automata 24

Page 25: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

Example

S → S+T | T

T → T ∗U | U

U → (S) | i

1. Initialise:

δ(q0,ε,Z) = {(q1,SZ)}

2. Expand non-terminals:

δ(q1,ε,S) = {(q1,S+T ),(q1,T )}δ(q1,ε,T ) = {(q1,T ∗U),(q1,U)}δ(q1,ε,U) = {(q1,(S)),(q1, i)}

COMP 2600 — Pushdown Automata 25

Page 26: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

3. Match and pop terminals:

δ(q1,+,+) = {(q1,ε)}

δ(q1,∗,∗) = {(q1,ε)}

δ(q1, i, i) = {(q1,ε)}

δ(q1,(,() = {(q1,ε)}

δ(q1,),)) = {(q1,ε)}

4. Terminate:

δ(q1,ε,Z) = {(q2,ε)}

COMP 2600 — Pushdown Automata 26

Page 27: Pushdown Automata - Research School of Computer Science · Pushdown Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester

IDs on input i*i

(q0, i∗ i, Z) ` (q1, i∗ i, SZ)

` (q1, i∗ i, T Z)

` (q1, i∗ i, T ∗UZ)

` (q1, i∗ i, U ∗UZ)

` (q1, i∗ i, i∗UZ)

` (q1, ∗i, ∗UZ)

` (q1, i, UZ)

` (q1, i, iZ)

` (q1, ε, Z)

` (q2, ε, ε)

accept by empty stack and final state

COMP 2600 — Pushdown Automata 27