← Learn
Converter · step by step

Context-free grammar to PDA

Type a grammar and get the pushdown automaton that accepts the same language — the standard three-state construction, where the stack carries out a leftmost derivation. Then run strings through it and watch the stack.

Try it — type any grammar

One rule per line, alternatives separated by |. Uppercase letters are variables, ε is the empty string, and the first rule’s variable is the start symbol.

1/4
try
PDA · being built
ε,z₀/S,z₀ε,z₀/S,z₀q0q1q2
Three states
q0 pushes the start symbol: ε, z₀ / Sz₀. All the work then happens in q1, and q2 accepts.
How it works
1

Push the start symbol

The first move puts S on the stack above the bottom marker z₀, without reading input.

2

A variable on top is replaced by one of its right-hand sides

For every rule A → α there is a move that pops A and pushes α, reading nothing. The machine guesses which rule to use — that is the nondeterminism.

3

A terminal on top is matched against the input

If the top of the stack is a, the next input symbol must be a; both are consumed.

4

Accept when only z₀ is left

Every variable has been expanded and every terminal matched, so the input was derived by the grammar.

Questions

Why is the result nondeterministic?

Because a grammar does not say which rule to apply next. The PDA tries all of them, so the simulator runs it as an NPDA and shows every live stack.

My grammar makes the run stop with “too many configurations”

Rules that grow the stack without reading input — left recursion such as S → Sa, or S → SS — let the machine expand forever. Rewrite them, for example S → (S)S | ε instead of S → SS | (S) | ε.

What can I use as symbols?

Single characters. Uppercase letters are variables; anything else is a terminal, except the comma, slash and semicolon, which the simulator uses inside transition labels.

More step-by-step tools