CYK Algorithm for Context Free Grammar

22 Jun, 2020
Prerequisite – Converting Context Free Grammar to Chomsky Normal FormCYK algorithm is a parsing algorithm for context free grammar.In order to apply CYK algorithm to a grammar, it must be in Chomsky Normal Form. It uses a dynamic programming algorithm to tell whether a string is in the language of a grammar.
Algorithm :Let w be the n length string to be parsed. And G represent the set of rules in our grammar with start state S.
Construct a table DP for size n × n.If w = e (empty string) and S -> e is a rule in G then we accept the string else we reject.For i = 1 to n:
  For each variable A:
     We check if A -> b is a rule and b = wi for some i:
        If so, we place A in cell (i, i) of our table. For l = 2 to n:
  For i = 1 to n-l+1:
       j = i+l-1
        For k = i to j-1:
           For each rule A -> BC: 
        We check if (i, k) cell contains B and (k + 1, j) cell contains C:
             If so, we put A in cell (i, j) of our table. We check if S is in (1, n):
  If so, we accept the string
  Else, we reject.
Construct a table DP for size n × n.
If w = e (empty string) and S -> e is a rule in G then we accept the string else we reject.
For i = 1 to n:
  For each variable A:
     We check if A -> b is a rule and b = wi for some i:
        If so, we place A in cell (i, i) of our table. 
For i = 1 to n:
  For each variable A:
     We check if A -> b is a rule and b = wi for some i:
        If so, we place A in cell (i, i) of our table. 
For l = 2 to n:
  For i = 1 to n-l+1:
       j = i+l-1
        For k = i to j-1:
           For each rule A -> BC: 
        We check if (i, k) cell contains B and (k + 1, j) cell contains C:
             If so, we put A in cell (i, j) of our table. 
For l = 2 to n:
  For i = 1 to n-l+1:
       j = i+l-1
        For k = i to j-1:
           For each rule A -> BC: 
        We check if (i, k) cell contains B and (k + 1, j) cell contains C:
             If so, we put A in cell (i, j) of our table. 
We check if S is in (1, n):
  If so, we accept the string
  Else, we reject.
We check if S is in (1, n):
  If so, we accept the string
  Else, we reject.
Example –Let our grammar G be:
S -> AB | BC
A -> BA | a
B -> CC | b
C -> AB | a 
We check if baaba is in L(G):
We first insert single length rules into our table.We then fill the remaining cells of our table.We observe that S is in the cell (1, 5), Hence, the string baaba belongs to L(G).
We first insert single length rules into our table.
We then fill the remaining cells of our table.
We observe that S is in the cell (1, 5), Hence, the string baaba belongs to L(G).
Time and Space Complexity :
Time Complexity –O(n3.|G|) Where |G| is the number of rules in the given grammar.
O(n3.|G|) 
Where |G| is the number of rules in the given grammar.
Space Complexity –O(n2)  
O(n2)  
Theory of Computation & Automata
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Boyer-Moore Majority Voting Algorithm
Variation of Turing Machine
Post Correspondence Problem
Halting Problem in Theory of Computation
Decidable and Undecidable problems in Theory of Computation
Pushdown Automata Acceptance by Final State
Arden's Theorem in Theory of Computation
Program to construct a DFA which accept the language L = {anbm | n mod 2=0, m≥1}
Context-sensitive Grammar (CSG) and Language (CSL)
Church’s Thesis for Turing Machine