0% found this document useful (0 votes)
95 views10 pages

CFG To CNF Conversion

Chomsky Normal Form (CNF) requires context-free grammar (CFG) productions to be in specific forms: A→a or A→BC. The document outlines the steps to convert a CFG to CNF, including removing null and unit productions, and restructuring productions with more than two variables. CNF helps avoid ambiguity during parsing and provides an upper bound for parsing complexity.

Uploaded by

Fire Ice Yt
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)
95 views10 pages

CFG To CNF Conversion

Chomsky Normal Form (CNF) requires context-free grammar (CFG) productions to be in specific forms: A→a or A→BC. The document outlines the steps to convert a CFG to CNF, including removing null and unit productions, and restructuring productions with more than two variables. CNF helps avoid ambiguity during parsing and provides an upper bound for parsing complexity.

Uploaded by

Fire Ice Yt
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/ 10

Chomsky Normal Form

CFG is in Chomsky Normal Form if the


productions are in the following forms
A→ a
A→ BC

RHS should be either be two variables or a terminal


Chomsky Normal Form (CNF)
Steps to Convert CFG to CNF

1. If Start Symbol S occurs in some RHS, create new start symbol S1and
new production rule S1 → S

2. Remove Null Productions

3. Remove Unit Productions

4. Replace each production A → B1 | B2 ......Bn when n>2 with


A → B1C where C → B2.....Bn. Repeat this step for all productions
having two or more symbols on RHS

5. If RHS is in form A → aB where a is terminal and A,B are nonterminals


Then production is replace by A → XB and X → a. Repeat this step for
every production which is of the form A → aB
Chomsky Normal Form (CNF)
◼ Example
◼ Convert CFG to CNF

◼ S → ASA | aB
◼A→B|S
◼B→b|€

◼ Above CFG is not in CNF


Chomsky Normal Form (CNF)
◼ Step 1

◼ S occurs in RHS , create new S1

◼ S1 → S
◼ S → ASA | a B
◼A→B|S
◼B→b|€
Chomsky Normal Form (CNF)
◼ Step 2
◼ Remove Null Productions

After removing B → €
S1 → S S1 → S
S → ASA | a B S → ASA | a B | a
A→ B | S A→ B | S| €
B→b|€ B→b
After removing A → €
S1 → S S1 → S
S → ASA | a B | a S → ASA | a B | a | AS | SA | S
A→ B | S| € A→ B | S
B→b B→b
Chomsky Normal Form (CNF)
◼ Step 3
◼ Remove Unit Productions S → S, S1 → S,
A → B, A → S

After removing S → S
S1 → S S1 → S
◼→ ASA | a B | a | AS | SA | S
S S → ASA | a B | a | AS | SA
A→ B | S A→ B | S
◼→ b
B B→b
After removing S1 → S
S1 → ASA | a B | a | AS | SA
S → ASA | a B | a | AS | SA
A→ B | S
B→b
Chomsky Normal Form (CNF)
◼ Step 3
◼ Remove Unit Productions

After removing A → B
S1 → ASA | a B | a | AS | SA S1 → ASA | a B | a | AS | SA
S → ASA | a B | a | AS | SA S → ASA | a B | a | AS | SA
A→ B | S A→ b | S
B→b B→b
After removing A → S
S1 → ASA | a B | a | AS | SA
S → ASA | a B | a | AS | SA
A → b | ASA | a B | a | AS | SA
B→b
Chomsky Normal Form (CNF)
◼ Step 4
◼ Find out productions that have more than
two variables in RHS
Productions that have more than two
variables in RHS
S1 → ASA | a B | a | AS | SA S1 → ASA
S → ASA | a B | a | AS | SA S → ASA
A → b | ASA | a B | a | AS | SA A → ASA
B→b
Replace SA with X
S1 → AX | a B | a | AS | SA
S → AX | a B | a | AS | SA
A → b | AX | a B | a | AS | SA
B→b
X → SA
Chomsky Normal Form (CNF)
◼ Step 5
◼ Change Productions
◼ S1 → aB, S → aB, A → aB
Productions that have one terminal
and one nonterminal in RHS
S1 → AX | a B | a | AS | SA S1 → a B
S → AX | a B | a | AS | SA S→aB
A → b | AX | a B | a | AS | SA A→ a B
B→b
X → SA
Replace a with Y
Required Chomsky Normal Form S1 → AX | Y B | a | AS | SA
S → AX | Y B | a | AS | SA
A → b | AX | Y B | a | AS | SA
B→b
X → SA
Y → a
Chomsky Normal Form(CNF)
◼ Chomsky Normal Form(CNF) puts some
constraints on the grammar rules while
preserving the same language. The benefit is
that if a grammar is in CNF, then we can
avoid the ambiguity problem during
parsing. Another benefit of CNF is that it
provides an upper bound for parsing
complexity.

You might also like