How to convert CFG to NPDA
Overview
A pushdown automaton (PDA) is a finite automaton with a stack for memory purposes. A nondeterministic pushdown automaton (NPDA) has one, or more than one, transition for each input that it reads.
A context-free grammar (CFG) is the generator of a language, whereas a PDA and NPDA are the recognizers of a non-regular language.
Components of an NPDA
A PDA is defined by six tuples
is the set of all the states is the language or the input alphabet is the stack alphabet is the transition function and is the start state and is the final state and
Steps to convert CFG to NPDA
To convert a CFG to an NPDA, we follow these steps:
- The stack is initially empty. Make the first transition as
. - Before reading or popping the CFG, push the start variable on the stack.
- Read nothing from the CFG in case of the start variable, pop it from the top of the stack, and push all the productions that the start variable produces. In the same step, read all the inputs of the CFG, pop, and push from the top of the stack according to the grammar rules mentioned in the CFG. This step transitions into a self-loop until all productions are read, popped, and pushed according to the CFG.
- When there are no more productions to be read as input, pop the
symbol and push nothing. Make the last state the final state.
Examples
The components of making a PDA are writing the steps that construct the PDA and the pushdown automaton itself.
Let's take an example of the following CFG:
Step 1: Push as the stack symbol
Input nothing from the CFG, pop nothing from the stack as it is already empty, and push
Stack view
NPDA view
Step 2: Push
Push
Stack view
NPDA view
Step 3: Self-loop state
Read all possible inputs from the CFG, pop the same input from the stack, and push the productions they produce accordingly.
In the case of the start variable, as it is already on the top of the stack initially, pop it and push all the productions it creates.
Stack view
NPDA view
Step 4: Pop
After all the productions have been read, pushed, and popped, only the
Stack view
NPDA view
This is the final form of the NPDA formed by the CFG, where:
Note: To learn more about PDA, we can visit here.
Free Resources