0% found this document useful (0 votes)
354 views29 pages

Every CFG G can also be converted to an equivalent grammar in - A context-free grammar G = (V, Σ, P, S) is in Greibach Normal Form iff its productions are of the form

The document discusses Greibach normal form (GNF) for context-free grammars (CFGs). It states that every CFG can be converted to an equivalent grammar in GNF. A grammar is in GNF if its productions are of the form A → aBC, A → aB, A → a, or S → λ, where A, B, C are nonterminals, a is a terminal, S → λ is allowed if λ is in the language, and S does not appear on the right side. Grammars in GNF avoid left recursion and allow for top-down parsing without loops. The document provides an example of converting a CFG to its equivalent in GNF.

Uploaded by

manu manu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
354 views29 pages

Every CFG G can also be converted to an equivalent grammar in - A context-free grammar G = (V, Σ, P, S) is in Greibach Normal Form iff its productions are of the form

The document discusses Greibach normal form (GNF) for context-free grammars (CFGs). It states that every CFG can be converted to an equivalent grammar in GNF. A grammar is in GNF if its productions are of the form A → aBC, A → aB, A → a, or S → λ, where A, B, C are nonterminals, a is a terminal, S → λ is allowed if λ is in the language, and S does not appear on the right side. Grammars in GNF avoid left recursion and allow for top-down parsing without loops. The document provides an example of converting a CFG to its equivalent in GNF.

Uploaded by

manu manu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

182 CHAPTER 3.

CONTEXT-FREE LANGUAGES AND PDA’S

3.6 The Greibach Normal Form

Every CFG G can also be converted to an equivalent


grammar in Greibach Normal Form (for short, GNF).
A context-free grammar G = (V, Σ, P, S) is in Greibach
Normal Form iff its productions are of the form
A → aBC,
A → aB,
A → a, or
S → ,
where A, B, C ∈ N , a ∈ Σ, S →  is in P iff  ∈
L(G), and S does not occur on the right-hand side of
any production.
3.6. THE GREIBACH NORMAL FORM 183

Note that a grammar in Greibach Normal Form does not


have -rules other than possibly S → . More impor-
tantly, except for the special rule S → , every rule pro-
duces some terminal symbol.

An important consequence of the Greibach Normal Form


is that every nonterminal is not left recursive. A nonter-
+
minal A is left recursive iff A =⇒ Aα for some α ∈ V ∗.
Left recursive nonterminals cause top-down determinitic
parsers to loop. The Greibach Normal Form provides a
way of avoiding this problem.

There are no easy proofs that every CFG can be converted


to a Greibach Normal Form. We will give an elegant
method due to Rosenkrantz (using matrices).
184 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Lemma 3.6.1 Given any context-free grammar G =


(V, Σ, P, S), one can construct a context-free grammar
G = (V , Σ, P , S ) such that L(G) = L(G) and G is
in Greibach Normal Form, that is, a grammar whose
productions are of the form
A → aBC,
A → aB,
A → a, or
S  → ,
where A, B, C ∈ N , a ∈ Σ, S  →  is in P  iff  ∈
L(G), and S  does not occur on the right-hand side of
any production in P .
3.7. LEAST FIXED-POINTS 185

3.7 Least Fixed-Points

Context-free languages can also be characterized as least


fixed-points of certain functions induced by grammars.

This characterization yields a rather quick proof that ev-


ery context-free grammar can be converted to Greibach
Normal Form.

This characterization also reveals very clearly the recur-


sive nature of the context-free languages.

We begin by reviewing what we need from the theory of


partially ordered sets.
186 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Definition 3.7.1 Given a partially ordered set A, ≤,


an ω-chain (an)n≥0 is a sequence such that an ≤ an+1
for all n ≥ 0. The least-upper bound of an ω-chain (an)
is an element a ∈ A such that:
(1) an ≤ a, for all n ≥ 0;
(2) For any b ∈ A, if an ≤ b, for all n ≥ 0, then a ≤ b.
A partially ordered set A, ≤ is an ω-chain complete
poset iff it has a least element ⊥, and
 iff every ω-chain
has a least upper bound denoted as an.

Remark : The ω in ω-chain means that we are considering


countable chains (ω is the ordinal associated with the
order-type of the set of natural numbers).

For example, given any set X, the power set 2X ordered


by inclusion is an ω-chain complete poset with least ele-
ment ∅.
3.7. LEAST FIXED-POINTS 187

The Cartesian product 2X × ·


· · × 2X ordered such that
n

(A1, . . . , An) ≤ (B1, . . . , Bn)


iff Ai ⊆ Bi (where Ai, Bi ∈ 2X ) is an ω-chain complete
poset with least element (∅, . . . , ∅).

We are interested in functions between partially ordered


sets.

Definition 3.7.2 Given any two partially ordered sets


A1, ≤1 and A2, ≤2, a function f : A1 → A2 is mono-
tonic iff for all x, y ∈ A1,
x ≤1 y implies that f (x) ≤2 f (y).
If A1, ≤1 and A2, ≤2 are ω-chain complete posets, a
function f : A1 → A2 is ω-continuous iff it is monotonic,
and for every ω-chain (an),
 
f ( an) = f (an).
188 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Remark : Note that we are not requiring that an ω-


continuous function f : A1 → A2 preserve least elements,
i.e., it is possible that f (⊥1) =⊥2.

We now define the crucial concept of a least fixed-point.

Definition 3.7.3 Let A, ≤ be a partially ordered set,


and let f : A → A be a function. A fixed-point of f is an
element a ∈ A such that f (a) = a. The least fixed-point
of f is an element a ∈ A such that f (a) = a, and for
every b ∈ A such that f (b) = b, then a ≤ b.

The following lemma gives sufficient conditions for the


existence of least fixed-points. It is one of the key lemmas
in denotational semantics.
3.7. LEAST FIXED-POINTS 189

Lemma 3.7.4 Let A, ≤ be an ω-chain complete poset


with least element ⊥. Every ω-continuous function
f : A → A has a unique least fixed-point x0 given by

x0 = f n(⊥).
Furthermore, for any b ∈ A such that f (b) ≤ b, then
x0 ≤ b.

The second part of lemma 3.7.4 is very useful to prove


that functions have the same least fixed-point.

For example, under the conditions of lemma 3.7.4, if


g: A → A is another ω-chain continuous function, letting
x0 be the least fixed-point of f and y0 be the least fixed-
point of g, if f (y0) ≤ y0 and g(x0) ≤ x0, we can deduce
that x0 = y0.
190 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Lemma 3.7.4 also shows that the least fixed-point x0 of


f can be approximated as much as desired, using the
sequence (f n(⊥)).

We will now apply this fact to context-free grammars.


For this, we need to show how a context-free grammar
G = (V, Σ, P, S) with m nonterminals induces an ω-
continuous map
Σ∗ Σ∗ Σ∗ Σ∗
ΦG: 2 × ·
· · × 2  → 2 × ·
·· × 2 .
m m
3.8. CONTEXT-FREE LANGUAGES AS LEAST FIXED-POINTS 191

3.8 Context-Free Languages as Least Fixed-Points

Given a context-free grammar G = (V, Σ, P, S) with


m nonterminals A1, . . . Am, grouping all the productions
having the same left-hand side, the grammar G can be
concisely written as
A1 → α1,1 + · · · + α1,n1 ,
··· → ···
Ai → αi,1 + · · · + αi,ni ,
··· → ···
Am → αm,1 + · · · + αm,nn .

Given any set A, let Pf in(A) be the set of finite subsets


of A.
192 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Definition 3.8.1 Let G = (V, Σ, P, S) be a context-


free grammar with m nonterminals A1, . . ., Am. For any
m-tuple Λ = (L1, . . . , Lm) of languages Li ⊆ Σ∗, we
define the function

Φ[Λ]: Pf in(V ∗) → 2Σ
inductively as follows:
Φ[Λ](∅) = ∅,
Φ[Λ]({}) = {},
Φ[Λ]({a}) = {a}, if a ∈ Σ,
Φ[Λ]({Ai}) = Li, if Ai ∈ N ,
Φ[Λ]({αX}) = Φ[Λ]({α})Φ[Λ]({X}),
if α ∈ V +, X ∈ V,
Φ[Λ](Q ∪ {α}) = Φ[Λ](Q) ∪ Φ[Λ]({α}),
if Q ∈ Pf in(V ∗), Q = ∅, α ∈ V ∗, α ∈
/ Q.
3.8. CONTEXT-FREE LANGUAGES AS LEAST FIXED-POINTS 193

Then, writing the grammar G as


A1 → α1,1 + · · · + α1,n1 ,
··· → ···
Ai → αi,1 + · · · + αi,ni ,
··· → ···
Am → αm,1 + · · · + αm,nn ,

we define the map


∗ ∗ ∗ ∗
ΦG: 2Σ × ·
· · × 2Σ → 2Σ × ·
· · × 2Σ
m m

such that
ΦG(L1, . . . Lm) =
(Φ[Λ]({α1,1, . . . , α1,n1 }), . . . , Φ[Λ]({αm,1, . . . , αm,nm }))
∗ ∗
for all Λ = (L1, . . . , Lm) ∈ 2Σ × ·
· · × 2Σ.
m

One should verify that the map Φ[Λ] is well defined, but
this is easy.
194 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

The following lemma is easily shown:

Lemma 3.8.2 Given a context-free grammar


G = (V, Σ, P, S) with m nonterminals A1, . . ., Am, the
map
∗ ∗ ∗ ∗
ΦG: 2Σ × ·
· · × 2Σ → 2Σ × ·
· · × 2Σ
m m
is ω-continuous.

Σ∗ Σ∗
Now, 2 × ·
· · × 2  is an ω-chain complete poset, and
m
the map ΦG is ω-continous.

Thus, by lemma 3.7.4, the map ΦG has a least-fixed point.

It turns out that the components of this least fixed-point


are precisely the languages generated by the grammars
(V, Σ, P, Ai).
3.8. CONTEXT-FREE LANGUAGES AS LEAST FIXED-POINTS 195

Example. Consider the grammar


G = ({A, B, a, b}, {a, b}, P, A) defined by the rules
A → BB + ab,
B → aBb + ab.

The least fixed-point of ΦG is the least upper bound of


the chain
(ΦnG(∅, ∅)) = ((ΦnG,A(∅, ∅), ΦnG,B (∅, ∅)),
where
Φ0G,A(∅, ∅) = Φ0G,B (∅, ∅) = ∅,
and
Φn+1
G,A (∅, ∅) = Φn
G,B (∅, ∅)ΦG,B (∅, ∅) ∪ {ab},
n

G,B (∅, ∅) = aΦG,B (∅, ∅)b ∪ {ab}.


Φn+1 n
196 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

It is easy to verify that


Φ1G,A(∅, ∅) = {ab},
Φ1G,B (∅, ∅) = {ab},
Φ2G,A(∅, ∅) = {ab, abab},
Φ2G,B (∅, ∅) = {ab, aabb},
Φ3G,A(∅, ∅) = {ab, abab, abaabb, aabbab, aabbaabb},
Φ3G,B (∅, ∅) = {ab, aabb, aaabbb}.

By induction, we can easily prove that the two compo-


nents of the least fixed-point are the languages
LA = {ambmanbn | m, n ≥ 1} ∪ {ab}
and
LB = {anbn | n ≥ 1}.

Letting GA = ({A, B, a, b}, {a, b}, P, A) and


GB = ({A, B, a, b}, {a, b}, P, B), it is indeed true that
LA = L(GA) and LB = L(GB ) .
3.8. CONTEXT-FREE LANGUAGES AS LEAST FIXED-POINTS 197

We have the following theorem due to Ginsburg and Rice:

Theorem 3.8.3 Given a context-free grammar G =


(V, Σ, P, S) with m nonterminals A1, . . ., Am, the least
fixed-point of the map ΦG is the m-tuple of languages
(L(GA1 ), . . . , L(GAm )),
where GAi = (V, Σ, P, Ai).

Proof . Writing G as
A1 → α1,1 + · · · + α1,n1 ,
··· → ···
Ai → αi,1 + · · · + αi,ni ,
··· → ···
Am → αm,1 + · · · + αm,nn ,
let M = max{|αi,j |} be the maximum length of right-
hand sides of rules in P .
198 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Let
ΦnG(∅, . . . , ∅) = (ΦnG,1(∅, . . . , ∅), . . . , ΦnG,m(∅, . . . , ∅)).

Then, for any w ∈ Σ∗, observe that


w ∈ Φ1G,i(∅, . . . , ∅)
iff there is some rule Ai → αi,j with w = αi,j , and that
w ∈ ΦnG,i(∅, . . . , ∅)
for some n ≥ 2 iff there is some rule Ai → αi,j with αi,j
of the form
αi,j = u1Aj1 u2 · · · uk Ajk uk+1,
where u1, . . . , uk+1 ∈ Σ∗, k ≥ 1, and some w1, . . . , wk ∈
Σ∗ such that
wh ∈ Φn−1
G,jh (∅, . . . , ∅),
and
w = u1w1u2 · · · uk wk uk+1.

We prove the following two claims:


3.8. CONTEXT-FREE LANGUAGES AS LEAST FIXED-POINTS 199

n
Claim 1: For every w ∈ Σ∗, if Ai =⇒ w, then w ∈
ΦpG,i(∅, . . . , ∅), for some p ≥ 1.

Claim 2: For every w ∈ Σ∗, if w ∈ ΦnG,i(∅, . . . , ∅), with


p
n ≥ 1, then Ai =⇒ w for some p ≤ (M + 1)n−1.

Combining Claim 1 and Claim 2, we have



L(GAi ) = ΦnG,i(∅, . . . , ∅),
n

which proves that the least fixed-point of the map ΦG is


the m-tuple of languages
(L(GA1 ), . . . , L(GAm )).

We now show how theorem 3.8.3 can be used to give


a short proof that every context-free grammar can be
converted to Greibach Normal Form.
200 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

3.9 Least Fixed-Points and the Greibach Normal Form

The hard part in converting a grammar G = (V, Σ, P, S)


to Greibach Normal Form is to convert it to a grammar
in so-called weak Greibach Normal Form, where the
productions are of the form
A → aα, or
S → ,
where a ∈ Σ, α ∈ V ∗, and if S →  is a rule, then S
does not occur on the right-hand side of any rule.

Indeed, if we first convert G to Chomsky Normal Form,


it turns out that we will get rules of the form A → aBC,
A → aB or A → a.

Using the algorithm for eliminating -rules and chain rules,


we can first convert the original grammar to a grammar
with no chain rules and no -rules except possibly S → ,
in which case, S does not appear on the right-hand side
of rules.
3.9. LEAST FIXED-POINTS AND THE GREIBACH NORMAL FORM 201

Thus, for the purpose of converting to weak Greibach


Normal Form, we can assume that we are dealing with
grammars without chain rules and without -rules.

Let us also assume that we computed the set T (G) of non-


terminals that actually derive some terminal string, and
that useless productions involving symbols not in T (G)
have been deleted.

Let us explain the idea of the conversion using the follow-


ing grammar:
A → AaB + BB + b.
B → Bd + BAa + aA + c.

The first step is to group the right-hand sides α into two


categories: those whose leftmost symbol is a terminal
(α ∈ ΣV ∗) and those whose leftmost symbol is a non-
terminal (α ∈ N V ∗).
202 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

It is also convenient to adopt a matrix notation, and we


can write the above grammar as

aB ∅
(A, B) = (A, B) + (b, {aA, c})
B {d, Aa}

Thus, we are dealing with matrices (and row vectors)


whose entries are finite subsets of V ∗.

For notational simplicity, braces around singleton sets are


omitted.

The finite subsets of V ∗ form a semiring, where addition


is union, and multiplication is concatenation.

Addition and multiplication of matrices are as usual, ex-


cept that the semiring operations are used.

We will also consider matrices whose entries are languages


over Σ.
3.9. LEAST FIXED-POINTS AND THE GREIBACH NORMAL FORM 203

Again, the languages over Σ form a semiring, where ad-


dition is union, and multiplication is concatenation. The
identity element for addition is ∅, and the identity ele-
ment for multiplication is {}.

As above, addition and multiplication of matrices are as


usual, except that the semiring operations are used.

For example, given any languages Ai,j and Bi,j over Σ,


where i, j ∈ {1, 2}, we have
 
A1,1 A1,2 B1,1 B1,2
A2,1 A2,2 B2,1 B2,2

A1,1B1,1 ∪ A1,2B2,1 A1,1B1,2 ∪ A1,2B2,2
=
A2,1B1,1 ∪ A2,2B2,1 A2,1B1,2 ∪ A2,2B2,2
204 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Letting X = (A, B), K = (b, {aA, c}), and



aB ∅
H=
B {d, Aa}

the above grammar can be concisely written as


X = XH + K.

More generally, given any context-free grammar G =


(V, Σ, P, S) with m nonterminals A1, . . ., Am, assum-
ing that there are no chain rules, no -rules, and that
every nonterminal belongs to T (G), letting
X = (A1, . . . , Am),
we can write G as
X = XH + K,
for some appropriate m × m matrix H in which every
entry contains a set (possibly empty) of strings in V +,
and some row vector K in which every entry contains a
set (possibly empty) of strings α each beginning with a
terminal (α ∈ ΣV ∗).
3.9. LEAST FIXED-POINTS AND THE GREIBACH NORMAL FORM 205

Given an m × m square matrix A = (Ai,j ) of languages


over Σ, we can define the matrix A∗ whose entry A∗i,j is
given by 

Ai,j = Ani,j ,
n≥0
where A0 = Idm, the identity matrix, and An is the n-th
power of A. Similarly, we define A+, where

+
Ai,j = Ani,j .
n≥1

Given a matrix A where the entries are finite subset of


V ∗, where N = {A1, . . . , Am}, for any m-tuple
Λ = (L1, . . . , Lm) of languages over Σ, we let
Φ[Λ](A) = (Φ[Λ](Ai,j )).

Given a system X = XH + K where H is an m × m


matrix and X, K are row matrices, if H and K do not
contain any nonterminals, we claim that the least fixed-
point of the grammar G associated with X = XH + K
is KH ∗.
206 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

This is easily seen by computing the approximations


X n = ΦnG(∅, . . . , ∅). Indeed, X 0 = K, and
X n = KH n + KH n−1 + · · · + KH + K
= K(H n + H n−1 + · · · + H + Im).

Similarly, if Y is an m × m matrix of nonterminals, the


least fixed-point of the grammar associated with Y =
HY + H is H + (provided that H does not contain any
nonterminals).

Given any context-free grammar G = (V, Σ, P, S) with


m nonterminals A1, . . ., Am, writing G as X = XH + K
as explained earlier, we can form another grammar GH
by creating m2 new nonterminals Yi,j , where the rules
of this new grammar are defined by the system of two
matrix equations

X = KY + K,
Y = HY + H,
where Y = (Yi,j ).
3.9. LEAST FIXED-POINTS AND THE GREIBACH NORMAL FORM 207

The following lemma is the key to the Greibach Normal


Form:

Lemma 3.9.1 Given any context-free grammar G =


(V, Σ, P, S) with m nonterminals A1, . . ., Am, writing
G as
X = XH + K
as explained earlier, if GH is the grammar defined by
the system of two matrix equations
X = KY + K,
Y = HY + H,
as explained above, then the components in X of the
least-fixed points of the maps ΦG and ΦGH are equal.

Note that the above lemma actually applies to any gram-


mar.
208 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

Applying lemma 3.9.1 to our example grammar, we get


the following new grammar:

Y1 Y2
(A, B) = (b, {aA, c}) + (b, {aA, c}),
Y3 Y4

Y1 Y2
=
Y3 Y4
  
aB ∅ Y1 Y2 aB ∅
+
B {d, Aa} Y3 Y4 B {d, Aa}

There are still some nonterminals appearing as leftmost


symbols, but using the equations defining A and B, we
can replace A with
{bY1, aAY3, cY3, b}
and B with
{bY2, aAY4, cY4, aA, c},
obtaining a system in weak Greibach Normal Form.

This amounts to converting the matrix



aB ∅
H=
B {d, Aa}
to
 the matrix L shown below
aB ∅
{bY2, aAY4, cY4, aA, c} {d, bY1a, aAY3a, cY3a, ba}
3.9. LEAST FIXED-POINTS AND THE GREIBACH NORMAL FORM 209

The weak Greibach Normal Form corresponds to the new


system
X = KY + K,
Y = LY + L.

This method works in general for any input grammar with


no -rules, no chain rules, and such that every nontermi-
nal belongs to T (G).

Under these conditions, the row vector K contains some


nonempty entry, all strings in K are in ΣV ∗, and all
strings in H are in V +.

After obtaining the grammar GH defined by the system


X = KY + K,
Y = HY + H,
we use the system X = KY + K to express every non-
terminal Ai in terms of expressions containing strings αi,j
involving a terminal as the leftmost symbol (αi,j ∈ ΣV ∗),
and we replace all leftmost occurrences of nonterminals
in H (occurrences Ai in strings of the form Aiβ, where
β ∈ V ∗) using the above expressions.
210 CHAPTER 3. CONTEXT-FREE LANGUAGES AND PDA’S

In this fashion, we obtain a matrix L, and it is immedi-


ately shown that the system
X = KY + K,
Y = LY + L,
generates the same tuple of languages. Furthermore, this
last system corresponds to a weak Greibach Normal Form.

It we start with a grammar in Chomsky Normal Form


(with no production S → ) such that every nonterminal
belongs to T (G), we actually get a Greibach Normal Form
(the entries in K are terminals, and the entries in H are
nonterminals).

The method is also quite economical, since it introduces


only m2 new nonterminals. However, the resulting gram-
mar may contain some useless nonterminals.

You might also like