Homework 4
Last updated: Thu, 15 Oct 2020 18:52:32 -0400
Out: Wed Oct 7, 00:00 EST Due: Tues Oct 20, 23:59 EST (Note: extended deadline)
This is the first homework for the material in chapter 2 of the textbook.
Homework Problems
String Derivations (4 + 4 = 8 pts)
CFGs (4 pts)
Pushdown Automata (6 pts)
Closure under Context-Free Languages (2 + 2 + 2 = 6 pts)
Regular => Context-Free (6 pts)
Total: 30 points
Submitting
Submit this assignment at Gradescope hw4.
You may write up your solution however you like but the submission should only include pdf or plain text files, and each file must be assigned to the correct problem in Gradescope.
1 String Derivations
Here’s a grammar for the core of an object-oriented language like Java:
\left\langle CLASS\right\rangle | \rightarrow | \texttt{class } \left\langle CLSID\right\rangle \texttt{ extends } \left\langle CLSID\right\rangle \,\{\left\langle FLDDECLS\right\rangle \left\langle CONS\right\rangle \left\langle METHS\right\rangle\} |
\left\langle FLDDECLS\right\rangle | \rightarrow | \left\langle FLDDECL\right\rangle \left\langle FLDDECLS\right\rangle \mid \varepsilon |
\left\langle FLDDECL\right\rangle | \rightarrow | \left\langle CLSID\right\rangle \left\langle FLDID\right\rangle\texttt{;} |
\left\langle CONS\right\rangle | \rightarrow | \left\langle CLSID\right\rangle(\left\langle ARGS\right\rangle)\{\texttt{super(}\left\langle EXPRS\right\rangle\texttt{)}; \left\langle FLDASSIGNS\right\rangle\} |
\left\langle FLDASSIGNS\right\rangle | \rightarrow | \left\langle FLDASSIGN\right\rangle \left\langle FLDASSIGNS\right\rangle \mid \varepsilon |
\left\langle FLDASSIGN\right\rangle | \rightarrow | \texttt{this.}\!\left\langle FLDID\right\rangle \texttt{=} \left\langle FLDID\right\rangle; |
\left\langle METHS\right\rangle | \rightarrow | \left\langle METH\right\rangle \left\langle METHS\right\rangle \mid \varepsilon |
\left\langle METH\right\rangle | \rightarrow | \left\langle CLSID\right\rangle \left\langle METHID\right\rangle(\left\langle ARGS\right\rangle)\{ \texttt{return } \left\langle EXPR\right\rangle; \} |
\left\langle ARGS\right\rangle | \rightarrow | \left\langle ARGS+\right\rangle \mid \left\langle ARG\right\rangle \mid \varepsilon |
\left\langle ARGS+\right\rangle | \rightarrow | \left\langle ARG\right\rangle, \left\langle ARGS+\right\rangle \mid \left\langle ARG\right\rangle |
\left\langle ARG\right\rangle | \rightarrow | \left\langle CLSID\right\rangle \left\langle ARGID\right\rangle |
\left\langle CLSID\right\rangle | \rightarrow | \textrm{ class names can be any string in \texttt{[a-zA-Z]+}} |
\left\langle METHID\right\rangle | \rightarrow | \textrm{ method names can be any string in \texttt{[a-zA-Z]+}} |
\left\langle FLDID\right\rangle | \rightarrow | \textrm{ field names can be any string \texttt{[a-zA-Z]+}} |
\left\langle ARGID\right\rangle | \rightarrow | \textrm{ arg names can can be any string in \texttt{[a-zA-Z]+}} |
\left\langle EXPRS\right\rangle | \rightarrow | \left\langle EXPRS+\right\rangle \mid \left\langle EXPR\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 EXPR\right\rangle | \rightarrow | \left\langle VAR\right\rangle \mid \left\langle EXPR\right\rangle\!.\!\left\langle FLDID\right\rangle \mid \left\langle EXPR\right\rangle\!.\!\left\langle METHID\right\rangle(\left\langle EXPRS\right\rangle)\mid |
\texttt{new } \left\langle CLSID\right\rangle(\left\langle EXPRS\right\rangle) \mid (\left\langle CLSID\right\rangle)\left\langle EXPR\right\rangle | ||
\left\langle VAR\right\rangle | \rightarrow | \textrm{ variable names can be any string in \texttt{[a-zA-Z]+}} |
The terminals of the grammar are the tokens, i.e., the "words", of the language, which includes keywords (like class), identifiers (like for class or field names), parens, braces, and punctuation (like dot, semicolon, or comma). Whitespace is not included in the terminals and can be ignored.
class A extends Object { A() { super(); } }
class Pair extends Object {
Object fst; Object snd;
Pair(Object fst, Object snd) {
super(); this.fst=fst; this.snd=snd;
}
Pair setfst(Object newfst) {
return new Pair(newfst, this.snd);
}
}
2 CFGs
Create a CFG that generates the following language. You can assume alphabet \Sigma = \{0,1\}.
L = \{w\mid w = BW(w)\}, where BW is The Backwards Operation from Homework 3.
3 Pushdown Automata
Create a pushdown automata for the language L from the previous CFGs problem.
You can either draw a diagram or give a formal description.
4 Closure under Context-Free Languages
union
concatentation
Kleene star
5 Regular => Context-Free
Give a proof by induction on regular expressions that:
For any language A, if A is regular, then A is also context-free.
You may assume that the statements from the previous Closure under Context-Free Languages problem are proved.