On this page:
1 A Closed Substring Operation? (Extra Credit)
2 Proving That a Language is Not a Regular Language
3 CFGs and Derivations

Homework 6🔗

Last updated: Thu, 22 Oct 2026 10:45:17 -0400

Out: Thu Oct 22, 12:30pm EDT Due: Thu Oct 29, 12:30pm EDT

Note: Assignments are not officially "released" until—and are subject to change without notice up to—the indicated "Out" date and time. If an assignment is posted early, students may look ahead but are responsible for ensuring that they are always working with the most recent version of the homework.

This assignment explores non-regular languages.

Homework Problems

  1. A Closed Substring Operation? (Extra Credit) (up to 20 extra points, or -5 points)

  2. Proving That a Language is Not a Regular Language (20 points)

  3. CFGs and Derivations (15 points)

  4. README (2 point)

Total: 57 (out of 37) points

Submitting

Submit your solution to this assignment in Gradescope hw6. 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 A Closed Substring Operation? (Extra Credit)🔗

NOTE 2026-10-22: This problem is extra credit and is thus optional. This means that it will also be optional for the graders. By submitting an answer you accept that graders can refuse to grade this problem for any reason related to the content of the submission. There will be no discussion of this matter and no regrade requests will be accepted. Further, if there is any foreign notation or terminology not from this course or extraneous words or hint of AI use in any submitted answer, then the submission will receive negative points. This will be determined solely by the graders and no regrade requests or discussion will be accepted about this issue. If you are unsure about anything in this note, then do not submit an answer for this problem. By submitting an answer to this problem, you accept these terms.

An operation that programmers commonly want to do is to take the substring of some string.

Assume we have some function \mathrm{SUB}: \textsf{Language}\times\textsf{NatNum}\rightarrow\textsf{Language}

For example \mathrm{SUB}(\{\texttt{nerds},\texttt{kitkat},\texttt{skittles}\},3)=\{\texttt{ner},\texttt{kit},\texttt{ski}\}

Prove the following statement

IF L IS-A Regular Language THEN \mathrm{SUB}(L,4) IS-A Regular Language

Make sure the proof has all the usual required components and uses correct constructors and follows correct proof syntax from this course.

2 Proving That a Language is Not a Regular Language🔗

We saw in the basic proof by induction lecture examples, and in the last homework, that we occasionally needed to prove "REFL" statements. That is, statements that are True due to the reflexivity of equality.

If we have alphabet \Sigma = \left\{\mathtt{1},\mathtt{0},\mathtt{=},\mathtt{x},\mathtt{y},\mathtt{+}\right\}

  1. give 3 strings, made from the alphabet characters, that are True "REFL" statements (if the numbers are interpreted as numbers, operators have their typical meaning, and other characters represent variables)

  2. give 3 strings, made from the alphabet characters, that are False "REFL" statements

  3. if the set of all True "REFL" statements made from characters in \Sigma is called REFL, prove

    not (REFL IS-A Regular Language)

For parts 1 and 2, you may post specific examples to Piazza and ask for confirmation about your understanding of the problem.

For Part 3, use the Pumping Lemma and proof by contradiction. Make sure your proof has all the correct components and use the proper constructors. Remember that a proof by contradiction begins with assuming the opposite of the statement to prove and then proceeds like any normal proof, EXCEPT the proof must end in the statement Contradiction! that is justified with a proof of some statement X \,\mathrm{AND}\, \textbf{not} X This contradiction allows the conclusion that the assumption must be False and that the original statement to prove is True.

More specifically, the contradictory statements must be s IS-Pumpable and not (s IS-Pumpable) where IS-Pumpable is defined by \mathrm{ThePumpingLemma} and s is a counterexample that you must carefully choose that is in the language REFL and longer than some "pumping length" p, also specified by \mathrm{ThePumpingLemma}.

3 CFGs and Derivations🔗

Here’s a context-free grammar (CFG), called \mathit{PY}_{420}, 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{:} \left\langle STMT\right\rangle \texttt{elif} \left\langle EXPR\right\rangle\!\!\texttt{:} \left\langle STMT\right\rangle \texttt{else:} \left\langle STMT\right\rangle

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

\left\langle DEFFN\right\rangle

\rightarrow

\texttt{def} \left\langle ID\right\rangle \texttt{(}\left\langle IDS\right\rangle \texttt{)}\texttt{:} \left\langle STMT\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 EXPR\right\rangle \texttt{/} \left\langle EXPR\right\rangle \mid \left\langle FUN\right\rangle \mid \left\langle ID\right\rangle\texttt{(} \left\langle EXPRS\right\rangle \texttt{)}

\left\langle FUN\right\rangle

\rightarrow

\texttt{fn} \left\langle IDS\right\rangle \!\!\texttt{:} \left\langle EXPR\right\rangle

\left\langle EXPRS\right\rangle

\rightarrow

\left\langle EXPR\right\rangle, \left\langle EXPRS\right\rangle \mid \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 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{v}\mid\texttt{w}\mid\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 (ignoring whitespace) (also, you may treat multi-symbol terminals as one terminal, e.g., \texttt{fn}).

  1. Give two strings in \mathit{PY}_{420}’s language by showing their derivation steps. Each given string must have at least six derivation steps and each derivation should use (mostly) different rules.

  2. Give a formal description, i.e., with a constructor, of the grammar \mathit{PY}_{420}. You may assume that the given rules are in a set called \mathit{PY420RULES}.

  3. Give parse trees for the following strings in the language of \mathit{PY}_{420}:
    1. \texttt{def w(x,y): x / x - y / y}

    2. \texttt{v = fn x,y: x - y; def w(x,y): v(x,y)-1}

    3. \texttt{if x == y: 1 elif v == w: 2 else: 3}