context free grammars_simplification
TRANSCRIPT
Costas Busch - LSU 1
Simplifications of
Context-Free Grammars
Costas Busch - LSU 2
A Substitution Rule
bBaABabBcAaaAAaBS
Substitute
Equivalentgrammar
aABabbcabBcA
aaAAabaBS
|
|
bB
Costas Busch - LSU 3
EquivalentgrammarabaAcabbcabBcA
aaAAaaAabaBS
||
||
aABabbcabBcA
aaAAabaBS
|
|
Substitute aAB
Costas Busch - LSU 4
In general:
1yB
xBzA
Substitute
zxyxBzA 1|equivalentgrammar
1yB
Costas Busch - LSU 5
Nullable Variables:production X
Nullable Variable: Y
Example:
MaMbMaMbS
Nullable variable production
Costas Busch - LSU 6
M
MaMbMaMbS
SubstituteabaMbMabaMbS|
|
Removing sproduction
After we remove all the all the nullable variables disappear(except for the start variable)
sproduction
Costas Busch - LSU 7
Unit-ProductionsYX Unit Production:
(a single variable in both sides)
bbBABBAaAaAS
Example:
Unit Productions
Costas Busch - LSU 8
bbBABBAaAaAS
SubstituteBA
bbBBAB
aAaBaAS
|
|
Removal of unit productions:
Costas Busch - LSU 9
Remove
bbBBAB
aAaBaAS
|
|
bbBABaA
aBaAS
|
BB
XX can be removed immediatelyUnit productions of form
Costas Busch - LSU 10
SubstituteAB
bbBaA
aAaBaAS
||
bbBABaA
aBaAS
|
Costas Busch - LSU 11
Remove repeated productions
bbBaA
aBaAS
|
bbBaA
aAaBaAS
||
Final grammar
Costas Busch - LSU 12
Useless Productions
aAAAS
SaSbS
aAaaaaAaAAS
Some derivations never terminate...
Useless Production
Costas Busch - LSU 13
bABA
aAAAS
Another grammar:
Not reachable from S
Useless Production
Costas Busch - LSU 14
In general:
If there is a derivation)(GLwxAyS
Then variable is usefulA
Otherwise, variable is uselessA
consists of terminals
Costas Busch - LSU 15
A production is useless if any of its variables is useless
xA
DCCBaAAAS
SaSbS
Productionsuselessuselessuselessuseless
Variablesuselessuselessuseless
Costas Busch - LSU 16
Example Grammar:
aCbCaaBaA
CAaSS
||
Removing Useless Variables and Productions
Costas Busch - LSU 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
Costas Busch - LSU 18
Then, remove productions that use variablesother than
aCbCaaBaA
CAaSS
||
},,{ SBA
aaBaA
AaSS
|
Costas Busch - LSU 19
Second:Find all variablesreachable from
aaBaA
AaSS
|
S A B
Use a Dependency Graphwhere nodes are variables
unreachable
S
Costas Busch - LSU 20
Keep only the variablesreachable from S
aaBaA
AaSS
|
aAAaSS
|
Final Grammar
Contains onlyuseful variables
Costas Busch - LSU 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