The construction of a PDA from a CFG on wikipedia (1) is like a nice exercise for implementing a minimal-and-slow-but-functional parsing algorithm. I have a question about termination of the PDA that is constructed. Specifically, given an input that is not in the language accepted by the PDA, is it guaranteed that the PDA will terminate?

For example, given the following CFG, which accepts an a followed by a string of b's:

A -> Ab
A -> a

Obviously, the string "b" is not accepted by this grammar. I suspect that if you run the PDA encoding of this CFG with input "b", the PDA will infinitely expand the A. I think this will happen because expanding a non-terminal does not consume an input token, so this process can just repeat. Am I missing a subtle part of the encoding, or is this encoding indeed sound but incomplete, in the sense that it accepts words in the language, but is not guaranteed to reject words not in the language?


2 Answers 2


You are right.

PDA are nondeterministic automata. As such, they are imaginary concepts that do not and cannot exist in the real world -- "magic".

One standard way to implement/simulate nondeterminism is by executing all paths (e.g., using fork()). If you do that, then you are correct, the (simulation of the) PDA might not terminate. The issue is cycles of $\epsilon$-transitions (you can even have cycles of $\epsilon$-transitions that push another symbol to the stack at each step).

This also means that on inputs where the PDA does ultimately accept, the running time can potentially be horrible, for most natural ways of simulating the automaton. In practice, non-termination and exponential running time are both pretty much equally bad.

For that reason and other reasons, most programming languages do not express their syntax with arbitrary CFGs. Instead they use some special subclass of CFGs (e.g., LL(1), LR(1)) for which we can come up with a translation and simulation algorithm that guarantees termination and guarantees efficient running times.

Or, if we do have an arbitrary CFG, we use parsing algorithms that are designed to be reasonably efficient for all inputs. This automatically ensures termination. e.g., https://en.wikipedia.org/wiki/CYK_algorithm, https://en.wikipedia.org/wiki/GLR_parser, https://en.wikipedia.org/wiki/Earley_parser, Are there any algorithms that decide if a PDA (pushdown automaton) accepts a sentence?

Loosely related: https://cstheory.stackexchange.com/q/32271/5038


is this encoding indeed sound but incomplete, in the sense that it accepts words in the language, but is not guaranteed to reject words not in the language?

A PDA is not a "decider". As a nondeterministic automaton the PDA does not "reject" strings. Its language consists of all strings that can be accepted by the automaton. In the same way that the language of a grammar consists of all strings for which there is a derivation. As D.W. writes in his answer, to understand which words are not accepted by the automaton (or not generated by the grammar) we need parsing techniques.

In this sense the construction is both sound and complete. A string is accepted by the PDA if and only if there exists a derivation by the original context-free grammar. As is hinted at in wikipedia, computations of the PDA are in one-to-one correspondence with leftmost derivations in the grammar (or equivalently, pre-order traversals of derivation trees).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.