On this page:
1 CFGs and Their String Derivations
2 Language of Tuples Is Regular?
3 Language of Tuples Is Context-Free?
4 A PDA for Tuples

Homework 5

Last updated: Thu, 13 Apr 2023 12:42:36 -0400

Out: Mon Mar 06, 00:00 EST Due: Mon Mar 19, 23:59 EST (note: 2 weeks)

This assignment explores context-free languages.

Homework Problems

  1. CFGs and Their String Derivations (9 points)

  2. Language of Tuples Is Regular? (8 pts)

  3. Language of Tuples Is Context-Free? (9 points)

  4. A PDA for Tuples (9 points)

  5. README (1 point)

Total: 36 points

Submitting

Submit your solution to this assignment in Gradescope hw5. 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 CFGs and Their String Derivations

Here’s a context-free grammar (CFG), called PY, representing the core of a Python-like language:

\left\langle STMTS+\right\rangle

\rightarrow

\left\langle STMT\right\rangle; \left\langle STMTS+\right\rangle \mid \left\langle STMT\right\rangle

\left\langle STMT\right\rangle

\rightarrow

\left\langle ID\right\rangle \texttt{=} \left\langle EXPR\right\rangle \mid \texttt{if} \left\langle EXPR\right\rangle\texttt{then} \left\langle STMT\right\rangle \texttt{else} \left\langle STMT\right\rangle

\mid \texttt{print(} \left\langle EXPR\right\rangle \texttt{)} \mid \left\langle EXPR\right\rangle

\left\langle EXPR\right\rangle

\rightarrow

\left\langle ID\right\rangle \mid \left\langle NUM\right\rangle \mid \left\langle EXPR\right\rangle \texttt{==} \left\langle EXPR\right\rangle \mid \left\langle EXPR\right\rangle \texttt{*} \left\langle EXPR\right\rangle

\mid \left\langle TUP\right\rangle \mid \texttt{fn (} \left\langle IDS\right\rangle \texttt{) :} \left\langle EXPR\right\rangle \mid \left\langle ID\right\rangle\texttt{(} \left\langle EXPRS\right\rangle \texttt{)}

\left\langle EXPRS\right\rangle

\rightarrow

\left\langle EXPRS+\right\rangle \mid \varepsilon

\left\langle EXPRS+\right\rangle

\rightarrow

\left\langle EXPR\right\rangle, \left\langle EXPRS+\right\rangle \mid \left\langle EXPR\right\rangle

\left\langle TUP\right\rangle

\rightarrow

\texttt{(} \left\langle EXPRS\right\rangle \texttt{)}

\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 IDS\right\rangle

\rightarrow

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

\left\langle IDS+\right\rangle

\rightarrow

\left\langle ID\right\rangle \texttt{,} \left\langle IDS+\right\rangle \mid \left\langle ID\right\rangle

\left\langle ID\right\rangle

\rightarrow

\texttt{x}\mid\texttt{y}\mid\texttt{z}

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

The variables (nonterminals) 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 two strings in PY’s language by showing their derivation steps. Each given string must have at least six derivation steps.

  2. Give a formal description of the grammar PY. You may assume that the given rules are in a set called \textit{PYRULES}.

  3. Give parse trees for the following strings in the language of PY:
    1. \texttt{(}1,2,\texttt{(}3,4\texttt{)}\texttt{)}

    2. \texttt{x = 2; z = fn(y): y * x; z(5)}

2 Language of Tuples Is Regular?

You learned in your previous Discrete Structures math course (e.g., CS 220) (and also Sipser Chapter 0) that a tuple is a finite sequence written with surrounding parentheses and elements separated by commas. Tuples may also contain other tuples as elements.

The grammar from the CFGs and Their String Derivations Problem allowed tuples of arbitrary things, but in this problem we will look at a restricted kind of tuple. Formally, define the language \mathrm{TUPS} to represent possibly-nested tuples containing single-digit numbers:

\mathrm{TUPS} = \left\{t\mid t\textrm{ is a tuple containing zero or more single-digit numbers or other tuples}\right\}

For example, the following are examples of strings in \mathrm{TUPS}:
  • ()\in\mathrm{TUPS}

  • (())\in\mathrm{TUPS}

  • (1,2,3)\in\mathrm{TUPS}

  • ((1,2),(3,4))\in\mathrm{TUPS}

  • (((1,2),3),4)\in\mathrm{TUPS}

Note that the following strings are not in \mathrm{TUPS}:
  • \varepsilon\notin\mathrm{TUPS}

  • (10,11)\notin\mathrm{TUPS}

Prove that \mathrm{TUPS} is not a regular language.

Your proof must be a proper proof by contradiction with all the required steps.

3 Language of Tuples Is Context-Free?

In the Language of Tuples Is Regular? Problem, you proved that the \mathrm{TUPS} language is not a regular language. Now prove that \mathrm{TUPS} is a context-free language (CFL).

Your proof must be in the form of a Statements and Justifications table, but it must use the CFG representation of CFLs.

4 A PDA for Tuples

Create a PDA that recognizes the TUPS language from the Language of Tuples Is Regular? Problem above.

Drawing the state diagram is sufficient. Using clear, reasonable shorthands, as discussed in class and the Textbook (for example, having one transition read multiple input characters, or push multiple input characters onto the stack) is acceptable.