Vous êtes sur la page 1sur 46

SIMPLIFICATION OF CONTEXT

FREE DIAGRAMS

Presentation On “CHOMSKY
NORMAL FORM”
BY
GAURAV NIGAM
IInd B.Tech
COMPUTER SCIENCE & ENGG. DEPARTMENT
H.B.T.I. KANPUR
AGENDA
Simplification of CFG
Removing useless Productions
Removing λ- Productions
Removing unit- Productions
Normal Forms :
Chomsky Normal Form
Example
In general:
A → xBz

B → y1

Substitute
B → y1

equivalent
A → xBz | xy1z grammar
A Substitution Rule
Equivalent
grammar
S → aB
S → aB | ab
A → aaA
Substitute A → aaA
A → abBc B→b A → abBc | abbc
B → aA
B → aA
B→b
A Substitution Rule
S → aB | ab
A → aaA
A → abBc | abbc
B → aA
Substitute
B → aA
S → aB | ab | aaA
A → aaA Equivalent
A → abBc | abbc | abaAc grammar
Substitution Rule
Let G = (V,T,S,P) be a context-free
grammar. & p be the production of form
A→ x1 B x2
Assume that A and B are different variable
and that
B → y1|y2|…|yn
A→ x1y1x2|x1y2x2|….x1ynx2
Example

A→ a | aaA |
A→a | aaA |
ababbAc |
abBc
Substitute abbc
B
B→ abbA | b
B→ abbA | b
Simplification of
CFG
Removing use less Production
Let G = (V,T,S,P) be a CFG. A variable AεV is
said to be useful iff there is at least one w ε
L(G) such that
S→* xAy→ * w,
with x,y,in(V џ T)*
Useless Productions
S → aSb
S →λ
S→A
A → aA Useless Production

Some derivations never terminate...

S ⇒ A ⇒ aA ⇒ aaA ⇒  ⇒ aa  aA ⇒ 
Another grammar:

S→A
A → aA
A→λ
B → bA Useless Production

Not reachable from S


In general: contains only
terminals
if S ⇒  ⇒ xAy ⇒  ⇒ w

w∈ L(G )

then variable A is useful

otherwise, variable A is useless


A production A → x is useless
if any of its variables is useless

S → aSb
S →λ Productions
Variables S→A useless

useless A → aA useless
useless B→C useless

useless C→D useless


Removing Useless Productions

Example Grammar:
S → aS | A | C
A→a
B → aa
C → aCb
First: find all variables that can produce
strings with only terminals

S → aS | A | C Round 1: { A, B}
A→a S→A
B → aa
C → aCb Round 2: { A, B, S }
Keep only the variables
that produce terminal symbols: { A, B, S }
(the rest variables are useless)

S → aS | A | C
A→a S → aS | A
B → aa A→a
C → aCb B → aa
Remove useless productions
Second: Find all variables
reachable from S

Use a Dependency Graph

S → aS | A
A→a S A B
B → aa not
reachable
Keep only the variables
reachable from S
(the rest variables are useless)

Final Grammar
S → aS | A
S → aS | A
A→a
A→a
B → aa

Remove useless productions


Removing λ -Production

Any production of a CFG of the form


A→ λ
is called a λ- production. Any variable for
which the derivation
A→ * λ
is possible is called Null able .
Example
S→ ABAC
A→ Aa | λ
B→ bB | λ
C→ c
Example
• S→ ABAC S→ ABC | BAC | BC
• A→ Aa | λ A→ a
• B→ bB | λ
• C→ c

Right side that


Contains A
Occurrence of A
S→ ABAC
A→ Aa
Cont…

• S→ ABAC • S→ABAC | ABC |


• A→ Aa | λ BAC | BC
• B→ bB | λ • A→ Aa | a
• C→ c • B→ bB | λ
• C →c
Cont…
S→ABAC | ABC |
• S→ABAC | ABC |
BAC | BC
BAC | BC
| AAC | AC | C
• A→ Aa | a
A →Aa | a
• B→ bB | λ
B →bB | b
• C →c
C →c

Final grammar
Removing unit production
Any production of a context- free grammar
of the form
A→ B,
where A,B ε V is called
Unit-Production
Example Grammar:

S → aA
A→a
A→ B
B→A
B → bb
S → aA
S → aA | aB
A→a
Substitute A→a
A→ B A→ B B → A| B
B→A
B → bb
B → bb
S → aA | aB S → aA | aB
A→a Remove A→a
B → A| B B→B B→A
B → bb B → bb
S → aA | aB
S → aA | aB | aA
A→a Substitute
B→A A→a
B→A
B → bb
B → bb
Remove repeated productions

Final grammar
S → aA | aB | aA S → aA | aB
A→a A→a
B → bb B → bb
Chomsky
Normal Form
Chomsky Normal Form
Each productions has form:

A → BC or A→a

variable variable terminal


Convertion to Chomsky
Normal Form
S → ABa
• Example: A → aab
B → Ac
Not Chomsky
Normal Form
Introduce variables for terminals: Ta , Tb , Tc

S → ABTa
S → ABa A → TaTaTb
A → aab B → ATc
B → Ac Ta → a
Tb → b
Tc → c
Introduce intermediate variable: V1

S → AV1
S → ABTa
V1 → BTa
A → TaTaTb
A → TaTaTb
B → ATc
B → ATc
Ta → a
Ta → a
Tb → b
Tb → b
Tc → c
Tc → c
Introduce intermediate variable: V2
S → AV1
S → AV1
V1 → BTa
V1 → BTa
A → TaV2
A → TaTaTb
V2 → TaTb
B → ATc
B → ATc
Ta → a
Ta → a
Tb → b
Tb → b
Tc → c
Tc → c
Final grammar in Chomsky Normal Form:
S → AV1
V1 → BTa
A → TaV2
Initial grammar
V2 → TaTb
S → ABa B → ATc
A → aab Ta → a
B → Ac Tb → b
Tc → c
Another example
• S → ASA | aB
• A→B|S
• B→ b|λ
• I will convert this to Chomsky Normal
Form………….
Eliminating the rule B → λ

• So → S • So → S
• S → ASA | aB • S → ASA | aB | a
• A→ B|S • A→ B|S|λ
• B→ b|λ • B→ b
Eliminating the rule A → λ

• So → S • So → S
• S → ASA | aB | a • S → ASA | AS | SA | S
• A→ B|S|λ | aB | a
• B→ b • A→ B|S
• B→ b
Eliminating the rule S → S

• So → S • So → S
• S → ASA | AS | SA | S • S → ASA | AS | SA |
| aB | a aB | a
• A→ B|S • A→ B|S
• B→ b • B→ b
Eliminating the rule So→ S

• So → S • So → ASA | AS | SA
| aB | a
• S → ASA | AS | SA |
aB | a • S → ASA | AS | SA |
aB | a
• A→ B|S
• A→ B|S
• B→ b
• B→ b
Eliminating the rule A→ B

• S0 → ASA | AS | SA • S0 → ASA | AS | SA |
| aB | a aB | a
• S → ASA | AS | SA | • S → ASA | AS | SA |
aB | a aB | a
• A→ B|S • A→ S|b
• B→ b • B→ b
Eliminating the rule A→ S

• S0 → ASA | AS | SA | • S0 → ASA | AS | SA |
aB | a aB | a
• S → ASA | AS | SA | • S → ASA | AS | SA |
aB | a aB | a
• A→ S|b • A → ASA | AS | SA |
• B→ b aB | a | b
• B→ b
Spliting the rule S→ ASA

• S0 → ASA | AS | • S0 → AA1 | AS | SA |
aB | a
SA | aB | a
• S → AA1 | AS | SA |
• S → ASA | AS | aB | a
SA | aB | a • A → AA1 | AS | SA |
• A → ASA | AS | aB | a | b
SA | aB | a | b • A1 → SA
• B→ b • B→ b
Final Grammar

• S0 → AA1 | AS | SA | UB | a
• S → AA1 | AS | SA | UB | a
• A → AA1 | AS | SA | UB | a | b
• A1→ SA
• B→ b
• U→a
Question ???
• Questions and Comments are welcome…

• ?
• THANKS

• Have a great Day !

Vous aimerez peut-être aussi