1. Gyanastra
  2. Theory of Computation
  3. Pushdown Automata
  4. Conversion of CFG in GNF to Pushdown Automata
Free lesson, no account needed 6 min read 14 practice questions after it
Asked in past papers14 times
  • GATE CSE202120152005200420021999199819941992
  • ISRO2017
  • UGC NET2022201820132012

Theory of Computation · Pushdown Automata

Conversion of CFG in GNF to Pushdown Automata

Overview

Greibach Normal Form (GNF) allows a context-free grammar (CFG) to be converted directly into a pushdown automaton (PDA), because every GNF production consumes exactly one input terminal.
The construction uses three states: an initial state that seeds the stack, a working state that simulates the productions, and a final state reached when the input is exhausted.

Key concepts

  • GNF production form: every production is A→aαA \to a\alpha, where aa is a terminal and α\alpha is a (possibly empty) string of variables. This guarantees one input symbol consumed per production applied.
  • Bottom-of-stack marker Z0Z_0: marks the bottom of the stack and is used to detect when the stack has been reduced to its base.
  • Start symbol SS: pushed onto the stack at the beginning (or AA, if AA is the start symbol).
  • States: Q0Q_0 (initial, pushes the start symbol), Q1Q_1 (simulates productions), QFQ_F (final).
  • Acceptance: by final state together with empty stack; leaving Z0Z_0 in place is sufficient, and emptying it with epsilon is also correct.

How it works

  1. Push the start symbol. In Q0Q_0, on epsilon input with Z0Z_0 on top, push the start symbol SS above Z0Z_0 and move to Q1Q_1: (Q0,ϵ,Z0)→(Q1,SZ0)(Q_0, \epsilon, Z_0) \to (Q_1, SZ_0). Q0Q_0 does nothing with the input.
  2. Simulate every GNF production in Q1Q_1. For each production A→aαA \to a\alpha, add a transition that, in Q1Q_1 on input aa with AA on top, stays in Q1Q_1 and replaces AA by α\alpha (pop AA, push α\alpha). Likewise for A→bβA \to b\beta: (Q1,b,A)→(Q1,β)(Q_1, b, A) \to (Q_1, \beta).
    Here α\alpha and β\beta may be epsilon or one or more variables.
  3. Add the final state. From Q1Q_1, on epsilon input with Z0Z_0 on top (input over, bottom of stack reached), go to QFQ_F: (Q1,ϵ,Z0)→(QF,Z0)(Q_1, \epsilon, Z_0) \to (Q_F, Z_0), or (QF,ϵ)(Q_F, \epsilon) if the stack is emptied.

The grammar must be in GNF before this procedure is applied.

Figure. Q0 pushes S above Z0 and moves to Q1. Each GNF production becomes a Q1 self loop consuming one terminal, and Q1 reaches QF on epsilon with Z0 on top.

Formulas

  • GNF production form: A→aαA \to a\alpha — aa is a terminal, α\alpha is a string of variables, possibly epsilon.

  • Step 1: (Q0,ϵ,Z0)→(Q1,SZ0)(Q_0, \epsilon, Z_0) \to (Q_1, SZ_0) — SS is the start symbol, Z0Z_0 is the bottom-of-stack marker.

  • Step 2 for A→aαA \to a\alpha: (Q1,a,A)→(Q1,α)(Q_1, a, A) \to (Q_1, \alpha) — pop AA, push α\alpha.

  • Step 2 for A→bβA \to b\beta: (Q1,b,A)→(Q1,β)(Q_1, b, A) \to (Q_1, \beta).

  • Step 3: (Q1,ϵ,Z0)→(QF,Z0)(Q_1, \epsilon, Z_0) \to (Q_F, Z_0), or (QF,ϵ)(Q_F, \epsilon) if the stack is emptied.

  • PDA from GNF grammar

  • The three-state PDA construction: Q0 seeds the stack with the start symbol, Q1 simulates each GNF production while consuming one terminal, and QF is reached when input is over and Z0 is on top.

Worked examples

Convert a grammar to GNF, then to a PDA.

Given grammar (two productions), not in GNF because a production mixes variables and terminals (a terminal followed by a string containing both variables and terminals):

  • S→aSbS \to aSb
  • S→abS \to ab

Convert to GNF: introduce a new variable BB with B→bB \to b, and rewrite:

  • S→aSBS \to aSB
  • S→aBS \to aB
  • B→bB \to b

Every production is now a terminal followed by only variables, so the grammar is in GNF.

Build the PDA.

Step 1 — push start symbol:

StateInputStack topMoveNew stateStack action
Q0Q_0epsilonZ0Z_0push SS above Z0Z_0Q1Q_1Z0→SZ0Z_0 \to SZ_0

Step 2 — one transition per production:

StateInputStack topMoveNew stateStack action
Q1Q_1aaSSapply S→aSBS \to aSBQ1Q_1S→SBS \to SB (replace SS by SBSB)
Q1Q_1aaSSapply S→aBS \to aBQ1Q_1S→BS \to B (replace SS by BB)
Q1Q_1bbBBapply B→bB \to bQ1Q_1B→ϵB \to \epsilon (push nothing)

Step 3 — acceptance:

StateInputStack topMoveNew stateStack action
Q1Q_1epsilonZ0Z_0input over, bottom of stack reachedQFQ_Fleave Z0Z_0 (or empty it)

Revision summary

  • GNF makes CFG-to-PDA conversion direct: each production consumes exactly one input terminal.
  • Always convert the grammar to GNF first; a production like S→aSbS \to aSb is not in GNF and must be rewritten by introducing new variables (e.g., B→bB \to b).
  • The three steps: (1) push the start symbol from Q0Q_0 to Q1Q_1, (2) for each production A→aαA \to a\alpha add (Q1,a,A)→(Q1,α)(Q_1, a, A) \to (Q_1, \alpha), (3) add (Q1,ϵ,Z0)→(QF,Z0)(Q_1, \epsilon, Z_0) \to (Q_F, Z_0).
  • α\alpha and β\beta in a GNF production may be epsilon or one or more variables.
  • The final move gives acceptance by final state together with empty stack; leaving Z0Z_0 in place is sufficient, and emptying it is also allowed.

Previous-year exam insights

  • A language is accepted by some PDA by final state iff it is accepted by some PDA by empty stack; the two classes of accepted languages are identical (both equal the context-free languages).
    Asked inGATE 1999UGC NET 2018
  • Nondeterministic PDAs are strictly more powerful than deterministic PDAs: there is no algorithm to convert an NPDA into an equivalent DPDA, and some context-free languages are not deterministic context-free.
    Asked inGATE 1998GATE 1994GATE 1992ISRO 2017
  • A PDA whose stack is bounded to a fixed finite number of symbols is equivalent to a finite automaton, so it accepts exactly the regular languages.
    Asked inGATE 2002
  • Every regular language (and every NFA/DFA) can be converted to an equivalent PDA; the context-free languages properly contain the regular languages.
    Asked inISRO 2017UGC NET 2018
  • In the standard transition notation (q, Y) in delta(p, a, X), the PDA reads input a (or epsilon), pops X from the stack top, moves from p to q, and pushes the string Y (Y = epsilon means pop only;
    X = epsilon means no pop).
    Asked inGATE 2021GATE 2015GATE 2004
  • The complement of every context-free language is recursive (context-free languages are recursive, and recursive languages are closed under complement), even though CFLs are not closed under complement.
    Asked inISRO 2017
Pages 1–2 of 2