| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 24 Jul 2020 13:06:09 +0100 | |
| changeset 739 | b44a4631e089 | 
| parent 722 | 7c09b7eadc6b | 
| child 748 | fca7f33a426c | 
| 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 | TODO: Testcases for expressions | 
| 11 | \url{https://github.com/ArashPartow/math-parser-benchmark-project}
 | |
| 12 | ||
| 13 | ||
| 719 | 14 | \noindent This coursework is worth 5\% and is due on \cwTHREE{} 
 | 
| 15 | at 18:00. You are asked to implement a parser for the | |
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 16 | WHILE language and also an interpreter. You can do the | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 17 | implementation in any programming language you like, but you | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 18 | need to submit the source code with which you answered the | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 19 | questions, otherwise a mark of 0\% will be awarded. You should | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 20 | use the lexer from the previous coursework for the parser. | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 21 | |
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 22 | \subsection*{Disclaimer}
 | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 23 | |
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 24 | It should be understood that the work you submit represents | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 25 | your own effort. You have not copied from anyone else. An | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 26 | exception is the Scala code I showed during the lectures, | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 27 | which you can use. You can also use your own code from the | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 28 | CW~1 and CW~2. | 
| 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 | |
| 56 | list, of tokens generated by the tokenizer from the previous | |
| 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 | |
| 59 | Figures~\ref{fib}, \ref{loop} and \ref{primes} (if your lexer cannot
 | |
| 60 | deal with comments you can delete them from the prime number program). | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 61 | In addition give the parse tree for the statement: | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 62 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 63 | \begin{lstlisting}[language=While,numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 64 | 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 | 65 | \end{lstlisting}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 66 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 67 | \noindent | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 68 | A (possibly incomplete) datatype for parse trees in Scala would | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 69 | look as in Figure~\ref{trees}.
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 70 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 71 | \begin{figure}
 | 
| 
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 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 83 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 84 | case class Var(s: String) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 85 | case class Num(i: Int) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 86 | 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 | 87 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 88 | case object True extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 89 | case object False extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 90 | case class Bop(o: String, a1: AExp, a2: AExp) extends BExp | 
| 682 | 91 | case class Lop(o: String, b1: BExp, b2: BExp) extends BExp | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 92 | \end{lstlisting}
 | 
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 93 | \caption{The datatype for parse trees in Scala.\label{trees}}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 94 | \end{figure}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 95 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 96 | \subsection*{Question 3}
 | 
| 205 
0b59588d28d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
204diff
changeset | 97 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 98 | 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 | 99 | and parsed in Question 1 and 2. This interpreter should take | 
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 100 | as input a parse tree. However be careful because, programs | 
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 101 | contain variables and variable assignments. This means | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 102 | 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 | 103 | 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 | 104 | 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 | 105 | evaluation function (interpreter) needs to look roughly as | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 106 | follows | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 107 | |
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 108 | \begin{lstlisting}[numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 109 | eval_stmt(stmt, env) | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 110 | \end{lstlisting}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 111 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 112 | \noindent | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 113 | where \pcode{stmt} corresponds to the parse tree
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 114 | 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 | 115 | acting as a store for variable values. | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 116 | Consider the Fibonacci program in Figure~\ref{fib}.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 117 | 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 | 118 | 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 | 119 | the variables \pcode{minus1} and \pcode{minus2}
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 120 | 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 | 121 | 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 | 122 | according to straightforward rules: for example an | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 123 | 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 | 124 | evaluates to \pcode{true}, otherwise the else-branch.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 125 | Loops should be run as long as the boolean is \pcode{true}.
 | 
| 205 
0b59588d28d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
204diff
changeset | 126 | |
| 684 | 127 | |
| 128 | ||
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 129 | Give some time measurements for your interpreter | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 130 | 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 | 131 | 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 | 132 | 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 | 133 | you scale this value if you are willing to wait, say | 
| 473 | 134 | 1 Minute? | 
| 202 
180cbfc1520a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
201diff
changeset | 135 | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 136 | \begin{figure}[p]
 | 
| 684 | 137 | \lstinputlisting[language=while,xleftmargin=20mm]{../progs/fib.while}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 138 | \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 | 139 | \end{figure}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 140 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 141 | \begin{figure}[p]
 | 
| 684 | 142 | \lstinputlisting[language=while,xleftmargin=20mm]{../progs/loops.while}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 143 | \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 | 144 | Usually used for timing measurements.\label{loop}}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 145 | \end{figure}
 | 
| 214 
5be68de225e9
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
213diff
changeset | 146 | |
| 684 | 147 | \begin{figure}[p]
 | 
| 148 | \lstinputlisting[language=while,xleftmargin=0mm]{../progs/primes.while}
 | |
| 149 | \caption{Prime number program.\label{primes}}
 | |
| 150 | \end{figure}
 | |
| 151 | ||
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 152 | \end{document}
 | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 153 | |
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 154 | %%% Local Variables: | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 155 | %%% mode: latex | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 156 | %%% TeX-master: t | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 157 | %%% End: |