Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
1
Normal Formsfor
Context-free Grammars
Linz 6th, Section 6.2
“Two Important Normal Forms,”
pages 171--178
2
3
Chomsky Normal Form
All productions have form:
BCA
variable variable
aAand
terminal
4
Examples:
bA
SAA
aS
ASS
Not Chomsky
Normal Form
aaA
SAA
AASS
ASS
Chomsky
Normal Form
5
Conversion to Chomsky Normal Form
Example:
AcB
aabA
ABaS
Not Chomsky
Normal Form
6
AcB
aabA
ABaS
Introduce variables for terminals:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
cba TTT ,,
7
Introduce intermediate variable:
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
8
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
9
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
10
From any context-free grammar
not in Chomsky Normal Form
we can obtain:
An equivalent grammar
in Chomsky Normal Form
In general:
11
The Procedure
First remove:
Nullable variables
Unit productions
12
For every symbol : a
In productions: replace with a aT
Add production aTa
New variable: aT
13
Replace any productionnCCCA 21
with
nnn CCV
VCV
VCA
12
221
11
New intermediate variables: 221 ,,, nVVV
14
Theorem: For any context-free grammar G such that the empty string is not in L(G), there is an equivalent grammar in Chomsky Normal Form
Linz, 6th, Theorem 6.6, page 172.
15
Observations
• Chomsky normal forms are good
for parsing and proving theorems
• It is very easy to find the Chomsky normal
form of any context-free grammar
16
Greibach Normal Form
All productions have form:
kVVVaA 21
symbol variables
0k
17
Examples:
bB
bbBaAA
cABS
||
Greibach
Normal Form
aaS
abSbS
Not Greibach
Normal Form
18
aaS
abSbS
Conversion to Greibach Normal Form:
bT
aT
aTS
STaTS
b
a
a
bb
Greibach
Normal Form
19
Theorem: For any context-free grammar
G such that the empty string is not in L(G), there is an equivalent grammar
in Greibach Normal Form
Linz, 6th, Theorem 6.7, page 176.Proof not given because it is too
complicated.
20
Observations
• Greibach normal forms are very good
for parsing
• It is hard to find the Greibach normal
form of any context-free grammar
21
An Applicationof
Chomsky Normal Forms
22
The CYK Membership Algorithm
J. Cocke, D. H. Younger, and T. KasamiInput:
• Grammar in Chomsky Normal Form G
• String
Output:
find if )(GLw
w
Considers every possible consecutive subsequence of letters and sets K∈T[i,j] if the sequence of letters starting from i to j can be generated from the non-terminal K.
Once it has considered sequences of length
1, it goes on to sequences of length 2, and so on.
23
For subsequences of length 2 and greater, it considers every possible partition of the subsequence into two halves, and checks to see if there is some production A-> BC such that B matches the first half and C Matches the second half. If so, it records A as matching the whole subsequence.
Once this process is completed, the sentence is recognized by the grammar if the entire string is matched by the start symbol.
24
25
The Algorithm
• Grammar :G
bB
ABB
aA
BBA
ABS
• String : w aabbb
Input example:
26
[0:1]
[1:2] [2:3] [3:4] [4:5]
[0:2] [1:3] [2:4] [3:5]
[0:3] [1:4] [2:5]
[0:4] [1:5]
[0:5]
aabbb
27
a a b b b
aa ab bb bb
aab abb bbb
aabb abbb
aabbb
aabbb
28
a
A
a
A
b
B
b
B
b
B
aa ab bb bb
aab abb bbb
aabb abbb
aabbb
bB
ABB
aA
BBA
ABS
29
a
A
a
A
b
B
b
B
b
B
aa ab
S,B
bb
A
bb
A
aab abb bbb
aabb abbb
aabbb
bB
ABB
aA
BBA
ABS
30
a
A
a
A
b
B
b
B
b
B
aa ab
S,B
bb
A
bb
A
aab
S,B
abb
A
bbb
S,B
aabb
A
abbb
S,B
aabbb
S,B
bB
ABB
aA
BBA
ABS
31
Therefore: )(GLaabbb
Time Complexity:3|| w
The CYK algorithm can be
easily converted to a parser
Observation:
32
Pushdown AutomataPDAs
33
Pushdown Automaton -- PDA
Input String
Stack
States
34
Initial Stack Symbol
Stack
$
Stack
z
bottomspecial symbol
35
The States
q1 q2a, b c
Input
symbol
Pop
symbolPush
symbol
36
q1 q2a, b c
a
b top
input
stack
a
Replaceeh
$eh
$
c
37
q1 q2a, c
a a
Pushb
eh
$eh
$
b
c
top
input
stack
38
q1 q2a, b
a a
Popb
eh
$eh
$
top
input
stack
39
q1 q2a,
a a
No Changeb
eh
$eh
$
btop
input
stack
40
Non-Determinism
q1
q2a, b c
q3a, b c
q1 q2, b c
transition
These are allowed transitions in
a Non-deterministic PDA (NPDA)
41
NPDA: Non-Deterministic PDA
Example:
,
a, a
b, aq0 q1 q2 q3
b, a
, $ $
42
a, a
b, a0q q1 q2 q3
Execution Example:
Input
a a a b b b
current
state
b, a
Time 0
, , $ $
Stack
$
43
a, a
b, aq0 q1 q2 q3
Input
a a a b b b
b, a
Time 1
, , $ $
Stack
$
44
a, a
b, aq0 q1 q2 q3
Input
Stack
a a a b b b
$
a
b, a
Time 2
, , $ $
45
a, a
b, aq0 q1 q2 q3
Input
Stack
a a a b b b
$
a
a
b, a
Time 3
, , $ $
46
a, a
b, aq0 q1 q2 q3
Input
Stack
a a a b b b
$
a
a
a
b, a
Time 4
, , $ $
47
a, a
b, aq0 q1 q2 q3
Input
a a a b b b
Stack
$
a
a
a
b, a
Time 5
, , $ $
48
a, a
b, aq0 q1 q2 q3
Input
a a a b b b
$
a
Stack
b, a
Time 6
, , $ $
a
49
a, a
b, aq0 q1 q2 q3
Input
a a a b b b
$
Stack
b, a
Time 7
, , $ $
a
50
a, a
b, aq0 q1 q2 q3
Input
a a a b b b
b, a
Time 8
accept
, , $ $
$
Stack
51
A string is accepted if there is
a computation such that:
• All the input is consumed
• The last state is a final state
At the end of the computation,
we do not care about the stack contents
52
The input string
is accepted by the NPDA:
aaabbb
a, a
b, aq0 q1 q2 q3
b, a
, , $ $
53
}0:{ nbaL nn
is the language accepted by the NPDA:
a, a
b, aq0 q1 q2 q3
b, a
In general,
, , $ $
54
Another NPDA example
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
NPDA M
}{)( RwwML
55
Execution Example:
Input
Time 0
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a ab b
56
Input
a ab
Time 1
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
ab
57
Input
Time 2
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
aa ab b
b
58
Input
Time 3
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
aa ab b
b
Guess the middle
of string
59
Input
Time 4
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
aa ab b
b
60
Input
Time 5
Stack
$
, $ $1q q2
bb
aa
,
,
, q0
bb
aa
,
,
a ab b a
61
Input
Time 6
Stack
$
, $ $q1
bb
aa
,
,
, q0
bb
aa
,
,
a ab b
accept
q2
62
Rejection Example:
Input
Time 0
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a b b b
63
Input
Time 1
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
aa b b b
64
Input
Time 2
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a
ba b b b
65
Input
Time 3
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a
b
Guess the middle
of string
a b b b
66
Input
Time 4
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a
ba b b b
67
Input
Time 5
Stack
$
, $ $1q q2
bb
aa
,
,
, q0
bb
aa
,
,
aa b b b
There is no possible transition.
Input is not consumed
68
Another computation on same string:
Input Time 0
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a b b b
69
Input
Time 1
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
aa b b b
70
Input
Time 2
Stack
$
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
a
ba b b b
71
Input
Time 3
Stack
$
a
ba b b b
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
b
72
Input
Time 4
Stack
a b b b
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
$
a
b
b
b
73
Input
Time 5
Stack
a b b b
$
a
b
b
b
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
No final state
is reached
74
, $ $q1
q2
bb
aa
,
,
, q0
bb
aa
,
,
There is no computation
that accepts string abbb
)(MLabbb