On this page:
1 Not a Regular Language
2 Deriving Strings Using CFGs
3 Design a CFG

Homework 4

Last updated: Thu, 17 Feb 2022 19:36:41 -0500

Out: Mon Feb 21, 00:00 EST Due: Sun Feb 27, 23:59 EST

This assignment begins to explore context-free languages and context-free grammars.

Homework Problems

  1. Not a Regular Language (8 points)

  2. Deriving Strings Using CFGs (8 points)

  3. Design a CFG (8 points)

  4. README (1 point)

Total: 25 points

Submitting

Submit your solution to this assignment in Gradescope hw4. Please assign each page to the correct problem and make sure your solutions are legible.

A submission must also include a README containing the required information.

1 Not a Regular Language

Prove that the following language is not a regular language:

L = \left\{w\mid w = \mathrm{rev}(w)\right\}

where \mathrm{rev} is the function you defined in Reversing Strings from Homework 3

You can assume alphabet \Sigma = \{\texttt{x},\texttt{y}\}.

2 Deriving Strings Using CFGs

Here’s a CFG representing a tiny subset of JavaScript.

\left\langle EXPR\right\rangle

\rightarrow

\left\langle ID\right\rangle \mid \left\langle NUM\right\rangle \mid \left\langle STR\right\rangle \mid \left\langle EXPR\right\rangle \texttt{===} \left\langle EXPR\right\rangle \mid \texttt{func (} \left\langle IDS\right\rangle \texttt{)} \{\texttt{ return } \left\langle EXPR\right\rangle \}

\mid\left\{\left\langle PROPS\right\rangle\right\}\mid \left\langle EXPR\right\rangle \texttt{.} \left\langle ID\right\rangle \mid \left\langle EXPR\right\rangle\texttt{[}\left\langle STR\right\rangle\texttt{]} \mid \left\langle EXPR\right\rangle \texttt{.} \left\langle ID\right\rangle \texttt{=} \left\langle EXPR\right\rangle

\left\langle PROPS\right\rangle

\rightarrow

\left\langle PROP\right\rangle, \left\langle PROPS\right\rangle \mid \left\langle PROP\right\rangle \mid \varepsilon

\left\langle PROP\right\rangle

\rightarrow

\left\langle ID\right\rangle:\left\langle EXPR\right\rangle

\left\langle NUM\right\rangle

\rightarrow

0\mid 1\mid 2\mid 3\mid 4\mid 5\mid 6\mid 7\mid 8\mid 9

\left\langle STR\right\rangle

\rightarrow

\texttt{"}\left\langle ID\right\rangle\texttt{"}

\left\langle IDS\right\rangle

\rightarrow

\left\langle ID\right\rangle \left\langle IDS\right\rangle \mid \varepsilon

\left\langle ID\right\rangle

\rightarrow

\texttt{a}\mid\texttt{b}\mid\texttt{c}\mid\cdots\mid\texttt{x}\mid\texttt{y}\mid\texttt{z}

(Yes, real-world languages are much more complicated than textbook examples.)

The variables of the CFG are all the names enclosed in angle brackets, e.g., \left\langle EXPR\right\rangle is a variable name. All other symbols used in the rules are terminals.

  1. Give a formal description of this grammar. You may assume that the rules are already given (i.e., you don’t need to rewrite them into your solutions).

  2. Give derivations or parse trees for the following strings:
    1. \texttt{func (x y) \{ return x === y \}}

    2. \texttt{\{a: 1, b: "c"\}["a"]}

    3. \texttt{\{x: 8\}.x = 9}

3 Design a CFG

Create a CFG that generates the following language L.

L = \left\{w\mid w = \mathrm{rev}(w)\right\}

where \mathrm{rev} is the function you defined in Reversing Strings from Homework 3

You can assume alphabet \Sigma = \{\texttt{x},\texttt{y}\}.