Conversion of CFG in GNF to NPDA
A Nondeterministic Pushdown Automaton (NPDA) is a computational model that extends a finite automaton with a stack, giving it the ability to recognize context-free languages (CFLs).
A Context-Free Grammar (CFG) is a formal grammar that generates CFLs, whereas an NPDA recognizes them. Since both CFGs and NPDAs describe the same class of languages, any CFG can be systematically converted into an equivalent NPDA.
When the CFG is expressed in Greibach Normal Form (GNF), the conversion process to NPDA becomes straightforward:
In GNF, every production is of the form:
[ A \rightarrow a\alpha ]
where:Ais a variable (nonterminal),ais a terminal,αis a (possibly empty) string of variables.
This structure ensures that every derivation step begins with a terminal, allowing the NPDA to consume one input symbol while manipulating the stack.
Conversion of CFG (GNF) → NPDA
To construct an NPDA ( M ) from a CFG ( G = (V, Σ, R, S) ):
Initialization
- Start in the initial state
q0. - Push the start variable
Sonto the stack.
- Start in the initial state
Variable Expansion
- If the top of the stack is a variable
A, and there exists a productionA → aα:- On reading terminal
a, popAfrom the stack. - Push the string
α(with the rightmost symbol on top).
- On reading terminal
- If the top of the stack is a variable
Terminal Matching
- If the top of the stack is a terminal
aand the current input symbol is alsoa:- Consume
afrom the input. - Pop
afrom the stack.
- Consume
- If the top of the stack is a terminal
Acceptance
- The NPDA accepts the input if the entire string is read and the stack becomes empty.