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
AaαA \to a\,\alpha
where:

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

For the given grammar:

SXYS \to XY XYSbX \to YS \mid b YSXaY \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:

  • SXYS \to XY
  • XYSbX \to YS \mid b
  • YSXaY \to SX \mid a

Only two rules already start with terminals:

  • XbX \to b
  • YaY \to a

But:

  • SXYS \to XY starts with XX (nonterminal)
  • XYSX \to YS starts with YY (nonterminal)
  • YSXY \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 SXYS \to XY until the first symbol is aa or bb."

Resolve X- and Y-started alternatives

3

Rewrite XYSX \to YS and YSXY \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 Aaα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 SXYS \to XY. Substitute XYSbX \to YS \mid b and YSXaY \to SX \mid a to expose terminal prefixes.

  3. 3
    Step 3

    From SXYS \to XY, consider XX alternatives: if XbX \to b then SbYS \to bY. If XYSX \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 XYSX \to YS, expand YaSXY \to a \mid SX. If YaY \to a then XaSX \to aS. If YSXY \to SX then X(SX)SX \to (SX)S and we must re-expand the leftmost SS.

  5. 5
    Step 5

    For YSXY \to SX, expand SXYS \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: SXYS \to XY and: XYSb,YSXaX \to YS \mid b,\quad Y \to SX \mid a

Consider the two cases for XX in SXYS \to XY.

Case 1: XbX \to b

SbYS \Rightarrow bY

So we get a GNF-shaped production: SbYS \to bY

Case 2: XYSX \to YS

S(YS)YS \Rightarrow (YS)Y

i.e.

SYSYS \to YSY

Now expand the first YY using YSXaY \to SX \mid a:

  • If first YaY \to a, then: SaSYS \to aSY
  • If first YSXY \to SX, then: S(SX)SY=SXSYS \to (SX)SY = SXSY which still begins with SS; expand SS again via SXYS\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 XbX→b or YaY→a at the earliest possible step. This avoids needing deeper expansions than necessary."

Step 2: Rewrite X productions in terminal-first form

We have: XYSbX \to YS \mid b

One rule already is: XbX \to b

For the other: XYSX \to YS expand YY:

  • If YaY \to a, then: XaSX \to aS
  • If YSXY \to SX, then: X(SX)SX \to (SX)S i.e.: XSXSX \to SXS

This again begins with SS. To keep terminal-first shape, we expand SXYS \to XY and then immediately choose XbX \to b or YaY \to a as the first terminal-producing option.

Since SXYS \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:

  • XbX \to b in SXYS \to XY, giving SbYS \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 SXYS→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: YSXaY \to SX \mid a and already: YaY \to a

For YSXY \to SX, expand SXYS \to XY:

Y(XY)XY \to (XY)X

So:

YXYXY \to XYX

Now expand the leftmost XX inside XYXXYX:

  • If XbX \to b, then: YbYXY \to bYX begins with terminal bb.
  • If XYSX \to YS, then: Y(YS)YXY \to (YS)YX begins with YY, and expanding that YaY \to a yields: YaSYXY \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:

SbYaSYXbaSbYSXYabYXaSYX\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 Aaα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?

Explore Related Topics

1

Ambiguity in the Grammar \(S \rightarrow ABA,\; A \rightarrow aA \mid \epsilon,\; B \rightarrow bB \mid \epsilon\)

The grammar (S\rightarrow ABA,;A\rightarrow aA\mid\epsilon,;B\rightarrow bB\mid\epsilon) is ambiguous because the two (A) nonterminals can distribute the same (a)-string in multiple ways, especially when (B) derives (\epsilon).

  • It generates the language (L(S)={a^i b^j a^k\mid i,j,k\ge0}=a^*b^a^).
  • The shortest string (a) has two distinct left‑most derivations (or parse trees), proving ambiguity.
  • Every string (a^n) with (n\ge1) can be split between the two (A) symbols in (n+1) ways, yielding multiple parse trees.
  • The ambiguity stems from both (A) producing (a^*) and (B) being able to vanish via (\epsilon).
  • An equivalent unambiguous grammar can be constructed, showing the ambiguity is a property of this grammar, not necessarily of the language.
2

Type 2 Grammar (Context-Free Grammar): A Short Note with Formal Meaning and Examples

3

Syntax-Directed Translation: Infix to Prefix Notation

The module shows how a syntax‑directed translation scheme using only synthesized attributes can convert infix arithmetic expressions into prefix (Polish) notation while preserving operator precedence and left‑associativity.

  • Grammar: E → E + T | E - T | T; T → T * F | F; F → digit, enforcing precedence ( *  > + / - ).
  • Semantic actions compute a val string for each non‑terminal, concatenating the operator before its operand strings.
  • Example results: 9 - 5 + 2+ - 9 5 2; 9 - 5 * 2- 9 * 5 2.
  • Synthesized (S‑attributed) attributes allow immediate bottom‑up evaluation during LR‑style parsing.
  • Left‑recursive rules enable left‑associativity; to use LL parsers the grammar must be transformed and inherited attributes introduced.