0% found this document useful (0 votes)
204 views17 pages

Cyk Algo

The document describes the Cocke-Younger-Kasami (CYK) algorithm for parsing context-free grammars and determining if a string is in the language generated by a given grammar. It provides an example of running the CYK algorithm on the grammar G and string "baaba" to determine that "baaba" is in the language of G. The document concludes by asking the reader to apply the CYK algorithm to determine if two additional strings are in the language of G.

Uploaded by

abida rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views17 pages

Cyk Algo

The document describes the Cocke-Younger-Kasami (CYK) algorithm for parsing context-free grammars and determining if a string is in the language generated by a given grammar. It provides an example of running the CYK algorithm on the grammar G and string "baaba" to determine that "baaba" is in the language of G. The document concludes by asking the reader to apply the CYK algorithm to determine if two additional strings are in the language of G.

Uploaded by

abida rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

The Cocke-Younger-Kasami 1

Algorithm*
CYK Algorithm
CYK Algorithm for Deciding
Context Free Languages

X1, 5
X1, 4 X2, 5
X1, 3 X2, 4 X3, 5
X1, 2 X2, 3 X3, 4 X4, 5
X1, 1 X2, 2 X3, 3 X4, 4 X5, 5
w1 w2 w3 w4 w5

If the S in the set X1n where ‘n’ = 5 then W Є L(G)


Example CYK Algorithm

• Show the CYK Algorithm with the following


example:
• CNF grammar G
• S  AB | BC
• A  BA | a
• B  CC | b
• C  AB | a
• w is baaba
• Question Is baaba in L(G)?
S  AB | BC
A  BA | a
B  CC | b
C  AB | a

{B} {A, C} {A, C} {B} {A, C}


b a a b a

Calculating the Bottom ROW


ba
{B} {A,C}
{BA,BC}
S  AB | BC
{A,S}  {S,A}
A  BA | a
B  CC | b
C  AB | a

{S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
aa
{A,C} {A,C}
{AA,AC,CA,CC } S  AB | BC
{B}  {B} A  BA | a
B  CC | b
C  AB | a

{S, A} {B}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
ab
{A,C} {B}
{AB,CB} S  AB | BC
{S,C}  {S,C} A  BA | a
B  CC | b
C  AB | a

{S, A} {B} {S, C}


{B} {A, C} {A, C} {B} {A, C}
b a a b a
ba
{B} {A,C}
{BA,BC}
{A,S}  {S,A}
S  AB | BC
A  BA | a
B  CC | b
C  AB | a

{S, A} {B} {S, C} {S, A}


{B} {A, C} {A, C} {B} {A, C}
b a a b a
baa
baa | baa
{B} {B} {S,A} {A,C}
{BB} empty {SA,SC,AA,AC}  emty

S  AB | BC
A  BA | a
B  CC | b
C  AB | a

Ø
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
aab
aa b | a ab
{B} {B} {A,C} {S,C}
{BB} empty {AS,AC,CS,CC} {B}

S  AB | BC
A  BA | a
B  CC | b
C  AB | a
Ø {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
aba
ab a | a ba
{S,C} {A,C} {A,C} {S,A}
{SA,SC,CA,CC} {AS,AA,CS,CA} empty
{B}

S  AB | BC
A  BA | a
B  CC | b
C  AB | a

Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
baab
ba ab | baa b |b aab
{S,A} {S,C} Ø {B} {B} {B}
{SS,SC,AS,AC} empty {BB}empty
empty

S  AB | BC
A  BA | a
B  CC | b
C  AB | a
Ø
Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
aaba
aa ba | aab a |a aba
{B} {S,A} {B} {A,C} {A,C} {B}
{BS,BA} {BA,BC} {AB,CB}
{A} {A,S} {S,C}

{A,S,C} {S,A,C}c
S  AB | BC
A  BA | a
B  CC | b
C  AB | a
Ø {S, A, C}
Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
baaba
baa ba | ba aba |baab a |b aaba
Ø {S,A} {S,A} {B} Ø {B} {B} {S,A,C}
Empty {SB,AB} Empty {BS,BA,BC}
{S,C} {A,S}

{S,A,C}

S  AB | BC
{S, A, C} A  BA | a
Ø {S, A, C} B  CC | b
C  AB | a
Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a
S  AB | BC
A  BA | a
B  CC | b
C  AB | a

{S, A, C}  X1, 5
Ø {S, A, C}
Ø {B} {B}
{S, A} {B} {S, C} {S, A}
{B} {A, C} {A, C} {B} {A, C}
b a a b a

- Table for string ‘w’ that has length 5


- The algorithm populates the triangular table
(Result)

• Is baaba in L(G)?

Yes

We can see the S in the set X1n where ‘n’ = 5


We can see the table
the cell X15 = (S, A, C) then
if S Є X15 then baaba Є L(G)
Try Your self 17

• Q: Consider the grammar G given by


Productions;
• S→AA|AS|b
• A→SA|AS|a
1.Is x = abaab in L(G )
2.Is x = bbab in L(G )

You might also like