… NPDAs continued
Pushing Strings Pop symbol Input symbol Push string
Example: input pushed string stack top Push
Another NPDA example NPDA
Execution Example: Time 0 Input Stack current state
Time 1 Input Stack
Time 3 Input Stack
Time 4 Input Stack
Time 5 Input Stack
Time 6 Input Stack
Time 7 Input Stack
Time 8 Input Stack accept
Formalities for NPDAs
Transition function:
Transition function:
Formal Definition Non-Deterministic Pushdown Automaton NPDA Final states States Input alphabet Stack start symbol Transition function Initial state Stack alphabet
Instantaneous Description Current state Current stack contents Remaining input
Example: Instantaneous Description Input Time 4: Stack
Example: Instantaneous Description Input Time 5: Stack
We write: Time 4 Time 5
A computation:
For convenience we write:
Formal Definition Language of NPDA : Initial state Final state
Example: NPDA :
NPDA :
Therefore: NPDA :
NPDAs Accept Context-Free Languages
Theorem: Context-Free Languages (Grammars) Languages Accepted by NPDAs
Proof - Step 1: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any context-free grammar to a NPDA with:
Proof - Step 2: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any NPDA to a context-free grammar with:
Converting Context-Free Grammars to NPDAs
An example grammar: What is the equivalent NPDA?
Grammar: NPDA:
The NPDA simulates leftmost derivations of the grammar L(Grammar) = L(NPDA)
Grammar: A leftmost derivation:
NPDA execution: Time 0 Input Stack current state
Time 1 Input Stack
Time 2 Input Stack
Time 3 Input Stack
Time 4 Input Stack
Time 5 Input Stack
Time 6 Input Stack
Time 7 Input Stack
Time 8 Input Stack
Time 9 Input Stack
Time 10 Input Stack accept
In general: Given any grammar We can construct a NPDA With
Constructing NPDA from grammar : For any production For any terminal
Grammar generates string if and only if NPDA accepts
Therefore: For any context-free language there is an NPDA that accepts the same language
Converting NPDAs to Context-Free Grammars
For any NPDA we will construct a context-free grammar with
Intuition: The grammar simulates the machine A derivation in Grammar : Current configuration in NPDA
A derivation in Grammar : terminals variables Input processed Stack contents in NPDA
Some Necessary Modifications First, we modify the NPDA: It has a single final state It empties the stack when it accepts the input Empty Stack Original NPDA
Second, we modify the NPDA transitions: all transitions will have form or
Example of a NPDA in correct form:
The Grammar Construction In grammar : Stack symbol Variables: states Terminals: Input symbols of NPDA
For each transition We add production
For each transition We add production For all states
Stack bottom symbol Start Variable: Start state final state
Example: Grammar production:
Example: Grammar productions:
Example: Grammar production:
Resulting Grammar:
Derivation of string
In general, in Grammar: if and only if is accepted by the NPDA
Explanation: By construction of Grammar: if and only if in the NPDA going from to the stack doesn’t change below and is removed from stack