A langauge is context-free iff a PDA recognizes it.
Top-Down Approach
Simulates left-most derivation.
Suppose a CFG is given. The corresponding PDA is defined as where:
Only 3 states are enough.- (input alphabet) is the same as of
Stack contains the terminals, the non-terminals and .
And is defined as follows:
| Configuration | Next State | Stack Top Replacement | Description |
|---|---|---|---|
| Move to and push start symbol to the stack | |||
| Only move to from (not ) when stack has only | |||
Bottom-Up Approach
Simulates right-most derivation in reverse.
Suppose . The corresponding PDA where:
Only 3 states are enough.- is the same as CFG
Stack contains the terminals, the non-terminals and .
And the transition function is defined as:
| Configuration | Next State | Stack Replacement | Description |
|---|---|---|---|
| Move to working state. | |||
| Push input symbol onto stack. | |||
| If . Reverse of production. Main step of bottom-up parsing. | |||
| Accept when reduced to start symbol. |