coursework/cw02.tex
author Christian Urban <urbanc@in.tum.de>
Wed, 25 Sep 2019 11:24:34 +0100
changeset 630 9b1c15c3eb6f
parent 598 e3ad67cd5123
child 657 732cb155d806
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
630
9b1c15c3eb6f updated
Christian Urban <urbanc@in.tum.de>
parents: 598
diff changeset
     1
% !TEX program = xelatex
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\documentclass{article}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
     3
\usepackage{../style}
216
f5ec7c597c5b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
     4
\usepackage{../langs}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
     8
\section*{Coursework 2 (Strand 1)}
198
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
     9
630
9b1c15c3eb6f updated
Christian Urban <urbanc@in.tum.de>
parents: 598
diff changeset
    10
\noindent This coursework is worth 5\% and is due on 4
567
4573d36d0b2f updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    11
November at 18:00. You are asked to implement the Sulzmann \&
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    12
Lu lexer for the WHILE language. You can do the
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    13
implementation in any programming language you like, but you
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    14
need to submit the source code with which you answered the
395
e57d3d92b856 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 384
diff changeset
    15
questions, otherwise a mark of 0\% will be awarded. You can
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    16
submit your answers in a txt-file or as pdf. Code submit as 
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    17
code.
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    18
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    19
\subsection*{Disclaimer}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    21
It should be understood that the work you submit represents
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    22
your own effort. You have not copied from anyone else. An
363
0d6deecdb2eb updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    23
exception is the Scala code from KEATS and the code I showed
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    24
during the lectures, which you can both freely use. You can
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    25
also use your own code from the CW~1.
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    27
\subsection*{Question 1}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    29
To implement a lexer for the WHILE language, you first
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    30
need to design the appropriate regular expressions for the
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    31
following eight syntactic entities:
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    33
\begin{enumerate}
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    34
\item keywords are
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    35
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    36
\begin{quote}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    37
\texttt{while}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    38
\texttt{if}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    39
\texttt{then}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    40
\texttt{else}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    41
\texttt{do}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    42
\texttt{for}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    43
\texttt{to}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    44
\texttt{true}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    45
\texttt{false}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    46
\texttt{read}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    47
\texttt{write},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    48
\texttt{skip}
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    49
\end{quote} 
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    50
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    51
\item operators are
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    52
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    53
\begin{quote}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    54
\texttt{+}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    55
\texttt{-}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    56
\texttt{*}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    57
\texttt{\%},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    58
\texttt{/},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    59
\texttt{==}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    60
\texttt{!=}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    61
\texttt{>}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    62
\texttt{<}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    63
\texttt{:=},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    64
\texttt{\&\&},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    65
\texttt{||}
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    66
\end{quote} 
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    67
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    68
\item strings are enclosed by \texttt{"\ldots"} 
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    69
\item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    70
\item there are semicolons \texttt{;}
447
68769db65185 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 428
diff changeset
    71
\item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
68769db65185 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 428
diff changeset
    72
  \texttt{$\backslash$t}
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    73
\item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    74
or digits
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    75
\item numbers are \pcode{0}, \pcode{1}, \ldots and so on; give 
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    76
a regular expression that can recognise \pcode{0}, but not numbers 
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    77
with leading zeroes, such as \pcode{001}
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    78
\end{enumerate}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
\noindent
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    81
You can use the basic regular expressions 
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    83
\[
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    84
\ZERO,\; \ONE,\; c,\; r_1 + r_2,\; r_1 \cdot r_2,\; r^*
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    85
\]
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    87
\noindent
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    88
but also the following extended regular expressions
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
    89
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
    90
\begin{center}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    91
\begin{tabular}{ll}
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
    92
$[c_1,c_2,\ldots,c_n]$ & a set of characters\\
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    93
$r^+$ & one or more times $r$\\
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    94
$r^?$ & optional $r$\\
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    95
$r^{\{n\}}$ & n-times $r$\\
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
    96
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
    97
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
    98
458
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
    99
\noindent
473
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   100
Later on you will also need the record regular expression:
458
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   101
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   102
\begin{center}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   103
\begin{tabular}{ll}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   104
$REC(x:r)$ & record regular expression\\
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   105
\end{tabular}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   106
\end{center}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   107
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   108
\noindent Try to design your regular expressions to be as
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   109
small as possible. For example you should use character sets
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   110
for identifiers and numbers. Feel free to use the general
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   111
character constructor \textit{CFUN} introduced in CW 1.
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   112
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   113
\subsection*{Question 2}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   114
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   115
Implement the Sulzmann \& Lu lexer from the lectures. For
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   116
this you need to implement the functions $nullable$ and $der$
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   117
(you can use your code from CW~1), as well as $mkeps$ and
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   118
$inj$. These functions need to be appropriately extended for
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   119
the extended regular expressions from Q1. Write down the 
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   120
clauses for
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   121
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   122
\begin{center}
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   123
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   124
$mkeps([c_1,c_2,\ldots,c_n])$  & $\dn$ & $?$\\
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   125
$mkeps(r^+)$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   126
$mkeps(r^?)$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   127
$mkeps(r^{\{n\}})$             & $\dn$ & $?$\medskip\\
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   128
$inj\, ([c_1,c_2,\ldots,c_n])\,c\,\ldots$  & $\dn$ & $?$\\
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   129
$inj\, (r^+)\,c\,\ldots$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   130
$inj\, (r^?)\,c\,\ldots$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   131
$inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   132
\end{tabular}
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   133
\end{center}
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   134
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   135
\noindent where $inj$ takes three arguments: a regular
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   136
expression, a character and a value. Test your lexer code
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   137
with at least the two small examples below:
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   138
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   139
\begin{center}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   140
\begin{tabular}{ll}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   141
regex: & string:\smallskip\\
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   142
$a^{\{3\}}$ & $aaa$\\
458
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   143
$(a + \ONE)^{\{3\}}$ & $aa$
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   144
\end{tabular}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   145
\end{center}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   146
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   147
598
e3ad67cd5123 updated
Christian Urban <urbanc@in.tum.de>
parents: 578
diff changeset
   148
\noindent Both strings should be successfully lexed by the
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   149
respective regular expression, that means the lexer returns 
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   150
in both examples a value.
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   151
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   152
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   153
Also add the record regular expression from the
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   154
lectures to your lexer and implement a function, say
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   155
\pcode{env}, that returns all assignments from a value (such
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   156
that you can extract easily the tokens from a value).\medskip 
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   157
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   158
\noindent
384
4629448c1bd9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   159
Finally give the tokens for your regular expressions from Q1 and the
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   160
string
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   161
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   162
\begin{center}
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   163
\code{"read n;"}
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   164
\end{center} 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   165
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   166
\noindent
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   167
and use your \pcode{env} function to give the token sequence.
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   168
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   169
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   170
\subsection*{Question 3}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   171
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   172
Extend your lexer from Q2 to also simplify regular expressions
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   173
after each derivation step and rectify the computed values after each
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   174
injection. Use this lexer to tokenize the programs in
364
50ce3667c190 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 363
diff changeset
   175
Figure~\ref{fib} and \ref{loop}. Give the tokens of these
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   176
programs where whitespaces are filtered out.\bigskip
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   177
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   179
\begin{figure}[h]
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   180
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/fib.while}}
181
1f98d215df71 added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 180
diff changeset
   181
\caption{Fibonacci program in the WHILE language.\label{fib}}
1f98d215df71 added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 180
diff changeset
   182
\end{figure}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   184
\begin{figure}[h]
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   185
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/loops.while}}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   186
\caption{The three-nested-loops program in the WHILE language. 
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   187
(Usually used for timing measurements.)\label{loop}}
181
1f98d215df71 added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 180
diff changeset
   188
\end{figure}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
%%% End: