0% found this document useful (0 votes)
84 views30 pages

Killing Null Productions in CFGs

Uploaded by

Ali Ahmad
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
0% found this document useful (0 votes)
84 views30 pages

Killing Null Productions in CFGs

Uploaded by

Ali Ahmad
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

Chapter 13

(Λ Productions, Unit Production, CNF)


Λ Productions
• Productions of the form
– N→Λ
– are called null (Λ) productions
• All grammars that generate the Λ string include at
least one null production
• Some grammars that do not generate Λ string still
might contain null productions
– S → aX
– X→Λ
Λ Productions

• Hazards of Λ Productions
– Create ambiguity in word derivation
• Solution
– Kill Them !!!
Killing Null Productions
• Theorem
– If L is a context free language generated by CFG
that includes Λ-productions then there is a
different CFG that has no Λ- productions that
generates exactly the same language L with the
exception of only Λ.
Killing Λ Productions

• Constructive Algorithm
– Identify Null Productions
– Remove each of them one by one
– For each NT having a null production, add
productions where the NT has been replaced
by null
• Example
– S  aSa | bSb |Λ becomes
– S  aSa | bSb |aa |bb
• S  aSa
• S  bSb
• SΛ
• ………………
• S aSa
• Saa
• SbSb
• S bb
Killing Λ Productions
• Problem Identified !!!
– S  a | Xb | aYa
–XY|Λ
–Yb|X
– -----------------
– S a | Xb | b | aYa | aa
– X Y
– Y b | X
Killing Λ Productions

• Null able Non-terminal


– In CFG a nonterminal N is called nullable if
• There is a production N → Λ, or
• There is a derivation that starts at N and leads
to Λ (N  ….  Λ)
Killing Λ Productions
• Problem Solved !!!
• Modified Replacement Rule
– Delete all Λ-productions
– Add the following productions: For every
production X → old string add new productions of
the form X → .. Where the right side will account
for any modification of the old string that can be
formed by deleting all possible subsets of
nullable nonterminals while avoiding introduction
of a null production in this process
Killing Null Productions
• Not So Fast !!!!!!!!!!
– S → Xay | YY | aX | ZYX
– X → Za | bZ | ZZ | Yb
– Y → Ya| XY | Λ
– Z → aX | YYY
– How could one identify a nullable NT in
such a complex grammar
• Solution
– A bucket of Blue Paint
Example
Consider the CFG
S  a | Xb | aYa
XY|Λ
Yb|X
Old New So the new CFG is
Production Production
Sa nothing
S  Xb Sb S  a | Xb | aa | aYa |b
S  aYa S  aa XY
XY nothing Yb|X
XΛ nothing
Yb nothing
YX nothing

11
Example
Consider the CFG
S  Xa
X  aX | bX | Λ

Old nullable New So the new CFG is


Production
roduction S  a | Xa
S  Xa Sa X  aX | bX | a | b

X  aX Xa

X  bX Xb
12
Example

S  XY
X  Zb

Y  bW
Z  AB
WZ
A  aA | bA | Λ
B  Ba | Bb | Λ

13
S  XY
X  Zb
Y  bW
Z  AB Example Contd.
WZ
A  aA | bA | Λ
B  Ba | Bb | Λ

Old nullable New So the new CFG is


Production Production
X  Zb Xb S  XY
Y  bW Yb X  Zb | b
Z  AB Z  A and Z  Y  bW | b
B
WZ Nothing new Z  AB | A | B
A  aA Aa WZ
A  bA Ab
B  Ba B a
A  aA | bA | a | b
B  Bb Bb B  Ba | Bb | a | b
14
Unit Productions
• A production of the form
– Nonterminal → one Nonterminal
– Is called a unit production

• Unit productions are some times required to


change the form of a working string
– (Arbitrary)A(arbitrary) aaDa
– (Arbitrary)B(Arbitrary)

• Unit Production are also problematic and


thus need to be exterminated
Killing Unit Productions
• Theorem
– If there is a CFG for the language that has no Λ-
productions, then there is also a CFG for L with no
Λ-productions and no unit productions
Killing Unit Productions
• Naïve Elimination Rule
– Eliminate unit productions one by one and replace them
with new productions without changing the language
being generated by the CFG
– Infinite loop and no benefit
• Example
– S → A |bb
– A→B|b
– B→S|a

• Modified Elimination Rule


– Eliminate all unit productions simultaneously
– Look for any sequence of productions that lead to a
replacement with a unit production. Replace all such
derived unit productions with the final replacement.
• S aBD
• D A
• A aB| bA | b
• B--> aa
• …………………
• S aBD
• DaB | bA | b
• A aB | bA | b
• B aa
Killing Unit Productions
• Example
– S → A | bb
– A→B|b
– B→S|a
• Unit Productions
– S→A
– A→B
– B→S
• Derived Unit Production
– S→A→B
– A→B→S
– B→S→A
Killing Unit Productions
– S → A | bb
– A→B|b
– B→S|a

• New CFG
– S → bb|b|a
– A → b|a|bb
– B → a|bb|b
– S a | Xb | b | aYa | aa
– X Y
– Y b | X
– -----------------
– S a | Xb | b | aYa | aa
– X b
– Y b
New Format for CFG
• Theorem
– If L is a language generated by some CFG,
then there is another CFG that generated
all the non-Λ words of L, all of whose
productions are of one of the two basic
forms
• Nonterminal → string of only Nonterminals
• Nonterminal → one terminal
• S ABD
• Aa
New Format for CFG
• Proof
– Suppose a CFG contains non terminals S, X1, X2,X3 …
and two terminals a and b
– Add two new nonterminals A and B and two productions
• A→a
• B→b
– For every previous production involving terminals,
replace each a with the nonterminal a and b with the
nonterminal B
– Any production which is already in the desired form
should be left untouched to avoid introduction of unit
productions
– All the productions now are of the form
• Nonterminal → strings of only nonterminals
• Nonterminal → one terminal
New format for CFG
• Example
– S → X1 | X2aX2 | aSb | b
– X1 → X2X2 | b
– X2 → aX2 | aaX1
– -------------------------------------------

– S X2X2 | X2aX2 | aSb | b


– X1 → X2X2 | b
– X2 → aX2 | aaX1
– S X2X2 | X2aX2 | aSb | b
– X1 → X2X2 | b
– X2 → aX2 | aaX1

– ……………………………………
• S X2X2 | X2 A X2 | ASB| b
• A a
• B b
• X1 → X2X2 | b
• X2 → AX2 | AAX
• S X2X2 | X2 A X2 | ASB| b
• A a
• B b
• X1 → X2X2 | b
• X2 → AX2 | AAX
• -----------------------------
• S X2X2 | D X2 | AC| b
• D X2 A
• C SB
• A a
• B b
Chomsky Normal Form: The
Ultimate Target !
• If a CFG has only productions of the form
– Nonterminals → strings of exactly two
Nonterminals
– Nonterminals → one terminal
• It is said to be in Chomsky Normal Form, or
CNF
• Theorem
– For any context Free language L, the non Λ words
of the language can be generated by a CFG in CNF
format
CNF
• Proof
– Any CFG can be converted to the following
format
• Nonterminal → strings of Nonterminals or
• Nonterminal → one terminal
– For this new CFG modify the productions so that
they become in the CNF
– This conversion requires addition of new
nonterminals
• S → X1X2X3X4 will be converted to
– S → AB
– A → X1X2
– B → X3X4
CNF
• Example
– S → aSa | bSb | a | b | aa | bb

– SASA | BSB | a | b | AA | BB
– Aa
– Bb
– ……………...
Quiz Time

You might also like