Professional Documents
Culture Documents
of
Context-Free Grammars
Equivalent
grammar
S aB
S aB | ab
A aaA
Substitute A aaA
A abBc B b A abBc | abbc
B aA
B aA
Bb
Prof. Busch - LSU 2
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
Equivalent
A aaA
A abBc | abbc | abaAc
grammar
Prof. Busch - LSU 3
In general: A xBz
B y1
Substitute
B y1
equivalent
A xBz | xy1z grammar
Prof. Busch - LSU 4
Nullable Variables
production : X
Nullable Variable: Y
Example: S aMb
M aMb
M
Example: S aA
Aa
A B
Unit Productions
BA
B bb
Prof. Busch - LSU 7
Removal of unit productions:
S aA
S aA | aB
Aa
Substitute Aa
A B A B B A| B
BA
B bb
B bb
S aA | aB S aA | aB
Aa Remove Aa
B A| B BB BA
B bb B bb
Final grammar
S aA | aB | aA S aA | aB
Aa Aa
B bb B bb
S aSb
S
SA
A aA Useless Production
S A aA aaA aaaA
Prof. Busch - LSU 12
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
If there is a derivation
S xAy w L(G)
consists of
terminals
Then variable A is useful
S aSb
S Productions
Variables S A useless
useless A aA useless
useless B C useless
useless C D useless
Prof. Busch - LSU 15
Removing All
Step 1: Remove Nullable Variables
A BC or Aa
S AS S AS
S a S AAS
A SA A SA
Ab A aa
Chomsky Not Chomsky
Normal Form Normal Form
Example: S ABa
Not Chomsky
A aab Normal Form
B Ac
we can obtain:
an equivalent grammar
in Chomsky Normal Form
First remove:
Nullable variables
Unit productions
(Useless variables optional)
Productions of form A a
do not need to change!
Prof. Busch - LSU 27
Replace any production A C1C2 Cn
with A C1V1
V1 C2V2
Vn2 Cn1Cn