cws/cw02.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 01 Oct 2023 10:57:32 +0100
changeset 932 5678414a3898
parent 918 53e7da9f372a
child 934 ee35eeb5831a
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}
918
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
     5
\usepackage[normalem]{ulem}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
     9
\section*{Coursework 2}
198
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 182
diff changeset
    10
835
08b157566a73 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 833
diff changeset
    11
\noindent This coursework is worth 10\% and is due on \cwTWO{} at
877
43460c7b2010 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
    12
16:00. You are asked to implement the Sulzmann \& Lu lexer for the
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    13
WHILE language. You can do the implementation in any programming
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    14
language you like, but you need to submit the source code with which
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    15
you answered the questions, otherwise a mark of 0\% will be
918
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    16
awarded. You need to submit your written
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    17
answers as pdf---see attached questionaire.  Code send as code. If you use
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    18
Scala in your code, a good place to start is the file \texttt{re3.sc}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    19
that is uploaded to Github.
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    20
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 748
diff changeset
    21
\subsection*{Disclaimer\alert}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
    23
It should be understood that the work you submit represents
918
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    24
your own effort. You have not copied from anyone else
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    25
including CoPilot, ChatGPT \& Co. An
363
0d6deecdb2eb updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
    26
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
    27
during the lectures, which you can both freely use. You can
918
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    28
also use your own code from the CW~1.
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    29
%But do not
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    30
%be tempted to ask Github Copilot for help or do any other
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
    31
%shenanigans like this!
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    33
\subsection*{Question 1}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
    35
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
    36
need to design the appropriate regular expressions for the
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    37
following eleven syntactic entities:
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    39
\begin{enumerate}
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    40
\item keywords are
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    41
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    42
\begin{center}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    43
\texttt{while}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    44
\texttt{if}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    45
\texttt{then}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    46
\texttt{else}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    47
\texttt{do}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    48
\texttt{for}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    49
\texttt{to}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    50
\texttt{true}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    51
\texttt{false}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    52
\texttt{read}, 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    53
\texttt{write},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    54
\texttt{skip}
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    55
\end{center} 
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    56
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    57
\item operators are:
275
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{>}, 
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    66
\texttt{<},
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    67
\texttt{<=}, 
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    68
\texttt{>=},
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    69
\texttt{:=},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    70
\texttt{\&\&},
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
    71
\texttt{||}
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    72
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    73
\item letters are uppercase and lowercase
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    74
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    75
\item symbols are letters plus the characters
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    76
  \texttt{.},
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    77
  \texttt{\_},
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    78
  \texttt{>},
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    79
  \texttt{<},
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    80
  \texttt{=},
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    81
  \texttt{;},
850
Christian Urban <christian.urban@kcl.ac.uk>
parents: 845
diff changeset
    82
  \texttt{,} (comma),
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 797
diff changeset
    83
  \texttt{$\backslash$} and
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    84
  \texttt{:}
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    85
850
Christian Urban <christian.urban@kcl.ac.uk>
parents: 845
diff changeset
    86
\item strings are enclosed by double quotes, like \texttt{"\ldots"}, and consisting of
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    87
  symbols, whitespaces and digits
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    88
\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
    89
\item there are semicolons \texttt{;}
447
68769db65185 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 428
diff changeset
    90
\item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
845
ddd9659971ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 835
diff changeset
    91
  \texttt{$\backslash$t} or \texttt{$\backslash$r}
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    92
\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
    93
or digits
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
    94
\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
    95
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
    96
with leading zeroes, such as \pcode{001}
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
    97
\item comments start with \texttt{//} and contain symbols, spaces and digits until the end of the line
180
50e8dcd95ae3 added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    98
\end{enumerate}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
\noindent
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   101
You can use the basic regular expressions 
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   103
\[
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   104
\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
   105
\]
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   107
\noindent
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   108
but also the following extended regular expressions
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   109
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   110
\begin{center}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   111
\begin{tabular}{ll}
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   112
$[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
   113
$r^+$ & one or more times $r$\\
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   114
$r^?$ & optional $r$\\
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   115
$r^{\{n\}}$ & n-times $r$\\
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   116
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   117
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   118
458
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   119
\noindent
473
dc528091eb70 updated
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   120
Later on you will also need the record regular expression:
458
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   121
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   122
\begin{center}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   123
\begin{tabular}{ll}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   124
$REC(x:r)$ & record regular expression\\
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   125
\end{tabular}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   126
\end{center}
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   127
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   128
\noindent Try to design your regular expressions to be as
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   129
small as possible. For example you should use character sets
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   130
for identifiers and numbers. Feel free to use the general
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   131
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
   132
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   133
\subsection*{Question 2}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   134
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   135
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
   136
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
   137
(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
   138
$inj$. These functions need to be appropriately extended for
918
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   139
the extended regular expressions from Q1. Write down in the
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   140
questionaire at the end the 
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   141
clauses for
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   142
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   143
\begin{center}
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   144
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   145
$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
   146
$mkeps(r^+)$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   147
$mkeps(r^?)$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   148
$mkeps(r^{\{n\}})$             & $\dn$ & $?$\medskip\\
494
d0fc671bcbbf updated
Christian Urban <urbanc@in.tum.de>
parents: 473
diff changeset
   149
$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
   150
$inj\, (r^+)\,c\,\ldots$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   151
$inj\, (r^?)\,c\,\ldots$                   & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   152
$inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & $?$\\
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   153
\end{tabular}
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   154
\end{center}
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   155
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   156
\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
   157
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
   158
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
   159
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   160
\begin{center}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   161
\begin{tabular}{ll}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   162
regex: & string:\smallskip\\
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   163
$a^{\{3\}}$ & $aaa$\\
458
896a5f91838d updated
Christian Urban <urbanc@in.tum.de>
parents: 447
diff changeset
   164
$(a + \ONE)^{\{3\}}$ & $aa$
396
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   165
\end{tabular}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   166
\end{center}
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   167
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   168
598
e3ad67cd5123 updated
Christian Urban <urbanc@in.tum.de>
parents: 578
diff changeset
   169
\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
   170
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
   171
in both examples a value.
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   172
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   173
4cd75c619e06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 395
diff changeset
   174
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
   175
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
   176
\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
   177
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
   178
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 364
diff changeset
   179
\noindent
384
4629448c1bd9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
   180
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
   181
string
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   182
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   183
\begin{center}
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   184
\code{"read n;"}
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   185
\end{center} 
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   186
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   187
\noindent
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   188
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
   189
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   190
419
4110ab35e5d8 updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 396
diff changeset
   191
\subsection*{Question 3}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   192
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   193
Extend your lexer from Q2 to also simplify regular expressions after
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   194
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
   195
injection. Use this lexer to tokenize the programs in
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   196
Figures~\ref{fib} -- \ref{collatz}. You can find the programms also on
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   197
KEATS. Give the tokens of these programs where whitespaces are
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   198
filtered out. Make sure you can tokenise \textbf{exactly} these
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   199
programs.\bigskip
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 181
diff changeset
   200
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   202
\begin{figure}[h]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 850
diff changeset
   203
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/fib.while}}
181
1f98d215df71 added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 180
diff changeset
   204
\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
   205
\end{figure}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   207
\begin{figure}[h]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 850
diff changeset
   208
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/loops.while}}
275
618c7640cf66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   209
\caption{The three-nested-loops program in the WHILE language. 
578
6e5e3adc9eb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 567
diff changeset
   210
(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
   211
\end{figure}
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
659
15b69ca63b29 optional
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
   213
\begin{figure}[h]
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 850
diff changeset
   214
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/factors.while}}
659
15b69ca63b29 optional
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
   215
\caption{A program that calculates factors for numbers in the WHILE
15b69ca63b29 optional
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
   216
  language.\label{factors}}
15b69ca63b29 optional
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
   217
\end{figure}
15b69ca63b29 optional
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
   218
748
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   219
\begin{figure}[h]
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   220
\mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../progs/while-tests/collatz2.while}}
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   221
\caption{A program that calculates the Collatz series for numbers
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   222
  between 1 and 100.\label{collatz}}
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   223
\end{figure}
383f2a5952ce updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 719
diff changeset
   224
918
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   225
\clearpage
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   226
\newpage
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   227
\section*{Answers}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   228
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   229
\mbox{}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   230
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   231
\noindent
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   232
\textbf{Question 2:}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   233
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   234
\begin{center}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   235
  \def\arraystretch{1.6}  
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   236
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   237
$mkeps([c_1,c_2,\ldots,c_n])$  & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   238
$mkeps(r^+)$                   & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   239
$mkeps(r^?)$                   & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   240
$mkeps(r^{\{n\}})$             & $\dn$ & \uline{\hspace{8cm}}\bigskip\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   241
$inj\, ([c_1,c_2,\ldots,c_n])\,c\,\ldots$  & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   242
$inj\, (r^+)\,c\,\ldots$                   & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   243
$inj\, (r^?)\,c\,\ldots$                   & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   244
$inj\, (r^{\{n\}})\,c\,\ldots$             & $\dn$ & \uline{\hspace{8cm}}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   245
\end{tabular}
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   246
\end{center}\bigskip
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   247
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   248
\noindent
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   249
Tokens for \code{"read n;"}\\
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   250
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   251
\noindent
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   252
\uline{\hfill}\medskip
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   253
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   254
\noindent
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   255
\uline{\hfill}\medskip
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   256
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   257
\noindent
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   258
\uline{\hfill}\medskip
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   259
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   260
\noindent
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   261
\uline{\hfill}\medskip
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   262
53e7da9f372a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 886
diff changeset
   263
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
%%% End: