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
Bb
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
Aa
A B
BA
B  bb
Prof. Busch - LSU
Unit Productions
7
Removal of unit productions:
S  aA
Aa
A B
BA
B  bb
Substitute
A B
S  aA | aB
Aa
B  A| B
B  bb
Prof. Busch - LSU
8
Unit productions of form
X X
can be removed immediately
S  aA | aB
Aa
B  A| B
Remove
BB
S  aA | aB
Aa
BA
B  bb
B  bb
Prof. Busch - LSU
9
S  aA | aB
Aa
Substitute
BA
BA
B  bb
Prof. Busch - LSU
S  aA | aB | aA
Aa
B  bb
10
Remove repeated productions
Final grammar
S  aA | aB | aA
S  aA | aB
Aa
Aa
B  bb
B  bb
Prof. Busch - LSU
11
Useless Productions
S  aSb
S 
SA
A  aA
Useless Production
Some derivations never terminate...
S  A  aA  aaA    aaaA  
Prof. Busch - LSU
12
Another grammar:
SA
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
SA
A  aA
useless B  C
useless
useless
CD
Prof. Busch - LSU
Productions
useless
useless
useless
useless
15
Removing Useless Variables and Productions
Example Grammar:
S  aS | A | C
Aa
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
Aa
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
Aa
S  aS | A
Aa
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
Aa
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
Aa
Aa
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
Aa
terminal
23
Examples:
S  AS
S a
S  AS
S  AAS
A  SA
Ab
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

Vn2  Cn1Cn
New intermediate variables:
Prof. Busch - LSU
V1, V2 , ,Vn2
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
Bb
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