100% found this document useful (1 vote)
234 views33 pages

Ch-6 CNF and GNF

The document discusses converting context-free grammars to Chomsky normal form and Greibach normal form. It describes three preliminary simplifications of eliminating useless symbols, epsilon productions, and unit productions. It then explains how to arrange productions in Chomsky normal form by breaking bodies into cascades of two variables. Finally, it provides an example of converting a grammar to Greibach normal form by rewriting productions with the form A->aX where a is a terminal and X is nonterminals.

Uploaded by

TEJES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
234 views33 pages

Ch-6 CNF and GNF

The document discusses converting context-free grammars to Chomsky normal form and Greibach normal form. It describes three preliminary simplifications of eliminating useless symbols, epsilon productions, and unit productions. It then explains how to arrange productions in Chomsky normal form by breaking bodies into cascades of two variables. Finally, it provides an example of converting a grammar to Greibach normal form by rewriting productions with the form A->aX where a is a terminal and X is nonterminals.

Uploaded by

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

Department of Computer and Science Engineering (CSE)

UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Theory of Computation (CST-353)

Topic: CNF and GNF DISCOVER . LEARN .


EMPOWER
Department of Computer and Science Engineering (CSE)

Learning Objectives & Outcomes


Objective:
• To understand the concept of CNF and GNF.

Outcome:
• Student will understand the
 CNF
 GNF
Chomsky Normal Form
• A context free grammar is said to be in Chomsky Normal
Form if all productions are in the following form:
A → BC
A→α
• A, B and C are non terminal symbols
• α is a terminal symbol
Department of Computer and Science Engineering (CSE)

Preliminary Simplifications

There are three preliminary simplifications

1 Eliminate Useless Symbols

2 Eliminate ε productions

3 Eliminate unit productions


Department of Computer and Science Engineering (CSE)

Eliminate Useless Symbols:

We need to determine if the symbol is useful by


identifying if a symbol is generating and is reachable

* ω for some terminal string ω.


• X is generating if X 
* αXβ
• X is reachable if there is a derivation X 
for some α and β
Department of Computer and Science Engineering (CSE)

Example: Removing non-generating symbols

S → AB | a
A→b Initial CFL grammar

S → AB | a
Identify generating symbols
A→b

S→a
Remove non-generating
A→b
Department of Computer and Science Engineering (CSE)

Example: Removing non-reachable symbols

S→a
Identify reachable symbols
A→b

S→a Eliminate non-reachable


Department of Computer and Science Engineering (CSE)

The order is important.

Looking first for non-reachable symbols and then


for non-generating symbols can still leave some
useless symbols.

S → AB | a S→a
A→b A→b
Department of Computer and Science Engineering (CSE)

Finding generating symbols

If there is a production A → α, and every symbol


of α is already known to be generating. Then A is
generating

We cannot use S → AB
S → AB | a because B has not been
A→b established to be generating
Department of Computer and Science Engineering (CSE)

Finding reachable symbols

S is surely reachable. All symbols in the body of a


production with S in the head are reachable.

S → AB | a In this example the symbols


A→b {S, A, B, a, b} are reachable.
Department of Computer and Science Engineering (CSE)

Eliminate ε Productions

• In a grammar ε productions are convenient but


not essential
• If L has a CFG, then L – {ε} has a CFG

* ε
A 

Nullable variable
Department of Computer and Science Engineering (CSE)

If A is a nullable variable

• Whenever A appears on the body of a production


A might or might not derive ε

S → ASA | aB
A→B|S Nullable: {A, B}
B→b|ε
Department of Computer and Science Engineering (CSE)

Eliminate ε Productions

• Create two version of the production, one with


the nullable variable and one without it
• Eliminate productions with ε bodies

S → ASA | aB S → ASA | aB | AS | SA | S | a
A→B|S A→B|S
B→b|ε B→b
Department of Computer and Science Engineering (CSE)

Eliminate ε Productions

• Create two version of the production, one with


the nullable variable and one without it
• Eliminate productions with ε bodies

S → ASA | aB S → ASA | aB | AS | SA | S | a
A→B|S A→B|S
B→b|ε B→b
Department of Computer and Science Engineering (CSE)

Eliminate ε Productions

• Create two version of the production, one with


the nullable variable and one without it
• Eliminate productions with ε bodies

S → ASA | aB S → ASA | aB | AS | SA | S | a
A→B|S A→B|S
B→b|ε B→b
Department of Computer and Science Engineering (CSE)

Eliminate unit productions

A unit production is one of the form A → B where both


A and B are variables

Identify unit pairs

* B
A 

A → B, B → ω, then A → ω
Department of Computer and Science Engineering (CSE)

Example:
T = {*, +, (, ), a, b, 0, 1} Pairs Productions
( E, E ) E→E+T
I → a | b | Ia | Ib | I0 | I1 ( E, T ) E→T*F
F → I | (E) ( E, F ) E → (E)
T→F|T*F ( E, I ) E → a | b | Ia | Ib | I0 | I1

E→T|E+T ( T, T ) T→T*F
( T, F ) T → (E)
( T, I ) T → a | b | Ia |Ib | I0 | I1
Basis: (A, A) is a unit pair ( F, F ) F → (E)
of any variable A, if ( F, I ) F → a | b | Ia | Ib | I0 | I1
A  * A by 0 steps. ( I, I ) I → a | b | Ia | Ib | I0 | I1
Department of Computer and Science Engineering (CSE)

Example:
Pairs Productions
… …
( T, T ) T→T*F
( T, F ) T → (E)
( T, I ) T → a | b | Ia |Ib | I0 | I1
… …

I → a | b | Ia | Ib | I0 | I1
E → E + T | T * F | (E ) | a | b | la | lb | l0 | l1
T → T * F | (E) | a | b | Ia | Ib | I0 | I1
F → (E) | a | b | Ia | Ib | I0 | I1
Department of Computer and Science Engineering (CSE)

Final Simplification
Chomsky Normal Form (CNF)

Starting with a CFL grammar with the preliminary


simplifications performed

1. Arrange that all bodies of length 2 or more to


consists only of variables.
2. Break bodies of length 3 or more into a cascade of
productions, each with a body consisting of two
variables.
Department of Computer and Science Engineering (CSE)

Step 1: For every terminal α that appears in a body


of length 2 or more create a new variable that has
only one production.
E → E + T | T * F | (E ) | a | b | la | lb | l0 | l1
T → T * F | (E) | a | b | Ia | Ib | I0 | I1
F → (E) | a | b | Ia | Ib | I0 | I1
I → a | b | Ia | Ib | I0 | I1
E → EPT | TMF | LER | a | b | lA | lB | lZ | lO
T → TMF | LER | a | b | IA | IB | IZ | IO
F → LER | a | b | IA | IB | IZ | IO
I → a | b | IA | IB | IZ | IO
A→a B→b Z→0 O→1
P→+ M→* L→( R→)
Department of Computer and Science Engineering (CSE)

Step 2: Break bodies of length 3 or more adding


more variables

E → EPT | TMF | LER | a | b | lA | lB | lZ | lO


T → TMF | LER | a | b | IA | IB | IZ | IO
C1 → PT
F → LER | a | b | IA | IB | IZ | IO
I → a | b | IA | IB | IZ | IO C2 → MF
A→a B→b Z→0 O→1 C3 → ER
P→+ M→* L→( R→)
Department of Computer and Science Engineering (CSE)

Greibach Normal Form

A context free grammar is said to be in Greibach


Normal Form if all productions are in the following
form:

A → αX

• A is a non terminal symbols


• α is a terminal symbol
• X is a sequence of non terminal symbols.
It may be empty.
Department of Computer and Science Engineering (CSE)

Example:

S → XA | BB S = A1 A1 → A2A3 | A4A4
B → b | SB X = A2 A4 → b | A1A4
X→b A = A3 A2 → b
A→a
B = A4 A3 → a

CNF New Labels Updated CNF


Department of Computer and Science Engineering (CSE)

Example:

A1 → A2A3 | A4A4 First Step Ai → AjXk j > i


A4 → b | A1A4
A2 → b Xk is a string of zero
A3 → a
or more variables

A4 → A1A4
Department of Computer and Science Engineering (CSE)

Example:

First Step Ai → AjXk j > i

A4 → A1A4 A1 → A2A3 | A4A4


A4 → A2A3A4 | A4A4A4 | b A4 → b | A1A4
A4 → bA3A4 | A4A4A4 | b A2 → b
A3 → a
Department of Computer and Science Engineering (CSE)

Example:

A1 → A2A3 | A4A4 Second Step


A4 → bA3A4 | A4A4A4 | b
Eliminate Left
A2 → b Recursions
A3 → a

A4 → A4A4A4
Department of Computer and Science Engineering (CSE)

Example:
Second Step
Eliminate Left
Recursions

A4 → bA3A4 | b | bA3A4Z | bZ A1 → A2A3 | A4A4


Z → A4A4 | A4A4Z A4 → bA3A4 | A4A4A4 | b
A2 → b
A3 → a
Department of Computer and Science Engineering (CSE)

Example:

A1 → A2A3 | A4A4
A4 → bA3A4 | b | bA3A4Z | bZ A → αX
Z → A4A4 | A4A4 Z
A2 → b GNF
A3 → a
Department of Computer and Science Engineering (CSE)

Example:
A1 → A2A3 | A4A4
A4 → bA3A4 | b | bA3A4Z | bZ
Z → A4A4 | A4A4 Z
A2 → b
A3 → a

A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4


Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4
Department of Computer and Science Engineering (CSE)

Example:

A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4


A4 → bA3A4 | b | bA3A4Z | bZ
Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4
A2 → b
A3 → a

Grammar in Greibach Normal Form


Department of Computer and Science Engineering (CSE)

Summary (Some properties)


• Every CFG that doesn’t generate the empty string
can be simplified to the Chomsky Normal Form and
Greibach Normal Form
• The derivation tree in a grammar in CNF is a binary
tree
• In the GNF, a string of length n has a derivation of
exactly n steps
• Grammars in normal form can facilitate proofs
• CNF is used as starting point in the algorithm CYK
Department of Computer and Science Engineering (CSE)

FAQ :
1. Convert the following grammar to the Chomsky Normal Form.
S→P
P → aPb | ε
2. Is the following grammar context-free?
S → aBSc | abc
Ba → aB
Bb → bb
3. Prove that the language L = { anbncn | n ≥ 1 } is not context-free.
4. Convert the following grammar to the Greibach Normal Form.
S -> a | CD | CS
A -> a | b | SS
C -> a
D -> AS
Department of Computer and Science Engineering (CSE)

REFERENCES :
• Martin J.C., “Introduction to Languages and Theory of
Computation”, Tata McGraw-Hill Publising Company
Limited, 3rd Edition.
• Hopcroft J.E. and Ullman J.D., “Introduction to Automata
Theory Languages and Computation”, Narosa
Publications.
• https://www.erode-sengunthar.ac.in/wp-content/
uploads/2019/04/NORMAL-FORMS.ppt

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy