| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Sun, 29 Oct 2023 00:06:30 +0100 | |
| changeset 946 | 2d579af2bb98 | 
| parent 942 | 7f52427568ff | 
| child 949 | 58720903336e | 
| permissions | -rw-r--r-- | 
| 630 | 1 | % !TEX program = xelatex | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 2 | \documentclass{article}
 | 
| 299 
6322922aa990
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
298diff
changeset | 3 | \usepackage{../style}
 | 
| 216 
f5ec7c597c5b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
214diff
changeset | 4 | \usepackage{../langs}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 5 | |
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 6 | \begin{document}
 | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 7 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 8 | \section*{Coursework 3}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 9 | |
| 722 | 10 | |
| 11 | ||
| 750 | 12 | \noindent This coursework is worth 10\% and is due on \cwTHREE{} at
 | 
| 877 | 13 | 16:00. You are asked to implement a parser for the WHILE language and | 
| 748 | 14 | also an interpreter. You can do the implementation in any programming | 
| 15 | language you like, but you need to submit the source code with which | |
| 16 | you answered the questions, otherwise a mark of 0\% will be | |
| 17 | awarded. You should use the lexer from the previous coursework for the | |
| 942 | 18 | parser. Please submit your code to Github by the deadline. | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 19 | |
| 750 | 20 | \subsection*{Disclaimer\alert}
 | 
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 21 | |
| 750 | 22 | It should be understood that the work you submit represents your own | 
| 23 | effort. You have not copied from anyone else. An exception is the | |
| 24 | Scala code I showed during the lectures or uploaded to KEATS, which | |
| 25 | you can both use. You can also use your own code from the CW~1 and | |
| 886 | 26 | CW~2. But do not | 
| 27 | be tempted to ask Github Copilot for help or do any other | |
| 28 | shenanigans like this! | |
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 29 | |
| 299 
6322922aa990
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
298diff
changeset | 30 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 31 | \subsection*{Question 1}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 32 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 33 | Design a grammar for the WHILE language and give the grammar | 
| 544 | 34 | rules. The main categories of non-terminals should be: | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 35 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 36 | \begin{itemize}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 37 | \item arithmetic expressions (with the operations from the | 
| 682 | 38 |   previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*},
 | 
| 39 |   \pcode{/} and \pcode{\%})
 | |
| 40 | \item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>},
 | |
| 686 | 41 |   \code{>=}, \code{<=}, 
 | 
| 682 | 42 |   \code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false})
 | 
| 43 | \item single statements (that is \pcode{skip}, assignments, \pcode{if}s,
 | |
| 44 |   \pcode{while}-loops, \pcode{read} and \pcode{write})
 | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 45 | \item compound statements separated by semicolons | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 46 | \item blocks which are enclosed in curly parentheses | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 47 | \end{itemize}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 48 | |
| 683 | 49 | \noindent | 
| 50 | Make sure the grammar is not left-recursive. | |
| 51 | ||
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 52 | \subsection*{Question 2}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 53 | |
| 684 | 54 | You should implement a parser for the WHILE language using parser | 
| 55 | combinators. Be careful that the parser takes as input a stream, or | |
| 750 | 56 | list, of \emph{tokens} generated by the tokenizer from the previous
 | 
| 684 | 57 | coursework. For this you might want to filter out whitespaces and | 
| 58 | comments. Your parser should be able to handle the WHILE programs in | |
| 801 | 59 | Figures~\ref{fib} -- \ref{collatz}.  In addition give the
 | 
| 901 | 60 | parse tree according to your grammar for the statement: | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 61 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 62 | \begin{lstlisting}[language=While,numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 63 | if (a < b) then skip else a := a * b + 1 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 64 | \end{lstlisting}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 65 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 66 | \noindent | 
| 901 | 67 | The output of the parser is an abstract syntax tree (AST). | 
| 68 | A (possibly incomplete) datatype for ASTs of the WHILE language | |
| 69 | is shown in Figure~\ref{trees}.
 | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 70 | |
| 750 | 71 | \begin{figure}[p]
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 72 | \begin{lstlisting}[language=Scala]
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 73 | abstract class Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 74 | abstract class AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 75 | abstract class BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 76 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 77 | type Block = List[Stmt] | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 78 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 79 | case object Skip extends Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 80 | case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 81 | case class While(b: BExp, bl: Block) extends Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 82 | case class Assign(s: String, a: AExp) extends Stmt | 
| 750 | 83 | case class Read(s: String) extends Stmt | 
| 84 | case class WriteVar(s: String) extends Stmt | |
| 85 | case class WriteStr(s: String) extends Stmt | |
| 86 | // for printing variables and strings | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 87 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 88 | case class Var(s: String) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 89 | case class Num(i: Int) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 90 | case class Aop(o: String, a1: AExp, a2: AExp) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 91 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 92 | case object True extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 93 | case object False extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 94 | case class Bop(o: String, a1: AExp, a2: AExp) extends BExp | 
| 682 | 95 | case class Lop(o: String, b1: BExp, b2: BExp) extends BExp | 
| 750 | 96 | // logical operations: and, or | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 97 | \end{lstlisting}
 | 
| 901 | 98 | \caption{The datatype for abstract syntax trees in Scala.\label{trees}}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 99 | \end{figure}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 100 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 101 | \subsection*{Question 3}
 | 
| 205 
0b59588d28d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
204diff
changeset | 102 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 103 | Implement an interpreter for the WHILE language you designed | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 104 | and parsed in Question 1 and 2. This interpreter should take | 
| 901 | 105 | as input an AST. However be careful because, programs | 
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 106 | contain variables and variable assignments. This means | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 107 | you need to maintain a kind of memory, or environment, | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 108 | where you can look up a value of a variable and also | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 109 | store a new value if it is assigned. Therefore an | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 110 | evaluation function (interpreter) needs to look roughly as | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 111 | follows | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 112 | |
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 113 | \begin{lstlisting}[numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 114 | eval_stmt(stmt, env) | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 115 | \end{lstlisting}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 116 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 117 | \noindent | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 118 | where \pcode{stmt} corresponds to the parse tree
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 119 | of the program and \pcode{env} is an environment
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 120 | acting as a store for variable values. | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 121 | Consider the Fibonacci program in Figure~\ref{fib}.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 122 | At the beginning of the program this store will be | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 123 | empty, but needs to be extended in line 3 and 4 where | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 124 | the variables \pcode{minus1} and \pcode{minus2}
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 125 | are assigned values. These values need to be reassigned in | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 126 | lines 7 and 8. The program should be interpreted | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 127 | according to straightforward rules: for example an | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 128 | if-statement will ``run'' the if-branch if the boolean | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 129 | evaluates to \pcode{true}, otherwise the else-branch.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 130 | Loops should be run as long as the boolean is \pcode{true}.
 | 
| 750 | 131 | Programs you should be able to run are shown in | 
| 132 | Figures \ref{fib} -- \ref{collatz}.
 | |
| 684 | 133 | |
| 134 | ||
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 135 | Give some time measurements for your interpreter | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 136 | and the loop program in Figure~\ref{loop}. For example
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 137 | how long does your interpreter take when \pcode{start}
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 138 | is initialised with 100, 500 and so on. How far can | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 139 | you scale this value if you are willing to wait, say | 
| 473 | 140 | 1 Minute? | 
| 202 
180cbfc1520a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
201diff
changeset | 141 | |
| 750 | 142 | \begin{figure}[h]
 | 
| 860 | 143 | \lstinputlisting[language=while,xleftmargin=20mm]{../cwtests/cw03/fib.while}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 144 | \caption{Fibonacci program in the WHILE language.\label{fib}}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 145 | \end{figure}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 146 | |
| 750 | 147 | \begin{figure}[h]
 | 
| 860 | 148 | \lstinputlisting[language=while,xleftmargin=20mm]{../cwtests/cw03/loops.while}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 149 | \caption{The three-nested-loops program in the WHILE language. 
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 150 | Usually used for timing measurements.\label{loop}}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 151 | \end{figure}
 | 
| 214 
5be68de225e9
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
213diff
changeset | 152 | |
| 750 | 153 | \begin{figure}[h]
 | 
| 860 | 154 | \lstinputlisting[language=while,xleftmargin=0mm]{../cwtests/cw03/primes.while}
 | 
| 750 | 155 | \caption{Prime number program.\label{primes}}
 | 
| 156 | \end{figure}
 | |
| 157 | ||
| 158 | ||
| 684 | 159 | \begin{figure}[p]
 | 
| 860 | 160 | \lstinputlisting[language=while,xleftmargin=0mm]{../cwtests/cw03/collatz2.while}
 | 
| 750 | 161 | \caption{Collatz series program.\label{collatz}}
 | 
| 684 | 162 | \end{figure}
 | 
| 163 | ||
| 942 | 164 | \clearpage | 
| 165 | \newpage | |
| 166 | \section*{Answers}
 | |
| 167 | ||
| 168 | \noindent | |
| 169 | \textbf{Question 1 (Grammar):}\\
 | |
| 170 | ||
| 171 | \mbox{}\\[9cm]
 | |
| 172 | ||
| 173 | \noindent | |
| 174 | \textbf{Question 2 (Prase Tree):}\\
 | |
| 175 | ||
| 176 | \mbox{}\\[8cm]
 | |
| 177 | ||
| 178 | ||
| 179 | \noindent | |
| 180 | \textbf{Question 3 (Timings):}\\
 | |
| 181 | ||
| 182 | \begin{center}
 | |
| 183 |   \def\arraystretch{1.5}
 | |
| 184 |   \begin{tabular}{l|l}
 | |
| 185 | n & secs\\ | |
| 186 | \hline | |
| 187 | 100 & \\ | |
| 188 | 500 & \\ | |
| 189 | 700 & \\ | |
| 190 | 1000 & \\ | |
| 191 | \\ | |
| 192 | \\ | |
| 193 | \\ | |
| 194 | \\ | |
| 195 |    \end{tabular} 
 | |
| 196 | \end{center}  
 | |
| 197 | ||
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 198 | \end{document}
 | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 199 | |
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 200 | %%% Local Variables: | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 201 | %%% mode: latex | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 202 | %%% TeX-master: t | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 203 | %%% End: |