author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 28 Sep 2015 23:47:34 +0100 | |
changeset 333 | 8890852e18b7 |
parent 328 | bc03ff3d347c |
child 358 | b3129cff41e9 |
permissions | -rw-r--r-- |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
2 |
\usepackage{../style} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
214
diff
changeset
|
3 |
\usepackage{../langs} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
\begin{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
309
640e4a05cd9b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
304
diff
changeset
|
7 |
\section*{Coursework 3 (Strand 1)} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
\noindent |
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
10 |
This coursework is worth 5\% and is due on 26 November at 16:00. You |
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
11 |
are asked to implement a parser for the WHILE language and also |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
12 |
an interpreter. You should use the lexer from the previous |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
13 |
coursework for the parser. |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
|
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
16 |
|
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
17 |
\subsection*{Question 1 (marked with 1\%)} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
19 |
Design a grammar for the WHILE language and give the grammar |
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
20 |
rules. The main categories of non-terminal should be: |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
21 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
22 |
\begin{itemize} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
23 |
\item arithmetic expressions (with the operations from the |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
24 |
previous coursework, such as \pcode{+}, \pcode{*} and so on) |
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
25 |
\item boolean expressions (such as \pcode{<}, \code{!=} and |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
26 |
so on) |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
27 |
\item single statements (such as \pcode{skip}, assignments, \pcode{if}s, |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
28 |
\pcode{while}-loops and so on) |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
29 |
\item compound statements separated by semicolons |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
30 |
\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
|
31 |
\end{itemize} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
|
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
33 |
\subsection*{Question 2 (marked with 2\%)} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
34 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
35 |
You should implement a parser for the WHILE language using |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
36 |
parser combinators. Be careful that the parser takes as input |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
37 |
a stream, or list, of tokens generated by the tokenizer from |
304
1daaf6f6e45b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
301
diff
changeset
|
38 |
the previous coursework. For this you might filter out |
1daaf6f6e45b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
301
diff
changeset
|
39 |
whitespaces and comments. Your parser should be able to handle |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
40 |
the WHILE programs in Figures~\ref{fib} and \ref{loop}. |
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
41 |
In addition give the parse tree for the statement: |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
42 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
43 |
\begin{lstlisting}[language=While,numbers=none] |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
44 |
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
|
45 |
\end{lstlisting} |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
46 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
47 |
\noindent |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
48 |
A (possibly incomplete) datatype for parse trees in Scala would |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
49 |
look as in Figure~\ref{trees}. |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
50 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
51 |
\begin{figure} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
52 |
\begin{lstlisting}[language=Scala] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
53 |
abstract class Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
54 |
abstract class AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
55 |
abstract class BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
56 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
57 |
type Block = List[Stmt] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
58 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
59 |
case object Skip extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
case class Assign(s: String, a: AExp) extends Stmt |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
63 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
64 |
case class Var(s: String) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
65 |
case class Num(i: Int) extends AExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
66 |
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
|
67 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
68 |
case object True extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
69 |
case object False extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
70 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
71 |
\end{lstlisting} |
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
72 |
\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
|
73 |
\end{figure} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
74 |
|
299
6322922aa990
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
75 |
\subsection*{Question 3 (marked with 2\%)} |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
76 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
77 |
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
|
78 |
and parsed in Question 1 and 2. This interpreter should take |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
79 |
as input a parse tree. However be careful because programs |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
80 |
contain variables and variable assignments. This means |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
follows |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
86 |
|
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
87 |
\begin{lstlisting}[numbers=none] |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
88 |
eval_stmt(stmt, env) |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
89 |
\end{lstlisting} |
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
90 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
91 |
\noindent |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
acting as a store for variable values. |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
the variables \pcode{minus1} and \pcode{minus2} |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
99 |
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
|
100 |
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
|
101 |
according to straightforward rules: for example an |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
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:
204
diff
changeset
|
105 |
|
301
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
106 |
Give some time measurements for your interpreter |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
you scale this value if you are willing to wait, say |
e8c0269c8ff5
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
300
diff
changeset
|
111 |
1 Minute. |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
112 |
|
300
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
113 |
\begin{figure}[p] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
114 |
\begin{center} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
115 |
\mbox{\lstinputlisting[language=while]{../progs/fib.while}} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
116 |
\end{center} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
117 |
\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
|
118 |
\end{figure} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
119 |
|
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
120 |
\begin{figure}[p] |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
121 |
\begin{center} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
122 |
\mbox{\lstinputlisting[language=while]{../progs/loops.while}} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
123 |
\end{center} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
124 |
\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
|
125 |
Usually used for timing measurements.\label{loop}} |
08d99acd35e8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
299
diff
changeset
|
126 |
\end{figure} |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
127 |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
\end{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
%%% Local Variables: |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
%%% mode: latex |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
%%% TeX-master: t |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
%%% End: |