CYK Membership Test for the String “baaba”

CYK Membership Test for the String “baaba”

Verified Sources
Sep 11, 2026

This lesson applies the CYK algorithm to the grammar

SABBCABAaBCCbCABa\begin{aligned} S &\to AB \mid BC\\ A &\to BA \mid a\\ B &\to CC \mid b\\ C &\to AB \mid a \end{aligned}

and the input string

w=baaba.w=\text{baaba}.

The goal is to determine whether wL(G)w\in L(G). CYK is a bottom-up parsing method that fills a triangular chart of substrings. It normally requires Chomsky Normal Form; this grammar already satisfies that requirement because every production is either binary or produces one terminal.

CYK records every nonterminal capable of deriving each contiguous substring. The final cell contains the answer: if it includes SS, the string is generated by the grammar.

Footnotes

  1. CYK algorithm - Wikipedia - Describes CYK as a bottom-up dynamic-programming parser requiring CNF-style grammar rules.

Grammar and input

Nonterminals: {S,A,B,C}\{S,A,B,C\}. Terminals: {a,b}\{a,b\}. Start symbol: SS. Input: w=baabaw=\text{baaba}, whose length is n=5n=5.

CYK chart notation

Let T[i,j]T[i,j] denote the set of nonterminals that derive the substring beginning at position ii and ending at position jj, using one-based indexing.

For example:

  • T[1,1]T[1,1] describes the substring b\text{b}.
  • T[2,4]T[2,4] describes the substring aab\text{aab}.
  • T[1,5]T[1,5] describes the complete string baaba\text{baaba}.

For a substring of length greater than one, CYK tries every split point:

w[i:j]=w[i:k]  w[k+1:j].w[i:j]=w[i:k]\;w[k+1:j].

If a production XYZX\to YZ exists, and

YT[i,k]andZT[k+1,j],Y\in T[i,k] \qquad\text{and}\qquad Z\in T[k+1,j],

then XX is inserted into T[i,j]T[i,j].

Footnotes

  1. Parsing I: Context-Free Grammars and the CYK Algorithm - Explains chart cells, binary splits, and the CYK recurrence.

CYK Recognition Procedure

  1. 1
    Step 1

    Write the symbols as w1=bw_1=\text{b}, w2=aw_2=\text{a}, w3=aw_3=\text{a}, w4=bw_4=\text{b}, and w5=aw_5=\text{a}.

  2. 2
    Step 2

    For each terminal symbol, insert every nonterminal that directly produces it. Here, AaA\to a, CaC\to a, and BbB\to b.

  3. 3
    Step 3

    For lengths =2,3,4,5\ell=2,3,4,5, examine every substring and every possible split into two nonempty parts.

  4. 4
    Step 4

    Use the binary rules SABS\to AB, SBCS\to BC, ABAA\to BA, BCCB\to CC, and CABC\to AB.

  5. 5
    Step 5

    The string belongs to the language exactly when ST[1,5]S\in T[1,5].

Step 1: Length-one substrings

The terminal productions are

Aa,Ca,Bb.A\to a,\qquad C\to a,\qquad B\to b.

The input positions are:

PositionSymbolDeriving nonterminalsReason
1bb{B}\{B\}BbB\to b
2aa{A,C}\{A,C\}Aa, CaA\to a,\ C\to a
3aa{A,C}\{A,C\}Aa, CaA\to a,\ C\to a
4bb{B}\{B\}BbB\to b
5aa{A,C}\{A,C\}Aa, CaA\to a,\ C\to a

Thus,

T[1,1]={B}T[2,2]={A,C}T[3,3]={A,C}T[4,4]={B}T[5,5]={A,C}.\begin{aligned} T[1,1]&=\{B\}\\ T[2,2]&=\{A,C\}\\ T[3,3]&=\{A,C\}\\ T[4,4]&=\{B\}\\ T[5,5]&=\{A,C\}. \end{aligned}

Number of Deriving Nonterminals in Length-One Cells

Each input symbol may be derived by one or more grammar variables.

Step 2: Length-two substrings

For each length-two substring, there is only one split.

Substring w1w2=baw_1w_2=\text{ba}

The split is

ba.\text{b}\mid\text{a}.

The left cell contains {B}\{B\} and the right cell contains {A,C}\{A,C\}. Therefore, the possible pairs are

(B,A),(B,C).(B,A),\qquad (B,C).

The grammar contains ABAA\to BA, but no rule XBCX\to BC. Hence,

T[1,2]={A}.T[1,2]=\{A\}.

Indeed,

ABAba.A\Rightarrow BA\Rightarrow ba.

Substring w2w3=aaw_2w_3=\text{aa}

The possible pairs are

(A,A), (A,C), (C,A), (C,C).(A,A),\ (A,C),\ (C,A),\ (C,C).

The only matching binary rule is

BCC.B\to CC.

Therefore,

T[2,3]={B}.T[2,3]=\{B\}.

Substring w3w4=abw_3w_4=\text{ab}

The left cell is {A,C}\{A,C\} and the right cell is {B}\{B\}. The possible pairs are

(A,B),(C,B).(A,B),\qquad (C,B).

The rules SABS\to AB and CABC\to AB match (A,B)(A,B). Thus,

T[3,4]={S,C}.T[3,4]=\{S,C\}.

Substring w4w5=baw_4w_5=\text{ba}

This has the same symbol pattern as positions 1122:

T[4,5]={A}.T[4,5]=\{A\}.

The length-two results are therefore

T[1,2]={A},T[2,3]={B},T[3,4]={S,C},T[4,5]={A}.\boxed{ T[1,2]=\{A\},\quad T[2,3]=\{B\},\quad T[3,4]=\{S,C\},\quad T[4,5]=\{A\}. }

Step 3: Length-three substrings

Substring w1w3=baaw_1w_3=\text{baa}

There are two splits.

Split 1: baa\text{b}\mid\text{aa}

The pair is

(B,B).(B,B).

No production has right-hand side BBBB.

Split 2: baa\text{ba}\mid\text{a}

The pair combinations are

(A,A),(A,C).(A,A),\qquad (A,C).

No production has right-hand side AAAA or ACAC.

Therefore,

T[1,3]=.T[1,3]=\varnothing.

Substring w2w4=aabw_2w_4=\text{aab}

Split 1: aab\text{a}\mid\text{ab}

The left cell is {A,C}\{A,C\} and the right cell is {S,C}\{S,C\}. The possible pairs include

(A,S), (A,C), (C,S), (C,C).(A,S),\ (A,C),\ (C,S),\ (C,C).

The rule BCCB\to CC matches only if the left side contains CC and the right side contains CC. Hence,

BT[2,4].B\in T[2,4].

Split 2: aab\text{aa}\mid\text{b}

The pair is

(B,B),(B,B),

which matches no binary production.

Thus,

T[2,4]={B}.T[2,4]=\{B\}.

Substring w3w5=abaw_3w_5=\text{aba}

Split 1: aba\text{a}\mid\text{ba}

The pair combinations are

(A,A),(C,A).(A,A),\qquad (C,A).

Neither matches a binary production.

Split 2: aba\text{ab}\mid\text{a}

Since T[3,4]={S,C}T[3,4]=\{S,C\} and T[5,5]={A,C}T[5,5]=\{A,C\}, the pair (C,A)(C,A) is possible, but no rule has right-hand side CACA.

Therefore,

T[3,5]=.T[3,5]=\varnothing.

The length-three cells are

T[1,3]=,T[2,4]={B},T[3,5]=.\boxed{ T[1,3]=\varnothing,\quad T[2,4]=\{B\},\quad T[3,5]=\varnothing. }

Step 4: Length-four substrings

Substring w1w4=baabw_1w_4=\text{baab}

Split 1: baab\text{b}\mid\text{aab}

The pair is

(B,B).(B,B).

No rule matches BBBB.

Split 2: baab\text{ba}\mid\text{ab}

The cells are

T[1,2]={A},T[3,4]={S,C}.T[1,2]=\{A\}, \qquad T[3,4]=\{S,C\}.

The possible pairs are (A,S)(A,S) and (A,C)(A,C); neither occurs on the right-hand side of a binary production.

Split 3: baab\text{baa}\mid\text{b}

Since T[1,3]=T[1,3]=\varnothing, this split contributes nothing.

Therefore,

T[1,4]=.T[1,4]=\varnothing.

Substring w2w5=aabaw_2w_5=\text{aaba}

Split 1: aaba\text{a}\mid\text{aba}

Since T[3,5]=T[3,5]=\varnothing, this split contributes nothing.

Split 2: aaba\text{aa}\mid\text{ba}

The cells are

T[2,3]={B},T[4,5]={A}.T[2,3]=\{B\}, \qquad T[4,5]=\{A\}.

The pair is (B,A)(B,A), which matches

ABA.A\to BA.

Therefore,

T[2,5]={A}.T[2,5]=\{A\}.

Split 3: aaba\text{aab}\mid\text{a}

The cells are {B}\{B\} and {A,C}\{A,C\}, giving (B,A)(B,A) and (B,C)(B,C). Again, only BABA matches, so this split also derives AA.

Thus,

T[2,5]={A}.T[2,5]=\{A\}.

The length-four cells are

T[1,4]=,T[2,5]={A}.\boxed{ T[1,4]=\varnothing,\qquad T[2,5]=\{A\}. }

Step 5: Length-five substring

The complete input is

w1w5=baaba.w_1w_5=\text{baaba}.

There are four possible split points.

SplitLeft substringLeft cellRight substringRight cellMatching pairResult
1b\text{b}{B}\{B\}aaba\text{aaba}{A}\{A\}(B,A)(B,A)ABAA\to BA gives AA
2ba\text{ba}{A}\{A\}aba\text{aba}\varnothingnonenone
3baa\text{baa}\varnothingba\text{ba}{A}\{A\}nonenone
4baab\text{baab}\varnothinga\text{a}{A,C}\{A,C\}nonenone

The first split derives AA:

T[1,1]={B},T[2,5]={A},T[1,1]=\{B\},\qquad T[2,5]=\{A\},

and because

ABA,A\to BA,

we obtain

AT[1,5].A\in T[1,5].

No split produces SS:

  • SABS\to AB would require a split whose left cell contains AA and whose right cell contains BB.
  • SBCS\to BC would require a split whose left cell contains BB and whose right cell contains CC.

Neither condition is satisfied.

Hence,

T[1,5]={A}.T[1,5]=\{A\}.

Complete CYK triangular table

The table is conventionally displayed with longer substrings above shorter substrings.

Span lengthSubstringCYK cell
5baaba\text{baaba}{A}\{A\}
4baab\text{baab}\varnothing
4aaba\text{aaba}{A}\{A\}
3baa\text{baa}\varnothing
3aab\text{aab}{B}\{B\}
3aba\text{aba}\varnothing
2ba\text{ba}{A}\{A\}
2aa\text{aa}{B}\{B\}
2ab\text{ab}{S,C}\{S,C\}
2ba\text{ba}{A}\{A\}
1b\text{b}{B}\{B\}
1a\text{a}{A,C}\{A,C\}
1a\text{a}{A,C}\{A,C\}
1b\text{b}{B}\{B\}
1a\text{a}{A,C}\{A,C\}

A positional triangular representation is:

1: b2: a3: a4: b5: a
1{B}\{B\}{A}\{A\}\varnothing\varnothing{A}\{A\}
2{A,C}\{A,C\}{B}\{B\}{B}\{B\}{A}\{A\}
3{A,C}\{A,C\}{S,C}\{S,C\}\varnothing
4{B}\{B\}{A}\{A\}
5{A,C}\{A,C\}

The top-right cell is

T[1,5]={A}.T[1,5]=\{A\}.

Final membership test

The start symbol SS is not present in the top cell: S otinT[1,5]S\ otin T[1,5]. Therefore, the string is rejected by the grammar.

Conclusion

The CYK algorithm returns

baabaL(G).\boxed{\text{baaba}\notin L(G)}.

Although the entire string can be derived from nonterminal AA,

ABAbAbBAbaaba,A\Rightarrow BA \Rightarrow bA \Rightarrow bB A \Rightarrow baaba,

this does not establish membership in L(G)L(G) because the grammar’s designated start symbol is SS, not AA.

A valid derivation from the grammar must have the form

Sbaaba.S\Rightarrow^* \text{baaba}.

Since ST[1,5]S\notin T[1,5], no such derivation exists.

Common Questions and Verification Notes

CYK and This Grammar

1 / 6
Question · Term

What does T[i,j] represent?

Click to reveal
Answer · Definition

The set of nonterminals that derive the substring from position i through position j.

Exam Strategy

Always distinguish between “some nonterminal derives the string” and “the start symbol derives the string.” For CYK membership, inspect only whether SS appears in the final top-right cell.

Knowledge Check

Question 1 of 5
Q1Single choice

What is the initial CYK cell for the first symbol bb?

Explore Related Topics

1

When Can a Language Be Accepted by a Turing Machine?

2

LangChain vs LangGraph: A Comprehensive Comparison

LangChain and LangGraph are complementary frameworks in the LangChain ecosystem: LangChain offers fast, linear pipeline composition via LCEL, while LangGraph provides a graph‑based runtime with persistent state, loops, branching, and human‑in‑the‑loop capabilities.

  • LangChain’s modular components (models, prompts, memory, tools) are combined with the pipe operator | to build simple to moderate linear workflows such as retrieve‑summarize‑answer.
  • LangGraph introduces three primitives—State, Nodes, and Edges—enabling cycles, conditional branches, multi‑agent coordination, and checkpoint‑driven fault tolerance.
  • State management differs: LangChain relies on chain‑scoped Memory; LangGraph uses a global State object with checkpointers that support time‑travel debugging and rollback.
  • Decision guidance: choose LangChain for straightforward RAG or Q&A bots; adopt LangGraph when you need loops, branching, persistent state, or production‑grade resilience.

Statet+1=merge(Statet, nNodestfn(Statet))\text{State}_{t+1} = \text{merge}\big(\text{State}_t,\ \bigcup_{n \in \text{Nodes}_t} f_n(\text{State}_t)\big)

3

Functional Dependencies and Candidate Keys in $R(A,B,C)$

In R(A,B,C)R(A,B,C) with functional dependencies ABA\rightarrow B and BAB\rightarrow A, neither single attribute determines all three attributes, so AA and BB are not keys; the minimal candidate keys are {A,C}\{A,C\} and {B,C}\{B,C\}.

  • A+={A,B}A^{+}= \{A,B\} and B+={A,B}B^{+}= \{A,B\}, both missing CC → not superkeys.
  • Adding CC yields (AC)+=(BC)+={A,B,C}(AC)^{+}= (BC)^{+}= \{A,B,C\}, making ACAC and BCBC candidate keys.
  • Mutual determination (ABA\leftrightarrow B) does not imply key status without covering the whole schema.
  • A common exam trap is assuming AA or BB are keys because they determine each other.
  • Heuristic: any attribute not derivable from others (here CC) must appear in every candidate key.