Sei sulla pagina 1di 17

Chomsky Normal Form Or CNF

Any contex-free language without is


generated by a grammar in which all
the productions are of the form of
A BC or A a Here, A, B and C
are the variables and a ia a terminal.

Chomsky Normal Form Or CNF


Consider this grammar
G = ({ S, A, B} {a,b}, P, S)
That has the productions:
S bA|aB
AbAA|aS| a
B aBB|bS|b
Find the equivalent grammar in CNF

Chomsky Normal Form Or CNF

First the only production in the proper


form are A a and B b
There are no unit production so we may
begin by rplacing terminals on the right
by variable. Except in the of the
production Aa and B b.

S bA -------(1)
SaB -------(2)
AbAA ----(3)
AaS ----(4)
A a ----(5)
B aBB ----(6)
BbS ----(7)
Bb ----(8)

S bA-------(1)
C1 b
S C1A
SaB -------(2)
C2 a
S C2B
AaS ----(4)
A C2S
BbS ----(7)
BC1S
AbAA ----(3)
A C1AA

B aBB ----(6)
B C2BB

AbAA ----(3)
D AA
AC1 D

B aBB ----(6)
EBB
BC2E

A a ----(5)
Bb ----(8)
C 1 b
S C1A
C 2 a
S C2B

A C2S

BC1S
A C1AA

B aBB ----(6)
B C2BB

Greibach Normal Form


Every context-free language L without can
be generated by a grammar for which
every production is of the form of A a
Where A is a variable, a is a terminal and
is a (possibly empty) string of variables.

Greibach Normal Form


Convert to Greibach Normal Form to the given
grammar
G= ({ A1,A2, A3}, {a,b},P,A1}
Where P Consists of following :
A1 A2 A3 ---------(1)
A2 A3 A1 ---------(2)
A3 A1 A2 ---------(3)
A2 b ---------(4)
A3 a ---------(5)

Greibach Normal Form


Step1. Since the right hand side of the productions for A1 and
A2 start with the terminals or higher-numbered variables,
So we begin with the production A3 A1 A2
starts and substiute the string A1 A2 A3
The resulting production is:
A1 A2 A3 ---------(1)
A2 A3 A1 ---------(2)
A3 A2 A3 A2 ---------(3)
A2 b ---------(4)
A3 a ---------(5)

Greibach Normal Form


Since the right side of the
production
A3 A2 A3 A2 ---------(3)
Begin with the lowernumbered variable, we
substitute A2 , A2 A3 A1
and A2 b
A1 A2 A3 ---------(1)
A2 A3 A1 ---------(2)
A3 A2 A3 A2 ---------(3)
A2 b ---------(4)
A3 a ---------(5)

A3 A2 A3 A2 ---------(3)
A3 A3 A1 A3 A2 -------(3a)
A3 b A3 A2-------------(3b)

Lemma 4.4
Let G =(V,T,P,S) be the context free grammar CFG
Let A A1|A2|A3.. Ar be the set of A-productions for which
A is the leftmost symbol of the right-hand side.
Let A 1|2|3.|s be the remaining A-productions.
Let G1= (V {B}, T,P1,S} be the context free grammar formed by adding
the variable B to V and replacing all the A-productions:
1) Ai
A iB Where 1 =< i <= s
2) B i
B iB where 1 =< i <= r
Then L(G) = L(G1)

Greibach Normal Form


We now apply the Lemma 4.4 to the productions
A3 A3 A1 A3 A2 -------(3a)
A3 b A3 A2-------------(3b)
A3 a ---------(5)
We introduce the symbol B the production
A3 A3 A1 A3 A2 -------(3a) is replaced by
A3 b A3 A2 B
A3 a B
B A1 A3 A2
B A1 A3 A2 B

Greibach Normal Form


The resulting set is:
A1 A2 A3 ---------(1)
A2 A3 A1| ---------(2)
A2 b ---------(3)
A3 a ---------(4)
A3 b A3 A2-------(5)
A3 b A3 A2 B-------(6)
A3 a B-------(7)
B A1 A3 A2-------(8)
B A1 A3 A2 B-------(9)

Greibach Normal Form


Step 2. Now all the productions
with A3 on the left have righthand side start with the
terminals. These are used to
replace A3 in the productions :
A2 A3 A1 and the A2 on the
right side of the A1 A2 A3
The result is as follows:
A3 a ---------(1)
A3 b A3 A2-------(2)
A3 b A3 A2 B-------(3)
A3 a B-------(4)
A2 aA1-------(5)
A2 b A3 A2 A1-------(6)
A2 b A3 A2 BA1-------(7)
A2 aBA1-------(8)
A2 b-------(9)

Now replace A2 in A1 A2 A3
production

A1
A1
A1
A1
A1

aA1 A3-------(10)
b A3 A2 A1 A3-------(11)
b A3 A2 B A3-------(12)
aBA1 A3-------(13)
b A3-------(14)

Greibach Normal Form


Step 3: The two B productions are converted to the proper form:
B A1 A3 A2-------(8)
B A1 A3 A2 B-------(9)
A1 aA1 A3-------(10)
A1 b A3 A2 A1 A3-------(11)
A1 b A3 A2 B A3-------(12)
A1 aBA1 A3-------(13)
A1 b A3-------(14)
By (8)
B aA1 A3 A3 A2
B b A3 A2 A1 A3 A3 A2
B b A3 A2 B A3 A3 A2
B aBA1 A3 A3 A2
B b A3 A3 A2

Greibach Normal Form


By (9)
B A1 A3 A2 B-------(9)
A1 aA1 A3-------(10)
A1 b A3 A2 A1 A3-------(11)
A1 b A3 A2 B A3-------(12)
A1 aBA1 A3-------(13)
A1 b A3-------(14)
B aA1 A3 A3 A2 B
B b A3 A2 A1 A3 A3 A2 B
B b A3 A2 B A3 A3 A2 B
B aBA1 A3 A3 A2 B
B b A3 A3 A2 B

Greibach Normal Form

Step 2. Now all the productions

with A3 on the left have righthand side start with the


terminals. These are used to
replace A3 in the productions :
A2 A3 A1 and the A2 on the
right side of the A1 A2 A3
The result is as follows:

A3 a ---------(1)
A3 b A3 A2-------(2)
A3 b A3 A2 B-------(3)
A3 a B-------(4)
A2 aA1-------(5)
A2 b A3 A2 A1-------(6)
A2 b A3 A2 BA1-------(7)
A2 aBA1-------(8)
A2 b-------(9)

Now replace A2 in A1 A2 A3
production
A1 aA1 A3-------(10)
A1 b A3 A2 A1 A3-------(11)
A1 b A3 A2 B A3-------(12)
A1 aBA1 A3-------(13)
A1 b A3-------(14)
B aA1 A3 A3 A2
B b A3 A2 A1 A3 A3 A2
B b A3 A2 B A3 A3 A2
B aBA1 A3 A3 A2
B b A3 A3 A2
B aA1 A3 A3 A2 B
B b A3 A2 A1 A3 A3 A2 B
B b A3 A2 B A3 A3 A2 B
B aBA1 A3 A3 A2 B
B b A3 A3 A2 B

Potrebbero piacerti anche