Fall 2006 Costas Buch - RPI 1 Simplifications of Context-Free Grammars
Mar 19, 2016
Fall 2006 Costas Buch - RPI 1
Simplifications of
Context-Free Grammars
Fall 2006 Costas Buch - RPI 2
A Substitution Rule
bBaABabBcAaaAAaBS
Substitute
Equivalentgrammar
aABabbcabBcA
aaAAabaBS
|
|
bB
Fall 2006 Costas Buch - RPI 3
EquivalentgrammarabaAcabbcabBcA
aaAAaaAabaBS
||
||
aABabbcabBcA
aaAAabaBS
|
|
Substitute aAB
Fall 2006 Costas Buch - RPI 4
In general:
1yB
xBzA
Substitute
zxyxBzA 1|equivalentgrammar
1yB
Fall 2006 Costas Buch - RPI 5
Nullable Variables:production X
Nullable Variable: Y
Example:
MaMbMaMbS
Nullable variable production
Fall 2006 Costas Buch - RPI 6
M
MaMbMaMbS
SubstituteabaMbMabaMbS|
|
Removing sproduction
After we remove all the all the nullable variables disappear(except for the start variable)
sproduction
Fall 2006 Costas Buch - RPI 7
Unit-ProductionsYX Unit Production:
(a single variable in both sides)
bbBABBAaAaAS
Example:
Unit Productions
Fall 2006 Costas Buch - RPI 8
bbBABBAaAaAS
SubstituteBA
bbBBAB
aAaBaAS
|
|
Removal of unit productions:
Fall 2006 Costas Buch - RPI 9
Remove
bbBBAB
aAaBaAS
|
|
bbBABaA
aBaAS
|
BB
XX can be removed immediatelyUnit productions of form
Fall 2006 Costas Buch - RPI 10
SubstituteAB
bbBaA
aAaBaAS
||
bbBABaA
aBaAS
|
Fall 2006 Costas Buch - RPI 11
Remove repeated productions
bbBaA
aBaAS
|
bbBaA
aAaBaAS
||
Final grammar
Fall 2006 Costas Buch - RPI 12
Useless Productions
aAAAS
SaSbS
aAaaaaAaAAS
Some derivations never terminate...
Useless Production
Fall 2006 Costas Buch - RPI 13
bABA
aAAAS
Another grammar:
Not reachable from S
Useless Production
Fall 2006 Costas Buch - RPI 14
In general:
If there is a derivation)(GLwxAyS
Then variable is usefulA
Otherwise, variable is uselessA
consists of terminals
Fall 2006 Costas Buch - RPI 15
A production is useless if any of its variables is useless
xA
DCCBaAAAS
SaSbS
Productionsuselessuselessuselessuseless
Variablesuselessuselessuseless
Fall 2006 Costas Buch - RPI 16
Example Grammar:
aCbCaaBaA
CAaSS
||
Removing Useless Variables and Productions
Fall 2006 Costas Buch - RPI 17
First:find all variables that can producestrings with only terminals or
aCbCaaBaA
CAaSS
|| },{ BA
},,{ SBA
Round 1:
Round 2:
(possible useful variables)
(the right hand side of production that has only terminals)
(the right hand side of a production has terminals and variables of previous round)
This process can be generalized
Fall 2006 Costas Buch - RPI 18
Then, remove productions that use variablesother than
aCbCaaBaA
CAaSS
||
},,{ SBA
aaBaA
AaSS
|
Fall 2006 Costas Buch - RPI 19
Second:Find all variablesreachable from
aaBaA
AaSS
|
S A B
Use a Dependency Graphwhere nodes are variables
unreachable
S
Fall 2006 Costas Buch - RPI 20
Keep only the variablesreachable from S
aaBaA
AaSS
|
aAAaSS
|
Final Grammar
Contains onlyuseful variables
Fall 2006 Costas Buch - RPI 21
Removing AllStep 1: Remove Nullable Variables
Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables
This sequence guarantees thatunwanted variables and productionsare removed
Fall 2006 Costas Buch - RPI 22
Normal Formsfor
Context-free Grammars
Fall 2006 Costas Buch - RPI 23
Chomsky Normal Form
Each productions has form:
BCA
variable variable
aAor
terminal
Fall 2006 Costas Buch - RPI 24
Examples:
bASAAaSASS
Not ChomskyNormal Form
aaASAAAASSASS
Chomsky Normal Form
Fall 2006 Costas Buch - RPI 25
Conversion to Chomsky Normal Form
Example:
AcBaabAABaS
Not ChomskyNormal Form
We will convert it to Chomsky Normal Form
Fall 2006 Costas Buch - RPI 26
AcBaabAABaS
Introduce new variables for the terminals:
cTbTaTATB
TTTAABTS
c
b
a
c
baa
a
cba TTT ,,
Fall 2006 Costas Buch - RPI 27
Introduce new intermediate variableto break first production:
cTbTaTATB
TTTAABTS
c
b
a
c
baa
a
cTbTaTATB
TTTABTVAVS
c
b
a
c
baa
a
1
1
1V
Fall 2006 Costas Buch - RPI 28
Introduce intermediate variable:
cTbTaTATBTTVVTABTVAVS
c
b
a
c
ba
a
a
2
2
1
1
2V
cTbTaTATB
TTTABTVAVS
c
b
a
c
baa
a
1
1
Fall 2006 Costas Buch - RPI 29
Final grammar in Chomsky Normal Form:
cTbTaTATBTTVVTABTVAVS
c
b
a
c
ba
a
a
2
2
1
1
AcBaabAABaS
Initial grammar
Fall 2006 Costas Buch - RPI 30
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 2006 Costas Buch - RPI 31
The Procedure
First remove:
Nullable variables
Unit productions
(Useless variables optional)
Fall 2006 Costas Buch - RPI 32
Then, for every symbol : a
In productions with length at least 2 replace with a aT
Add production aTa
New variable: aT
Productions of formdo not need to change!
aA
Fall 2006 Costas Buch - RPI 33
Replace any production nCCCA 21
with
nnn CCV
VCVVCA
12
221
11
New intermediate variables: 221 ,,, nVVV
Fall 2006 Costas Buch - RPI 34
Observations
• Chomsky normal forms are good for parsing and proving theorems
• It is easy to find the Chomsky normal form for any context-free grammar
Fall 2006 Costas Buch - RPI 35
Greinbach Normal Form
All productions have form:
kVVVaA 21
symbol variables
0k
Fall 2006 Costas Buch - RPI 36
Examples:
bBbbBaAA
cABS
||
GreinbachNormal Form
aaSabSbS
Not GreinbachNormal Form
Fall 2006 Costas Buch - RPI 37
aaSabSbS
Conversion to Greinbach Normal Form:
bTaTaTSSTaTS
b
a
a
bb
GreinbachNormal Form
Fall 2006 Costas Buch - RPI 38
Observations
• Greinbach normal forms are very good for parsing strings (better than Chomsky Normal Forms)
• However, it is difficult to find the Greinbach normal of a grammar