0% found this document useful (0 votes)
2 views

CYK-Notes

Uploaded by

Shreyans Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CYK-Notes

Uploaded by

Shreyans Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

AUTOMATA FORMAL

LANGUAGES AND LOGIC

Lecture notes
CYK (Membership algorithm)

Prepared by:
Kavitha K N
Assistant Professor

Department of Computer Science & Engineering


PES UNIVERSITY
(Established under Karnataka Act No.16 of 2013)
100-ft Ring Road, BSK III Stage, Bangalore – 560 085

1

Table of Contents:

Section Topic Page


number

1 Introduction 3

2 Example

Examples Solved:

# Problems on CYK Page


number

1 S -> aSb | bSa | SS | ​λ 4

2 S -> AB , A -> BB | a , B -> AB | b 6

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.

CYK algorithm operates only CFG given CNF.


The worst case running time of CYK algorithm is Θ(n3. |G|)
Where n is the length of the parsed string and |G| is the size of the CNF

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​​ w​2​ w​3​ w​4​ w​5
For this the table looks like

X​15

X​14 X​25

X​13 X​24 X​35

X​12 X​23 X​34 X​45

X​11 X​22 X​33 X​44 X​55

3
Each cell will enumerate some variable, for example ​X​11​ should see the terminal with length
1, ​X​12​ enumerated with ​w​2​ Length.
To enumerate each cell you should compute utmost n previously generated sets,

For example, to compute the ​X​12​ you should make use of ​X​11​, ​X​22​. If you want to fill the cell ,
you should see the previous pair in the row below.
To compute ​X​23 you
​ should see ​X​22​ , ​X​33​ to compute ​X​34​ you should see ​X​33 , X​
​ 44 and
​ so on.

Suppose we manage to get all the possible values of ​X​ij​ , then it is quite clear that the string
X belongs to L(G) iff
X​in, ​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 ​X​ij​ we should see what is previously computed utmost n pairs so ​X​ij​ can
be expanded as:

X​ij​= ( ​X​i,j X​
​ i+1, j​) ​∪ (​X​i,i+1 ,​X​i+2,j ​ … ​X​i, 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

1) Strings of the length 1 can be generated by


A -> a
B -> b
2) Strings of the length 2 can be generated by
For AB
S -> AB
For BA
S -> BA
For BB it is ∅

3) Strings of the length 3 can be generated by


a) A . ∅ ​∪ S . B = ​∅ . SB (SB is generated by C)
C -> SB
b) B .S ∪ ​∅ . A
BS is not generated by any rule
4) Strings of the length 4 can be generated by
A . ∅ ​∪ SS ∪ CA
S -> SS

The given string belongs to the grammar

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
=∅

The string does not belong to grammar

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

The string baaa belongs to the grammar

You might also like