LMU ☀️ CMSI 585
PROGRAMMING LANGUAGE FOUNDATIONS
HOMEWORK #2 PARTIAL ANSWERS
    1. This one in principle CAN be written out but you don't want to really write out every alternative:

      $\begin{array}{lcl} \mathit{oddLenPal} & \longrightarrow & \texttt{"A"} \\ & | & \texttt{"B"} \\ & | & \texttt{"C"} \\ & | & \ldots\;\textit{and so on for all the other Unicode letters} \\ & | & \texttt{"A"}\;\mathit{oddLenPal}\;\texttt{"A"} \\ & | & \texttt{"B"}\;\mathit{oddLenPal}\;\texttt{"B"} \\ & | & \texttt{"C"}\;\mathit{oddLenPal}\;\texttt{"C"} \\ & | & \texttt{"D"}\;\mathit{oddLenPal}\;\texttt{"D"} \\ & | & \ldots\;\textit{and so on for all the other Unicode letters} \\ \end{array}$

      Note that using the built-in rule $letter$ is tempting, but it can’t work. Grammars don’t have the notion of backreferences as in the regexes of most modern programming languages.

    2. This one was pretty easy to overthink, but it’s just this:

      $\begin{array}{lcl} \mathit{edcba} & \longrightarrow & \texttt{"e"}^*\;\texttt{"d"}^*\;\texttt{"c"}^*\;\texttt{"b"}^*\;\texttt{"a"}^*\; \end{array}$

    3. This is the language of (1) The same number of 0s then 1s, followed by any number of 2s, union (2) any number of 0s followed by the same number of 1s then 2s:

      $\begin{array}{lcl} s & \longrightarrow & x\;\texttt{"2"}^* \mid \texttt{"0"}^*\;y \\ x & \longrightarrow & (\texttt{"0"}\;x\;\texttt{"1"})? \\ y & \longrightarrow & (\texttt{"1"}\;y\;\texttt{"2"})? \end{array}$

    4. To do no-three-straight-zeros, you want to think of this as blocks of zero, one, or two zeros separated by ones.

      $\begin{array}{lcl} \mathit{noThreeStraightZeros} & \longrightarrow & (\mathit{atMostTwoZeros}\;\texttt{"1"})^*\;\mathit{atMostTwoZeros} \\ \mathit{atMostTwoZeros} & \longrightarrow & \varepsilon \mid \texttt{"0"} \mid \texttt{"00"} \end{array}$

    5. Here’s a grammar for the language of strings with twice as many a's as b's. It’s a series of chunks, each of which has twice as many a's as b's. Each chunk is one of three possibilities: a--a--b, a--b--a, or b--a--a. The grammar is:

      $\begin{array}{lcl} \mathit{twiceAB} & \longrightarrow & \mathit{chunk}^* \\ \mathit{chunk} & \longrightarrow & \texttt{"a"}\;\mathit{twiceAB}\;\texttt{"a"}\;\mathit{twiceAB}\;\texttt{"b"} \\ & | & \texttt{"a"}\;\mathit{twiceAB}\;\texttt{"b"}\;\mathit{twiceAB}\;\texttt{"a"} \\ & | & \texttt{"b"}\;\mathit{twiceAB}\;\texttt{"a"}\;\mathit{twiceAB}\;\texttt{"a"} \end{array}$

    6. Here’s a grammar for the language of strings of the form $a^nb^na^nb^n$:

      $\begin{array}{lcll} \mathit{anbnanbn} & \longrightarrow & (\texttt{"a"}\;0\;\texttt{"b"}\;\texttt{"a"}\;1\;\texttt{"b"})? & \textit{Place marks between as and bs} \\ 0 & \longrightarrow & \texttt{"a"}\;0\;\texttt{"b"}\;x & \textit{Gen new as and bs in left half} \\ x\;\texttt{"b"} & \longrightarrow & \texttt{"b"}\;x & \textit{Move xs to right of the first bs} \\ x\;\texttt{"a"} & \longrightarrow & \texttt{"a"}\;x & \textit{Move xs to the right of the second as} \\ x\;1 & \longrightarrow & \texttt{"a"}\;1\;\texttt{"b"} & \textit{When x hits the 1, gen as and bs at right} \\ 0 & \longrightarrow & \varepsilon & \textit{Drop mark} \\ 1 & \longrightarrow & \varepsilon & \textit{Drop mark} \\ \end{array}$

  1. Here’s one way do to the grammar. Note that it’s okay for this problem that the grammar be ambiguous, because the question did not specify any precedence or associativity:

    $\begin{array}{lcl} \mathit{Program} & \longleftarrow & \mathit{FunDecl}^*\;\mathit{Exp} \\ \mathit{FunDecl} & \longleftarrow & \mathit{id}\;\texttt{"="}\;\mathit{Params}\;\texttt{"=>"}\;\mathit{Exp} \\ \mathit{Params} & \longleftarrow & \mathit{id}\;(\texttt{","}\;\mathit{id}\,)^* \\ \mathit{Exp} & \longleftarrow & \mathit{Exp}\;\texttt{"?"}\;\mathit{Exp}\;\texttt{":"}\;\mathit{Exp} \\ & / & \mathit{Exp}\;(\texttt{"+"}/\texttt{"-"}/\texttt{"*"}/\texttt{"/"}/\texttt{"%"}/\texttt{"<"}/\texttt{"<="}/\texttt{">"}/\texttt{">="}/\texttt{"=="}/\texttt{"!="})\;\mathit{Exp} \\ & / & \texttt{"-"}\;\mathit{Exp}\;/\;\mathit{Exp}\;\texttt{"!"}\\ & / & \mathit{numlit}\;/\;\mathit{strlit}\;/\;\mathit{Call}\;/\;\mathit{id} \;/\;\texttt{"("}\;\mathit{Exp}\;\texttt{")"} \\ \mathit{Call} & \longleftarrow & \mathit{id}\;\texttt{"("}\;(\mathit{Exp}\;(\texttt{","}\;\mathit{Exp})^*)?\;\texttt{")"} \\ \mathit{numlit} & \longleftarrow & \textit{digit}^+\;(\texttt{"."}\;\textit{digit}^+\;)?\;((\texttt{"E"}/\texttt{"e"})\;(\texttt{"+"}/\texttt{"-"}\;)?\;\textit{digit}^+)? \\ \mathit{strlit} & \longleftarrow & \texttt{"\""}\;\textit{char}^*\;\texttt{"\""} \\ \mathit{char} & \longleftarrow & \texttt{"\\\\"}\;(\texttt{"'"}\;/\;\texttt{"\\""}\;/\;\texttt{"n"}\;/\; \texttt{"\\\\"}\;/\;\texttt{"u\{"}\;\textit{hexes}\;\texttt{"\}"}) \\ & / & \sim\texttt{"\\\\"}\;\sim\texttt{"\\""}\;\textit{any} \\ \mathit{hexes} & \longleftarrow & \textit{hexDigit}\;\textit{hexDigit}?\;\textit{hexDigit}?\;\textit{hexDigit}?\;\textit{hexDigit}?\;\textit{hexDigit}? \\ \textit{id} & \longleftarrow & (\textit{letter}/\texttt{"\$"})\;(\textit{letter}/\textit{digit}/\texttt{"_"}/\texttt{"\$"})^* \end{array}$

  2. The tree grammar is:

    $\begin{array}{l} n : \textsf{Numlit} \\ s : \textsf{Strlit} \\ i : \textsf{Identifier} \\ d : \textsf{FunDec} = i\;i^*\;e \\ e : \textsf{Exp} = n \mid s \mid i \mid -e \mid e\texttt{!} \mid e+e \mid e-e \mid e*e \mid e\,/\,e \mid e\;\%\;e \mid e\texttt{?}e\texttt{:}e \mid i\;e^* \\ p : \textsf{Program} = d^*\;e \end{array}$

    Remember that the tree grammar should treat all lexical categories as primitive.
  3. gcd = (x, y) => y ? gcd(y, x % y) : x
    cube = (x) => x * x * x
    "The answer is" + cube(gcd(30, 4!)) + "😦😦"
    

    The drawing program I used did not pick up emojis as text so I used escapes in my picture. Hopefully your drawing program was better:

    ast-for-madeup-lang.png

  4. import x from "x"
    console.log(93.8 * {x} << x.r[z])
    

    js-ast-1.png

    1. Syntax error, because super is a reserved word.
    2. Contextual error, since y is declared twice
    3. Lexical error, because ` is not part of any token
    4. No error
    5. Contextual error, because strings are not subscriptable (and this is only discovered after doing type checking)
    6. Not an error, this will just throw an error, not crash
    7. Syntax error, missing semicolon
    8. Contextual error, a return statement is needed, but the Java grammar can’t know this
    9. Contextual error, because in Java, this is a type mismatch!
    10. No error, since there is nothing wrong with a never-ending computation