CYK-Notes
CYK-Notes
Lecture notes
CYK (Membership algorithm)
Prepared by:
Kavitha K N
Assistant Professor
1
Table of Contents:
1 Introduction 3
2 Example
Examples Solved:
3 S → AA | BC , A → BA | a , B→ CC | b , C → AB | a 7
2
CYK Algorithm
1. Introduction
The CocKe - Younger - Kasami algorithm alternatively called as CYK or CKY is a parsing
algorithm for context free grammar named after its inventors, John , Daniel Younger and
Tadao Kasami. It is a membership algorithm for Context free grammar.. It employs bottom
up parsing and dynamic programming.is
It is used to decide whether a given string belongs to the language of the grammar or not.
To check whether the string belongs to the grammar , we should construct a table (similar
to table filling algorithm).
Construct a triangular table such that
-> Each row corresponds to the length of the substring
- Bottom row represents the substring of length 1
- second row from bottom row represents substring of length 2
- third row from bottom row represents substring of length 3 and so on
- top row represents the entire string ‘w’ length
For example , consider the string of length 5, w1 w2 w3 w4 w5
For this the table looks like
X15
X14 X25
3
Each cell will enumerate some variable, for example X11 should see the terminal with length
1, X12 enumerated with w2 Length.
To enumerate each cell you should compute utmost n previously generated sets,
For example, to compute the X12 you should make use of X11, X22. If you want to fill the cell ,
you should see the previous pair in the row below.
To compute X23 you
should see X22 , X33 to compute X34 you should see X33 , X
44 and
so on.
Suppose we manage to get all the possible values of Xij , then it is quite clear that the string
X belongs to L(G) iff
Xin, Contains the start symbol S, where n is the length if the string, (ie, the top cell should
contain S in it)
2. Example:
1) Parse the string abba using CYK algorithm ,
Grammar:
S -> aSb | bSa | SS | λ
Note:
If we want to fill Xij we should see what is previously computed utmost n pairs so Xij can
be expanded as:
Xij= ( Xi,j X
i+1, j) ∪ (Xi,i+1 ,Xi+2,j … Xi, j-1 , X
jj)
Solution:
Step 1:
Convert given CFG to CNF
Eliminate λ production
S -> aSb | bSa | ab | ba | S | SS | λ
Eliminate unit production
S -> aSb | bSa | ab | ba | SS | λ
There are no useless production
Conversion to CNF
S -> AB | BA | AC | BD | SS | λ
4
A -> a
B -> b
C -> SB
D -> SA
Step 2:
CYK algorithm
5
Example 2:
S -> AB
A -> BB | a
B -> AB | b
String : aabba
Length 3:
1) A (S, B) ∪ ∅.B ( S -> AB, B -> AB)
AS, AB , ∅
2) AA ∪ (S ,B) (B) (A -> BB)
AA ∪ SB,BB
3) A . ∅
∅
Length 4:
1) AA ∪ ∅. A ∪ (S,B) (B)
AA ∪ ∅ ∪ SB ∪ BB
(A -> BB)
2) A. ∅ ∪ (S,B) ∅ ∪ AA
∅ ∪ ∅ ∪ AA
=∅
Length 5:
A ∅ ∪ ∅ . ∅ ∪ (S,B) ∅ ∪ AA
=∅
6
Example 3:
S → AA | BC
A → BA | a
B→ CC | b
C → AB | a
W= baaa
Length 3:
1) BB ∪ {A, S }{A,C}
BB ∪ AA ∪ AC ∪ SA ∪ SC
=∅
2) (A,C) (B) ∪ B(A,C)
AB ∪ CB ∪ BA ∪BC
S → AB | BC A → BA
C → AB
Length 4:
B(S,A,C) ∪ (A,S) B ∪ ∅ (A,C)
= BS ∪ BA ∪ BC ∪ AB,SB
A → BA
S → BC
S → AB
C → AB