Post on 13-Jan-2022
Fall 2004 COMP 335 1
Simplifications of
Context-Free Grammars
Fall 2004 COMP 335 2
A Substitution Rule
bBaABabBcAaaAAaBS
→→→→→
Substitute
Equivalentgrammar
aABabbcabBcA
aaAAabaBS
→→→→
|
|
bB →
Fall 2004 COMP 335 3
A Substitution Rule
EquivalentgrammarabaAcabbcabBcA
aaAAaaAabaBS
||
||
→→→
aABabbcabBcA
aaAAabaBS
→→→→
|
|
Substitute aAB →
Fall 2004 COMP 335 4
In general:
1yB
xBzA
→
→
Substitute
zxyxBzA 1|→equivalentgrammar
1yB →
Fall 2004 COMP 335 5
Nullable Variables
:production−λ λ→A
Nullable Variable: λ⇒⇒KA
Fall 2004 COMP 335 6
Removing Nullable Variables
Example Grammar:
λ→→→
MaMbM
aMbS
Nullable variable
Fall 2004 COMP 335 7
λ→Mλ→
→→
MaMbM
aMbSSubstitute
abMaMbM
abSaMbS
→→→→
Final Grammar
Fall 2004 COMP 335 8
Unit-Productions
BA→Unit Production:
(a single variable in both sides)
Fall 2004 COMP 335 9
Removing Unit Productions
Observation:
AA→
Is removed immediately
Fall 2004 COMP 335 10
Example Grammar:
bbBABBAaAaAS
→→→→→
Fall 2004 COMP 335 11
bbBABBAaAaAS
→→→→→
SubstituteBA→
bbBBAB
aAaBaAS
→→→→
|
|
Fall 2004 COMP 335 12
Remove
bbBBAB
aAaBaAS
→→→→
|
|
bbBABaA
aBaAS
→→→→ |
BB →
Fall 2004 COMP 335 13
SubstituteAB →
bbBaA
aAaBaAS
→→→ ||
bbBABaA
aBaAS
→→→→ |
Fall 2004 COMP 335 14
Remove repeated productions
bbBaA
aBaAS
→→→ |
bbBaA
aAaBaAS
→→→ ||
Final grammar
Fall 2004 COMP 335 15
Useless Productions
aAAAS
SaSbS
→→→→
λ
KKK ⇒⇒⇒⇒⇒⇒ aAaaaaAaAAS
Some derivations never terminate...
Useless Production
Fall 2004 COMP 335 16
bABA
aAAAS
→→→→
λ
Another grammar:
Not reachable from S
Useless Production
Fall 2004 COMP 335 17
In general:
if wxAyS ⇒⇒⇒⇒ KK
then variable is usefulA
otherwise, variable is uselessA
)(GLw∈
contains only terminals
Fall 2004 COMP 335 18
A production is useless if any of its variables is useless
xA→
DCCBaAAAS
SaSbS
→→→→→→
λ Productionsuseless
useless
useless
useless
Variablesuseless
useless
useless
Fall 2004 COMP 335 19
Removing Useless Productions
Example Grammar:
aCbCaaBaA
CAaSS
→→→→ ||
Fall 2004 COMP 335 20
First: find all variables that can producestrings with only terminals
aCbCaaBaA
CAaSS
→→→→ || },{ BA
AS →
},,{ SBA
Round 1:
Round 2:
Fall 2004 COMP 335 21
Keep only the variablesthat produce terminal symbols:
aCbCaaBaA
CAaSS
→→→→ ||
},,{ SBA
aaBaA
AaSS
→→→ |
(the rest variables are useless)
Remove useless productions
Fall 2004 COMP 335 22
Second: Find all variablesreachable from
aaBaA
AaSS
→→→ |
S A B
Use a Dependency Graph
notreachable
S
Fall 2004 COMP 335 23
Keep only the variablesreachable from S
aaBaA
AaSS
→→→ |
aAAaSS
→→ |
Final Grammar
(the rest variables are useless)
Remove useless productions
Fall 2004 COMP 335 24
Removing All
Step 1: Remove λ-productions
Step 2: Remove Unit-productions
Step 3: Remove Useless productions
Fall 2004 COMP 335 25
Normal Formsfor
Context-free Grammars
Fall 2004 COMP 335 26
Chomsky Normal Form
Each productions has form:
BCA→
variable variable
aA→or
terminal
Fall 2004 COMP 335 27
Examples:
bASAAaSASS
→→→→
Not ChomskyNormal Form
aaASAAAASSASS
→→→→
Chomsky Normal Form
Fall 2004 COMP 335 28
Conversion to Chomsky Normal Form
Example:
AcBaabAABaS
→→→
Not ChomskyNormal Form
Fall 2004 COMP 335 29
AcBaabAABaS
→→→
Introduce variables for terminals:
cTbTaTATB
TTTAABTS
c
b
a
c
baa
a
→→→→→→
cba TTT ,,
Fall 2004 COMP 335 30
Introduce intermediate variable:
cTbTaTATB
TTTAABTS
c
b
a
c
baa
a
→→→→→→
cTbTaTATB
TTTABTVAVS
c
b
a
c
baa
a
→→→→→→→
1
1
1V
Fall 2004 COMP 335 31
Introduce intermediate variable:
cTbTaTATB
TTVVTA
BTVAVS
c
b
a
c
ba
a
a
→→→→→→→→
2
2
1
1
2V
cTbTaTATB
TTTABTVAVS
c
b
a
c
baa
a
→→→→→→→
1
1
Fall 2004 COMP 335 32
Final grammar in Chomsky Normal Form:
cTbTaTATB
TTVVTA
BTVAVS
c
b
a
c
ba
a
a
→→→→→→→→
2
2
1
1
AcBaabAABaS
→→→
Initial grammar
Fall 2004 COMP 335 33
From any context-free grammar(which doesn’t produce )not in Chomsky Normal Form
we can obtain:An equivalent grammar in Chomsky Normal Form
In general:
λ
Fall 2004 COMP 335 34
The Procedure
First remove:
Nullable variables
Unit productions
Fall 2004 COMP 335 35
Then, for every symbol :a
In productions: replace with a aT
Add production aTa →
New variable: aT
Fall 2004 COMP 335 36
Replace any production nCCCA L21→
with
nnn CCV
VCVVCA
12
221
11
−− →
→→
K
New intermediate variables: 221 ,,, −nVVV K
Fall 2004 COMP 335 37
Theorem: For any context-free grammar(which doesn’t produce )there is an equivalent grammar in Chomsky Normal Form
λ
Fall 2004 COMP 335 38
Observations
• Chomsky normal forms are goodfor parsing and proving theorems
• It is very easy to find the Chomsky normalform for any context-free grammar
Fall 2004 COMP 335 39
Greibach Normal Form
All productions have form:
kVVVaA L21→
symbol variables
0≥k
Fall 2004 COMP 335 40
Examples:
bBbbBaAA
cABS
→→→
||
GreibachNormal Form
aaSabSbS
→→
Not GreibachNormal Form
Fall 2004 COMP 335 41
aaSabSbS
→→
Conversion to Greibach Normal Form:
bTaT
aTSSTaTS
b
a
a
bb
→→→→
GreibachNormal Form
Fall 2004 COMP 335 42
Theorem: For any context-free grammar(which doesn’t produce ) there is an equivalent grammarin Greibach Normal Form
λ
Fall 2004 COMP 335 43
Observations
• Greibach normal forms are very goodfor parsing
• It is hard to find the Greibach normalform of any context-free grammar
Fall 2004 COMP 335 44
The CYK Parser
Fall 2004 COMP 335 45
The CYK Membership Algorithm
Input:
• Grammar in Chomsky Normal Form G
• String
Output:
find if )(GLw∈
w
Fall 2004 COMP 335 46
The Algorithm
• Grammar :G
bBABBaABBAABS
→→→→→
• String : w aabbb
Input example:
Fall 2004 COMP 335 47
a a b b b
aa ab bb bb
aab abb bbb
aabb abbb
aabbb
aabbb
Fall 2004 COMP 335 48
aA
aA
bB
bB
bB
aa ab bb bb
aab abb bbb
aabb abbb
aabbb
bBABBaABBAABS
→→→→→
Fall 2004 COMP 335 49
a A
a A
b B
b B
b B
aa
ab S,B
bb A
bb A
aab abb bbb
aabb abbb
aabbb
bBABBaABBAABS
→→→→→
Fall 2004 COMP 335 50
aA
aA
bB
bB
bB
aa abS,B
bbA
bbA
aabS,B
abbA
bbbS,B
aabbA
abbbS,B
aabbbS,B
bBABBaABBAABS
→→→→→
Fall 2004 COMP 335 51
Therefore: )(GLaabbb ∈
Time Complexity: 3|| w
The CYK algorithm can be easily converted to a parser(bottom up parser)
Observation: