cws/cw03.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:58:18 +0000
changeset 975 ae5c03560d4d
parent 968 d8d8911a3d6f
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}
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
968
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    15
also an interpreter. The parser needs to use parser combinators.  You
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    16
can do the implementation in any programming language you like, but
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    17
you need to submit the source code with which you answered the
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    18
questions, otherwise a mark of 0\% will be awarded. If you use Scala
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    19
in your code, a good place to start is the file \texttt{comb1.sc} and
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    20
\texttt{comb2.sc} uploaded to KEATS. Feel free to use the ``hack''
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    21
explained during the lectures. This might make your grammar
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    22
simpler. However, make sure you understand the code involved in the
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    23
``hack'' because if you just do ``mix-and-match'' you will receive
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    24
strange errors.  The main function that will be tested is called
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    25
\texttt{eval} and \texttt{Stmts.parse\_all}. The latter expects a list
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    26
of tokens as input and generates an AST. The former expects an AST and
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    27
``runs'' the program. The marks will be distributed such that 6 marks
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    28
are given for the correct grammar (and parsers); 4 marks for the correct
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    29
\texttt{eval} function.  You should use the lexer from CW2 for the
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    30
parser - you potentially need to make additions for CW2.  
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    32
\subsection*{Disclaimer\alert}
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    33
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    34
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
    35
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
    36
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
    37
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
    38
CW~2. But do not
8a8d87394608 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 877
diff changeset
    39
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
    40
shenanigans like this!
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
968
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    42
\subsection*{Syntax Error in Template File cw03.sc\alert}
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    43
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    44
Apologies, there is a small syntax error in the template file where a variable
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    45
needs to be called \texttt{tks} instead of \texttt{tk}. The code
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    46
in question is at the end of \texttt{cw03.sc} and should be like
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    47
this (see lines 5, 6 and 8):
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    48
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    49
\begin{lstlisting}[language=Scala,numbers=left]
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    50
@main
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    51
def test(file: String) = {
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    52
  val contents = os.read(os.pwd / "examples" / file)
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    53
  println(s"Lex $file: ")
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    54
  val tks = tokenise(contents)
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    55
  println(tks.mkString(","))
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    56
  println(s"Parse $file: ")
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    57
  val ast = Stmts.parse_all(tks).head
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    58
  println(ast)
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    59
  println(s"Eval $file: ")
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    60
  println(eval(ast))
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    61
}
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    62
\end{lstlisting}  
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    63
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    64
299
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    65
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    66
\subsection*{Question 1}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    68
Design a grammar for the WHILE language and give the grammar
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 537
diff changeset
    69
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
    70
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    71
\begin{itemize}
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    72
\item arithmetic expressions (with the operations from the
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    73
  previous coursework, that is \pcode{+}, \pcode{-}, \pcode{*},
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    74
  \pcode{/} and \pcode{\%})
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    75
\item boolean expressions (with the operations \pcode{==}, \pcode{<}, \pcode{>},
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 684
diff changeset
    76
  \code{>=}, \code{<=}, 
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    77
  \code{!=}, \pcode{&&}, \pcode{||}, \pcode{true} and \pcode{false})
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    78
\item single statements (that is \pcode{skip}, assignments, \pcode{if}s,
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
    79
  \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
    80
\item compound statements separated by semicolons
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    81
\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
    82
\end{itemize}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
683
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    84
\noindent
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    85
Make sure the grammar is not left-recursive.
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 682
diff changeset
    86
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    87
\subsection*{Question 2}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    88
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    89
You should implement a parser for the WHILE language using parser
968
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    90
combinators. Be careful that the parser takes as input a list of
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    91
\emph{tokens} generated by the tokenizer from the previous
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    92
coursework. For this you might want to filter out whitespaces and
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    93
comments. Your parser should be able to handle the WHILE programs in
968
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    94
the \texttt{examples} directory.  The output of the parser is an
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    95
abstract syntax tree (AST).  A (possibly incomplete) datatype for ASTs
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
    96
of the WHILE language is shown in Figure~\ref{trees}.
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    97
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    98
\begin{figure}[p]
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
    99
\begin{lstlisting}[language=Scala]
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   100
abstract class Stmt
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   101
abstract class AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   102
abstract class BExp 
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   103
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   104
type Block = List[Stmt]
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   105
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   106
case object Skip extends Stmt
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   107
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
   108
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
   109
case class Assign(s: String, a: AExp) extends Stmt
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   110
case class Read(s: String) extends Stmt
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   111
case class WriteVar(s: String) extends Stmt  
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   112
case class WriteStr(s: String) extends Stmt 
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
   113
                      // for printing variables and strings
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   114
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   115
case class Var(s: String) extends AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   116
case class Num(i: Int) extends AExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   117
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
   118
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   119
case object True extends BExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   120
case object False extends BExp
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   121
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
682
553b4d4e3719 updated
Christian Urban <urbanc@in.tum.de>
parents: 630
diff changeset
   122
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
   123
                     // logical operations: and, or
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   124
\end{lstlisting}
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   125
\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
   126
\end{figure}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   127
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
   128
\subsection*{Question 3}
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   129
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   130
Implement an interpreter for the WHILE language you designed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   131
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
   132
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
   133
contain variables and variable assignments. This means
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   134
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
   135
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
   136
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
   137
evaluation function (interpreter) needs to look roughly as 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   138
follows 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   139
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   140
\begin{lstlisting}[numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   141
eval_stmt(stmt, env)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   142
\end{lstlisting}
300
08d99acd35e8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 299
diff changeset
   143
301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   144
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   145
where \pcode{stmt} corresponds to the parse tree
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   146
of the program and \pcode{env} is an environment
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   147
acting as a store for variable values. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   148
Consider the Fibonacci program in Figure~\ref{fib}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   149
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
   150
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
   151
the variables \pcode{minus1} and \pcode{minus2}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   152
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
   153
lines 7 and 8. The program should  be interpreted
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   154
according to straightforward rules: for example an
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   155
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
   156
evaluates to \pcode{true}, otherwise the else-branch.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 300
diff changeset
   157
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
   158
Note also that some programs contain a read-statement,
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   159
which means you need to read and integer from the commandline
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   160
and store the value in the corresponding variable.
968
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   161
Programs you should be able to run are given in  the
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 956
diff changeset
   162
\texttt{examples} directory. The output
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   163
of the \texttt{primes.while} should look as follows:
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   164
950
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   165
\begin{figure}[h]
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   166
{\small
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   167
\begin{lstlisting}[numbers=none]
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   168
2
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   169
3
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   170
5
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   171
7
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   172
11
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   173
13
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   174
17
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   175
19
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   176
23
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   177
29
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   178
31
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   179
37
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   180
41
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   181
43
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   182
47
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   183
53
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   184
59
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   185
61
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   186
67
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   187
71
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   188
73
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   189
79
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   190
83
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   191
89
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   192
97
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   193
Map(end -> 100, n -> 100, f -> 4, tmp -> 1)
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   194
\end{lstlisting}}
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   195
\caption{Sample output for the file \texttt{primes.while}.\label{fib}}
fa97d2f60f11 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   196
\end{figure}
684
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   197
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   198
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
%%% End: