pushdown automata

57
CS 3240 – Chapter 7

Upload: admon

Post on 21-Mar-2016

65 views

Category:

Documents


0 download

DESCRIPTION

CS 3240 – Chapter 7. Pushdown Automata. Where Are We?. A Pushdown Automaton Machine for Context-Free Languages. Take an FA and add a stack A restricted form of unbounded memory Operations: Pop Push Ignore ( λ ) (Pop always precedes push). PDA Operations. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pushdown Automata

CS 3240 – Chapter 7

Page 2: Pushdown Automata

Language Machine GrammarRegular Finite Automaton Regular

Expression,Regular Grammar

Context-Free Pushdown Automaton

Context-Free Grammar

Recursively Enumerable

Turing Machine Unrestricted Phrase-Structure Grammar

2CS 3240 - Introduction

Page 3: Pushdown Automata

Take an FA and add a stack A restricted form of unbounded memory

Operations: Pop Push Ignore (λ) (Pop always precedes push)

3CS 3240 - Pushdown Automata

Page 4: Pushdown Automata

Three items per transition edge <read>,<pop>,<push> any or all could be λ

A PDA accepts when: An accepting state is reached, and The stack is empty

▪ Different from book!▪ The machine crashes when no move is

possible▪ implicit jail

4CS 3240 - Pushdown Automata

Page 5: Pushdown Automata

A deterministic PDA (no choices; λ is okay sometimes). The stack alphabet (Γ = {X}) can differ from the input alphabet (Σ = {a, b}) .

Trace aabb…5CS 3240 - Pushdown Automata

Page 6: Pushdown Automata

q0 aabb λq0 abb Xq0 bb XXq1 b Xq1 λ λ

6CS 3240 - Pushdown Automata

Traditional notation (see book):

(q0,aabb, λ) ⊢ (q0,abb,X) ⊢ (q0,bb,XX) ⊢ (q1,b,X) ⊢ (q1,λ, λ)

Also: (q0,aabb,λ) ⊢* (q1,λ, λ) (⇒ aabb ∈ L)

Page 7: Pushdown Automata

7CS 3240 - Pushdown Automata

Page 8: Pushdown Automata

8CS 3240 - Pushdown Automata

How would you do a2nbn?

Page 9: Pushdown Automata

CS 3240 - Pushdown Automata 9

Each b must pop 2 X’s

Page 10: Pushdown Automata

Is this deterministic?

Trace the string abaacaaba.

10CS 3240 - Pushdown Automata

Page 11: Pushdown Automata

Input: (state-1, input character, pop character)

Output: (state-2, push character(s))

Example: The edge (a,λ,a) on previous slide:

δ(q0,a,λ) = (q0,a)

Exercise: Rewrite wcwR in functional form

CS 3240 - Pushdown Automata 11

Page 12: Pushdown Automata

Is this deterministic?Trace the string abaaaaaba.Trace the string abaabaaba.Trace the string abaaaaba.

12CS 3240 - Pushdown Automata

Page 13: Pushdown Automata

CS 3240 - Pushdown Automata 13

Trace aaabbbb

The grammar:

S → aSb | aSbb | λ

Page 14: Pushdown Automata

CS 3240 - Pushdown Automata 14

Trace ababbaab

Page 15: Pushdown Automata

CS 3240 - Pushdown Automata 15

Page 16: Pushdown Automata

CS 3240 - Pushdown Automata 16

Try abababbbb.How would you do Twicea?

Reasoning: There must be 2 b’s for every a. When we read an a, there are three possibilities:

1)Its 2 b’s have already been read2)1 b has been read and 1 remains3)Both b’s are yet to be read

Page 17: Pushdown Automata

CS 3240 - Pushdown Automata 17

Page 18: Pushdown Automata

CS 3240 - Pushdown Automata 18

Page 19: Pushdown Automata

CS 3240 - Pushdown Automata 19

Page 20: Pushdown Automata

Q: set of states∑: input alphabetΓ: stack alphabetδ: Q x (∑∪λ) x (Γ∪λ) → finite subsets

of Q x Γ*

q0: start stateF ⊆ Q: final states

(z ⋲ Γ: optional stack start symbol)CS 3240 - Pushdown Automata 20

Page 21: Pushdown Automata

Not strictly necessary Can serve as a “bottom marker” to allow

detection of an empty stack A convenience

Can make things easier to design/understand

Sometimes handy when seeking an associated CFG

Can eliminate some lambda pops▪ Handy later

CS 3240 - Pushdown Automata 21

Page 22: Pushdown Automata

CS 3240 - Pushdown Automata 22

Page 23: Pushdown Automata

CS 3240 - Pushdown Automata 23

Page 24: Pushdown Automata

CS 3240 - Pushdown Automata 24

Page 25: Pushdown Automata

CS 3240 - Pushdown Automata 25

Page 26: Pushdown Automata

How do we know that PDAs are the correct machine model for CFLs?

1) Must show that for any CFG, there is a PDA that accepts its language

2) Must show that for any PDA, there is a CFG that generates its language

The first case is easier than the second!

It can be handy to go back and forthCS 3240 - Pushdown Automata 26

Page 27: Pushdown Automata

The leading character is readThe LHS variable is poppedThe RHS string is pushed Initial transition pushes S

See next slide

CS 3240 - Pushdown Automata 27

Page 28: Pushdown Automata

CS 3240 - Pushdown Automata 28

Page 29: Pushdown Automata

CS 3240 - Pushdown Automata 29

Page 30: Pushdown Automata

Very trivial (Yay!) But the (2-state) PDA is non-deterministic

State-2 is a final state 1) Push S on the stack; move to state-2 2) For every rule, have a transition that:

pops the left-hand side and pushes the right-hand side

3) For every character, c, in Σ, have a rule that: consumes and pops c simultaneously

CS 3240 - Pushdown Automata 30

Page 31: Pushdown Automata

CS 3240 - Pushdown Automata 31

S → aSb | aSbb | λ

Trace aaabbbbb: (q0,aaabbbbb,λ) ⊢ (q1,aaabbbbb,S) ⊢ (q1,aaabbbbb,aSbb) ⊢ (q1,aabbbbb,Sbb) ⊢ (q1,aabbbbb,aSbbbb) ⊢ (q1,abbbbb,Sbbbb) ⊢ (q1,abbbbb,aSbbbbb) ⊢ (q1,bbbbb,Sbbbbb) ⊢ (q1,bbbbb,bbbbb) ⊢* (q1,λ,λ)

Page 32: Pushdown Automata

CS 3240 - Pushdown Automata 32

S → aSbSbS | bSaSbS | bSbSaS | λ… And trace bbabaabbb

Page 33: Pushdown Automata

CS 3240 - Pushdown Automata 33

S => (S) | SS | λ

Derive ()(()) from the grammar.Then accept it by the machine.Then find a one-state DPDA for this language.

Page 34: Pushdown Automata

We need to relate PDA movement to a CFG production rule somehow

Key: consuming a character in the PDA corresponds to generating a character in the CFG

The stack contents must also be part of all this

CS 3240 - Pushdown Automata 34

Page 35: Pushdown Automata

PDAs of the form below have a natural translation to a CFG the reverse of CFG-to-PDA conversion (conveniently) call the stack start symbol S Translate each transition in state-2 into a rule:

▪ c,X,Y ⟺ X → cY (i.e., <pop> → <char> <push>)▪ (X cannot be λ)

CS 3240 - Pushdown Automata 35

Page 36: Pushdown Automata

CS 3240 - Pushdown Automata 36

S → aXS | bYS | λX → aXX | bYX | bY → bYY | aXY | a

Derive abbbaaba

Page 37: Pushdown Automata

CS 3240 - Pushdown Automata 37

Go back and find grammars for slides 22,24-25.

Also, work backward from the (()) DPDA to a grammar.

Page 38: Pushdown Automata

We must also track state changesOur variables must therefore contain

state and stack informationVery tricky!

CS 3240 - Pushdown Automata 38

Page 39: Pushdown Automata

Suppose X is on the stack and ‘a’ is read What can happen to X?

It will be popped (and possibly put back) It may be replaced by one or more other stack

symbols▪ Which symbols will also later be popped, replaced, etc…▪ The stack grows and shrinks and grows and shrinks …

Eventually, as more input is consumed, the effect of having pushed X on the stack must be erased (or we’ll never reach an empty stack!)

And the state may change many times!CS 3240 - Pushdown Automata 39

Page 40: Pushdown Automata

… (qi, x1x2…xk, Y1Y2…Yk) ⊢* (qj, x2…xk, Y2…Yk) ⊢* (qn, xk, Yk) ⊢* (qf, λ, λ) 40CS 3240 - Pushdown Automata

Page 41: Pushdown Automata

Let the symbol <qAp> represent the sequence of movements in a PDA that: starts in state q eventually ends in state p eventually removes A and its after-effects from the stack

The symbol <sλf> then represents accepting a valid string (if s is the start state and f is a final state)

These symbols will be our variables Because they track the machine configuration that

accepts strings Just as the grammar will generate those strings

41CS 3240 - Pushdown Automata

Page 42: Pushdown Automata

Consider the transition δ(q,a,X) = (p,Y) This means that a is consumed, X is popped, we move

directly to state p, and subsequent processing must eventually erase Y and its follow-on effects

A corresponding grammar rule is: <qX?> → a<pY?> (?’s represent the same state) We don’t know where we’ll eventually end up when the

stack is back to where it was before the X was first pushed

All we know is we immediately go through p So we entertain all possibilities (see 3 and 4 on next

slide)42CS 3240 - Pushdown Automata

Page 43: Pushdown Automata

1) S → <sλf> for all final states, f 2) <qλq> → λ for all states, q

These will serve as terminators 3) For transitions δ(q,a,X) = (p,Y):

<qXr> → a<pYr> for all states, r 4) For transitions δ(q,a,X) = (p,Y1Y2):

<qXr> → a<pY1s><sY2r> for all states, r, s

And so on, for longer pushed strings43CS 3240 - Pushdown Automata

Page 44: Pushdown Automata

The process requires: No multiple pops

▪ So spread them out over multiple states if needed Also, for every lambda-pop c,λ,X:

Add an equivalent rule for each letter of Γ (the stack alphabet) as follows:▪ (c,X,XX)▪ (c,Y,XY)▪ etc.

▪ And keep the original lambda pop!!!

CS 3240 - Pushdown Automata 44

Page 45: Pushdown Automata

45CS 3240 - Pushdown Automata

• Call the start state, s, and the final state, f.• Add the following transition on s: (a,X,XX)• 1) S → <sλf>• 2) <sλs> → λ; <fλf> → λ• Now go through each transition…

Page 46: Pushdown Automata

CS 3240 - Pushdown Automata 46

Page 47: Pushdown Automata

CS 3240 - Pushdown Automata 47

Page 48: Pushdown Automata

CS 3240 - Pushdown Automata 48

S → <sλf><sλs> → a<sXs> | λ<fλf> → λ<sλf> → a<sXf><sXs> → a<sXs><sXs><sXf> → a<sXs><sXf> | a<sXf><fXf> | b<fλf><fXf> → b<fλf>

Page 49: Pushdown Automata

CS 3240 - Pushdown Automata 49

S → <sλf><sλs> → a<sXs> | λ<sλf> → a<sXf><sXs> → a<sXs><sXs><sXf> → a<sXs><sXf> | a<sXf><fXf> | b<fXf> → b

Page 50: Pushdown Automata

CS 3240 - Pushdown Automata 50

S → <sλf><sλf> → a<sXf><sXf> → a<sXf><fXf> | b<fXf> → b

Page 51: Pushdown Automata

CS 3240 - Pushdown Automata 51

S → a<sXf><sXf> → a<sXf><fXf> | b<fXf> → b

Page 52: Pushdown Automata

CS 3240 - Pushdown Automata 52

S → a<sXf><sXf> → a<sXf>b | b

Page 53: Pushdown Automata

CS 3240 - Pushdown Automata 53

S → aXX → aXb | b

Done! (finally :-)

Page 54: Pushdown Automata

Find a CFG for nb = na + 1 using the generic conversion process

CS 3240 - Pushdown Automata 54

Page 55: Pushdown Automata

A PDA is deterministic if there are no choices: Depends on a state’s <character, pop symbol>

pair: 1) δ(q,a,X) has only one choice (a could be λ), and 2) If a is not λ, then there is no δ(q,λ,X) edge, and 3) if X is not λ, then there is no δ(q,a,λ) edge.

NOTE: Some PDAs are inherently non-deterministic (no deterministic equivalent, e.g., wwR)

CS 3240 - Pushdown Automata 55

Page 56: Pushdown Automata

Revisit previous PDAs and check for determinacy

CS 3240 - Pushdown Automata 56

Page 57: Pushdown Automata

Languages accepted bynondeterministic PDA

Languages accepted bydeterministic PDA

Languages accepted by DFA

CS 3240 - Pushdown Automata 57