| author | Christian Urban <christian.urban@kcl.ac.uk> |
| Wed, 16 Oct 2024 13:14:13 +0100 | |
| changeset 968 | d8d8911a3d6f |
| parent 946 | bee7c57c18c3 |
| child 969 | 0dfa2923a7c6 |
| permissions | -rw-r--r-- |
| 630 | 1 |
% !TEX program = xelatex |
|
178
d36363d648e3
added
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 | 5 |
\usepackage[normalem]{ulem}
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document}
|
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
| 748 | 9 |
\section*{Coursework 2}
|
|
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
10 |
|
| 835 | 11 |
\noindent This coursework is worth 10\% and is due on \cwTWO{} at
|
| 877 | 12 |
16:00. You are asked to implement the Sulzmann \& Lu lexer for the |
| 748 | 13 |
WHILE language. You can do the implementation in any programming |
14 |
language you like, but you need to submit the source code with which |
|
15 |
you answered the questions, otherwise a mark of 0\% will be |
|
| 968 | 16 |
awarded. %You need to submit your written answers as pdf---see attached |
17 |
% questionaire. Code send as code. |
|
18 |
If you use Scala in your code, a |
|
| 943 | 19 |
good place to start is the file \texttt{lexer.sc} and
|
20 |
\texttt{token.sc} uploaded to KEATS. The template file on Github is
|
|
| 968 | 21 |
called \texttt{cw02.sc}. The example files are in the subdirectory
|
22 |
\texttt{examples}. The main function that will be tested is
|
|
23 |
called \texttt{tokenise}. The marks will be distributed such that
|
|
24 |
3 marks are given for the correct \texttt{WHILE\_REGS} regular
|
|
25 |
expression; 5 marks for the correct \texttt{inj} and \texttt{mkeps}
|
|
26 |
definitions; and two marks when \texttt{tokenise} produces the correct
|
|
27 |
results for the example files. |
|
28 |
||
29 |
||
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
30 |
|
| 750 | 31 |
\subsection*{Disclaimer\alert}
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
|
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
33 |
It should be understood that the work you submit represents |
| 918 | 34 |
your own effort. You have not copied from anyone else |
35 |
including CoPilot, ChatGPT \& Co. An |
|
|
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
36 |
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
|
37 |
during the lectures, which you can both freely use. You can |
| 918 | 38 |
also use your own code from the CW~1. |
39 |
%But do not |
|
40 |
%be tempted to ask Github Copilot for help or do any other |
|
41 |
%shenanigans like this! |
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
43 |
\subsection*{Question 1}
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
|
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
45 |
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
|
46 |
need to design the appropriate regular expressions for the |
| 748 | 47 |
following eleven syntactic entities: |
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
|
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
49 |
\begin{enumerate}
|
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
50 |
\item keywords are |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
51 |
|
| 748 | 52 |
\begin{center}
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
53 |
\texttt{while},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
54 |
\texttt{if},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
55 |
\texttt{then},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
56 |
\texttt{else},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
57 |
\texttt{do},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
58 |
\texttt{for},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
59 |
\texttt{to},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
60 |
\texttt{true},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
61 |
\texttt{false},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
62 |
\texttt{read},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
63 |
\texttt{write},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
64 |
\texttt{skip}
|
| 748 | 65 |
\end{center}
|
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
66 |
|
| 748 | 67 |
\item operators are: |
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
68 |
\texttt{+},
|
|
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{\%},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
72 |
\texttt{/},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
73 |
\texttt{==},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
74 |
\texttt{!=},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
75 |
\texttt{>},
|
| 748 | 76 |
\texttt{<},
|
77 |
\texttt{<=},
|
|
78 |
\texttt{>=},
|
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
79 |
\texttt{:=},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
80 |
\texttt{\&\&},
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
81 |
\texttt{||}
|
| 748 | 82 |
|
83 |
\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
|
84 |
|
| 748 | 85 |
\item symbols are letters plus the characters |
86 |
\texttt{.},
|
|
87 |
\texttt{\_},
|
|
88 |
\texttt{>},
|
|
89 |
\texttt{<},
|
|
90 |
\texttt{=},
|
|
91 |
\texttt{;},
|
|
| 850 | 92 |
\texttt{,} (comma),
|
| 833 | 93 |
\texttt{$\backslash$} and
|
| 748 | 94 |
\texttt{:}
|
95 |
||
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
96 |
\item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
|
| 934 | 97 |
\item digits are \pcode{0} to \pcode{9}
|
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
98 |
\item there are semicolons \texttt{;}
|
|
447
68769db65185
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
428
diff
changeset
|
99 |
\item whitespaces are either \texttt{" "} (one or more) or \texttt{$\backslash$n} or
|
| 845 | 100 |
\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
|
101 |
\item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
|
| 934 | 102 |
or digits |
103 |
\item numbers for numbers give |
|
|
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
104 |
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
|
105 |
with leading zeroes, such as \pcode{001}
|
| 934 | 106 |
\item strings are enclosed by double quotes, like \texttt{"\ldots"}, and consisting of
|
107 |
symbols, digits, parentheses, whitespaces and \texttt{$\backslash$n} (note the latter is not the escaped version but \texttt{$\backslash$} followed by \texttt{n}, otherwise we would not be able to indicate in our strings when to write a newline).
|
|
| 946 | 108 |
\item comments start with \texttt{//} and contain symbols, spaces, parentheses and digits until the end-of-the-line markers
|
| 934 | 109 |
\item endo-of-line-markers are \texttt{$\backslash$n} and \texttt{$\backslash$r$\backslash$n}
|
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
110 |
\end{enumerate}
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
|
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
\noindent |
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
113 |
You can use the basic regular expressions |
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
115 |
\[ |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
116 |
\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
|
117 |
\] |
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
119 |
\noindent |
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
120 |
but also the following extended regular expressions |
|
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
121 |
|
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
122 |
\begin{center}
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
123 |
\begin{tabular}{ll}
|
| 494 | 124 |
$[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
|
125 |
$r^+$ & one or more times $r$\\ |
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
126 |
$r^?$ & optional $r$\\ |
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
127 |
$r^{\{n\}}$ & n-times $r$\\
|
|
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
128 |
\end{tabular}
|
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
129 |
\end{center}
|
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
130 |
|
| 458 | 131 |
\noindent |
| 473 | 132 |
Later on you will also need the record regular expression: |
| 458 | 133 |
|
134 |
\begin{center}
|
|
135 |
\begin{tabular}{ll}
|
|
136 |
$REC(x:r)$ & record regular expression\\ |
|
137 |
\end{tabular}
|
|
138 |
\end{center}
|
|
139 |
||
|
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
140 |
\noindent Try to design your regular expressions to be as |
| 494 | 141 |
small as possible. For example you should use character sets |
142 |
for identifiers and numbers. Feel free to use the general |
|
143 |
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
|
144 |
|
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
145 |
\subsection*{Question 2}
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
146 |
|
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
(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
|
150 |
$inj$. These functions need to be appropriately extended for |
| 968 | 151 |
the extended regular expressions from Q1. The definitions |
152 |
you need to create are: |
|
153 |
||
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
154 |
|
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
155 |
\begin{center}
|
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
156 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
|
| 494 | 157 |
$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
|
158 |
$mkeps(r^+)$ & $\dn$ & $?$\\ |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
159 |
$mkeps(r^?)$ & $\dn$ & $?$\\ |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
160 |
$mkeps(r^{\{n\}})$ & $\dn$ & $?$\medskip\\
|
| 494 | 161 |
$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
|
162 |
$inj\, (r^+)\,c\,\ldots$ & $\dn$ & $?$\\ |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
163 |
$inj\, (r^?)\,c\,\ldots$ & $\dn$ & $?$\\ |
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
164 |
$inj\, (r^{\{n\}})\,c\,\ldots$ & $\dn$ & $?$\\
|
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
165 |
\end{tabular}
|
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
166 |
\end{center}
|
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
167 |
|
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
168 |
\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
|
169 |
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
|
170 |
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
|
171 |
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
172 |
\begin{center}
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
173 |
\begin{tabular}{ll}
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
174 |
regex: & string:\smallskip\\ |
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
175 |
$a^{\{3\}}$ & $aaa$\\
|
| 458 | 176 |
$(a + \ONE)^{\{3\}}$ & $aa$
|
|
396
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
177 |
\end{tabular}
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
178 |
\end{center}
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
179 |
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
180 |
|
| 598 | 181 |
\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
|
182 |
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
|
183 |
in both examples a value. |
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
184 |
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
185 |
|
|
4cd75c619e06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
186 |
Also add the record regular expression from the |
| 968 | 187 |
lectures to your lexer and complete the function |
188 |
\pcode{env} so that it returns all assignments from a value (this then
|
|
189 |
allows you to extract easily the tokens from a value in the next |
|
190 |
question).\medskip |
|
|
369
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
191 |
|
|
43c0ed473720
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
364
diff
changeset
|
192 |
\noindent |
| 968 | 193 |
Finally make that the function \texttt{lexing\_simp} generates
|
194 |
with the regular expression from Q1 for the string |
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
195 |
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
196 |
\begin{center}
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
197 |
\code{"read n;"}
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
198 |
\end{center}
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
199 |
|
|
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
200 |
\noindent |
| 968 | 201 |
the following pairs: |
202 |
||
203 |
\begin{center}
|
|
204 |
\texttt{List((k,read), (w, ), (i,n), (s,;))}
|
|
205 |
\end{center}
|
|
206 |
||
207 |
||
208 |
||
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
209 |
|
|
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
210 |
|
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
396
diff
changeset
|
211 |
\subsection*{Question 3}
|
|
275
618c7640cf66
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
212 |
|
| 968 | 213 |
Make sure your lexer from Q2 also simplifies regular expressions after |
214 |
each derivation step and rectifies the computed values after each |
|
215 |
injection. Use this lexer to tokenise the six WHILE programs |
|
216 |
in the \texttt{examples} directory. Make sure that the \texttt{tokenise}
|
|
217 |
function filters out whitespaces and comments.\bigskip |
|
|
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
218 |
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
219 |
|
| 968 | 220 |
% \begin{figure}[h]
|
221 |
% \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/fib.while}}
|
|
222 |
% \caption{Fibonacci program in the WHILE language.\label{fib}}
|
|
223 |
% \end{figure}
|
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
224 |
|
| 968 | 225 |
% \begin{figure}[h]
|
226 |
% \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/loops.while}}
|
|
227 |
% \caption{The three-nested-loops program in the WHILE language.
|
|
228 |
% (Usually used for timing measurements.)\label{loop}}
|
|
229 |
% \end{figure}
|
|
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
230 |
|
| 968 | 231 |
% \begin{figure}[h]
|
232 |
% \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/factors.while}}
|
|
233 |
% \caption{A program that calculates factors for numbers in the WHILE
|
|
234 |
% language.\label{factors}}
|
|
235 |
% \end{figure}
|
|
| 659 | 236 |
|
| 968 | 237 |
% \begin{figure}[h]
|
238 |
% \mbox{\lstinputlisting[language=While,xleftmargin=10mm]{../cwtests/cw02/collatz2.while}}
|
|
239 |
% \caption{A program that calculates the Collatz series for numbers
|
|
240 |
% between 1 and 100.\label{collatz}}
|
|
241 |
% \end{figure}
|
|
| 748 | 242 |
|
| 968 | 243 |
% \clearpage |
244 |
% \newpage |
|
245 |
% \section*{Answers}
|
|
| 918 | 246 |
|
| 968 | 247 |
% \mbox{}
|
| 918 | 248 |
|
| 968 | 249 |
% \noindent |
250 |
% \textbf{Question 2:}\\ (Use mathematical notation, such as $r^+$, rather than code, such as \code{PLUS(r)})
|
|
| 918 | 251 |
|
| 968 | 252 |
% \begin{center}
|
253 |
% \def\arraystretch{1.6}
|
|
254 |
% \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
|
|
255 |
% $mkeps([c_1,c_2,\ldots,c_n])$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
256 |
% $mkeps(r^+)$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
257 |
% $mkeps(r^?)$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
258 |
% $mkeps(r^{\{n\}})$ & $\dn$ & \uline{\hspace{8cm}}\bigskip\\
|
|
259 |
% $inj\, ([c_1,c_2,\ldots,c_n])\,c\,\ldots$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
260 |
% $inj\, (r^+)\,c\,\ldots$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
261 |
% $inj\, (r^?)\,c\,\ldots$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
262 |
% $inj\, (r^{\{n\}})\,c\,\ldots$ & $\dn$ & \uline{\hspace{8cm}}\\
|
|
263 |
% \end{tabular}
|
|
264 |
% \end{center}\bigskip
|
|
| 918 | 265 |
|
| 968 | 266 |
% \noindent |
267 |
% Tokens for \code{"read n;"}\\
|
|
| 918 | 268 |
|
| 968 | 269 |
% \noindent |
270 |
% \uline{\hfill}\medskip
|
|
| 918 | 271 |
|
| 968 | 272 |
% \noindent |
273 |
% \uline{\hfill}\medskip
|
|
| 918 | 274 |
|
| 968 | 275 |
% \noindent |
276 |
% \uline{\hfill}\medskip
|
|
| 918 | 277 |
|
| 968 | 278 |
% \noindent |
279 |
% \uline{\hfill}\medskip
|
|
| 918 | 280 |
|
281 |
||
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
282 |
\end{document}
|
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
283 |
|
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
284 |
%%% Local Variables: |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
285 |
%%% mode: latex |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
286 |
%%% TeX-master: t |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
287 |
%%% End: |