| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 10 Oct 2025 10:18:05 +0100 | |
| changeset 1004 | 04353d465dfb | 
| parent 992 | d09d73808ca2 | 
| 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}
 | 
| 949 | 5 | \usepackage[normalem]{ulem}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 6 | |
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 7 | \begin{document}
 | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 8 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 9 | \section*{Coursework 3}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 10 | |
| 722 | 11 | |
| 12 | ||
| 988 | 13 | \noindent This coursework is worth 15\% and is due on \cwTHREE{} at
 | 
| 877 | 14 | 16:00. You are asked to implement a parser for the WHILE language and | 
| 967 | 15 | also an interpreter. The parser needs to use parser combinators. You | 
| 16 | can do the implementation in any programming language you like, but | |
| 17 | you need to submit the source code with which you answered the | |
| 18 | questions, otherwise a mark of 0\% will be awarded. If you use Scala | |
| 19 | in your code, a good place to start is the file \texttt{comb1.sc} and
 | |
| 985 | 20 | \texttt{comb2.sc} uploaded to KEATS. Make sure your parser combinators 
 | 
| 21 | process list of tokens as input, not strings. Feel free to use the ``hack'' | |
| 967 | 22 | explained during the lectures. This might make your grammar | 
| 23 | simpler. However, make sure you understand the code involved in the | |
| 24 | ``hack'' because if you just do ``mix-and-match'' you will receive | |
| 991 | 25 | strange error messages. The main function that will be tested is called | 
| 967 | 26 | \texttt{eval} and \texttt{Stmts.parse\_all}. The latter expects a list
 | 
| 27 | of tokens as input and generates an AST. The former expects an AST and | |
| 992 | 28 | ``runs'' the program. The marks will be distributed such that 9 marks | 
| 29 | are given for the correct grammar (and parsers); 6 marks for the correct | |
| 967 | 30 | \texttt{eval} function.  You should use the lexer from CW2 for the
 | 
| 991 | 31 | parser - you potentially need to make modifications to the regular | 
| 32 | expressions for CW3. | |
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 33 | |
| 750 | 34 | \subsection*{Disclaimer\alert}
 | 
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 35 | |
| 750 | 36 | It should be understood that the work you submit represents your own | 
| 37 | effort. You have not copied from anyone else. An exception is the | |
| 38 | Scala code I showed during the lectures or uploaded to KEATS, which | |
| 991 | 39 | you can both use. You can also use your own code from CW~1 and | 
| 985 | 40 | CW~2. | 
| 41 | %But do not | |
| 42 | %be tempted to ask Github Copilot for help or do any other | |
| 43 | %shenanigans like this! | |
| 967 | 44 | |
| 985 | 45 | %\subsection*{Syntax Error in Template File cw03.sc\alert}
 | 
| 46 | % | |
| 47 | %Apologies, there is a small syntax error in the template file where a variable | |
| 48 | %needs to be called \texttt{tks} instead of \texttt{tk}. The code
 | |
| 49 | %in question is at the end of \texttt{cw03.sc} and should be like
 | |
| 50 | %this (see lines 5, 6 and 8): | |
| 51 | % | |
| 52 | %\begin{lstlisting}[language=Scala,numbers=left]
 | |
| 53 | %@main | |
| 54 | %def test(file: String) = {
 | |
| 55 | % val contents = os.read(os.pwd / "examples" / file) | |
| 56 | % println(s"Lex $file: ") | |
| 57 | % val tks = tokenise(contents) | |
| 58 | %  println(tks.mkString(","))
 | |
| 59 | % println(s"Parse $file: ") | |
| 60 | % val ast = Stmts.parse_all(tks).head | |
| 61 | % println(ast) | |
| 62 | % println(s"Eval $file: ") | |
| 63 | % println(eval(ast)) | |
| 64 | %} | |
| 65 | %\end{lstlisting}  
 | |
| 967 | 66 | |
| 67 | ||
| 299 
6322922aa990
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
298diff
changeset | 68 | |
| 985 | 69 | \subsection*{Task 1}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 70 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 71 | Design a grammar for the WHILE language and give the grammar | 
| 544 | 72 | 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 | 73 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 74 | \begin{itemize}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 75 | \item arithmetic expressions (with the operations from the | 
| 682 | 76 |   previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*},
 | 
| 77 |   \pcode{/} and \pcode{\%})
 | |
| 78 | \item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>},
 | |
| 686 | 79 |   \code{>=}, \code{<=}, 
 | 
| 682 | 80 |   \code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false})
 | 
| 81 | \item single statements (that is \pcode{skip}, assignments, \pcode{if}s,
 | |
| 82 |   \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 | 83 | \item compound statements separated by semicolons | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 84 | \item blocks which are enclosed in curly parentheses | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 85 | \end{itemize}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 86 | |
| 683 | 87 | \noindent | 
| 88 | Make sure the grammar is not left-recursive. | |
| 89 | ||
| 985 | 90 | \subsection*{Task 2}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 91 | |
| 684 | 92 | You should implement a parser for the WHILE language using parser | 
| 967 | 93 | combinators. Be careful that the parser takes as input a list of | 
| 94 | \emph{tokens} generated by the tokenizer from the previous
 | |
| 684 | 95 | coursework. For this you might want to filter out whitespaces and | 
| 96 | comments. Your parser should be able to handle the WHILE programs in | |
| 967 | 97 | the \texttt{examples} directory.  The output of the parser is an
 | 
| 98 | abstract syntax tree (AST). A (possibly incomplete) datatype for ASTs | |
| 99 | of the WHILE language is shown in Figure~\ref{trees}.
 | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 100 | |
| 750 | 101 | \begin{figure}[p]
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 102 | \begin{lstlisting}[language=Scala]
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 103 | abstract class Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 104 | abstract class AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 105 | abstract class BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 106 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 107 | type Block = List[Stmt] | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 108 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 109 | case object Skip extends Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 110 | 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 | 111 | 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 | 112 | case class Assign(s: String, a: AExp) extends Stmt | 
| 750 | 113 | case class Read(s: String) extends Stmt | 
| 114 | case class WriteVar(s: String) extends Stmt | |
| 115 | case class WriteStr(s: String) extends Stmt | |
| 116 | // for printing variables and strings | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 117 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 118 | case class Var(s: String) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 119 | case class Num(i: Int) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 120 | 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 | 121 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 122 | case object True extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 123 | case object False extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 124 | case class Bop(o: String, a1: AExp, a2: AExp) extends BExp | 
| 682 | 125 | case class Lop(o: String, b1: BExp, b2: BExp) extends BExp | 
| 750 | 126 | // logical operations: and, or | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 127 | \end{lstlisting}
 | 
| 901 | 128 | \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 | 129 | \end{figure}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 130 | |
| 985 | 131 | \subsection*{Task 3}
 | 
| 132 | ||
| 133 | In addition to the simple assignments of the form \code{... := ...} 
 | |
| 134 | from Task 1, parse the assignments of the form | |
| 135 | ||
| 136 | \begin{quote}
 | |
| 991 | 137 | \texttt{... += ...} \;\;, \texttt{... -= ...} \;\; and\;\; \texttt{... *= ...}
 | 
| 985 | 138 | \end{quote}
 | 
| 139 | ||
| 991 | 140 | \noindent | 
| 985 | 141 | and translate them into simple assignments. For example | 
| 142 | ||
| 143 | \begin{quote}
 | |
| 144 | \texttt{cnt += 1}
 | |
| 145 | \end{quote}
 | |
| 146 | ||
| 991 | 147 | \noindent | 
| 148 | should produce the assignment \texttt{cnt := cnt + 1}. Similarly
 | |
| 149 | for \texttt{-=} and \texttt{*=}. Be careful to \emph{translate} these
 | |
| 150 | assignments---they should not produce a separate AST-node. | |
| 985 | 151 | |
| 152 | \subsection*{Task 4}
 | |
| 205 
0b59588d28d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
204diff
changeset | 153 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 154 | Implement an interpreter for the WHILE language you designed | 
| 985 | 155 | and parsed in Tasks 1 and 2. This interpreter should take | 
| 901 | 156 | 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 | 157 | contain variables and variable assignments. This means | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 158 | 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 | 159 | 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 | 160 | 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 | 161 | evaluation function (interpreter) needs to look roughly as | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 162 | follows | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 163 | |
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 164 | \begin{lstlisting}[numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 165 | eval_stmt(stmt, env) | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 166 | \end{lstlisting}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 167 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 168 | \noindent | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 169 | where \pcode{stmt} corresponds to the parse tree
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 170 | 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 | 171 | acting as a store for variable values. | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 172 | Consider the Fibonacci program in Figure~\ref{fib}.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 173 | 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 | 174 | 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 | 175 | the variables \pcode{minus1} and \pcode{minus2}
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 176 | 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 | 177 | 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 | 178 | according to straightforward rules: for example an | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 179 | 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 | 180 | evaluates to \pcode{true}, otherwise the else-branch.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 181 | Loops should be run as long as the boolean is \pcode{true}.
 | 
| 949 | 182 | Note also that some programs contain a read-statement, | 
| 985 | 183 | which means you need to read an integer from the commandline | 
| 949 | 184 | and store the value in the corresponding variable. | 
| 967 | 185 | Programs you should be able to run are given in the | 
| 186 | \texttt{examples} directory. The output
 | |
| 949 | 187 | of the \texttt{primes.while} should look as follows:
 | 
| 684 | 188 | |
| 949 | 189 | \begin{figure}[h]
 | 
| 190 | {\small
 | |
| 191 | \begin{lstlisting}[numbers=none]
 | |
| 192 | 2 | |
| 193 | 3 | |
| 194 | 5 | |
| 195 | 7 | |
| 196 | 11 | |
| 197 | 13 | |
| 198 | 17 | |
| 199 | 19 | |
| 200 | 23 | |
| 201 | 29 | |
| 202 | 31 | |
| 203 | 37 | |
| 204 | 41 | |
| 205 | 43 | |
| 206 | 47 | |
| 207 | 53 | |
| 208 | 59 | |
| 209 | 61 | |
| 210 | 67 | |
| 211 | 71 | |
| 212 | 73 | |
| 213 | 79 | |
| 214 | 83 | |
| 215 | 89 | |
| 216 | 97 | |
| 217 | Map(end -> 100, n -> 100, f -> 4, tmp -> 1) | |
| 218 | \end{lstlisting}}
 | |
| 219 | \caption{Sample output for the file \texttt{primes.while}.\label{fib}}
 | |
| 220 | \end{figure}
 | |
| 684 | 221 | |
| 942 | 222 | |
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 223 | \end{document}
 | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 224 | |
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 225 | %%% Local Variables: | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 226 | %%% mode: latex | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 227 | %%% TeX-master: t | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 228 | %%% End: |