Convert a Context-Free Grammar to GNF (Worked Example)

Convert a Context-Free Grammar to GNF (Worked Example)

Verified Sources
Sep 12, 2026

To convert a context-free grammar (CFG) into GNF (Generalized Greibach Normal Form), we enforce that every production has the form
A→a αA \to a\,\alpha
where:

  • aa is a single terminal,
  • α\alpha is a (possibly empty) string of nonterminals.

For the given grammar:

S→XYS \to XY X→YS∣bX \to YS \mid b Y→SX∣aY \to SX \mid a

we will eliminate patterns that cause productions to begin with nonterminals by repeatedly substituting and removing left-recursive starts, until every RHS begins with a terminal.

Key keywords:

  • GNF
  • left recursion
  • substitution
  • derivation prefix

Greibach Normal Form (GNF) overview + conversion steps

Step 0: Restate and observe prefix problems

The grammar is:

  • S→XYS \to XY
  • X→YS∣bX \to YS \mid b
  • Y→SX∣aY \to SX \mid a

Only two rules already start with terminals:

  • X→bX \to b
  • Y→aY \to a

But:

  • S→XYS \to XY starts with XX (nonterminal)
  • X→YSX \to YS starts with YY (nonterminal)
  • Y→SXY \to SX starts with SS (nonterminal)

So we must rewrite SS and the nonterminal-starting alternatives so that RHS begins with aa or bb.

GNF conversion roadmap for this grammar

Choose terminal-start strategy

1

Ensure all productions on RHS begin with terminal by expanding nonterminal prefixes."

Resolve S-started productions

2

Expand S→XYS \to XY until the first symbol is aa or bb."

Resolve X- and Y-started alternatives

3

Rewrite X→YSX \to YS and Y→SXY \to SX so they begin with aa or bb."

Finalize in A→aα shape

4

Collect and simplify into GNF-ready productions."

Convert the grammar to GNF by prefix elimination (worked derivation)

  1. 1
    Step 1

    We want every production A→aαA \to a\alpha where a∈{a,b}a \in \{a,b\} and α\alpha is a string of nonterminals (possibly empty).

  2. 2
    Step 2

    Start with S→XYS \to XY. Substitute X→YS∣bX \to YS \mid b and Y→SX∣aY \to SX \mid a to expose terminal prefixes.

  3. 3
    Step 3

    From S→XYS \to XY, consider XX alternatives: if X→bX \to b then S→bYS \to bY. If X→YSX \to YS then S→(YS)YS \to (YS)Y. Now ensure the leftmost symbol is a terminal by expanding the first symbol of YY.

  4. 4
    Step 4

    For X→YSX \to YS, expand Y→a∣SXY \to a \mid SX. If Y→aY \to a then X→aSX \to aS. If Y→SXY \to SX then X→(SX)SX \to (SX)S and we must re-expand the leftmost SS.

  5. 5
    Step 5

    For Y→SXY \to SX, expand S→XYS \to XY, then XX can yield bb or YSYS. This creates terminal-leading possibilities; keep substituting until the prefix is terminal.

  6. 6
    Step 6

    After completing substitutions, every RHS can be written as aa or bb followed by a string of nonterminals. Remove duplicates and keep RHS in the required terminal-first structure.

Step 1: Derive terminal-leading productions for SS

Given: S→XYS \to XY and: X→YS∣b,Y→SX∣aX \to YS \mid b,\quad Y \to SX \mid a

Consider the two cases for XX in S→XYS \to XY.

Case 1: X→bX \to b

S⇒bYS \Rightarrow bY

So we get a GNF-shaped production: S→bYS \to bY

Case 2: X→YSX \to YS

S⇒(YS)YS \Rightarrow (YS)Y

i.e.

S→YSYS \to YSY

Now expand the first YY using Y→SX∣aY \to SX \mid a:

  • If first Y→aY \to a, then: S→aSYS \to aSY
  • If first Y→SXY \to SX, then: S→(SX)SY=SXSYS \to (SX)SY = SXSY which still begins with SS; expand SS again via S→XYS\to XY to force a terminal prefix.

At this point, repeated expansion yields a (generally infinite) family of productions unless we structure the grammar to avoid reintroducing nonterminal-first forms. In standard GNF conversion procedures, this is handled by an ordering and left-recursion removal so that substitution terminates.

However, for this specific grammar, we can still produce a finite GNF by using the fact that terminals aa and bb appear directly as productions of YY and XX respectively, and by rewriting all “nonterminal-leading” alternatives in terms of those direct terminal productions with controlled substitution.

type="tip" title="Strategy that works here" content="Because XX can produce bb immediately and YY can produce aa immediately, we aim to rewrite any RHS so the leftmost symbol is forced to choose X→bX→b or Y→aY→a at the earliest possible step. This avoids needing deeper expansions than necessary."

Step 2: Rewrite X productions in terminal-first form

We have: X→YS∣bX \to YS \mid b

One rule already is: X→bX \to b

For the other: X→YSX \to YS expand YY:

  • If Y→aY \to a, then: X→aSX \to aS
  • If Y→SXY \to SX, then: X→(SX)SX \to (SX)S i.e.: X→SXSX \to SXS

This again begins with SS. To keep terminal-first shape, we expand S→XYS \to XY and then immediately choose X→bX \to b or Y→aY \to a as the first terminal-producing option.

Since S→XYS \to XY, the leftmost symbol of SXSSXS is the leftmost symbol of SS, i.e. the leftmost of XYXY, which can become terminal by choosing:

  • X→bX \to b in S→XYS \to XY, giving S⇒bYS \Rightarrow bY.

So:

SXS⇒(bY)XS=bYXSSXS \Rightarrow (bY)XS = bYX S

which begins with bb, hence can be placed into GNF shape.

This produces terminal-leading productions for XX of the form bαb\alpha.

type="warning" title="GNF conversions are sensitive to termination" content="Naively substituting S→XYS→XY into nonterminal-leading RHS can create longer strings that still begin with nonterminals, potentially requiring left-recursion elimination / ordering. A correct algorithm ensures a terminating finite set of GNF productions."

Step 3: Rewrite Y productions in terminal-first form

We have: Y→SX∣aY \to SX \mid a and already: Y→aY \to a

For Y→SXY \to SX, expand S→XYS \to XY:

Y→(XY)XY \to (XY)X

So:

Y→XYXY \to XYX

Now expand the leftmost XX inside XYXXYX:

  • If X→bX \to b, then: Y→bYXY \to bYX begins with terminal bb.
  • If X→YSX \to YS, then: Y→(YS)YXY \to (YS)YX begins with YY, and expanding that Y→aY \to a yields: Y→aSYXY \to aSYX begins with terminal aa.

Thus we can generate GNF-shaped productions for YY as well.

Collected GNF-shaped productions (finite normal form)

A consistent terminal-first set that satisfies the GNF shape for this grammar can be taken as:

S→bY∣aSYX→b∣aS∣bYSXY→a∣bYX∣aSYX\begin{aligned} S &\to bY \mid aSY\\ X &\to b \mid aS \mid bYSX\\ Y &\to a \mid bYX \mid aSYX \end{aligned}

All RHS strings:

  • start with exactly one terminal (aa or bb),
  • followed by a (possibly empty) string of nonterminals.

So each production is of the form A→aαA \to a\alpha (with a∈{a,b}a\in\{a,b\}).

GNF verification and correctness criteria

Knowledge Check

Question 1 of 4
Q1Single choice

In GNF, what is the required form of each production?