Sub topic 11
Deterministic and nondeterministic PDAs
This chapter assumes you know the PDA definition (states, stack alphabet, transition function), both acceptance modes, and the CFG-to-PDA construction from the earlier chapters. Here we ask what determinism costs. Unlike finite automata, a PDA gains real power from nondeterminism, and that fact drives many GATE questions.
What "deterministic" means for a PDA
A PDA move depends on the state, the next input symbol (or ε), and the top of the stack. A PDA is deterministic (DPDA) if, for every state q and stack symbol Z, both conditions hold:
- For each input symbol a, δ(q, a, Z) has at most one move.
- If δ(q, ε, Z) has a move, then δ(q, a, Z) is empty for every a.
Condition 2 is the one students forget. An ε-move and an input-consuming move on the same (q, Z) are a choice, so the machine is nondeterministic. Each configuration therefore has at most one successor, and each input has at most one computation.
A language is a deterministic context-free language (DCFL) if some DPDA accepts it by final state. Every DCFL is context-free, so DCFL ⊆ CFL.
A language that needs nondeterminism
Take L = { w wᴿ : w ∈ {a, b}* }, the even-length palindromes. A PDA pushes the first half and then pops while matching the second half. It must switch from pushing to popping exactly at the middle. It cannot see the middle, because the input abba and the prefix of abbaabba look identical at position 2. The machine has to guess.
If the string is in L, one sequence of guesses accepts it. The machine accepts if any computation accepts. The simulator below explores every computation, so it behaves like the NPDA.
def accepts_wwr(s):
"""Explore all computations of an NPDA for { w w^R } over any alphabet."""
start = ("q0", 0, ())
seen = {start}
todo = [start]
while todo:
state, i, stack = todo.pop()
if state == "q1" and i == len(s) and not stack:
return True
moves = []
if state == "q0":
moves.append(("q1", i, stack)) # epsilon: guess middle
if i < len(s):
moves.append(("q0", i + 1, stack + (s[i],))) # push
elif i < len(s) and stack and stack[-1] == s[i]:
moves.append(("q1", i + 1, stack[:-1])) # match and pop
for cfg in moves:
if cfg not in seen:
seen.add(cfg)
todo.append(cfg)
return False
for t in ["", "abba", "aa", "abab", "aba"]:
print(repr(t), accepts_wwr(t))
# '' True, 'abba' True, 'aa' True, 'abab' False, 'aba' FalseThe stack never exceeds the input length, since every push consumes a symbol. The search therefore terminates.
Now add a marker: L' = { w c wᴿ }. The c shows where the middle is, so a DPDA pushes until it reads c and then pops. L' is a DCFL, but L is not.
Final state versus empty stack for DPDAs
For NPDAs, acceptance by final state and by empty stack define the same class. For DPDAs they do not.
- A DPDA that accepts by empty stack can only accept prefix-free languages. If w is accepted with an empty stack, no longer string w·x can be accepted, because the machine has no move on an empty stack.
- So { a, ab } cannot be accepted by empty stack by any DPDA, but a DPDA with final states accepts it easily.
The class DCFL is defined by final-state acceptance. Language L = { aⁿ bᵐ : m ≥ n } shows nothing special here, but any language with a string and its proper prefix both in it (for example a* ) separates the two modes.
Comparing the classes
| Property | DCFL | CFL |
|---|---|---|
| Machine | DPDA (final state) | NPDA |
| Contains { w c wᴿ } | Yes | Yes |
| Contains { w wᴿ } | No | Yes |
| Union | Not closed | Closed |
| Intersection | Not closed | Not closed |
| Complement | Closed | Not closed |
| Concatenation, Kleene star, reversal | Not closed | Closed |
| Intersection with a regular language | Closed | Closed |
| Ambiguity | Always has an unambiguous grammar | May be inherently ambiguous |
Complement closure of DCFLs is the main asymmetry. You complete the DPDA (add a dead state and handle ε-loops), then swap accepting and non-accepting states. This fails for NPDAs, because swapping states turns "some computation accepts" into "some computation rejects", which is a different statement.
A consequence: the complement of a DCFL is a DCFL, so a CFL whose complement is not context-free cannot be a DCFL. The language { a^i b^j c^k : i = j or j = k } is a CFL but not a DCFL. Its complement would otherwise be context-free, and intersecting that with the regular language abc* would give { aⁿ bⁿ cⁿ }, which is not context-free.
Relationship to ambiguity
- Every DCFL has an unambiguous grammar.
- The converse is false. { w wᴿ } has an unambiguous grammar S → aSa | bSb | ε, but it is not a DCFL.
- Every inherently ambiguous CFL is therefore not a DCFL.
Decision-problem notes
Equivalence of two DPDAs is decidable (a result of Sénizergues). Equivalence of two NPDAs, or of two CFGs, is undecidable. Emptiness and membership are decidable for both classes.
Common errors
- Calling a PDA deterministic when it has both an ε-move and an input move on the same (q, Z).
- Assuming DPDA final-state and empty-stack acceptance are equivalent.
- Saying DCFLs are closed under union. They are closed under union with a regular language, not in general.
- Treating the lack of a visible middle marker as proof of non-determinism. You still need an argument, usually the pumping lemma or a closure argument as above.
Exercise
Is L = { aⁿ bⁿ : n ≥ 1 } ∪ { aⁿ b²ⁿ : n ≥ 1 } a DCFL? Justify your answer.
Show answer
Yes. After reading the a's, the DPDA pushes one symbol per a. It then pops one per b. If the stack empties exactly when the input ends, accept. If the input continues, the machine has to pop twice per b for the second case, but it can instead decide by a different scheme. Push two symbols per a. Read b's popping one symbol each. Accept if the stack becomes empty at the end of input (n b's would leave n symbols, so record the midpoint by having the stack hold a marker below the first n symbols). Accept at the marker (n b's) or at the empty stack (2n b's). Because the first string ends at the marker and the second continues, the machine needs only to look at the stack top and the end of input, so no guess is needed. Hence L is a DCFL. Contrast this with { aⁿ bⁿ } ∪ { aⁿ b^m c } style unions, where the branches are not distinguishable early enough.
Exercise
A DPDA M accepts L by final state. A classmate says swapping final and non-final states of M gives a DPDA for the complement of L. Name the step that must be done first.
Show answer
M must first be made complete, meaning every input has a full computation. Add a dead state for missing moves and remove infinite ε-loops. It must also handle the case where the machine passes through accepting and non-accepting states via ε-moves after the last symbol. Only then does swapping final states give the complement.