cyk lecture
TRANSCRIPT
-
8/9/2019 CYK Lecture
1/2
6.3
A
MEMBERSHIP ALGORITHM
FOR
CONTEXT-FREE GRi\MMARS
171
A IVmmbershfip
Algorithm
for Conte
xt-Fr
ee
Grammars*
In Section 5.2, we claim, without any
elaboration, that
membership and
parsing
algorithms
for context-free
grammars
exist
that
require approxi
mately u
3
steps to
parse
a
string
w. We
are
now in a position to justify
this
claim.
The
algorithm we will descr ibe here is called the CYK algorithm,
after
its originators J. Cocke, D. H. Younger,
and
T. Kasami. The algo
rithm works only if
the
grammar is-in Chomsky no rmal form
and
succeeds
by
breaking one problem into
a sequence of smaller ones in
the
following
way. Assume that we have a grammar
G
=
F,
T, S, P)
in Chomsky normal
form and a
string
We define
substrings
Wi j
=
ai'
a j
and
subsets of V
Clearly,
W
E
L
(G) i f and
only if
S
E FIn'
To
compute Vij
observe that
A
E
Vii
i f
and
only if
G
contains a pro
duction
A 4 ai .
Therefore,
Vi i
can be computed for all 1 S i ::; n by
inspection of
w
and
the
productions
of the grammar. To continue, notice
that
for
j
>
i,
A
derives
Wij
i
and
only
i f
there
is a
production
A
4
BG,
with
B ik
and
G
WkH j
for
some
k
with i
S k k
< j . In other
words,
ij = U {A:
A 4
BG, with
B
E
V ik,GE
Vk+l,j}
{G.t l
kE {i , i+I, ... j - I }
An inspection of
the
indices in (6.8) shows that it
can
be used to
compute
all
the Vij if we proceed in
the
sequence
1. Compute
V
, V
22
,
... , V
n n
>
2. Compute V
12
,
11
3
,
..
Vn-I,n,
and
so on.
-
8/9/2019 CYK Lecture
2/2
Determine whether the string
w = ruibbb
is in the language generated by
the grammar
S > AB,
A
> BBla,
B
>
AB/b.
First
note that
Wll =
a,
so Vl l is the
set
of all variables that immedi-
ately derive a, that is, V
u
= {A}.
Since W22
=
U, we also have
V
22
= {A}
and, similarly,
V
= {it} ,V
22
= {A} , V33 = {B} ,V44 = {B}
1
V55 = {B} .
Now we use 6.8) to get
V
I 2
=
{A: A > BC,B
E
Vil C
E
V
n}
Since V
= {A}
and
V
22
=
{A},
the set consists of
all
variables that occur
on
the
left side of a
production
whose right
side
ill AA. Since
there
are
none, Vi is empty Next,
V
23
= {A: A > BC,B E V
2
Z C
E V
33
}
,
so the required right side is AB, and we have
V
Z3
=
{S,B}.
A straightfor-
ward
argument
along these lines
then
gives
VIZ = 23 = {S,B}, V
34
= {A},
45
= {A},
Vi
=
{S,B} ,V
24
=
{A}, 35
= {S,B},
V
14
= {A}, V
25
= {S,B},
Vis =
{S,B} ,
so that w
E
L G).
The
CYK algorithm, as
described
here,
determines
membership for any
lcnguage
generated
by grammar in Chomsky normal form. With some
additions to keep track of how
th
elements of are derived it can be
converted into a parsing method. To see
that the
CYK
membership algo-
rithm requires 0 n
3
steps, notice
that
exactly n n
+
1)
/2
sets of ii have
to be computed. Each involves the evaluation of
at
most n
terms
in 6.8),
30
the
claimed result follows.