Greibach Normal Form in Automata Theory



A CFG is in Greibach Normal Form if the Productions are in the following forms −

A → b

A → bD1Dn

S → ε

where A, D1,....,Dn are non-terminals and b is a terminal.

Algorithm to Convert a CFG into Greibach Normal Form

Step 1 − If the start symbol S occurs on some right side, create a new start symbol S and a new production S → S.

Step 2 − Remove Null productions. (Using the Null production removal algorithm discussed earlier)

Step 3 − Remove unit productions. (Using the Unit production removal algorithm discussed earlier)

Step 4 − Remove all direct and indirect left-recursion.

Step 5 − Do proper substitutions of productions to convert it into the proper form of GNF.

Example 1

Convert the following CFG into CNF

S → XY | Xn | p

X → mX | m

Y → Xn | o

Solution

Here, S does not appear on the right side of any production and there are no unit or null productions in the production rule set. So, we can skip Step 1 to Step 3.

Now after replacing

X in S → XY | Xo | p

with

mX | m

we obtain

S → mXY | mY | mXo | mo | p.

And after replacing

X in Y → Xn | o

with the right side of

X → mX | m

we obtain

Y → mXn | mn | o.

Two new productions O → o and P → p are added to the production set and then we came to the final GNF as the following −

S → mXY | mY | mXC | mC | p

X → mX | m

Y → mXD | mD | o

O → o

P → p

Example 2

$$\mathrm{S \: \rightarrow \: aAB \: | \: bB \: A \: \rightarrow \: aA \: | \: a}$$

$$\mathrm{B \: \rightarrow \: bB \: | \: c}$$

  • Theorem − Let L be a CFL not containing {s}. Then there exists a GNF grammar G such that L = L(G).
  • Lemma 1 − Let L be a CFL. Then there exists a PDA M such that L = LE(M).
  • Proof − Assume without loss of generality that s is not in L. The construction can be modified to includes later.

Solution

Let G = (V, T, P, S) be a CFG, and assume without loss of generality that G is in GNF.

Construct M = (Q, E, r, 5, q, z, 0) where −

$$\mathrm{Q \:=\: \{q\}}$$

$$\mathrm{E \:=\: T}$$

$$\mathrm{r \:=\: V}$$

$$\mathrm{z \:=\: S}$$

5: for all a in E and A in r,

5(q, a, A) contains (q, y) if A → ay is in P or rather −

5(q, a, A) = {(q, y) | A → ay is in P and y is in r*}, for all a in E and A in r

For a given string x in E*, M will attempt to simulate a leftmost derivation of x with G.

Example 3

Consider the following CFG in GNF.

$$\mathrm{S^aS\: S\: a}$$

Construct M as follows −

$$\mathrm{Q \: = \: \{q\}}$$

$$\mathrm{E \: = \: T \: = \: \{a\} \: r \: = \: V \: = \: \{S\}}$$

$$\mathrm{z \: = \: S}$$

$$\mathrm{5(T \: a \: S) \: = \: \{(T \: S) \: (T \: s)\}}$$

$$\mathrm{5(q, \: s, \: S) \: = \: 0}$$

Advertisements