0% found this document useful (0 votes)
17 views24 pages

Lecture 10

Uploaded by

Moumer Zaryab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views24 pages

Lecture 10

Uploaded by

Moumer Zaryab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Normal Forms

for
Context-free Grammars

1
Why Normal Form?

• Working with CFG, it is convenient to


have them in simplified form

• Easy one is Chomsky Normal Form

• Usually longer/expanded than initial


CFG

• Helpful for algorithms


2
Chomsky Normal Form

A CFG is in Chomsky normal form, if


every rule is of the form:
A  BC or A a

Where a is any terminal and A, B, and C


are any variables- except that B and C
may not be the start variable.

3
Chomsky Normal Form

Each production has form:


Type-II
Type 1
A  BC or A a

variable variable terminal

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

We will convert it to Chomsky Normal Form

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

Bcde (Not allowed)


A BCDE (Not allowed)
A
Step 5:
Make sure

a (only 1: Must be a Terminal symbol)


A BC (only 2: Must be Variables)
A
7
Example:

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|

We will convert it to Chomsky Normal Form

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

Bcde (Not allowed)


A BCDE (Not allowed)
A

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

Bcde (Not allowed)


A BCDE (Not allowed)
A

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

a (only 1: Must be a Terminal symbol)


A BC (only 2: Must be Variables)
A

Re place
A  bC
With
A  A1C
A1  b

20
The Algorithm – CFG to CNF
Step 5:
Make sure

a (only 1: Must be a Terminal symbol)


A BC (only 2: Must be Variables)
A

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

a (only 1: Must be a Terminal symbol)


A BC (only 2: Must be Variables)
A

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

• Chomsky normal forms are good


for parsing and proving theorems

23
Parsing Trees
• However, Chomsky Normal form has its
own limitations

• It is not most efficient for parsing trees

• This leads to emergences of other


standards/normal form

• One better for parsing is Greibach form

24

You might also like