Lecture 10
Lecture 10
for
Context-free Grammars
1
Why Normal Form?
3
Chomsky Normal Form
4
Examples:
𝑆→𝐴𝐶 𝑆→𝐴𝑆
Chomsky Not Chomsky
Normal Form Normal Form
5
Conversion to Chomsky Normal Form
Example: S ABa
Not Chomsky
A aab Normal Form
B Ac
6
The Algorithm – CFG to CNF
Step 1:
Make sure start symbol does not
Appear on right hand side.
Ɛ (Not allowed)
Step 2:
Unless S Ɛ (Allowed)
Remove Rules like A
Step 3:
B (Not allowed)
Get Rid of all unit rules
A
Reason: One non-terminal going to be another non-terminal is
Just an overhead in parse tree
Step 4:
Get rid of rules with more than 2 symbols on right hand side
S ASA | aB
A B|S
B b|
Not Chomsky
Normal Form
8
Conversion to Chomsky Normal Form
Example: S ASA | aB
Not Chomsky
A B|S
Normal Form
B b|
9
The Algorithm – CFG to CNF
Step 1:
Make sure start symbol does not
Appear on right hand side.
S0 S
S ASA | aB
S ASA | aB
A B|S
A B|S
B b|
B b|
10
The Algorithm – CFG to CNF
Ɛ (Not allowed)
Step 2:
Unless S Ɛ (Allowed)
Remove Rules like A
S0 S
S ASA | aB
A B|S
B b|
S0 S
S ASA | aB | a
A B | S |
B b
11
The Algorithm – CFG to CNF
Ɛ (Not allowed)
Step 2:
Remove Rule A
S0 S
S ASA | aB | a
A B | S |
B b
S0 S
S ASA | aB | a | SA | AS | S
A B|S
B b
12
The Algorithm – CFG to CNF
Step 3:
B (Not allowed)
Get Rid of all unit rules
A
Reason: One non-terminal going to be another non-terminal is
Just an overhead in parse tree
S0 S
S ASA | aB | a | SA | AS | S
A B|S
B b
13
The Algorithm – CFG to CNF
Step 3:
B (Not allowed)
Get Rid of all unit rules
A
Reason: One non-terminal going to be another non-terminal is
Just an overhead in parse tree
S0 S
S ASA | aB | a | SA | AS | S
A B|S
B b
Remove S S
S0 S
S ASA | aB | a | SA | AS
A B|S
B b
14
The Algorithm – CFG to CNF
Step 3:
B (Not allowed)
Get Rid of all unit rules
A
Reason: One non-terminal going to be another non-terminal is
Just an overhead in parse tree
S0 S
S ASA | aB | a | SA | AS
A B|S
B b
Remove S0 S
S 0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A B|S
B b
15
The Algorithm – CFG to CNF
Step 3:
B (Not allowed)
Get Rid of all unit rules
A
Reason: One non-terminal going to be another non-terminal is
Just an overhead in parse tree
S 0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A B|S
B b
Remove A B
S 0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b|S
B b
16
The Algorithm – CFG to CNF
Step 3:
B (Not allowed)
Get Rid of all unit rules
A
Reason: One non-terminal going to be another non-terminal is
Just an overhead in parse tree
S 0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b|S
B b
Remove A S
S 0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
B b
17
The Algorithm – CFG to CNF
Step 4:
Get rid of rules with more than 2 symbols on right hand side
e.g .
A BCDE
Re placeWith
A BA1
A1 CA2
A2 DE
18
The Algorithm – CFG to CNF
Step 4:
Get rid of rules with more than 2 symbols on right hand side
S 0 ASA | aB | a | SA | AS
S ASA | aB | a | SA | AS
A b | ASA | aB | a | SA | AS
B b
S 0 AA1 | aB | a | SA | AS
S AA1 | aB | a | SA | AS
A b | AA1 | aB | a | SA | AS
B b
A1 SA
19
The Algorithm – CFG to CNF
Step 5:
Make sure
Re place
A bC
With
A A1C
A1 b
20
The Algorithm – CFG to CNF
Step 5:
Make sure
S 0 AA1 | aB | a | SA | AS
S AA1 | aB | a | SA | AS
A b | AA1 | aB | a | SA | AS
B b S 0 AA1 | A2 B | a | SA | AS
A1 SA S AA1 | A2 B | a | SA | AS
A b | AA1 | A2 B | a | SA | AS
B b
A1 SA
A2 a
21
The Algorithm – CFG to CNF
Step 5:
Make sure
S 0 AA1 | A2 B | a | SA | AS
S AA1 | A2 B | a | SA | AS
S ASA | aB
A b | AA1 | A2 B | a | SA | AS
A B|S
B b
B b|
A1 SA
A2 a
Chomsky Not Chomsky
Normal Form Normal Form
22
Observations
23
Parsing Trees
• However, Chomsky Normal form has its
own limitations
24