coursework/cw03.tex
author Christian Urban <urbanc@in.tum.de>
Wed, 06 Nov 2019 23:27:19 +0000
changeset 684 1ee523c4f098
parent 683 c6c79d21f8a8
child 686 05cfce0fdef7
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
630
9b1c15c3eb6f updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
     1
% !TEX program = xelatex
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\documentclass{article}
299
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\begin{document}
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
630
9b1c15c3eb6f updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
    10
\noindent This coursework is worth 5\% and is due on 22 
567
4573d36d0b2f updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    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: 333
diff 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: 333
diff 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: 333
diff 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: 333
diff 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: 333
diff changeset
    16
use the lexer from the previous coursework for the parser. 
200
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: 333
diff changeset
    18
\subsection*{Disclaimer}
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    19
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff 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: 333
diff 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: 333
diff 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: 333
diff 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: 333
diff changeset
    24
CW~1 and CW~2.
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
299
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    26
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    27
\subsection*{Question 1}
200
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: 358
diff changeset
    29
Design a grammar for the WHILE language and give the grammar
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 537
diff changeset
    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: 299
diff changeset
    31
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    32
\begin{itemize}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    33
\item arithmetic expressions (with the operations from the
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    34
  previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*},
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    35
  \pcode{/} and \pcode{\%})
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    36
\item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>},
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    37
  \code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false})
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    38
\item single statements (that is \pcode{skip}, assignments, \pcode{if}s,
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    39
  \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
    40
\item compound statements separated by semicolons
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    41
\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
    42
\end{itemize}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
683
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    44
\noindent
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    45
Make sure the grammar is not left-recursive.
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    46
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    47
\subsection*{Question 2}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    48
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    49
You should implement a parser for the WHILE language using parser
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    50
combinators. Be careful that the parser takes as input a stream, or
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    51
list, of tokens generated by the tokenizer from the previous
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    52
coursework. For this you might want to filter out whitespaces and
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    53
comments. Your parser should be able to handle the WHILE programs in
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    54
Figures~\ref{fib}, \ref{loop} and \ref{primes} (if your lexer cannot
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    55
deal with comments you can delete them from the prime number program).
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    56
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
    57
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    58
\begin{lstlisting}[language=While,numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    59
if (a < b) then skip else a := a * b + 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    60
\end{lstlisting}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    61
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    62
\noindent
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    63
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
    64
look as in Figure~\ref{trees}.
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    65
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    66
\begin{figure}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    67
\begin{lstlisting}[language=Scala]
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    68
abstract class Stmt
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    69
abstract class AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    70
abstract class BExp 
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    71
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    72
type Block = List[Stmt]
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    73
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    74
case object Skip extends Stmt
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    75
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
    76
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
    77
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
    78
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    79
case class Var(s: String) extends AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    80
case class Num(i: Int) extends AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    81
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
    82
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    83
case object True extends BExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    84
case object False extends BExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    85
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    86
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: 299
diff changeset
    87
\end{lstlisting}
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    88
\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
    89
\end{figure}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    90
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    91
\subsection*{Question 3}
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    92
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    93
Implement an interpreter for the WHILE language you designed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    94
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
    95
as input a parse tree. However be careful because, programs
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    96
contain variables and variable assignments. This means
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    97
you need to maintain a kind of memory, or environment,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    98
where you can look up a value of a variable and also
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    99
store a new value if it is assigned. Therefore an
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   100
evaluation function (interpreter) needs to look roughly as 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   101
follows 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   102
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   103
\begin{lstlisting}[numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   104
eval_stmt(stmt, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   105
\end{lstlisting}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   106
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   107
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   108
where \pcode{stmt} corresponds to the parse tree
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   109
of the program and \pcode{env} is an environment
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   110
acting as a store for variable values. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   111
Consider the Fibonacci program in Figure~\ref{fib}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   112
At the beginning of the program this store will be 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   113
empty, but needs to be extended in line 3 and 4 where 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   114
the variables \pcode{minus1} and \pcode{minus2}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   115
are assigned values. These values need to be reassigned in
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   116
lines 7 and 8. The program should  be interpreted
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   117
according to straightforward rules: for example an
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   118
if-statement will ``run'' the if-branch if the boolean
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   119
evaluates to \pcode{true}, otherwise the else-branch.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   120
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
   121
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   122
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   123
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   124
Give some time measurements for your interpreter
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   125
and the loop program in Figure~\ref{loop}. For example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   126
how long does your interpreter take when \pcode{start}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   127
is initialised with 100, 500 and so on. How far can
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   128
you scale this value if you are willing to wait, say
473
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 419
diff changeset
   129
1 Minute?
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   130
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   131
\begin{figure}[p]
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   132
\lstinputlisting[language=while,xleftmargin=20mm]{../progs/fib.while}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   133
\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
   134
\end{figure}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   135
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   136
\begin{figure}[p]
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   137
\lstinputlisting[language=while,xleftmargin=20mm]{../progs/loops.while}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   138
\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
   139
Usually used for timing measurements.\label{loop}}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   140
\end{figure}
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   141
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   142
\begin{figure}[p]
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   143
\lstinputlisting[language=while,xleftmargin=0mm]{../progs/primes.while}
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   144
\caption{Prime number program.\label{primes}}
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   145
\end{figure}
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   146
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
%%% End: