Theory of Computation & Computer Graphics β Introduction to Context Free Language, NEC licence examination syllabus (Nepal Engineering Council).
Greibach Normal Form (GNF)
Every production begins with a terminal β so every derivation step consumes exactly one input symbol.
π Where this lives: GNF is the formal justification for a technique every compiler writer uses: eliminating left recursion so that a recursive-descent parser can work at all. A grammar in Greibach normal form is guaranteed to have no left recursion, because every right-hand side starts with a terminal β the parser always consumes input before recursing, so it can never loop forever without progress. The conversion algorithm in this topic is where that guarantee comes from. Search "Greibach normal form left recursion elimination recursive descent".
The form and its purpose
A CONTEXT-FREE GRAMMAR IS IN GREIBACH NORMAL FORM IF EVERY
PRODUCTION HAS THE SHAPE
A β a Ξ±
where a is a SINGLE TERMINAL and Ξ± is a (possibly empty)
STRING OF VARIABLES.
So every right-hand side begins with EXACTLY ONE terminal,
followed only by variables β never by another terminal.
A β a is permitted (Ξ± empty)
A β aBC is permitted
A β aBc is NOT β the terminal c may not appear
A β BC is NOT β it does not begin with a terminal
As with CNF, S β Ξ΅ is permitted if Ξ΅ β L(G), provided S
appears on no right-hand side.
ββ THE DEFINING CONSEQUENCE ββββββββββββββββββββββββββββββββ
EVERY DERIVATION STEP PRODUCES EXACTLY ONE TERMINAL.
Therefore a string of length n is derived in EXACTLY n STEPS β
no more, no fewer. Compare CNF's 2n β 1.
THREE THINGS FOLLOW IMMEDIATELY:
1. NO LEFT RECURSION IS POSSIBLE. A production A β AΞ± would
have to begin with a variable, which the form forbids. THIS
IS THE PROPERTY THAT MAKES TOP-DOWN PARSING SAFE, since
recursive descent's failure mode is precisely a recursive
call made without consuming input.
2. MEMBERSHIP IS DECIDABLE BY BOUNDED SEARCH. To test whether
w with |w| = n is generated, explore derivations to depth
exactly n. The search space is finite, so the procedure
terminates. (CNF gives the same guarantee with bound
2n β 1; GNF's bound is tighter and the argument simpler.)
3. THE LEFTMOST DERIVATION MATCHES THE INPUT POSITION BY
POSITION. After k steps, exactly k input symbols have been
produced β which is why GNF is the natural form for
proving the equivalence of context-free grammars and
pushdown automata.
ββ WHY GNF PROVES CFG = PDA ββββββββββββββββββββββββββββββββ
THE MAIN THEORETICAL USE, and the reason the form was
invented.
Given a GNF grammar, build a one-state pushdown automaton:
Β· the STACK holds the string of variables still to be
derived
Β· start with S on the stack
Β· to process input symbol a with variable A on top: pop A
and push Ξ±, for each production A β aΞ±
Β· accept by empty stack
THE CORRESPONDENCE IS EXACT: THE STACK CONTENTS ARE PRECISELY
THE SENTENTIAL FORM'S REMAINING VARIABLES, AND EACH INPUT
SYMBOL TRIGGERS EXACTLY ONE PRODUCTION. That one-to-one match
between input symbols and derivation steps is what GNF was
designed to supply, and it is why the proof is short in GNF
and awkward in any other form.
ββ THE CONVERSION ALGORITHM ββββββββββββββββββββββββββββββββ
START FROM CHOMSKY NORMAL FORM β the conversion assumes no
Ξ΅-productions and no unit productions, which CNF already
guarantees.
STEP 1 β ORDER THE VARIABLES Aβ, Aβ, β¦, Aβ arbitrarily.
STEP 2 β ENSURE EVERY PRODUCTION Aα΅’ β Aβ±ΌΞ± HAS j > i.
Working through i = 1, 2, β¦, n:
(a) For each production Aα΅’ β Aβ±ΌΞ± with j < i, SUBSTITUTE
every right-hand side of Aβ±Ό for Aβ±Ό. Repeat until every
remaining variable-initial production has j β₯ i.
(b) If Aα΅’ β Aα΅’Ξ± remains β IMMEDIATE LEFT RECURSION β
eliminate it with a new variable Bα΅’:
Aα΅’ β Aα΅’Ξ±β | β¦ | Aα΅’Ξ±β | Ξ²β | β¦ | Ξ²β
becomes
Aα΅’ β Ξ²β | β¦ | Ξ²β | Ξ²βBα΅’ | β¦ | Ξ²βBα΅’
Bα΅’ β Ξ±β | β¦ | Ξ±β | Ξ±βBα΅’ | β¦ | Ξ±βBα΅’
THE IDEA: the original generates Ξ² followed by any
number of Ξ±'s; the replacement generates Ξ² first and
then loops through the Ξ±'s using Bα΅’, so the recursion
has moved to the RIGHT.
STEP 3 β WORK BACKWARDS FROM Aβ.
After step 2, Aβ's productions all begin with a terminal
(there is no Aβ±Ό with j > n). Substitute Aβ's right-hand
sides into every production beginning with Aβ, then handle
A_{nβ1}, and so on down to Aβ. EACH SUBSTITUTION PROPAGATES
A LEADING TERMINAL ONE LEVEL DOWN.
STEP 4 β FIX THE Bα΅’ VARIABLES the same way, substituting to
give each a leading terminal.
STEP 5 β REPLACE ANY REMAINING TERMINAL that is not in the
leading position by a new variable, as in CNF step 4.
ββ A WORKED CONVERSION βββββββββββββββββββββββββββββββββββββ
GIVEN (already in CNF):
Aβ β Aβ Aβ
Aβ β Aβ Aβ | b
Aβ β Aβ Aβ | a
STEP 2, i = 3: Aβ β AβAβ has j = 1 < 3, so substitute Aβ:
Aβ β AβAβAβ | a
Now Aβ β AβAβAβ has j = 2 < 3, so substitute Aβ:
Aβ β AβAβAβAβ | bAβAβ | a
IMMEDIATE LEFT RECURSION in Aβ β AβAβAβAβ. Eliminate with
Bβ, where Ξ± = AβAβAβ and the non-recursive parts are
bAβAβ and a:
Aβ β bAβAβ | a | bAβAβBβ | aBβ
Bβ β AβAβAβ | AβAβAβBβ
STEP 3, working backwards.
Aβ's productions now all start with a terminal (b or a). β
Aβ β AβAβ starts with Aβ, so substitute:
Aβ β bAβAβAβ | aAβ | bAβAβBβAβ | aBβAβ | b
Aβ β AβAβ starts with Aβ, so substitute Aβ's new
right-hand sides:
Aβ β bAβAβAβAβ | aAβAβ | bAβAβBβAβAβ | aBβAβAβ | bAβ
STEP 4 β Bβ β AβAβAβ begins with Aβ, so substitute Aβ's
right-hand sides, each of which now begins with a terminal.
EVERY PRODUCTION NOW BEGINS WITH A SINGLE TERMINAL FOLLOWED
ONLY BY VARIABLES. β
THE OBSERVATION WORTH MAKING: THE GRAMMAR HAS GROWN
CONSIDERABLY. GNF conversion is notorious for producing large
grammars, since each substitution multiplies out the
alternatives β which is why it is a theoretical tool rather
than a practical one.
Comparison and use
ββ CNF VERSUS GNF, THE FULL COMPARISON βββββββββββββββββββββ
CHOMSKY NF GREIBACH NF
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
form A β BC or A β a A β aΞ±, Ξ± β V*
RHS begins with a variable or a ALWAYS exactly one
terminal TERMINAL
derivation
length exactly 2n β 1 EXACTLY n
tree shape BINARY arbitrary branching
left recursion POSSIBLE IMPOSSIBLE by
construction
main use CYK parsing; proving CFG β‘ PDA;
the context-free top-down parsing
pumping lemma
conversion cost can blow up on typically LARGER
Ξ΅-removal still
what it controls the SHAPE of the the PROGRESS of the
tree derivation
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
THE LAST ROW IS THE ONE TO REMEMBER. CNF makes the tree binary
so that substrings can be combined pairwise β a structural
guarantee. GNF makes every step consume one symbol β a
temporal guarantee. Each normal form was designed for a
specific algorithm, and neither is "better".
ββ WHERE GNF MATTERS IN PRACTICE βββββββββββββββββββββββββββ
1. LEFT RECURSION ELIMINATION. The full GNF conversion is
rarely performed, but STEP 2(b) β the immediate left
recursion elimination β is used constantly, because
recursive descent parsers cannot handle left-recursive
grammars at all.
E β E + T | T becomes E β T Eβ²
Eβ² β + T Eβ² | Ξ΅
THIS IS EXACTLY THE GNF STEP, applied selectively.
2. PROVING TERMINATION. Any parser working on a GNF grammar
terminates, because each step consumes input and the input
is finite. This is the cleanest termination argument
available for top-down parsing.
3. THE PDA EQUIVALENCE, as above β the theoretical result that
makes the whole pushdown automaton section work.
4. BOUNDING DERIVATION LENGTH in proofs about context-free
languages.
ββ THE LIMITATION SHARED WITH CNF ββββββββββββββββββββββββββ
NEITHER NORMAL FORM PRESERVES THE PARSE TREE'S SHAPE, and
neither removes ambiguity. Converting an ambiguous grammar
yields an ambiguous grammar in normal form.
NORMAL FORMS REGULARISE THE **SYNTAX OF THE PRODUCTIONS**,
NOT THE **STRUCTURE OF THE LANGUAGE**. A defect that lies in
the language β inherent ambiguity β survives every
transformation, exactly as the previous topic said it must.
ββ THE PLACE OF BOTH FORMS βββββββββββββββββββββββββββββββββ
Normal forms are the context-free analogue of the DFA
minimisation of the last section: a canonical-ish
representation that makes algorithms possible.
BUT NOTE THE WEAKER GUARANTEE. The minimal DFA is UNIQUE, so
DFA equivalence is decidable. CNF AND GNF ARE NOT UNIQUE β
many grammars in normal form generate the same language β
AND CONTEXT-FREE GRAMMAR EQUIVALENCE IS UNDECIDABLE.
THE LOSS OF CANONICITY IS EXACTLY THE LOSS OF DECIDABILITY,
and that correspondence is one of the most instructive
patterns in the whole subject.
CNF controls the shape of the tree, making it binary so substrings combine pairwise. GNF controls the progress of the derivation, guaranteeing each step consumes one symbol. Each normal form was built for a specific algorithm, and neither is better than the other.
π Go further: The loss of canonicity is worth dwelling on, because it explains a sharp break between two sections of this syllabus. The minimal DFA is unique, so DFA equivalence is decidable by minimising both and comparing. CNF and GNF are not unique β many normal-form grammars generate the same language β and correspondingly, deciding whether two context-free grammars generate the same language is undecidable. The pattern generalises: wherever a formalism admits a computable canonical form, its equivalence problem tends to be decidable; where it does not, equivalence tends to be undecidable. Search "canonical form decidability grammar equivalence undecidable".
π‘ Exam angle: state the form A β aΞ± precisely β one terminal, then only variables β and give the consequence that a length-n string takes exactly n derivation steps. Explain why left recursion is impossible in GNF and why that matters for top-down parsing. Be able to perform immediate left recursion elimination (A β AΞ± | Ξ² becomes A β Ξ² | Ξ²B, B β Ξ± | Ξ±B), which is the most examinable single step. Know the conversion outline: order the variables, ensure Aα΅’ β Aβ±ΌΞ± has j > i, remove immediate left recursion, then substitute backwards from Aβ. Finish with the CNF versus GNF comparison table.
Syllabus points
GNF rules
Converting a CFG to GNF (numerical)
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Introduction to Context Free Language