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.

CNF and GNF

Embed Size (px)

Citation preview

Page 1: CNF and GNF

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.

Page 2: CNF and GNF

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

Page 3: CNF and GNF

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.

Page 4: CNF and GNF

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

Page 5: CNF and GNF

• AbAA ----(3)

D AA

AC1 D

• B aBB ----(6)

EBB BC2E

Page 6: CNF and GNF

• A a ----(5)• Bb ----(8) C1 b

S C1A• C2 a

S C2B• A C2S• BC1S

A C1AA

• B aBB ----(6)

B C2BB

Page 7: CNF and GNF

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.

Page 8: CNF and GNF

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)

Page 9: CNF and GNF

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)

Page 10: CNF and GNF

Greibach Normal Form

Since the right side of the production

A3 A2 A3 A2 ---------(3)Begin with the lower-

numbered 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)

Page 11: CNF and GNF

Lemma 4.4

Let G =(V,T,P,S) be the context free grammar CFG

Let A Aα1|Aα2|Aα3………….. Aαr 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) Aβi

A βiB Where 1 =< i <= s

2) B αi

B αiB where 1 =< i <= r

Then L(G) = L(G1)

Page 12: CNF and GNF

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

Page 13: CNF and GNF

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)

Page 14: CNF and GNF

Greibach Normal Form Step 2. Now all the productions

with A3 on the left have right-hand 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)

Page 15: CNF and GNF

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

Page 16: CNF and GNF

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

Page 17: CNF and GNF

Greibach Normal Form Step 2. Now all the productions with A3 on the left have right-hand 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