Convert a Context-Free Grammar to GNF (Worked Example)
To convert a context-free grammar (CFG) into GNF (Generalized Greibach Normal Form), we enforce that every production has the form
where:
- is a single terminal,
- is a (possibly empty) string of nonterminals.
For the given grammar:
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:
Only two rules already start with terminals:
But:
- starts with (nonterminal)
- starts with (nonterminal)
- starts with (nonterminal)
So we must rewrite and the nonterminal-starting alternatives so that RHS begins with or .
GNF conversion roadmap for this grammar
Choose terminal-start strategy
1Ensure all productions on RHS begin with terminal by expanding nonterminal prefixes."
Resolve S-started productions
2Expand until the first symbol is or ."
Resolve X- and Y-started alternatives
3Rewrite and so they begin with or ."
Finalize in A→aα shape
4Collect and simplify into GNF-ready productions."
Convert the grammar to GNF by prefix elimination (worked derivation)
- 1Step 1
We want every production where and is a string of nonterminals (possibly empty).
- 2Step 2
Start with . Substitute and to expose terminal prefixes.
- 3Step 3
From , consider alternatives: if then . If then . Now ensure the leftmost symbol is a terminal by expanding the first symbol of .
- 4Step 4
For , expand . If then . If then and we must re-expand the leftmost .
- 5Step 5
For , expand , then can yield or . This creates terminal-leading possibilities; keep substituting until the prefix is terminal.
- 6Step 6
After completing substitutions, every RHS can be written as or followed by a string of nonterminals. Remove duplicates and keep RHS in the required terminal-first structure.
Step 1: Derive terminal-leading productions for
Given: and:
Consider the two cases for in .
Case 1:
So we get a GNF-shaped production:
Case 2:
i.e.
Now expand the first using :
- If first , then:
- If first , then: which still begins with ; expand again via 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 and appear directly as productions of and 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 can produce immediately and can produce immediately, we aim to rewrite any RHS so the leftmost symbol is forced to choose or at the earliest possible step. This avoids needing deeper expansions than necessary."
Step 2: Rewrite X productions in terminal-first form
We have:
One rule already is:
For the other: expand :
- If , then:
- If , then: i.e.:
This again begins with . To keep terminal-first shape, we expand and then immediately choose or as the first terminal-producing option.
Since , the leftmost symbol of is the leftmost symbol of , i.e. the leftmost of , which can become terminal by choosing:
- in , giving .
So:
which begins with , hence can be placed into GNF shape.
This produces terminal-leading productions for of the form .
type="warning" title="GNF conversions are sensitive to termination" content="Naively substituting 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: and already:
For , expand :
So:
Now expand the leftmost inside :
- If , then: begins with terminal .
- If , then: begins with , and expanding that yields: begins with terminal .
Thus we can generate GNF-shaped productions for 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:
All RHS strings:
- start with exactly one terminal ( or ),
- followed by a (possibly empty) string of nonterminals.
So each production is of the form (with ).
GNF verification and correctness criteria
Knowledge Check
In GNF, what is the required form of each production?
Explore Related Topics
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.
Type 2 Grammar (Context-Free Grammar): A Short Note with Formal Meaning and Examples
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
valstring 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.