Properties of Context-Free Languages - Examples

CS390, Spring 2024

Last modified: Jan 3, 2023
Contents:

Abstract

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\} \]

Reveal

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.)

Reveal

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} \]

(video): 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.

    Reveal
  2. Eliminate unit productions

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

    Reveal
  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).

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

    Reveal

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} \]

Reveal

5 Properties

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

Reveal

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

Reveal