2080 3

CSC262 · 2080 · 10 marks

Define context free grammar with an example. Explain with example, how context free grammar is converted to Chomsky Normal Form.

Define context free grammar with an example. Explain with example, how context free grammar is converted to Chomsky Normal Form.[10]

Theory of ComputationChapter 4: Context Free GrammarChomsky Normal FormNumerical: CFG to Chomsky Normal Form

Context Free Grammar and Conversion to Chomsky Normal Form

Given

This is a theory and derivation question. The only structural data needed is the grammar chosen for the worked example, which is:

$$ \begin{aligned} S &\to ASB \mid \varepsilon\ A &\to aAS \mid a\ B &\to SbS \mid A \mid bb \end{aligned} $$

No data is missing; the question is fully answerable.


Solution

Part 1: Context Free Grammar (CFG)

A Context Free Grammar is a 4-tuple $G = (V, T, P, S)$ where:

  • $V$ = finite set of non-terminals (variables)
  • $T$ = finite set of terminals, with $V \cap T = \varnothing$
  • $P$ = finite set of productions of the form $A \to \alpha$, where $A \in V$ and $\alpha \in (V \cup T)^*$
  • $S \in V$ = start symbol

Every production has exactly one non-terminal on the left, so it can be replaced regardless of surrounding context - hence "context free."

Example: $$S \to 0S1 \mid \varepsilon$$

  • $V={S},\ T={0,1},\ S=S$
  • Generates ${0^n1^n \mid n \ge 0} = {\varepsilon, 01, 0011, \dots}$

CFGs describe the syntax of programming languages and drive parser/compiler design.


Part 2: Chomsky Normal Form (CNF)

A CFG is in CNF if every production is of the form:

  1. $A \to BC$ (two non-terminals), or
  2. $A \to a$ (single terminal)

with the sole exception $S \to \varepsilon$ allowed if $\varepsilon$ is in the language, and $S$ must not appear on any RHS.

Conversion steps:

  1. Add a new start symbol.
  2. Eliminate $\varepsilon$-productions.
  3. Eliminate unit productions.
  4. Reduce RHS length to 2 (introduce variables).
  5. Replace terminals in long productions with new variables.

Worked Example

Original grammar: $$ S \to ASB \mid \varepsilon,\quad A \to aAS \mid a,\quad B \to SbS \mid A \mid bb $$

Step 1: New start symbol $S_0$

$$ S_0 \to S,\quad S \to ASB \mid \varepsilon,\quad A \to aAS \mid a,\quad B \to SbS \mid A \mid bb $$

Step 2: Eliminate $\varepsilon$-productions

Nullable variables: $S$ (since $S\to\varepsilon$). Insert versions with $S$ deleted:

  • $S \to ASB \Rightarrow S \to ASB \mid AB$
  • $A \to aAS \Rightarrow A \to aAS \mid aA$
  • $B \to SbS \Rightarrow B \to SbS \mid bS \mid Sb \mid b$
  • $S_0 \to S$ keeps $S_0 \to \varepsilon$ (since $\varepsilon$ is in the language)

$$ \begin{aligned} S_0 &\to S \mid \varepsilon\ S &\to ASB \mid AB\ A &\to aAS \mid aA \mid a\ B &\to SbS \mid bS \mid Sb \mid b \mid A \mid bb \end{aligned} $$

Step 3: Eliminate unit productions

Unit pairs: $S_0 \to S$, $B \to A$.

  • $S_0 \to S$: $S_0 \to ASB \mid AB$
  • $B \to A$: $B \to aAS \mid aA \mid a$

$$ \begin{aligned} S_0 &\to ASB \mid AB \mid \varepsilon\ S &\to ASB \mid AB\ A &\to aAS \mid aA \mid a\ B &\to SbS \mid bS \mid Sb \mid b \mid aAS \mid aA \mid a \mid bb \end{aligned} $$

Step 4: Replace terminals in multi-symbol productions

Introduce $X_a \to a$, $X_b \to b$. Replace terminals wherever they sit with other symbols:

$$ \begin{aligned} S_0 &\to ASB \mid AB \mid \varepsilon\ S &\to ASB \mid AB\ A &\to X_aAS \mid X_aA \mid a\ B &\to SX_bS \mid X_bS \mid SX_b \mid b \mid X_aAS \mid X_aA \mid a \mid X_bX_b\ X_a &\to a\ X_b &\to b \end{aligned} $$

Step 5: Reduce RHS length to 2

Introduce helper variables:

  • $ASB$: let $C_1 \to SB$, so $\to A C_1$
  • $X_aAS$: let $C_2 \to AS$, so $\to X_a C_2$
  • $SX_bS$: let $C_3 \to X_bS$, so $\to S C_3$

Final CNF grammar: $$ \begin{aligned} S_0 &\to AC_1 \mid AB \mid \varepsilon\ S &\to AC_1 \mid AB\ A &\to X_aC_2 \mid X_aA \mid a\ B &\to SC_3 \mid X_bS \mid SX_b \mid b \mid X_aC_2 \mid X_aA \mid a \mid X_bX_b\ C_1 &\to SB\ C_2 &\to AS\ C_3 &\to X_bS\ X_a &\to a,\qquad X_b \to b \end{aligned} $$

Every production is now $A \to BC$ or $A \to a$ (plus the permitted $S_0 \to \varepsilon$), so the grammar is in Chomsky Normal Form.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.