Transcript In general
Simplifications
of
Context-Free Grammars
Prof. Busch - LSU
1
A Substitution Rule
S aB
A aaA
Substitute
A abBc
B b
B aA
Bb
Prof. Busch - LSU
Equivalent
grammar
S aB | ab
A aaA
A abBc | abbc
B aA
2
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
A aaA
A abBc | abbc | abaAc
Prof. Busch - LSU
Equivalent
grammar
3
In general:
A xBz
B y1
Substitute
B y1
A xBz | xy1z
Prof. Busch - LSU
equivalent
grammar
4
Nullable Variables
production :
X
Nullable Variable:
Y
Example:
S aMb
M aMb
M
production
Nullable variable
Prof. Busch - LSU
5
Removing
S aMb
M aMb
productions
Substitute
M
M
S aMb | ab
M aMb | ab
After we remove all the productions
all the nullable variables disappear
(except for the start variable)
Prof. Busch - LSU
6
Unit-Productions
X Y
Unit Production:
(a single variable in both sides)
Example:
S aA
Aa
A B
BA
B bb
Prof. Busch - LSU
Unit Productions
7
Removal of unit productions:
S aA
Aa
A B
BA
B bb
Substitute
A B
S aA | aB
Aa
B A| B
B bb
Prof. Busch - LSU
8
Unit productions of form
X X
can be removed immediately
S aA | aB
Aa
B A| B
Remove
BB
S aA | aB
Aa
BA
B bb
B bb
Prof. Busch - LSU
9
S aA | aB
Aa
Substitute
BA
BA
B bb
Prof. Busch - LSU
S aA | aB | aA
Aa
B bb
10
Remove repeated productions
Final grammar
S aA | aB | aA
S aA | aB
Aa
Aa
B bb
B bb
Prof. Busch - LSU
11
Useless Productions
S aSb
S
SA
A aA
Useless Production
Some derivations never terminate...
S A aA aaA aaaA
Prof. Busch - LSU
12
Another grammar:
SA
A aA
A
B bA
Useless Production
Not reachable from S
Prof. Busch - LSU
13
In general:
If there is a derivation
S xAy w L(G)
Then variable
A is useful
Otherwise, variable
consists of
terminals
A is useless
Prof. Busch - LSU
14
A production A x is useless
if any of its variables is useless
S aSb
S
Variables
SA
A aA
useless B C
useless
useless
CD
Prof. Busch - LSU
Productions
useless
useless
useless
useless
15
Removing Useless Variables and Productions
Example Grammar:
S aS | A | C
Aa
B aa
C aCb
Prof. Busch - LSU
16
First: find all variables that can produce
strings with only terminals or
(possible useful variables)
S aS | A | C
Aa
B aa
C aCb
Round 1:
{ A, B}
(the right hand side of production
that has only terminals)
Round 2:
{ A, B, S}
(the right hand side of a production
has terminals and
variables of previous round)
This process can be generalized
Prof. Busch - LSU
17
Then, remove productions that use variables
other than { A, B, S }
S aS | A | C
Aa
S aS | A
Aa
B aa
B aa
C aCb
Prof. Busch - LSU
18
Second: Find all variables
reachable from
S
Use a Dependency Graph
where nodes are variables
S aS | A
Aa
S
A
B
unreachable
B aa
Prof. Busch - LSU
19
Keep only the variables
reachable from S
Final Grammar
S aS | A
S aS | A
Aa
Aa
B aa
Contains only
useful variables
Prof. Busch - LSU
20
Removing All
Step 1: Remove Nullable Variables
Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables
This sequence guarantees that
unwanted variables and productions
are removed
Prof. Busch - LSU
21
Normal Forms
for
Context-free Grammars
Prof. Busch - LSU
22
Chomsky Normal Form
Each production has form:
A BC
variable
or
variable
Prof. Busch - LSU
Aa
terminal
23
Examples:
S AS
S a
S AS
S AAS
A SA
Ab
A SA
A aa
Chomsky
Normal Form
Not Chomsky
Normal Form
Prof. Busch - LSU
24
Conversion to Chomsky Normal Form
Example:
S ABa
A aab
B Ac
Not Chomsky
Normal Form
We will convert it to Chomsky Normal Form
Prof. Busch - LSU
25
Introduce new variables for the terminals:
Ta ,Tb ,Tc
S ABTa
A TaTaTb
S ABa
B ATc
A aab
Ta a
B Ac
Tb b
Tc c
Prof. Busch - LSU
26
Introduce new intermediate variable
to break first production:
V1
S AV1
S ABTa
V1 BTa
A TaTaTb
A TaTaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c
Prof. Busch - LSU
27
Introduce intermediate variable:
V2
S AV1
S AV1
V1 BTa
V1 BTa
A TaV2
A TaTaTb
V2 TaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Prof. Busch - LSU
Tc c
28
Final grammar in Chomsky Normal Form:
S AV1
V1 BTa
A TaV2
Initial grammar
V2 TaTb
S ABa
B ATc
A aab
Ta a
B Ac
Tb b
Prof. Busch - LSU
Tc c
29
In general:
From any context-free grammar
(which doesn’t produce )
not in Chomsky Normal Form
we can obtain:
an equivalent grammar
in Chomsky Normal Form
Prof. Busch - LSU
30
The Procedure
First remove:
Nullable variables
Unit productions
(Useless variables optional)
Prof. Busch - LSU
31
Then, for every symbol
New variable:
Add production
a:
Ta
Ta a
In productions with length at least 2
replace a with Ta
Productions of form A a
do not need to change!
Prof. Busch - LSU
32
Replace any production
with
A C1C2 Cn
A C1V1
V1 C2V2
Vn2 Cn1Cn
New intermediate variables:
Prof. Busch - LSU
V1, V2 , ,Vn2
33
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
(which doesn’t generate )
Prof. Busch - LSU
34
Greinbach Normal Form
All productions have form:
A a V1V2 Vk
symbol
k 0
variables
Prof. Busch - LSU
35
Examples:
S cAB
S abSb
A aA | bB | b
S aa
Bb
Greinbach
Normal Form
Not Greinbach
Normal Form
Prof. Busch - LSU
36
Conversion to Greinbach Normal Form:
S aTb STb
S abSb
S aTa
S aa
Ta a
Tb b
Greinbach
Normal Form
Prof. Busch - LSU
37
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
Prof. Busch - LSU
38