author | Christian Urban <christian.urban@kcl.ac.uk> |
Tue, 01 Sep 2020 12:44:07 +0100 | |
changeset 750 | e93a9e74ca8e |
parent 748 | 383f2a5952ce |
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:
298
diff
changeset
|
3 |
\usepackage{../style} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
214
diff
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:
358
diff
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 |
748 | 13 |
18:00. You are asked to implement a parser for the WHILE language and |
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 |
|
18 |
parser. Please package everything(!) in a zip-file that creates a |
|
19 |
directory with the name \texttt{YournameYourFamilyname} on my end. |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
750 | 21 |
\subsection*{Disclaimer\alert} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
22 |
|
750 | 23 |
It should be understood that the work you submit represents your own |
24 |
effort. You have not copied from anyone else. An exception is the |
|
25 |
Scala code I showed during the lectures or uploaded to KEATS, which |
|
26 |
you can both use. You can also use your own code from the CW~1 and |
|
27 |
CW~2. |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
29 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
30 |
\subsection*{Question 1} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
32 |
Design a grammar for the WHILE language and give the grammar |
544 | 33 |
rules. The main categories of non-terminals should be: |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
34 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
35 |
\begin{itemize} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
36 |
\item arithmetic expressions (with the operations from the |
682 | 37 |
previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*}, |
38 |
\pcode{/} and \pcode{\%}) |
|
39 |
\item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>}, |
|
686 | 40 |
\code{>=}, \code{<=}, |
682 | 41 |
\code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false}) |
42 |
\item single statements (that is \pcode{skip}, assignments, \pcode{if}s, |
|
43 |
\pcode{while}-loops, \pcode{read} and \pcode{write}) |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
44 |
\item compound statements separated by semicolons |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
45 |
\item blocks which are enclosed in curly parentheses |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
46 |
\end{itemize} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
683 | 48 |
\noindent |
49 |
Make sure the grammar is not left-recursive. |
|
50 |
||
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
51 |
\subsection*{Question 2} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
52 |
|
684 | 53 |
You should implement a parser for the WHILE language using parser |
54 |
combinators. Be careful that the parser takes as input a stream, or |
|
750 | 55 |
list, of \emph{tokens} generated by the tokenizer from the previous |
684 | 56 |
coursework. For this you might want to filter out whitespaces and |
57 |
comments. Your parser should be able to handle the WHILE programs in |
|
750 | 58 |
Figures~\ref{fib}, \ref{loop} and \ref{primes}. In addition give the |
59 |
parse tree for the statement: |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
60 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
61 |
\begin{lstlisting}[language=While,numbers=none] |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
62 |
if (a < b) then skip else a := a * b + 1 |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
63 |
\end{lstlisting} |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
64 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
65 |
\noindent |
750 | 66 |
A (possibly incomplete) datatype for parse trees in Scala is shown |
67 |
in Figure~\ref{trees}. |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
68 |
|
750 | 69 |
\begin{figure}[p] |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
70 |
\begin{lstlisting}[language=Scala] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
71 |
abstract class Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
72 |
abstract class AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
73 |
abstract class BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
74 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
75 |
type Block = List[Stmt] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
76 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
77 |
case object Skip extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
78 |
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:
299
diff
changeset
|
79 |
case class While(b: BExp, bl: Block) extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
80 |
case class Assign(s: String, a: AExp) extends Stmt |
750 | 81 |
case class Read(s: String) extends Stmt |
82 |
case class WriteVar(s: String) extends Stmt |
|
83 |
case class WriteStr(s: String) extends Stmt |
|
84 |
// for printing variables and strings |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
85 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
86 |
case class Var(s: String) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
87 |
case class Num(i: Int) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
88 |
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:
299
diff
changeset
|
89 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
90 |
case object True extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
91 |
case object False extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
92 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
682 | 93 |
case class Lop(o: String, b1: BExp, b2: BExp) extends BExp |
750 | 94 |
// logical operations: and, or |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
95 |
\end{lstlisting} |
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
96 |
\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:
299
diff
changeset
|
97 |
\end{figure} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
98 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
99 |
\subsection*{Question 3} |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
100 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
101 |
Implement an interpreter for the WHILE language you designed |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
102 |
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:
358
diff
changeset
|
103 |
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:
300
diff
changeset
|
104 |
contain variables and variable assignments. This means |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
105 |
you need to maintain a kind of memory, or environment, |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
106 |
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:
300
diff
changeset
|
107 |
store a new value if it is assigned. Therefore an |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
108 |
evaluation function (interpreter) needs to look roughly as |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
109 |
follows |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
110 |
|
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
111 |
\begin{lstlisting}[numbers=none] |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
112 |
eval_stmt(stmt, env) |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
113 |
\end{lstlisting} |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
114 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
115 |
\noindent |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
116 |
where \pcode{stmt} corresponds to the parse tree |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
117 |
of the program and \pcode{env} is an environment |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
118 |
acting as a store for variable values. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
119 |
Consider the Fibonacci program in Figure~\ref{fib}. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
120 |
At the beginning of the program this store will be |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
121 |
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:
300
diff
changeset
|
122 |
the variables \pcode{minus1} and \pcode{minus2} |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
123 |
are assigned values. These values need to be reassigned in |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
124 |
lines 7 and 8. The program should be interpreted |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
125 |
according to straightforward rules: for example an |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
126 |
if-statement will ``run'' the if-branch if the boolean |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
127 |
evaluates to \pcode{true}, otherwise the else-branch. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
128 |
Loops should be run as long as the boolean is \pcode{true}. |
750 | 129 |
Programs you should be able to run are shown in |
130 |
Figures \ref{fib} -- \ref{collatz}. |
|
684 | 131 |
|
132 |
||
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
133 |
Give some time measurements for your interpreter |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
134 |
and the loop program in Figure~\ref{loop}. For example |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
135 |
how long does your interpreter take when \pcode{start} |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
136 |
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:
300
diff
changeset
|
137 |
you scale this value if you are willing to wait, say |
473 | 138 |
1 Minute? |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
139 |
|
750 | 140 |
\begin{figure}[h] |
141 |
\lstinputlisting[language=while,xleftmargin=20mm]{../progs/while-tests/fib.while} |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
142 |
\caption{Fibonacci program in the WHILE language.\label{fib}} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
143 |
\end{figure} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
144 |
|
750 | 145 |
\begin{figure}[h] |
146 |
\lstinputlisting[language=while,xleftmargin=20mm]{../progs/while-tests/loops.while} |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
147 |
\caption{The three-nested-loops program in the WHILE language. |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
148 |
Usually used for timing measurements.\label{loop}} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
149 |
\end{figure} |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
150 |
|
750 | 151 |
\begin{figure}[h] |
152 |
\lstinputlisting[language=while,xleftmargin=0mm]{../progs/while-tests/primes.while} |
|
153 |
\caption{Prime number program.\label{primes}} |
|
154 |
\end{figure} |
|
155 |
||
156 |
||
684 | 157 |
\begin{figure}[p] |
750 | 158 |
\lstinputlisting[language=while,xleftmargin=0mm]{../progs/while-tests/collatz2.while} |
159 |
\caption{Collatz series program.\label{collatz}} |
|
684 | 160 |
\end{figure} |
161 |
||
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
\end{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
%%% Local Variables: |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
%%% mode: latex |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
%%% TeX-master: t |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
%%% End: |