Sub topic 9
Acceptance by final state and by empty stack
This chapter assumes you know the PDA tuple (states, input alphabet, stack alphabet, transition function, start state, start stack symbol) and how a single move changes an instantaneous description (ID). An ID is a triple (state, remaining input, stack). Here you will learn the two standard rules for deciding when a PDA accepts, how to convert between them, and where they stop being equivalent.
The two acceptance conditions
A PDA reads its whole input along some sequence of moves. The acceptance mode decides which end configurations count as success.
- Acceptance by final state: L(P) is the set of strings w such that (q0, w, Z0) ⊢* (f, ε, γ) for some final state f and any stack γ.
- Acceptance by empty stack: N(P) is the set of strings w such that (q0, w, Z0) ⊢* (q, ε, ε) for any state q. A PDA used this way needs no final states.
In both modes the input must be fully consumed. A PDA is nondeterministic, so w is accepted if at least one computation path succeeds. The other paths can get stuck or end badly without affecting the result.
| Mode | Needs final states? | Stack at the end | State at the end |
|---|---|---|---|
| Final state | Yes | Anything | Must be in F |
| Empty stack | No | Must be empty | Anything |
A worked example: a^n b^n, n ≥ 1
The PDA below uses states q0 (reading a's) and q1 (reading b's), with start stack symbol Z.
- δ(q0, a, Z) = {(q0, AZ)} and δ(q0, a, A) = {(q0, AA)}: push one A per a.
- δ(q0, b, A) = {(q1, ε)} and δ(q1, b, A) = {(q1, ε)}: pop one A per b.
- δ(q1, ε, Z) = {(q1, ε)}: when only Z is left, pop it.
On aabb the moves are: (q0, aabb, Z) ⊢ (q0, abb, AZ) ⊢ (q0, bb, AAZ) ⊢ (q1, b, AZ) ⊢ (q1, ε, Z) ⊢ (q1, ε, ε). The stack is empty after all input is read, so aabb is in N(P).
For the final-state version, replace rule 3 with δ(q1, ε, Z) = {(q2, Z)} and declare q2 final. The same trace now ends in q2.
The simulator below checks both versions. It explores all configurations breadth-first, so it handles nondeterminism. The stack is a string with its top at the left, and limit stops ε-moves from growing the stack forever.
from collections import deque
def run_pda(delta, start, z0, finals, w, mode, limit=1000):
"""delta maps (state, input_symbol_or_'', stack_top) to a set of
(new_state, push_string). push_string has its top symbol first;
'' means pop. mode is 'final' or 'empty'."""
first = (start, 0, z0)
seen = {first}
queue = deque([first])
while queue:
q, i, stack = queue.popleft()
if i == len(w):
if mode == "final" and q in finals:
return True
if mode == "empty" and stack == "":
return True
if stack == "":
continue # no move is possible without a top symbol
top, rest = stack[0], stack[1:]
moves = []
if i < len(w):
for p, push in delta.get((q, w[i], top), ()):
moves.append((p, i + 1, push + rest))
for p, push in delta.get((q, "", top), ()):
moves.append((p, i, push + rest))
for cfg in moves:
if cfg not in seen and len(cfg[2]) <= limit:
seen.add(cfg)
queue.append(cfg)
return False
base = {
("q0", "a", "Z"): {("q0", "AZ")},
("q0", "a", "A"): {("q0", "AA")},
("q0", "b", "A"): {("q1", "")},
("q1", "b", "A"): {("q1", "")},
}
empty_delta = {**base, ("q1", "", "Z"): {("q1", "")}}
final_delta = {**base, ("q1", "", "Z"): {("q2", "Z")}}
for w in ["aabb", "aab", "abab", ""]:
print(repr(w),
run_pda(empty_delta, "q0", "Z", set(), w, "empty"),
run_pda(final_delta, "q0", "Z", {"q2"}, w, "final"))
# 'aabb' True True
# 'aab' False False
# 'abab' False False
# '' False FalseConverting between the modes
Both conversions use a new bottom-of-stack marker X0. Without it, the original PDA could empty its stack by accident, or the new PDA could have no symbol left to make a move.
Empty stack to final state. Given P with N(P) = L, build P' as follows:
- Add a new start state p0 and a new final state pf.
- Add the move δ(p0, ε, X0) = {(q0, Z0X0)}. P' now runs P above the marker.
- For every state q of P, add δ(q, ε, X0) = {(pf, ε)}. P emptying its own stack means X0 becomes the top.
Final state to empty stack. Given P with L(P) = L, build P' as follows:
- Add a new start state p0 and a drain state p.
- Add δ(p0, ε, X0) = {(q0, Z0X0)}.
- For every final state f and every stack symbol Y (including X0), add δ(f, ε, Y) = {(p, ε)}.
- Add δ(p, ε, Y) = {(p, ε)} for every Y.
The marker X0 is what makes step 3 safe. P cannot reach an empty stack by itself, because X0 stays below its stack. P' empties its stack only after entering p.
Both directions give the same class of languages for nondeterministic PDAs. This is why the textbook equivalence of PDAs and CFGs can use whichever mode is more convenient. The CFG-to-PDA construction usually uses empty stack.
Where the equivalence fails: deterministic PDAs
For deterministic PDAs the two modes differ. Final-state DPDAs accept the deterministic context-free languages (DCFLs). Empty-stack DPDAs accept only the DCFLs that have the prefix property: no string in the language is a proper prefix of another string in it.
The reason is mechanical. After a DPDA empties its stack, it has no top symbol and cannot move. If w is accepted by empty stack, no longer string w·x can also be accepted, because the machine stopped at w.
The closing marker trick fixes this. Appending an end marker such as $ to every string makes any DCFL prefix-free, which is why parsers often add an end-of-input symbol.
Common errors
- Forgetting that input must be fully consumed. Reaching a final state or an empty stack midway does not count.
- Requiring both conditions. A PDA accepts by one chosen mode, not both together.
- Accepting ε by accident. In the empty-stack PDA above, ε is rejected because Z remains. If a PDA starts with Z0 and accepts ε by empty stack, it must have a move δ(q0, ε, Z0) = {(q, ε)}.
- Treating the modes as equal for DPDAs. They are equal for NPDAs only.
- Dropping the new bottom marker in conversions. Without X0, the final-to-empty construction can reject strings the original accepted, or the empty-to-final one can miss the moment the stack empties.
GATE-style pattern
Questions often give a PDA and ask for L(P) or N(P) for the same machine. These can differ. In the example above, the same transitions with q1 declared final and no pop of Z would accept a^n b^n by final state, yet its stack would still hold Z at the end. Trace one accepted string in each mode, then check one boundary string (ε or a proper prefix) before choosing an answer.
Exercise
Can a deterministic PDA accept the language a* by empty stack? Can one accept it by final state? Explain both answers.
Show answer
By final state, yes: a single start state that is also final, with a move that pushes or ignores each a, accepts every a^n, including ε. By empty stack, no. The string a would empty the stack, leaving the DPDA unable to move, so it could not also accept aa, which has a as a proper prefix. Empty-stack DPDAs accept only prefix-free languages.
Exercise
In the final-state to empty-stack conversion, why is a new bottom marker X0 needed? Describe a case where the construction fails without it.
Show answer
The original PDA P may empty its stack on a string that it does not accept by final state, for example by popping its last symbol in a non-final state. Without X0, the converted PDA would then accept that string by empty stack, so it would accept a larger language. The marker keeps the stack non-empty until the new drain state p is entered, which happens only from a final state.