| author | Christian Urban <urbanc@in.tum.de> | 
| Sun, 26 Jan 2020 14:16:30 +0000 | |
| changeset 707 | 566f4b2750e8 | 
| parent 686 | 5fe95ea0bad0 | 
| child 719 | 0ba5aa9ecaa4 | 
| 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 | |
| 630 | 10 | \noindent This coursework is worth 5\% and is due on 22 | 
| 567 | 11 | November 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 | 12 | 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 | 13 | 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 | 14 | 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 | 15 | 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 | 16 | 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 | 17 | |
| 358 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 18 | \subsection*{Disclaimer}
 | 
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 19 | |
| 
b3129cff41e9
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 20 | 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 | 21 | 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 | 22 | 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 | 23 | 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 | 24 | CW~1 and CW~2. | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 25 | |
| 299 
6322922aa990
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
298diff
changeset | 26 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 27 | \subsection*{Question 1}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 28 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 29 | Design a grammar for the WHILE language and give the grammar | 
| 544 | 30 | 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 | 31 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 32 | \begin{itemize}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 33 | \item arithmetic expressions (with the operations from the | 
| 682 | 34 |   previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*},
 | 
| 35 |   \pcode{/} and \pcode{\%})
 | |
| 36 | \item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>},
 | |
| 686 | 37 |   \code{>=}, \code{<=}, 
 | 
| 682 | 38 |   \code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false})
 | 
| 39 | \item single statements (that is \pcode{skip}, assignments, \pcode{if}s,
 | |
| 40 |   \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 | 41 | \item compound statements separated by semicolons | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 42 | \item blocks which are enclosed in curly parentheses | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 43 | \end{itemize}
 | 
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 44 | |
| 683 | 45 | \noindent | 
| 46 | Make sure the grammar is not left-recursive. | |
| 47 | ||
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 48 | \subsection*{Question 2}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 49 | |
| 684 | 50 | You should implement a parser for the WHILE language using parser | 
| 51 | combinators. Be careful that the parser takes as input a stream, or | |
| 52 | list, of tokens generated by the tokenizer from the previous | |
| 53 | coursework. For this you might want to filter out whitespaces and | |
| 54 | comments. Your parser should be able to handle the WHILE programs in | |
| 55 | Figures~\ref{fib}, \ref{loop} and \ref{primes} (if your lexer cannot
 | |
| 56 | 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 | 57 | 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 | 58 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 59 | \begin{lstlisting}[language=While,numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 60 | 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 | 61 | \end{lstlisting}
 | 
| 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 | \noindent | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 64 | 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 | 65 | look as in Figure~\ref{trees}.
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 66 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 67 | \begin{figure}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 68 | \begin{lstlisting}[language=Scala]
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 69 | abstract class Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 70 | abstract class AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 71 | abstract class BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 72 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 73 | type Block = List[Stmt] | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 74 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 75 | case object Skip extends Stmt | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 76 | 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 | 77 | 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 | 78 | 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 | 79 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 80 | case class Var(s: String) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 81 | case class Num(i: Int) extends AExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 82 | 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 | 83 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 84 | case object True extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 85 | case object False extends BExp | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 86 | case class Bop(o: String, a1: AExp, a2: AExp) extends BExp | 
| 682 | 87 | 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 | 88 | \end{lstlisting}
 | 
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 89 | \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 | 90 | \end{figure}
 | 
| 201 
c813506e0ee8
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
200diff
changeset | 91 | |
| 419 
4110ab35e5d8
updated courseworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
358diff
changeset | 92 | \subsection*{Question 3}
 | 
| 205 
0b59588d28d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
204diff
changeset | 93 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 94 | 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 | 95 | 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 | 96 | 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 | 97 | contain variables and variable assignments. This means | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 98 | 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 | 99 | 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 | 100 | 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 | 101 | evaluation function (interpreter) needs to look roughly as | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 102 | follows | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 103 | |
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 104 | \begin{lstlisting}[numbers=none]
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 105 | eval_stmt(stmt, env) | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 106 | \end{lstlisting}
 | 
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 107 | |
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 108 | \noindent | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 109 | where \pcode{stmt} corresponds to the parse tree
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 110 | 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 | 111 | acting as a store for variable values. | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 112 | Consider the Fibonacci program in Figure~\ref{fib}.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 113 | 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 | 114 | 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 | 115 | the variables \pcode{minus1} and \pcode{minus2}
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 116 | 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 | 117 | 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 | 118 | according to straightforward rules: for example an | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 119 | 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 | 120 | evaluates to \pcode{true}, otherwise the else-branch.
 | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 121 | 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 | 122 | |
| 684 | 123 | |
| 124 | ||
| 301 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 125 | Give some time measurements for your interpreter | 
| 
e8c0269c8ff5
update
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
300diff
changeset | 126 | 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 | 127 | 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 | 128 | 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 | 129 | you scale this value if you are willing to wait, say | 
| 473 | 130 | 1 Minute? | 
| 202 
180cbfc1520a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
201diff
changeset | 131 | |
| 300 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 132 | \begin{figure}[p]
 | 
| 684 | 133 | \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 | 134 | \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 | 135 | \end{figure}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 136 | |
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 137 | \begin{figure}[p]
 | 
| 684 | 138 | \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 | 139 | \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 | 140 | Usually used for timing measurements.\label{loop}}
 | 
| 
08d99acd35e8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
299diff
changeset | 141 | \end{figure}
 | 
| 214 
5be68de225e9
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
213diff
changeset | 142 | |
| 684 | 143 | \begin{figure}[p]
 | 
| 144 | \lstinputlisting[language=while,xleftmargin=0mm]{../progs/primes.while}
 | |
| 145 | \caption{Prime number program.\label{primes}}
 | |
| 146 | \end{figure}
 | |
| 147 | ||
| 200 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 148 | \end{document}
 | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 149 | |
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 150 | %%% Local Variables: | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 151 | %%% mode: latex | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 152 | %%% TeX-master: t | 
| 
7415871b1ef5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 153 | %%% End: |