cws/cw03.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 28 Nov 2023 11:42:31 +0000
changeset 956 ae9782e62bdd
parent 950 fa97d2f60f11
child 968 d8d8911a3d6f
permissions -rw-r--r--
updated
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}
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
     5
\usepackage[normalem]{ulem}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
     9
\section*{Coursework 3}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
722
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    11
14914b57e207 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    12
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    13
\noindent This coursework is worth 10\% and is due on \cwTHREE{} at
877
43460c7b2010 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
    14
16:00. You are asked to implement a parser for the WHILE language and
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 950
diff changeset
    15
also an interpreter. The parser needs to use parser combinators.
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 950
diff changeset
    16
You can do the implementation in any programming
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    17
language you like, but you need to submit the source code with which
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    18
you answered the questions, otherwise a mark of 0\% will be
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 722
diff changeset
    19
awarded. You should use the lexer from the previous coursework for the
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
    20
parser.  Please submit your code to Github by the deadline.
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    22
\subsection*{Disclaimer\alert}
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    23
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    24
It should be understood that the work you submit represents your own
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    25
effort. You have not copied from anyone else. An exception is the
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    26
Scala code I showed during the lectures or uploaded to KEATS, which
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    27
you can both use. You can also use your own code from the CW~1 and
886
8a8d87394608 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 877
diff changeset
    28
CW~2. But do not
8a8d87394608 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 877
diff changeset
    29
be tempted to ask Github Copilot for help or do any other
8a8d87394608 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 877
diff changeset
    30
shenanigans like this!
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
299
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    32
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    33
\subsection*{Question 1}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    35
Design a grammar for the WHILE language and give the grammar
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 537
diff changeset
    36
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
    37
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    38
\begin{itemize}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    39
\item arithmetic expressions (with the operations from the
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    40
  previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*},
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    41
  \pcode{/} and \pcode{\%})
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    42
\item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>},
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 684
diff changeset
    43
  \code{>=}, \code{<=}, 
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    44
  \code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false})
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    45
\item single statements (that is \pcode{skip}, assignments, \pcode{if}s,
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    46
  \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
    47
\item compound statements separated by semicolons
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    48
\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
    49
\end{itemize}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
683
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    51
\noindent
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    52
Make sure the grammar is not left-recursive.
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    53
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    54
\subsection*{Question 2}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    55
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    56
You should implement a parser for the WHILE language using parser
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    57
combinators. Be careful that the parser takes as input a stream, or
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    58
list, of \emph{tokens} generated by the tokenizer from the previous
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    59
coursework. For this you might want to filter out whitespaces and
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    60
comments. Your parser should be able to handle the WHILE programs in
801
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 752
diff changeset
    61
Figures~\ref{fib} -- \ref{collatz}.  In addition give the
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    62
parse tree according to your grammar for the statement:
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    63
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    64
\begin{lstlisting}[language=While,numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    65
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
    66
\end{lstlisting}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    67
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
    68
\noindent
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    69
The output of the parser is an abstract syntax tree (AST).
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    70
A (possibly incomplete) datatype for ASTs of the WHILE language
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    71
is shown in Figure~\ref{trees}.
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    72
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    73
\begin{figure}[p]
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    74
\begin{lstlisting}[language=Scala]
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    75
abstract class Stmt
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    76
abstract class AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    77
abstract class BExp 
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
type Block = List[Stmt]
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    80
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    81
case object Skip extends Stmt
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    82
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
    83
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
    84
case class Assign(s: String, a: AExp) extends Stmt
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    85
case class Read(s: String) extends Stmt
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    86
case class WriteVar(s: String) extends Stmt  
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    87
case class WriteStr(s: String) extends Stmt 
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    88
                      // for printing variables and strings
300
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 class Var(s: String) extends AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    91
case class Num(i: Int) extends AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    92
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
    93
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    94
case object True extends BExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    95
case object False extends BExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    96
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    97
case class Lop(o: String, b1: BExp, b2: BExp) extends BExp
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    98
                     // logical operations: and, or
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    99
\end{lstlisting}
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   100
\caption{The datatype for abstract syntax trees in Scala.\label{trees}}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   101
\end{figure}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   102
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
   103
\subsection*{Question 3}
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   104
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   105
Implement an interpreter for the WHILE language you designed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   106
and parsed in Question 1 and 2. This interpreter should take
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   107
as input an AST. However be careful because, programs
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   108
contain variables and variable assignments. This means
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   109
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
   110
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
   111
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
   112
evaluation function (interpreter) needs to look roughly as 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   113
follows 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   114
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   115
\begin{lstlisting}[numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   116
eval_stmt(stmt, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   117
\end{lstlisting}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   118
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   119
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   120
where \pcode{stmt} corresponds to the parse tree
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   121
of the program and \pcode{env} is an environment
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   122
acting as a store for variable values. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   123
Consider the Fibonacci program in Figure~\ref{fib}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   124
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
   125
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
   126
the variables \pcode{minus1} and \pcode{minus2}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   127
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
   128
lines 7 and 8. The program should  be interpreted
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   129
according to straightforward rules: for example an
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   130
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
   131
evaluates to \pcode{true}, otherwise the else-branch.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   132
Loops should be run as long as the boolean is \pcode{true}.
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   133
Note also that some programs contain a read-statement,
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   134
which means you need to read and integer from the commandline
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   135
and store the value in the corresponding variable.
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   136
Programs you should be able to run are shown in
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   137
Figures \ref{fib} -- \ref{collatz}. The output
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   138
of the \texttt{primes.while} should look as follows:
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   139
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   140
\begin{figure}[h]
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   141
{\small
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   142
\begin{lstlisting}[numbers=none]
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   143
2
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   144
3
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   145
5
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   146
7
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   147
11
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   148
13
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   149
17
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   150
19
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   151
23
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   152
29
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   153
31
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   154
37
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   155
41
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   156
43
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   157
47
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   158
53
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   159
59
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   160
61
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   161
67
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   162
71
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   163
73
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   164
79
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   165
83
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   166
89
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   167
97
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   168
Map(end -> 100, n -> 100, f -> 4, tmp -> 1)
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   169
\end{lstlisting}}
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   170
\caption{Sample output for the file \texttt{primes.while}.\label{fib}}
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   171
\end{figure}
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   172
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   173
\noindent
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   174
Give some time measurements for your interpreter
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   175
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
   176
how long does your interpreter take when \pcode{start}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   177
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
   178
you scale this value if you are willing to wait, say
473
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 419
diff changeset
   179
1 Minute?
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   180
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   181
\clearpage
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   182
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   183
\begin{figure}[h]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 801
diff changeset
   184
\lstinputlisting[language=while,xleftmargin=20mm]{../cwtests/cw03/fib.while}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   185
\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
   186
\end{figure}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   187
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   188
\begin{figure}[h]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 801
diff changeset
   189
\lstinputlisting[language=while,xleftmargin=20mm]{../cwtests/cw03/loops.while}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   190
\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
   191
Usually used for timing measurements.\label{loop}}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   192
\end{figure}
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   193
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   194
\begin{figure}[h]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 801
diff changeset
   195
\lstinputlisting[language=while,xleftmargin=0mm]{../cwtests/cw03/primes.while}
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   196
\caption{Prime number program.\label{primes}}
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   197
\end{figure}
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   198
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   199
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   200
\begin{figure}[p]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 801
diff changeset
   201
\lstinputlisting[language=while,xleftmargin=0mm]{../cwtests/cw03/collatz2.while}
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   202
\caption{Collatz series program.\label{collatz}}
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   203
\end{figure}
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   204
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   205
\clearpage
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   206
\newpage
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   207
\section*{Answers}
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   208
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   209
\mbox{}  
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   210
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   211
\noindent
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   212
\textbf{Name:}\uline{\hfill}\bigskip
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   213
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   214
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   215
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   216
\noindent
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   217
\textbf{Question 1 (Grammar):}\\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   218
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   219
\mbox{}\\[9cm]
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   220
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   221
\newpage
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   222
\noindent
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   223
\textbf{Question 2 (Parse Tree):}\\
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   224
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   225
\mbox{}\\[8cm]
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   226
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   227
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   228
\noindent
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   229
\textbf{Question 3 (Timings):}\\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   230
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   231
\begin{center}
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   232
  \def\arraystretch{1.5}
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   233
  \begin{tabular}{l|l}
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   234
    n & secs\\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   235
    \hline
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   236
    100 & \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   237
    500 & \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   238
    700 & \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   239
    1000 & \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   240
    \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   241
    \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   242
    \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   243
    \\
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   244
   \end{tabular} 
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   245
\end{center}  
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   246
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
%%% End: