Pushdown Automata

Pushdown automata resemble nondeterministic finite automata but have access to unlimited memory, modeled as a stack that operates on a first-in, last-out (FILO) principle.

Definition. A pushdown automaton is a six-tuple ⟨Q,Σ,Γ,δ,S,F⟩\langle Q, \Sigma, \Gamma, \delta, S, F \rangle, where

  • QQ is a finite set of states,
  • Σ\Sigma is a finite set of input symbols,
  • Γ\Gamma is a finite set of stack symbols,
  • δ:Q×Σϵ×Γϵ→P(Q×Γϵ)\delta : Q \times \Sigma_{\epsilon} \times \Gamma_{\epsilon} \to P(Q \times \Gamma_\epsilon) is the transition function, where Σϵ=Σ∪{ϵ}\Sigma_{\epsilon} = \Sigma \cup \{ \epsilon \}, Γϵ=Γ∪{ϵ}\Gamma_\epsilon = \Gamma \cup \{ \epsilon \}, and P(Q×Γϵ)P(Q \times \Gamma_\epsilon) is the power set of Q×ΓϵQ \times \Gamma_\epsilon,
  • S∈QS \in Q is the start state and
  • F⊆QF \subseteq Q is a finite set of accept states.

At the core of a pushdown automaton's definition is its transition function. The next move is determined by three elements from the domain Q×Σϵ×ΓϵQ \times \Sigma_{\epsilon} \times \Gamma_{\epsilon}:

  • The current state.
  • The next input symbol σ∈Σϵ\sigma \in \Sigma_\epsilon:
    • If σ=ϵ\sigma=\epsilon, the machine moves without consuming input.
  • The symbol γ∈Γϵ\gamma \in \Gamma_\epsilon at the top of the stack:
    • If γ=ϵ\gamma=\epsilon, the machine moves without reading or popping the stack.
    • Otherwise, the machine pops γ\gamma from the stack.

The machine transitions to a new state and updates the stack based on a pair from the domain Q×ΓϵQ \times \Gamma_\epsilon:

  • It enters a (possibly new) state.
  • It may push a symbol γ∈Γϵ\gamma \in \Gamma_\epsilon onto the stack:
    • If γ=ϵ\gamma=\epsilon, no symbol is written.

Example Operations

If we want the automaton to move to state q1q_1 and push a symbol γ∈Γ\gamma \in \Gamma onto the stack when the machine is in state q0q_0 and reads input σ\sigma, we specify the transition function δ\delta by including (q1,γ)(q_1, \gamma) in δ(q0,σ,ϵ)\delta(q_0, \sigma, \epsilon).

Conversely, if we want the automaton to move to state q1q_1 and pop the symbol on top of the stack when

  • the machine is in state q0q_0,
  • reads input σ\sigma, and
  • the symbol on top of the stack is γ∈Γ\gamma \in \Gamma,

we specify the transition function δ\delta by including (q1,ϵ)(q_1, \epsilon) in δ(q0,σ,γ)\delta(q_0, \sigma, \gamma).

Computation

A pushdown automaton M=⟨Q,Σ,Γ,δ,S,F⟩M = \langle Q, \Sigma, \Gamma, \delta, S, F \rangle computes as follows. It accepts input ww if ww can be written as w=w1w2…wmw = w_1 w_2 \ldots w_m, where each wi∈Σϵw_i \in \Sigma_\epsilon, and sequences of states r0,r1,…,rm∈Qr_0, r_1, \ldots, r_m \in Q and strings s0,s1,…,sm∈Γ∗s_0, s_1, \ldots, s_m \in \Gamma^* exist that satisfy the following three conditions. The strings sis_i represent the sequence of stack contents that MM has on the accepting branch of the computation.

  1. r0=Sr_0 = S and s0=ϵs_0 = \epsilon. This condition signifies that MM starts properly, in the start state with an empty stack.

  2. For i=0,…,m−1i = 0, \ldots, m - 1, we have (ri+1,b)∈δ(ri,wi+1,a)(r_{i + 1}, b) \in \delta(r_i, w_{i + 1}, a), where si=ats_i = at and si+1=bts_{i+1} = bt for some a,b∈Γϵa, b \in \Gamma_\epsilon and t∈Γ∗t \in \Gamma^*. This condition states that MM transitions correctly based on its state, stack, and input symbol.

  3. rm∈Fr_m \in F. This condition ensures that MM reaches an accept state at the end of the input.

What can be computed by Pushdown Automata

Theorem. The class of context-free languages is closed under union, concatenation, and star operations.

What cann't be computed by Pushdown Automata

Pumping Lemma. For every context-free language LL, there is a number pp, such that for every ω∈L\omega \in L, if the length of ω\omega is greater than pp, then ω\omega can be devided into five pieces ω=axyzb\omega = axyzb, satisfying the following three conditions:

  1. axiyzib∈Lax^iyz^ib \in L, for all i≥0i \ge 0,
  2. ∣xyz∣≤p|xyz| \le p and
  3. ∣x∣≥0|x| \ge 0 or ∣z∣≥0|z| \ge 0.

Context-Free Grammar

Definition. A context-free grammar is a four-tuple ⟨N,Σ,S,R⟩\langle N, \Sigma, S, R\rangle, where

  • NN is a finite set of symbols called the variables,
  • Σ\Sigma is a finite set of symbols, disjoint from NN, called the terminals,
  • S∈NS \in N is the start symbol, and
  • RR is a finite set of rules, with each rule of the form X→ωX \to \omega, where X∈NX \in N and ω∈(N∪Σ)+\omega \in (N \cup \Sigma)^+.

Computation. Let u,v,w∈(N∪Σ)∗u, v, w \in (N \cup \Sigma)^*.

  • Say that uAvuAv yields uwvuwv, written uAv⇒uwvuAv \Rightarrow uwv, if there exists a rule A→wA \to w of the grammar,
  • Say that uu derives vv, written u⇒∗vu \stackrel{*}{\Rightarrow} v, if u=vu = v or if a sequence u1,u2,…,uku_1, u_2, \ldots, u_k exists for k≥0k \ge 0 and
u⇒u1⇒u2⇒…⇒uk⇒v. u \Rightarrow u_1 \Rightarrow u_2 \Rightarrow \ldots \Rightarrow u_k \Rightarrow v.

The language of the grammar is {w∈Σ∗∣S⇒∗w}\{ w \in \Sigma^* | S \stackrel{*}{\Rightarrow} w \}.

Chomsky Normal Form. A context-free grammar ⟨N,Σ,S,R⟩\langle N, \Sigma, S, R\rangle is in chomsky normal form if each rule is of one of the two forms

  • X→YZX \to YZ, where X∈NX \in N, and Y,Z∈N∖{S}Y, Z \in N \setminus \{ S \},
  • X→aX \to a, where X∈N,a∈ΣX \in N, a \in \Sigma.

In addition, we permit S→ϵS \to \epsilon, where SS is the start variable.

Equivalence between Context-Free Grammar and Pushdown Automata

  1. For every context-free grammar GG, there is an automaton MM, such that MM recognizes the language generated by GG.

  2. If a pushdown automaton recognizes some language, then it is context-free.

Proof: For the convenience of proof, we modify a pushdown automaton M=⟨Q,Σ,Γ,δ,S,F⟩M = \langle Q, \Sigma, \Gamma, \delta, S, F \rangle to give it three features:

  • MM accepts by entering empty stack.
  • These is only one accept state qaccpetq_{accpet} in FF.
  • Each transition either pushes a symbol onto the stack or pops one off the stack, but it does not do both at the same time.

The way to construct a context-free grammar GG from the push-down automaton MM is given by follow:

(1) ∀p∈Q\forall p \in Q, we put the rule Npp→ϵN_{pp} \to \epsilon in G.
(2) ∀p,q,r∈Q\forall p,q,r \in Q, we add to GG a new rule Npq→NprNrqN_{pq} \to N_{pr} N_{rq},
(3) ∀p,q,r,s∈Q\forall p, q, r, s \in Q, if ∃a,b∈Σϵ\exists a, b\in \Sigma_\epsilon, t∈Γt \in \Gamma, s.t. (r,t)∈δ(p,a,ϵ)(r,t) \in \delta(p,a,\epsilon) and (q,ϵ)∈δ(s,b,t)(q,\epsilon) \in \delta(s,b,t), then we put the rule Npq→aNrsbN_{pq} \to a N_{rs}b in GG.

It is easy to see that (1) and (2) may give rise to many meaningless grammar rules, i.e., grammar rules that will never be used to derive terminal strings. (3) seems to be the only exception at the first glance. However, it indeed does the same. Consider the following example,

pushdown automaton M2

Some of the grammar productions constructed according to (1), (2), (3) are:

grammar productions contructed according to [(1)/(2)/(3)]
Nav→NbcN_{av} \to N_{bc} [according to (3)]
Nbc→NbdNdcN_{bc} \to N_{bd}N_{dc} [according to (2)]
Nbd→NccN_{bd} \to N_{cc} [according to (3)]
Ncc→ϵN_{cc} \to \epsilon [according to (1)]
Ndc→NdtNtcN_{dc} \to N_{dt}N_{tc} [according to (2)]
Ndt→NesN_{dt} \to N_{es} [according to (3)]
Ntc→NuuyN_{tc} \to N_{uu}y [according to (3)]
Nuu→ϵN_{uu} \to \epsilon [according to (1)]
... ...

Heading down this way, we can get the grammar for pushdown automaton M1M1.
But following (3), we also get productions like:

Ndn→NemN_{dn} \to N_{em}
Nek→NfjN_{ek} \to N_{fj}

These rules are useless. They can not derive any string of terminals. To understand the real cause of the problem, take Ndn→NemN_{dn} \to N_{em} for example. When the push down automaton goes from state qdq_d to state qeq_e, it pushes a YY into the stack. And a YY is poped during the transition from state qmq_m to qnq_n. But the YY poped is not the same YY pushed during the transition from state qdq_d to qeq_e. At state qnq_n, the stack does not recover to its initial state before the automaton entering state qdq_d. Some additional non-terminal symbols are still in the stack.

Now let's look at another automaton M2 that recognizes the same language as M1.

pushdown automaton M1

Interestingly, such unnecessary rules disappear in M2M2. See the following rules:

grammar productions contructed according to [(1)/(2)/(3)]
Ndc→NdjNjcN_{dc} \to N_{dj}N_{jc} according to (2)
Ndj→NecN_{dj} \to N_{ec} according to (3)
Njc→NccyN_{jc} \to N_{cc}y according to (3)

When the push down automaton goes from state qdq_d to state qeq_e, it pushes a YY into the stack. And a YY is poped during the transition from state qjq_j to qcq_c. When entering state qjq_j, the stack may not be what it is before going to state qdq_d. But the rule Ndj→NecN_{dj} \to N_{ec} is valid. It can indeed derive terminal strings.

Actually, M2M2 will enter qjq_j twice, with XY$XY\$ and $\$ in the stack respectively. In the former case, the automaton will loop on qcq_c. It enters the accept state until $\$ is left on the stack.

The grammar corresponding to M2M2:

Nak→NbcN_{ak} \to N_{bc}
Nbc→NbdNdcN_{bc} \to N_{bd}N_{dc}
Nbd→NccN_{bd} \to N_{cc}
Ndc→NdjNjcN_{dc} \to N_{dj}N_{jc}
Ndj→NecN_{dj} \to N_{ec}
Nec→NeiNicN_{ec} \to N_{ei}N_{ic}
Nei→NfcN_{ei} \to N_{fc}
Nfc→NfjNjcN_{fc} \to N_{fj}N_{jc}
Nfj→NgcN_{fj} \to N_{gc}
Ngc→NgiNicN_{gc} \to N_{gi}N_{ic}
Ngi→NhcN_{gi} \to N_{hc}
Nhc→NccaN_{hc} \to N_{cc}a
Njc→NccyN_{jc} \to N_{cc}y
Nic→NccxN_{ic} \to N_{cc}x
Ncc→ϵN_{cc} \to \epsilon

In the book Introduction To Automata Theory, Languages, and Computation, John E. Hopcroft, Rajeev Motwani, Jeffery D. Ullman give another way of converting a pushdown automaton into context-free grammar. In that way, we can avoid construct meaningless rules even for pushdown automaton M1M1. The key idea is, rule like Npq→aNrsbN_{pq} \to a N_{rs}b is constructed if and only if in state qq the stack is the same as what it is before the automaton entering state pp.

One more thing to mention about rule(3)

(3) ∀p,q,r,s∈Q\forall p, q, r, s \in Q, if ∃a,b∈Σϵ,t∈Γ\exists a, b\in \Sigma_\epsilon, t \in \Gamma, s.t. (r,t)∈δ(p,a,ϵ)(r,t) \in \delta(p,a,\epsilon) and (q,ϵ)∈δ(s,b,t)(q,\epsilon) \in \delta(s,b,t), then we put the rule Npq→aNrsbN_{pq} \to a N_{rs}b in G.

is that tt need not to be bb. We will give an example, though it is kind of weird.

pushdown automaton M3

Reference

[1] Introduction To The Theory Of Computation, 2nd Edition, by Michael Sipser