Pushdown Automata 7-0 Pushdown Automata The automata we - - PDF document

pushdown automata
SMART_READER_LITE
LIVE PREVIEW

Pushdown Automata 7-0 Pushdown Automata The automata we - - PDF document

Griffith University 3130CIT Theory of Computation (Based on slides by Harald Sndergaard of The University of Melbourne) Pushdown Automata 7-0 Pushdown Automata The automata we saw so far were limited by their


slide-1
SLIDE 1

✬ ✫ ✩ ✪ Griffith University 3130CIT Theory of Computation (Based on slides by Harald Søndergaard of The University of Melbourne)

Pushdown Automata

7-0

slide-2
SLIDE 2

✬ ✫ ✩ ✪

Pushdown Automata

The automata we saw so far were limited by their lack of memory. A pushdown automaton (PDA) is a finite-state automaton, equipped with a stack.

stack state control b a a y y x x input

Language {aibi | i ≥ 0} is not recognised by any DFA as it requires the ability of a recogniser to remember how many a’s were in the input.

7-1

slide-3
SLIDE 3

✬ ✫ ✩ ✪

Fine but Important Points

We shall consider the non-deterministic version of a PDA. It may, in one transition step, read a symbol from input and pop the top stack symbol. Based on these, and the current state, it will decide on which state to go to next, as well as what sequence of symbols to push on the stack. It may choose to leave out the popping, or pushing, or both. It may also ignore the input.

7-2

slide-4
SLIDE 4

✬ ✫ ✩ ✪

Pushdown Automata Formally

A pushdown automaton is a 6-tuple (Q, Σ, Γ, δ, q0, F) where

  • Q is a finite set of states,
  • Σ is the finite input alphabet,
  • Γ is the finite stack alphabet,
  • δ : Q × Σǫ × Γǫ → P(Q × Γ∗)

is the transition function,

  • q0 ∈ Q is the start state, and
  • F ⊆ Q are the accept states.

7-3

slide-5
SLIDE 5

✬ ✫ ✩ ✪

PDA Example 1

This PDA recognises {0n1n | n ≥ 0}:

  • q0

ǫ, ǫ → $

  • q1

1, 0 → ǫ

  • 0, ǫ → 0
  • q3
  • q2

ǫ, $ → ǫ

  • 1, 0 → ǫ
  • 7-4
slide-6
SLIDE 6

✬ ✫ ✩ ✪

Acceptance Precisely

The PDA (Q, Σ, Γ, δ, q0, F) accepts input w iff w = w1w2 · · · wn with each wi ∈ Σǫ, and there are states r0, r1, . . . , rn ∈ Q and strings s0, s1, . . . , sn ∈ Γ∗ such that

  • 1. r0 = q0 and s0 = ǫ.
  • 2. (ri+1, b) ∈ δ(ri, wi+1, a), si = at,

si+1 = b1 . . . bk t, with a ∈ Γǫ, b1 . . . bk ∈ Γ∗ and t ∈ Γ∗.

  • 3. rn ∈ F.

Note 1: There is no requirement that sn = ǫ, so the stack may be non-empty when the machine stops (even when it accepts). Note 2: Trying to pop an empty stack leads to rejection of input, rather than “runtime error”.

7-5

slide-7
SLIDE 7

✬ ✫ ✩ ✪

PDA Example 2

This PDA recognises {wwR | w ∈ {0, 1}∗}:

  • q0

ǫ, ǫ → $

  • q1

ǫ, ǫ → ǫ

  • 0, ǫ → 0

1, ǫ → 1

  • q3
  • q2

ǫ, $ → ǫ

  • 0, 0 → ǫ

1, 1 → ǫ

  • 7-6
slide-8
SLIDE 8

✬ ✫ ✩ ✪

CFLs Have PDAs

Lemma: Some PDA recognises CFL L. Proof: Given a CFG G, we show how to construct a PDA P such that L(P) = L(G). The idea is to let the PDA use its stack to store a list of “pending” recogniser tasks. For example, if S → xAy is a rule in G, and the PDA finds the symbol S on top of its stack, it may pop S and push y, A, and x, in that order. y state control input stack x y y S state control input stack x y y x A If it finds the terminal x on top of the stack, and x is the next input symbol, it may consume the input and pop x.

7-7

slide-9
SLIDE 9

✬ ✫ ✩ ✪

CFLs Have PDAs (cont.)

Construct the PDA with an initial state, an intermediate state q, and a final state. Add a self-loop from q for each terminal a. ǫ,ǫ→S$

  • q

ǫ,$→ǫ a,a→ǫ

  • Also add, for each rule A → w1 . . . wn, another

self-loop from q.

  • q

ǫ,S→w1...wn

  • 7-8
slide-10
SLIDE 10

✬ ✫ ✩ ✪

Example Recogniser

For the grammar S → a S b | b | ǫ we get

  • ǫ, ǫ → S$
  • q

a, a → ǫ b, b → ǫ ǫ, S → aSb ǫ, S → b ǫ, S → ǫ

  • ǫ, $ → ǫ
  • 7-9
slide-11
SLIDE 11

✬ ✫ ✩ ✪

PDAs Recognise CFLs

Lemma: If some PDA recognises L then L is context-free. Proof sketch: We show how to construct a CFG G which “simulates” the given PDA P. The variables will be Apq where p and q are states in P. The idea is that Apq will generate a string w iff w takes P from state p to state q leaving the stack unchanged. Without loss of generality we can assume that P has only one accept state, qf, that P empties its stack, and that each transition either pops or pushes a symbol. The start variable will be Aq0qf where q0 is P’s start state.

7-10

slide-12
SLIDE 12

✬ ✫ ✩ ✪

PDAs Recognise CFLs (cont.)

  • 1

ǫ, ǫ → $

  • ǫ, ǫ → $
  • 2

1, 0 → ǫ

  • 0, ǫ → 0
  • 5

ǫ, $ → ǫ

  • 4
  • 3

ǫ, $ → ǫ

  • 1, 0 → ǫ
  • Identify all pairs of transitions where the same

symbol is pushed then popped. For $: A14 → ǫ A23 ǫ | ǫ A55 ǫ | ǫ A25 ǫ | ǫ A53 ǫ. For 0: A23 → 0 A22 1 | 0 A23 1. Add the five rules Aii → ǫ. Add the 125 rules Aik → Aij Ajk. However, most of these variables will be unreachable.

7-11

slide-13
SLIDE 13

✬ ✫ ✩ ✪

PDAs Recognise CFLs (cont.)

Cleaning up, the PDA

  • 1

ǫ, ǫ → $

  • ǫ, ǫ → $
  • 2

1, 0 → ǫ

  • 0, ǫ → 0
  • 5

ǫ, $ → ǫ

  • 4
  • 3

ǫ, $ → ǫ

  • 1, 0 → ǫ
  • is simulated by the CFG

A14 → A23 | ǫ A23 → 0 1 | 0 A23 1

7-12

slide-14
SLIDE 14

✬ ✫ ✩ ✪

PDAs Recognise CFLs (cont.)

The construction precisely: Let P = (Q, Σ, Γ, δ, q0, {qf}). The variables of G are {Apq | p, q ∈ Q} and the start variable is Aq0qf .

  • Add rule Apq → a Ars b whenever δ(p, a, ǫ)

contains (r, t) and δ(s, b, t) contains (q, ǫ).

  • Add rule Apq → Apr Arq for all p, q, r ∈ Q.
  • Add rule App → ǫ for all p ∈ Q.

We then have: Apq generates x iff x can bring P from p to q with unchanged stack. The detailed proof is by induction on the length

  • f the derivation Apq

⇒ x.

7-13

slide-15
SLIDE 15

✬ ✫ ✩ ✪

PDAs Recognise Exactly the CFLs

We have established: Theorem: L is context-free iff some PDA recognises L. Since an NFA is also a PDA (which ignores its stack), we have: Corollary: Every regular language is context-free.

7-14