Sub topic 11
Deterministic and Nondeterministic PDAs
Assume you know context-free grammars, parse trees, finite automata, and the basic definition of a pushdown automaton (PDA). A PDA has a finite control state and a stack. The stack gives it memory that can grow with the input.
Two kinds of choices
A nondeterministic PDA (NPDA) may have several possible moves for the same combination of:
- current state,
- next input symbol, or an epsilon move,
- top stack symbol.
It accepts an input if at least one sequence of choices reaches an accepting configuration. Other branches may fail.
A deterministic PDA (DPDA) has at most one possible move in every configuration. In particular, it cannot allow both an input-consuming move and an epsilon move when they apply to the same state and stack-top symbol. It must also know when an important boundary occurs.
Expressive power
Every language accepted by a DPDA is context-free. However, not every context-free language is accepted by a DPDA.
| Machine | Language family | Main ability |
|---|---|---|
| DPDA | Deterministic context-free languages | One computation for each input |
| NPDA | Context-free languages | Several possible computations |
Thus,
\[ \text{DCFL} \subsetneq \text{CFL}. \]
The inclusion is strict. For example, the language
\[ L=\{ww^R \mid w\in\{0,1\}^*\} \]
contains even-length palindromes. An NPDA can guess the midpoint, push the first half, and then pop while matching the reverse half. A DPDA cannot determine the midpoint from the input alone. If a separator is added, as in \(\{w\#w^R\}\), the separator tells a DPDA exactly when to switch from pushing to popping.
A context-free language can have an ambiguous grammar and still be deterministic. Ambiguity in one grammar does not prove that the language is outside DCFL. The issue is whether some deterministic parsing strategy exists.
Worked deterministic example
For \( \{a^n b^n\mid n\geq1\} \), a DPDA pushes one stack marker for every a. When the first b appears, it switches permanently to popping mode.
def accepts_anbn(word):
stack = []
mode = "push"
for symbol in word:
if mode == "push" and symbol == "a":
stack.append("A")
elif symbol == "b" and stack:
mode = "pop"
stack.pop()
else:
return False
return mode == "pop" and not stack
print(accepts_anbn("aaabbb")) # True
print(accepts_anbn("aab")) # False
print(accepts_anbn("abab")) # FalseThe first b is an unambiguous signal. No guess is needed.
GATE strategy
When classifying a language, ask whether the machine must guess a boundary, matching point, or structural choice. A visible delimiter may make the choice deterministic. If successful recognition requires trying several possible choices, an NPDA may be necessary.
Exercise
Is every language accepted by a DPDA also accepted by an NPDA, and are the two machine types equally powerful?
Show answer
Every DPDA can be simulated by an NPDA, so DCFL is contained in CFL. The containment is strict: some context-free languages, such as unmarked even palindromes, require nondeterminism.