Upload
aileen-higgins
View
226
Download
0
Tags:
Embed Size (px)
Citation preview
Prof. Busch - LSU 2
A Substitution Rule
bB
aAB
abBcA
aaAA
aBS
Substitute
Equivalentgrammar
aAB
abbcabBcA
aaAA
abaBS
|
|
bB
Prof. Busch - LSU 3
EquivalentgrammarabaAcabbcabBcA
aaAA
aaAabaBS
||
||
aAB
abbcabBcA
aaAA
abaBS
|
|
Substitute aAB
Prof. Busch - LSU 5
Nullable Variables
:production X
Nullable Variable: Y
Example:
M
aMbM
aMbS
Nullable variable production
Prof. Busch - LSU 6
M
M
aMbM
aMbSSubstitute
abaMbM
abaMbS
|
|
Removing sproduction
After we remove all the all the nullable variables disappear(except for the start variable)
sproduction
Prof. Busch - LSU 7
Unit-Productions
YX Unit Production:
(a single variable in both sides)
bbB
AB
BA
aA
aAS
Example:
Unit Productions
Prof. Busch - LSU 9
Remove
bbB
BAB
aA
aBaAS
|
|
bbB
AB
aA
aBaAS
|
BB
XX can be removed immediately
Unit productions of form
Prof. Busch - LSU 12
Useless Productions
aAA
AS
S
aSbS
aAaaaaAaAAS
Some derivations never terminate...
Useless Production
Prof. Busch - LSU 14
In general:
If there is a derivation
)(GLwxAyS
Then variable is usefulA
Otherwise, variable is uselessA
consists of terminals
Prof. Busch - LSU 15
A production is useless if any of its variables is useless
xA
DC
CB
aAA
AS
S
aSbS
Productions
useless
useless
useless
useless
Variables
useless
useless
useless
Prof. Busch - LSU 16
Example Grammar:
aCbC
aaB
aA
CAaSS
||
Removing Useless Variables and Productions
Prof. Busch - LSU 17
First:find all variables that can producestrings with only terminals or
aCbC
aaB
aA
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
Prof. Busch - LSU 18
Then, remove productions that use variablesother than
aCbC
aaB
aA
CAaSS
||
},,{ SBA
aaB
aA
AaSS
|
Prof. Busch - LSU 19
Second:Find all variablesreachable from
aaB
aA
AaSS
|
S A B
Use a Dependency Graphwhere nodes are variables
unreachable
S
Prof. Busch - LSU 20
Keep only the variablesreachable from S
aaB
aA
AaSS
|
aA
AaSS
|
Final Grammar
Contains onlyuseful variables
Prof. Busch - LSU 21
Removing All
Step 1: Remove Nullable Variables
Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables
This sequence guarantees thatunwanted variables and productionsare removed
Prof. Busch - LSU 23
Chomsky Normal Form
Each production has form:
BCA
variable variable
aAor
terminal
Prof. Busch - LSU 24
Examples:
bA
SAA
aS
ASS
Not ChomskyNormal Form
aaA
SAA
AASS
ASS
Chomsky Normal Form
Prof. Busch - LSU 25
Conversion to Chomsky Normal Form
Example:
AcB
aabA
ABaS
Not ChomskyNormal Form
We will convert it to Chomsky Normal Form
Prof. Busch - LSU 26
AcB
aabA
ABaS
Introduce new variables for the terminals:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
cba TTT ,,
Prof. Busch - LSU 27
Introduce new intermediate variableto break first production:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
cT
bT
aT
ATB
TTTA
BTV
AVS
c
b
a
c
baa
a
1
1
1V
Prof. Busch - LSU 28
Introduce intermediate variable:
cT
bT
aT
ATB
TTV
VTA
BTV
AVS
c
b
a
c
ba
a
a
2
2
1
1
2V
cT
bT
aT
ATB
TTTA
BTV
AVS
c
b
a
c
baa
a
1
1
Prof. Busch - LSU 29
Final grammar in Chomsky Normal Form:
cT
bT
aT
ATB
TTV
VTA
BTV
AVS
c
b
a
c
ba
a
a
2
2
1
1
AcB
aabA
ABaS
Initial grammar
Prof. Busch - LSU 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:
Prof. Busch - LSU 31
The Procedure
First remove:Nullable variablesUnit productions
(Useless variables optional)
Prof. Busch - LSU 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
Prof. Busch - LSU 33
Replace any production nCCCA 21
with
nnn CCV
VCV
VCA
12
221
11
New intermediate variables: 221 ,,, nVVV
Prof. Busch - LSU 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 (which doesn’t generate )
Prof. Busch - LSU 36
Examples:
bB
bbBaAA
cABS
||
GreinbachNormal Form
aaS
abSbS
Not GreinbachNormal Form
Prof. Busch - LSU 37
aaS
abSbS
Conversion to Greinbach Normal Form:
bT
aT
aTS
STaTS
b
a
a
bb
GreinbachNormal Form