handouts/ho09.tex
author Christian Urban <urbanc@in.tum.de>
Wed, 06 Dec 2017 00:06:37 +0000
changeset 543 16adebf18ef9
parent 539 ed8f014217be
child 677 decfd8cf8180
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
539
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\usepackage{../style}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
\usepackage{../langs}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
\usepackage{../graphics}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\usepackage{../grammar}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\usepackage{multicol}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
\begin{document}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\fnote{\copyright{} Christian Urban, King's College London, 2014}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
%% why are shuttle flights so good with software
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
%%http://www.fastcompany.com/28121/they-write-right-stuff
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
\section*{Handout 9 (Static Analysis)}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
If we want to improve the safety and security of our programs,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
we need a more principled approach to programming. Testing is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
good, but as Edsger Dijkstra famously wrote: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
\begin{quote}\it 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
``Program testing can be a very effective way to show the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
\underline{\smash{presence}} of bugs, but it is hopelessly
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
inadequate for showing their \underline{\smash{absence}}.''
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
\end{quote}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
\noindent While such a more principled approach has been the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
subject of intense study for a long, long time, only in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
past few years some impressive results have been achieved. One
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
is the complete formalisation and (mathematical) verification
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
of a microkernel operating system called seL4.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
\url{http://sel4.systems}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
\noindent In 2011 this work was included in the MIT Technology
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
Review in the annual list of the world’s ten most important
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
emerging
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
technologies.\footnote{\url{http://www2.technologyreview.com/tr10/?year=2011}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
While this work is impressive, its technical details are too
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
enormous for an explanation here. Therefore let us look at
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
something much simpler, namely finding out properties about
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
programs using \emph{static analysis}.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
Static analysis is a technique that checks properties of a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
program without actually running the program. This should
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
raise alarm bells with you---because almost all interesting
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
properties about programs  are equivalent to the halting
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
problem, which we know is undecidable. For example estimating
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
the memory consumption of programs is in general undecidable,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
just like the halting problem. Static analysis circumvents
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
this undecidability-problem by essentially allowing answers
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
\emph{yes} and \emph{no}, but also \emph{don't know}. With
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
this ``trick'' even the halting problem becomes
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
decidable\ldots{}for example we could always say \emph{don't
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
know}. Of course this would be silly. The point is that we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
should be striving for a method that answers as often as
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
possible either \emph{yes} or \emph{no}---just in cases when
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
it is too difficult we fall back on the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
\emph{don't-know}-answer. This might sound all like abstract
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
nonsense. Therefore let us look at a concrete example.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
\subsubsection*{A Simple, Idealised Programming Language}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
Our starting point is a small, idealised programming language.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
It is idealised because we cut several corners in comparison
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
with real programming languages. The language we will study
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
contains, amongst other things, variables holding integers.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
Using static analysis, we want to find out what the sign of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
these integers (positive or negative) will be when the program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
runs. This sign-analysis seems like a very simple problem. But
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
even such simple problems, if approached naively, are in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
general undecidable, just like Turing's halting problem. I let
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
you think why?
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
Is sign-analysis of variables an interesting problem? Well,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
yes---if a compiler can find out that for example a variable
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
will never be negative and this variable is used as an index
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
for an array, then the compiler does not need to generate code
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
for an underflow-check. Remember some languages are immune to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
buffer-overflow attacks, but they need to add underflow and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
overflow checks everywhere. According to John Regher, an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
expert in the field of compilers, overflow checks can cause
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
5-10\% slowdown, in some languages even 100\% for tight
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
loops.\footnote{\url{http://blog.regehr.org/archives/1154}} If
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
the compiler can omit the underflow check, for example, then
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
this can potentially drastically speed up the generated code. 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
What do programs in our simple programming language look like?
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
The following grammar gives a first specification:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
\begin{multicols}{2}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
\begin{plstx}[rhs style=,one per line,left margin=9mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
: \meta{Stmt} ::= \meta{label} \texttt{:}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
                | \meta{var} \texttt{:=} \meta{Exp}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
                | \texttt{jmp?} \meta{Exp} \meta{label}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
                | \texttt{goto} \meta{label}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
: \meta{Prog} ::= \meta{Stmt} \ldots{} \meta{Stmt}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
\end{plstx}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
\columnbreak
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
\begin{plstx}[rhs style=,one per line]
: \meta{Exp} ::= \meta{Exp} \texttt{+} \meta{Exp}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
               | \meta{Exp} \texttt{*} \meta{Exp}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
               | \meta{Exp} \texttt{=} \meta{Exp} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
               | \meta{num}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
               | \meta{var}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
\end{plstx}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
\end{multicols}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
\noindent I assume you are familiar with such
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
grammars.\footnote{\url{http://en.wikipedia.org/wiki/Backus–Naur_Form}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
There are three main syntactic categories: \emph{statments}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
and \emph{expressions} as well as \emph{programs}, which are
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
sequences of statements. Statements are either labels,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
variable assignments, conditional jumps (\pcode{jmp?}) and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
unconditional jumps (\pcode{goto}). Labels are just strings,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
which can be used as the target of a jump. We assume that in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
every program the labels are unique---if there is a clash,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
then we do not know where to jump to. The conditional jumps
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
and variable assignments involve (arithmetic) expressions.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
Expressions are either numbers, variables or compound
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
expressions built up from \pcode{+}, \pcode{*} and \emph{=}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
(for simplicity reasons we do not consider any other
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
operations). We assume we have negative and positive numbers,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
\pcode{2}\ldots{} An example program that calculates the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
factorial of 5 is in our programming language as follows:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
\begin{lstlisting}[language={},xleftmargin=10mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
      a := 1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
      n := 5 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
top:  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
      jmp? n = 0 done 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
      a := a * n 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
      n := n + -1 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
      goto top 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
\end{lstlisting}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
\noindent As can be seen each line of the program contains a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
statement. In the first two lines we assign values to the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
variables \pcode{a} and \pcode{n}. In line 4 we test whether
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
\pcode{n} is zero, in which case we jump to the end of the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
program marked with the label \pcode{done}. If \pcode{n} is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
not zero, we multiply the content of \pcode{a} by \pcode{n},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
decrease \pcode{n} by one and jump back to the beginning of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
the loop, marked with the label \pcode{top}. Another program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
in our language is shown in Figure~\ref{fib}. I let you think
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
what it calculates.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
\begin{figure}[t]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
\begin{lstlisting}[numbers=none,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
                   language={},xleftmargin=10mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
      n := 6
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
      m1 := 0
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
      m2 := 1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
loop: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
      jmp? n = 0 done
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
      tmp := m2
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
      m2 := m1 + m2
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
      m1 := tmp
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
      n := n + -1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
      goto top
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
\end{lstlisting}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
\caption{A mystery program in our idealised programming language.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
Try to find out what it calculates! \label{fib}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
\end{figure}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
Even if our language is rather small, it is still Turing
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
complete---meaning quite powerful. However, discussing this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
fact in more detail would lead us too far astray. Clearly, our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
programming is rather low-level and not very comfortable for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
writing programs. It is inspired by real machine code, which
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
is the code that is executed by a CPU. So a more interesting
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
question is what is missing in comparison with real machine
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
code? Well, not much\ldots{}in principle. Real machine code,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
of course, contains many more arithmetic instructions (not
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
just addition and multiplication) and many more conditional
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
jumps. We could add these to our language if we wanted, but
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
complexity is really beside the point here. Furthermore, real
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
machine code has many instructions for manipulating memory. We
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
do not have this at all. This is actually a more serious
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
simplification because we assume numbers to be arbitrary small
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
or large, which is not the case with real machine code. In
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
real machine code, basic number formats have a range and might
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
over-flow or under-flow this range. Also the number of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
variables in our programs is potentially unlimited, while
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
memory in an actual computer, of course, is always limited. To
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
sum up, our language might look ridiculously simple, but it is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
not too far removed from practically relevant issues.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
\subsubsection*{An Interpreter}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
Designing a language is like playing god: you can say what
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
names for variables you allow; what programs should look like;
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
most importantly you can decide what each part of the program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
should mean and do. While our language is quite simple and the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
meaning of statements, for example, is rather straightforward,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
there are still places where we need to make real choices. For
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
example consider the conditional jumps, say the one in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
factorial program: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
\code{jmp? n = 0 done}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
\noindent How should they work? We could introduce Booleans
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
(\pcode{true} and \pcode{false}) and then jump only when the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
condition is \pcode{true}. However, since we have numbers in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
our language anyway, why not just encoding \pcode{true} as
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
one, and \pcode{false} as zero? In this way we can dispense
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
with the additional concept of Booleans.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
I hope the above discussion makes it already clear we need to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
be a bit more careful with our programs. Below we shall
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
describe an interpreter for our programming language, which
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
specifies exactly how programs are supposed to be
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
run\ldots{}at least we will specify this for all \emph{good}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
programs. By good programs I mean where all variables are
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
initialised, for example. Our interpreter will just crash if
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
it cannot find out the value for a variable when it is not
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
initialised. Also, we will assume that labels in good programs
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
are unique, otherwise our programs will calculate ``garbage''.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
First we will pre-process our programs. This will simplify the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
definition of our interpreter later on. By pre-processing our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
programs we will transform programs into \emph{snippets}. A
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
snippet is a label and all the code that comes after the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
label. This essentially means a snippet is a \emph{map} from
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
labels to code.\footnote{Be sure you know what maps are. In a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
programming context they are often represented as association
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
list where some data is associated with a key.} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
Given that programs are sequences (or lists) of statements, we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
can easily calculate the snippets by just traversing this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
sequence and recursively generating the map. Suppose a program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
is of the general form
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
$stmt_1\;stmt_2\; \ldots\; stmt_n$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
\noindent The idea is to go through this sequence of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
statements one by one and check whether they are a label. If
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
yes, we add the label and the remaining statements to our map.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
If no, we just continue with the next statement. To come up
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
with a recursive definition for generating snippets, let us
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
write $[]$ for the program that does not contain any
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
statement. Consider the following definition:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
$\textit{snippets}([])$ & $\dn$ & $\varnothing$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
$\textit{snippets}(stmt\;\; rest)$ & $\dn$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
$\begin{cases}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
   \textit{snippets}(rest)[label := rest] & \text{if}\;stmt = \textit{label:}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
   \textit{snippets}(rest)                & \text{otherwise}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
\end{cases}$   
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
\noindent In the first clause we just return the empty map for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
the program that does not contain any statement. In the second
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
clause, we have to distinguish the case where the first
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
statement is a label or not. As said before, if not, then we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
just ``throw away'' the label and recursively calculate the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
snippets for the rest of the program (the otherwise clause).
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
If yes, then we do the same, but also update the map so that
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
$label$ now points to the rest of the statements (the if
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
clause). This looks all realtively straightforward, but there
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
is one small problem we need to overcome: our two programs
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
shown so far have no label as \emph{entry point}---that is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
where the execution is supposed to start. We usually assume
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
that the first statement will be run first. To make this the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
default, it is convenient if we add to all our programs a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
default label, say \pcode{""} (the empty string). With this we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
can define our pre-processing of programs as follows
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
$\textit{preproc}(prog) \dn \textit{snippets}(\pcode{"":}\;\; prog)$ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
\end{center} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
\noindent Let us see how this pans out in practice. If we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
pre-process the factorial program shown earlier, we obtain the 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
following map:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
\small
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
\lstset{numbers=none,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
        language={},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
        xleftmargin=0mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
        aboveskip=0.5mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
        belowskip=0.5mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
        frame=single,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
        framerule=0mm,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
        framesep=0mm}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
\begin{tikzpicture}[node distance=0mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
\node (A1) [draw]{\pcode{""}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
\node (B1) [right=of A1] {$\mapsto$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
\node (C1) [right=of B1,anchor=north west] {
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
\begin{minipage}{3.5cm}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
\begin{lstlisting}[language={},xleftmargin=0mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
  a := 1
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
  n := 5 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
top:  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
  jmp? n = 0 done 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  a := a * n 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  n := n + -1 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
  goto top 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
\end{lstlisting}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
\end{minipage}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
\node (A2) [right=of C1.north east,draw] {\pcode{top}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
\node (B2) [right=of A2] {$\mapsto$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
\node (C2) [right=of B2, anchor=north west] {
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
\begin{minipage}{3.5cm}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
\begin{lstlisting}[language={},xleftmargin=0mm]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
  jmp? n = 0 done 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
  a := a * n 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
  n := n + -1 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
  goto top 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
done:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
\end{lstlisting} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
\end{minipage}}; 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
\node (A3) [right=of C2.north east,draw] {\pcode{done}};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
\node (B3) [right=of A3] {$\mapsto$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
\node (C3) [right=of B3] {$[]$};
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
\end{tikzpicture}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
\noindent I highlighted the \emph{keys} in this map. Since
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
there are three labels in the factorial program (remember we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
added \pcode{""}), there are three keys. When running the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
factorial program and encountering a jump, then we only have
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
to consult this snippets-map in order to find out what the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
next statements should be.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
We should now be in the position to define how a program
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
should be run. In the context of interpreters, this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
``running'' of programs is often called \emph{evaluation}. Let
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
us start with the definition of how expressions are evaluated.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
A first attempt might be the following recursive function:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
$\textit{eval\_exp}(\texttt{n})$ & $\dn$ & $n$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
  \qquad\text{if}\; \texttt{n}\; \text{is a number like} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  \ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
\pcode{2}\ldots{}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
                    \texttt{e}_\texttt{2})$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) + 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
           \textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
                    \texttt{e}_\texttt{2})$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}) * 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
           \textit{eval\_exp}(\texttt{e}_\texttt{2})$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
                    \texttt{e}_\texttt{2})$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  $\dn$ & $\begin{cases}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}) =
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
                 \textit{eval\_exp}(\texttt{e}_\texttt{2})\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
  0 & \text{otherwise}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
  \end{cases}$          
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
\noindent While this should look all rather intuitive`, still
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
be very careful. There is a subtlety which can be easily
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
overlooked: The function \textit{eval\_exp} takes an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
expression of our programming language as input and returns a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
number as output. Therefore whenever we encounter a number in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
our program, we just return this number---this is defined in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
the first clause above. Whenever we encounter an addition,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
well then we first evaluate the left-hand side
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
$\texttt{e}_\texttt{1}$ of the addition (this will give a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
number), then evaluate the right-hand side
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
$\texttt{e}_\texttt{2}$ (this gives another number), and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
finally add both numbers together. Here is the subtlety: on
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
the left-hand side of the $\dn$ we have a \texttt{+} (in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
teletype font) which is the symbol for addition in our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
programming language. On the right-hand side we have $+$ which
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
stands for the arithmetic operation from ``mathematics'' of
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
adding two numbers. These are rather different concepts---one
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
is a symbol (which we made up), and the other a mathematical
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
operation. When we will have a look at an actual
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
implementation of our interpreter, the mathematical operation
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
will be the function for addition from the programming
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
language in which we \underline{\smash{implement}} our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
interpreter. While the \texttt{+} is just a symbol that is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   396
used in our programming language. Clearly we have to use a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   397
symbol that is a good mnemonic for addition, otherwise we will
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
confuse the programmers working with our language. Therefore
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   399
we use $\texttt{+}$. A similar choice is made for times in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
third clause and equality in the fourth clause. 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
Remember I wrote at the beginning of this section about being
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
god when designing a programming language. You can see this
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
here: we need to give meaning to symbols. At the moment
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
however, we are a poor fallible god. Look again at the grammar
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
of our programming language and our definition. Clearly, an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
expression can contain variables. So far we have ignored them.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
What should our interpreter do with variables? They might
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
change during the evaluation of a program. For example the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
variable \pcode{n} in the factorial program counts down from 5
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
down to 0. How can we improve our definition above to give also
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
an answer whenever our interpreter encounters a variable in an
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
expression? The solution is to add an \emph{environment},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
written $env$, as an additional input argument to our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
\textit{eval\_exp} function.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
$\textit{eval\_exp}(\texttt{n}, env)$ & $\dn$ & $n$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  \qquad\text{if}\; \texttt{n}\; \text{is a number like} 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
  \ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1},
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
\pcode{2}\ldots{}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{+}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
                    \texttt{e}_\texttt{2}, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) + 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
           \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{*}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
                    \texttt{e}_\texttt{2}, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
  $\dn$ & $\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) * 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
           \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
$\textit{eval\_exp}(\texttt{e}_\texttt{1} \,\texttt{=}\, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
                    \texttt{e}_\texttt{2}, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
  $\dn$ & $\begin{cases}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  1 & \text{if}\;\textit{eval\_exp}(\texttt{e}_\texttt{1}, env) =
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
                 \textit{eval\_exp}(\texttt{e}_\texttt{2}, env)\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
  0 & \text{otherwise}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   437
  \end{cases}$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   438
$\textit{eval\_exp}(\texttt{x}, env)$ & $\dn$ & $env(x)$      
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   439
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
\noindent This environment $env$ also acts like a map: it
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
associates variables with their current values. For example
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
after evaluating the first two lines in our factorial
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
program, such an environment might look as follows
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
\begin{tabular}{ll}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
$\fbox{\texttt{a}} \mapsto \texttt{1}$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
$\fbox{\texttt{n}} \mapsto \texttt{5}$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   452
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   454
\noindent Again I highlighted the keys. In the clause for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   455
variables, we can therefore consult this environment and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   456
return whatever value is currently stored for this variable.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   457
This is written $env(x)$---meaning we query this map with $x$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   458
we obtain the corresponding number. You might ask what happens
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   459
if an environment does not contain any value for, say, the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   460
variable $x$? Well, then our interpreter just ``crashes'', or
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   461
more precisely will raise an exception. In this case we have a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   462
``bad'' program that tried to use a variable before it was
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   463
initialised. The programmer should not have done this. In a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   464
real programming language, we would of course try a bit harder
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   465
and for example give an error at compile time, or design our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   466
language in such a way that this can never happen. With the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   467
second version of \textit{eval\_exp} we completed our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
definition for evaluating expressions.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
Next comes the evaluation function for statements. We define
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
this function in such a way that we recursively evaluate a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
whole sequence of statements. Assume a program $p$ (you want
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
to evaluate) and its pre-processed snippets $sn$. Then we can
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
define:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
$\textit{eval\_stmts}([], env)$ & $\dn$ & $env$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
$\textit{eval\_stmts}(\texttt{label:}\;rest, env)$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
  $\dn$ & $\textit{eval\_stmts}(rest, env)$ \\ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
$\textit{eval\_stmts}(\texttt{x\,:=\,e}\;rest, env)$ & 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
  $\dn$ & $\textit{eval\_stmts}(rest, 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
           env[x := \textit{eval\_exp}(\texttt{e}, env)])$\\  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
$\textit{eval\_stmts}(\texttt{goto\,lbl}\;rest, env)$ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
 & $\dn$ & $\textit{eval\_stmts}(sn(\texttt{lbl}), env)$\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
$\textit{eval\_stmts}(\texttt{jmp?\,e\,lbl}\;rest, env)$ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
 & $\dn$ & $\begin{cases}\begin{array}{@{}l@{\hspace{-12mm}}r@{}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
 \textit{eval\_stmts}(sn(\texttt{lbl}), env)\\ 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
 & \text{if}\;\textit{eval\_exp}(\texttt{e}, env) = 1\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
 \textit{eval\_stmts}(rest, env) & \text{otherwise}\\
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
 \end{array}\end{cases}$  
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
\noindent The first clause is for the empty program, or when
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
we arrived at the end of the program. In this case we just
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
return the environment. The second clause is for when the next
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
statement is a label. That means the program is of the form
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
$\texttt{label:}\;rest$ where the label is some string and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
$rest$ stands for all following statements. This case is easy,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
because our evaluation function just discards the label and
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
evaluates the rest of the statements (we already extracted all
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
important information about labels when we pre-processed our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
programs and generated the snippets). The third clause is for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
variable assignments. Again we just evaluate the rest for the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
statements, but with a modified environment---since the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
variable assignment is supposed to introduce a new variable or
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
change the current value of a variable. For this modification
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
of the environment we first evaluate the expression
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
$\texttt{e}$ using our evaluation function for expressions.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
This gives us a number. Then we assign this number to the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
variable $x$ in the environment. This modified environment
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
will be used to evaluate the rest of the program. The fourth
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
clause is for the unconditional jump to a label, called
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
\texttt{lbl}. That means we have to look up in our snippets
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
map $sn$ what are the next statements for this label.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
Therefore we will continue with evaluating, not with the rest
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
of the program, but with the statements stored in the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
snippets-map under the label $\texttt{lbl}$. The fifth clause
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
for conditional jumps is similar, but to decide whether to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
make the jump we first need to evaluate the expression
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
$\texttt{e}$ in order to find out whether it is $1$. If yes,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
we jump, otherwise we just continue with evaluating the rest
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
of the program.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
Our interpreter works in two stages: First we pre-process our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
program generating the snippets map $sn$, say. Second we call
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
the evaluation function with the default entry point and the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
empty environment:
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
$\textit{eval\_stmts}(sn(\pcode{""}), \varnothing)$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
\noindent It is interesting to note that our interpreter when
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
it comes to the end of the program returns an environment. Our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
programming language does not contain any constructs for input
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
and output. Therefore this environment is the only effect we
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
can observe when running the program (apart from that our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
interpreter might need some time before finishing the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
evaluation of the program and the CPU getting hot). Evaluating 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
the factorial program with our interpreter we receive as
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
``answer''-environment
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
\begin{tabular}{ll}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
$\fbox{\texttt{a}} \mapsto \texttt{120}$ &
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
$\fbox{\texttt{n}} \mapsto \texttt{0}$
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
\end{tabular}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
\noindent While the discussion above should have illustrated
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
the ideas, in order to do some serious calculations, we clearly
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
need to implement the interpreter.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
\subsubsection*{Scala Code for the Interpreter}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
Functional programming languages are very convenient for
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
implementations of interpreters. A good choice for a
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
functional programming language is Scala, a programming
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
language that combines functional and object-oriented
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
pro\-gramming-styles. It has received in the last five years or
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
so quite a bit of attention. One reason for this attention is
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
that, like the Java programming language, Scala compiles to
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
the Java Virtual Machine (JVM) and therefore Scala programs
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
can run under MacOSX, Linux and Windows.\footnote{There are
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
also experimental backends for Android and JavaScript.} Unlike
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
Java, however, Scala often allows programmers to write very
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
concise and elegant code. Some therefore say Scala is the much
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
better Java. A number of companies, The Guardian, Twitter,
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
Coursera, FourSquare, LinkedIn to name a few, either use Scala
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
exclusively in production code, or at least to some
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
substantial degree. If you want to try out Scala yourself, the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
Scala compiler can be downloaded from
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
\begin{quote}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
\url{http://www.scala-lang.org}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
\end{quote}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
Let us have a look at the Scala code shown in
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
Figure~\ref{code}. It shows the entire code for the
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
interpreter, though the implementation is admittedly no
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
frills.
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
\begin{figure}[t]
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
\small
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
\lstinputlisting[language=Scala]{../progs/inter.scala}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
\caption{The entire code of the interpreter for our
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
idealised programming language.\label{code}}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
\end{figure}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
\subsubsection*{Static Analysis}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
Finally we can come back to our original problem, namely 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
finding out what the signs of variables are 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
\begin{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
\end{center}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
\end{document}
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
%% list of static analysers for C
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
http://spinroot.com/static/index.html
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
%% NASA coding rules for C
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
%%% Local Variables: 
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
%%% mode: latex
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   614
%%% TeX-master: t
ed8f014217be updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
%%% End: