Properties of Context-Free Languages - Examples

CS390, Spring 2020

Last modified: Jul 23, 2019


Practice designing and working with CFLs and with more advanced manipulations of CFGs.

1 Prove this Language is Context-Free

\[ L = \{ a^ib^ia^jb^j | i, j \geq 0\} \]


2 Prove this Language is Not Context-Free

\[ L = \{ a^ib^ia^ib^i \} \]

(Hint: This type of problem is often addressed via the pumping lemma.)


3 Converting to CNF

Consider this grammar for expressions with properly nested parentheses & brackets.

\[ \begin{align} S &\rightarrow SA | SB | \epsilon \\ A &\rightarrow ( S ) | S | E \\ B &\rightarrow [ S ] | E \\ E &\rightarrow x \\ \end{align} \]

A discussion of what this grammar “means” and how to read it:

There are 4 steps in converting a grammar to Chomsky Normal Form.

See if you can do each of them in turn:

  1. Eliminate the $\epsilon$ productions.

    Rewrite the grammar so that all productions that can derive the empty string are removed.

  2. Eliminate unit productions

    Rewrite the grammar to remove all productions where the entire right-hand-side consists of a single variable.

  3. Eliminate useless variables and their productions.

    Rewrite the grammar to eliminate any useless variables (variables that can never be derived or that will never generate a terminal).

  4. Factor the long productions to get them into the form $A \rightarrow a$ or $A \rightarrow B C$


4 CYK parsing

Parse the string ([x])(x) using the grammar:

\[ \begin{align} S &\rightarrow SA | C D | C E | x | SB | F G | F H \\ A &\rightarrow C D | C E | SA | x | SB | F G | F H \\ B &\rightarrow F G | F H | x \\ C &\rightarrow ( \\ D &\rightarrow S E \\ E &\rightarrow ) \\ F &\rightarrow [ \\ G &\rightarrow S H \\ H &\rightarrow ] \\ \end{align} \]


5 Properties

5.1 Prove that, if $F$ is any finite set of strings, $F$ is a CFL.


5.2 Prove that, if $L$ is a CFL and $F$ is any finite set of strings, $L-F$ is a CFL.