View
236
Download
2
Tags:
Embed Size (px)
Citation preview
1
Simplifications of
Context-Free Grammars
2
A Substitution Rule
substitute B
bB
abbAB
abBcA
aaAA
aA
abbcA
ababbAcA
aaAA
aA
equivalentgrammar
3
In general:
321 ||| yyyB
xBzA
zxyzxyzxyA n||| 21
substitute B
equivalentgrammar
4
Useless Productions
aAA
AS
S
aSbS
5
aAA
AS
S
aSbS
aAaaaaAaAAS
never terminates...
6
aAA
AS
S
aSbS
Useless Production
7
bAB
A
aAA
AS
Another grammar:
8
bAB
A
aAA
AS
Not reachable from S
Useless Production
9
In general:
If wxAyS
Then A
is uselessOtherwise A
is useful
)(GLw
10
Removing Useless Productions
Example Grammar
aCbC
aaB
aA
CAaSS
||
11
First: find all variables that producestrings with only terminals
aCbC
aaB
aA
CAaSS
|| },{ BA
},,{ SBA
Round 1:
Round 2:
12
Keep only the variablesthat produce terminal symbols
aCbC
aaB
aA
CAaSS
||
},,{ SBA
aaB
aA
AaSS
|
13
Second: Find all variablesreachable from S
aaB
aA
AaSS
|
S A B
Dependency Graph
notreachable
14
Keep only the variablesreachable from S
aaB
aA
AaSS
|
aA
AaSS
|
Final Grammar
15
Nullable Variables
:production A
Nullable Variable: A
16
Removing Nullable Variables
Example Grammar:
M
aMbM
aMbS
Nullable variable
17
Substitute
M
M
aMbM
aMbS
abM
aMbM
abS
aMbS
Final Grammar
18
Unit-Productions
BAUnit Production:
19
Removing Unit Productions
Observation:
AA
Is removed immediately
20
Example Grammar:
bbB
AB
BA
aA
aAS
21
bbB
AB
BA
aA
aAS
substitute
BA
bbB
BAB
aA
aBaAS
|
|
22
bbB
BAB
aA
aBaAS
|
|
bbB
AB
aA
aBaAS
|
23
substitute
AB
bbB
aA
aAaBaAS
||
bbB
AB
aA
aBaAS
|
24
Remove repeated productions
bbB
aA
aBaAS
|
bbB
aA
aAaBaAS
||
Final grammar
25
Removing All
1. Remove Nullable Variables
2. Remove Unit-Productions
3. Remove Useless Variables
26
Normal Formsfor
Context-free Grammars
27
Chomsky Normal Form
All productions have form:
BCA
variable variable
aAOR
terminal
28
Examples:
bA
SAA
aS
ASS
ChomskyNormal Form
aaA
SAA
AASS
ASS
NOTChomskyNormal Form
29
Convertion to Chomsky Normal Form
Example:
AcB
aabA
ABaS
ChomskyNormal Form
NOT
30
AcB
aabA
ABaS
Introduce variables for terminals:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
cba TTT ,,
31
Introduce intermediate variables:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
cT
bT
aT
ATB
TTV
VTA
BTV
AVS
c
b
a
c
ba
a
a
2
2
1
1
21, VV
32
Final grammar inChomsky 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
33
From any context-free grammar Not in Chomsky Normal Form
We can obtain: An equivalent grammar in Chomsky Normal Form
In general:
34
The Procedure:
Removenullable variablesunit productions
35
For every symbol :
aT
a
Add new variable
In productions, replace with a aT
Add production aTa
36
Replace any production: nCCCA 21
with:
nnn CCV
VCV
VCA
12
221
11
New variables: 221 ,,, nVVV
37
Theorem:
For any context-free grammarthere is an equivalent grammarin Chomsky Normal Form
38
Greinbach Normal Form
All rules have form:
kVVVaA 21
symbol variables
0k
39
Examples:
bB
bbBaAA
cABS
||
GreinbachNormal Form
aaS
abSbS
GreinbachNormal Form
NOT
40
aaS
abSbS
Conversion to Greinbach Normal Form
bT
aT
aTS
STaTS
b
a
a
bb
GreinbachNormal Form
41
Theorem:
For any context-free grammar
there is an equivalent grammarin Greinbach Normal Form
42
An Applicationof
Chomsky Normal Forms
43
The CYK Membership Algorithm
Inputs: • Grammar in Chomsky Normal Form
G
• String w
Output:
find if )(GLw
44
Algorithm: Input Examples
Grammar :G
bB
ABB
aA
BBA
ABS
String : w aabbb
45
a a b b b
aa ab bb bb
aab abb bbb
aabb abbb
aabbb
aabbb
46
aA
aA
bB
bB
bB
aa ab bb bb
aab abb bbb
aabb abbb
aabbb
bB
ABB
aA
BBA
ABS
47
aA
aA
bB
bB
bB
aa abS,B
bbA
bbA
aab abb bbb
aabb abbb
aabbb
bB
ABB
aA
BBA
ABS
48
aA
aA
bB
bB
bB
aa abS,B
bbA
bbA
aabS,B
abbA
bbbS,B
aabbA
abbbS,B
aabbbS,B
bB
ABB
aA
BBA
ABS
49
Therefore: )(GLaabbb
Time Complexity:3||w
It can be easily converted to a parser