author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 29 Nov 2024 18:59:32 +0000 | |
changeset 976 | e9eac62928f5 |
parent 966 | 4189cb63e5db |
permissions | -rw-r--r-- |
630 | 1 |
% !TEX program = xelatex |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\documentclass{article} |
253
75c469893514
added coursework
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:
133
diff
changeset
|
4 |
\usepackage{../langs} |
918 | 5 |
\usepackage[normalem]{ulem} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
492 | 7 |
\usepackage{array} |
8 |
||
888 | 9 |
%TEST |
492 | 10 |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
\begin{document} |
492 | 12 |
\newcolumntype{C}[1]{>{\centering}m{#1}} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
|
966 | 14 |
\section*{Coursework 1 (Update for 2024: CW is OPTIONAL but recommended)} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
|
966 | 16 |
%You are asked to implement a regular expression matcher and submit a |
17 |
%document containing the answers for the questions below. You can do |
|
18 |
%the implementation in any programming language you like, but you need |
|
19 |
%to submit the source code with which you answered the questions, |
|
20 |
%otherwise a mark of 0\% will be awarded. You need to submit your |
|
21 |
%written answers as pdf---see attached questionaire. Code send as |
|
22 |
%code. If you use Scala in your code, a good place to start is the file |
|
23 |
%\texttt{re3.sc} that is uploaded to Github. |
|
748 | 24 |
|
918 | 25 |
%Please package everything |
26 |
%inside a zip-file that creates a directory with the name |
|
27 |
%\[\texttt{YournameYourfamilyname}\] |
|
28 |
% |
|
29 |
%\noindent on my end. Thanks! |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
351
diff
changeset
|
30 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
351
diff
changeset
|
31 |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
32 |
|
966 | 33 |
%\subsubsection*{Disclaimer\alert} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
966 | 35 |
%It should be understood that the work you submit represents |
36 |
%your \textbf{own} effort. You have not copied from anyone else |
|
37 |
%including CoPilot, ChatGPT \& Co. An |
|
38 |
%exception is the Scala code I showed during the lectures or |
|
39 |
%uploaded to KEATS, which you can freely use. Do not |
|
40 |
%be tempted to ask Github Copilot for help or do any other |
|
41 |
%shenanigans like this!\bigskip |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
42 |
|
966 | 43 |
%\noindent |
44 |
%If you have any questions, please send me an email in \textbf{good} |
|
45 |
%time!\bigskip |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
46 |
|
492 | 47 |
\subsection*{Task} |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
48 |
|
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
49 |
The task is to implement a regular expression matcher based on |
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
50 |
derivatives of regular expressions. The implementation should |
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
51 |
be able to deal with the usual (basic) regular expressions |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
\[ |
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
54 |
\ZERO,\; \ONE,\; c,\; r_1 + r_2,\; r_1 \cdot r_2,\; r^* |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
\] |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
\noindent |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
58 |
but also with the following extended regular expressions: |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
\begin{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
\begin{tabular}{ll} |
492 | 62 |
$[c_1,c_2,\ldots,c_n]$ & a set of characters---for character ranges\\ |
63 |
$r^+$ & one or more times $r$\\ |
|
64 |
$r^?$ & optional $r$\\ |
|
907 | 65 |
$r_1 \,\&\, r_2$ & intersection (matched by both $r_1$ and $r_2$)\\ |
492 | 66 |
$r^{\{n\}}$ & exactly $n$-times\\ |
67 |
$r^{\{..m\}}$ & zero or more times $r$ but no more than $m$-times\\ |
|
68 |
$r^{\{n..\}}$ & at least $n$-times $r$\\ |
|
69 |
$r^{\{n..m\}}$ & at least $n$-times $r$ but no more than $m$-times\\ |
|
70 |
$\sim{}r$ & not-regular-expression of $r$\\ |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
\end{tabular} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
\end{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
|
492 | 74 |
\noindent You can assume that $n$ and $m$ are greater or equal than |
75 |
$0$. In the case of $r^{\{n,m\}}$ you can also assume $0 \le n \le m$.\bigskip |
|
76 |
||
77 |
\noindent {\bf Important!} Your implementation should have explicit |
|
567 | 78 |
case classes for the basic regular expressions, but also explicit case |
79 |
classes for |
|
80 |
the extended regular expressions.\footnote{Please call them |
|
938 | 81 |
\code{RANGE}, \code{PLUS}, \code{OPTIONAL}, \code{INTER}, \code{NTIMES}, |
630 | 82 |
\code{UPTO}, \code{FROM} and \code{BETWEEN}.} |
83 |
That means do not treat the extended regular expressions |
|
966 | 84 |
by just translating them into the basic ones. See also Question 1, |
567 | 85 |
where you are asked to explicitly give the rules for \textit{nullable} |
748 | 86 |
and \textit{der} for the extended regular expressions. Something like |
87 |
||
88 |
\[der\,c\,(r^+) \dn der\,c\,(r\cdot r^*)\] |
|
89 |
||
966 | 90 |
\noindent is \emph{not} allowed as answer in Question 1 and \emph{not} |
748 | 91 |
allowed in your code.\medskip |
492 | 92 |
|
93 |
\noindent |
|
94 |
The meanings of the extended regular expressions are |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
\begin{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
\begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l} |
492 | 98 |
$L([c_1,c_2,\ldots,c_n])$ & $\dn$ & $\{[c_1], [c_2], \ldots, [c_n]\}$\\ |
99 |
$L(r^+)$ & $\dn$ & $\bigcup_{1\le i}.\;L(r)^i$\\ |
|
100 |
$L(r^?)$ & $\dn$ & $L(r) \cup \{[]\}$\\ |
|
907 | 101 |
$L(r_1 \,\&\, r_2)$ & $\dn$ & $L(r_1) \cap L(r_2)$\\ |
492 | 102 |
$L(r^{\{n\}})$ & $\dn$ & $L(r)^n$\\ |
103 |
$L(r^{\{..m\}})$ & $\dn$ & $\bigcup_{0\le i \le m}.\;L(r)^i$\\ |
|
104 |
$L(r^{\{n..\}})$ & $\dn$ & $\bigcup_{n\le i}.\;L(r)^i$\\ |
|
105 |
$L(r^{\{n..m\}})$ & $\dn$ & $\bigcup_{n\le i \le m}.\;L(r)^i$\\ |
|
106 |
$L(\sim{}r)$ & $\dn$ & $\Sigma^* - L(r)$ |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
\end{tabular} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
\end{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
110 |
\noindent whereby in the last clause the set $\Sigma^*$ stands |
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
111 |
for the set of \emph{all} strings over the alphabet $\Sigma$ |
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
112 |
(in the implementation the alphabet can be just what is |
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
113 |
represented by, say, the type \pcode{Char}). So $\sim{}r$ |
492 | 114 |
means in effect ``all the strings that $r$ cannot match''.\medskip |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
|
492 | 116 |
\noindent |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
117 |
Be careful that your implementation of \textit{nullable} and |
492 | 118 |
\textit{der} satisfies for every regular expression $r$ the following |
966 | 119 |
two properties (see also Question 1): |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
\begin{itemize} |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
122 |
\item $\textit{nullable}(r)$ if and only if $[]\in L(r)$ |
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
123 |
\item $L(der\,c\,r) = Der\,c\,(L(r))$ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
\end{itemize} |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
125 |
|
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
126 |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
|
966 | 128 |
%\subsection*{Question 1 (Unmarked)} |
129 |
% |
|
130 |
%What is your King's email address (you will need it in |
|
131 |
%Question 5)? Also, could you please let me know whether you are |
|
132 |
%a BSc / MSci and the year you are in (in case of MSci). Thanks! |
|
907 | 133 |
|
545 | 134 |
|
966 | 135 |
%\subsection*{Question 2 (Unmarked)} |
136 |
% |
|
137 |
%Can you please also list all programming languages in which you have |
|
138 |
%already written programs (include only instances where you have spent |
|
139 |
%at least a good working day fiddling with a program)? This is just |
|
140 |
%for my curiosity to estimate what your background is. |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
141 |
|
966 | 142 |
\subsection*{Question 1} |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
143 |
|
918 | 144 |
From the lectures you have seen the definitions for the functions |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
145 |
\textit{nullable} and \textit{der} for the basic regular |
918 | 146 |
expressions. Implement and \underline{write down} the rules for the |
147 |
extended regular expressions (see questionaire at the end). |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
149 |
|
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
150 |
\noindent |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
151 |
Remember your definitions have to satisfy the two properties |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
152 |
|
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
153 |
\begin{itemize} |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
154 |
\item $\textit{nullable}(r)$ if and only if $[]\in L(r)$ |
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
155 |
\item $L(der\,c\,r)) = Der\,c\,(L(r))$ |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
156 |
\end{itemize} |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
157 |
|
473 | 158 |
\noindent |
492 | 159 |
Given the definitions of \textit{nullable} and \textit{der}, it is |
160 |
easy to implement a regular expression matcher. Test your regular |
|
907 | 161 |
expression matcher with (\underline{at least}) the examples: |
492 | 162 |
|
163 |
||
164 |
\begin{center} |
|
165 |
\def\arraystretch{1.2} |
|
748 | 166 |
\begin{tabular}{@{}r|m{3mm}|m{6mm}|m{6mm}|m{10mm}|m{6mm}|m{10mm}|m{10mm}|m{10mm}} |
718 | 167 |
string & $a^?$ & $\sim{}a$ & $a^{\{3\}}$ & $(a^?)^{\{3\}}$ & $a^{\{..3\}}$ & |
168 |
$(a^?)^{\{..3\}}$ & $a^{\{3..5\}}$ & $(a^?)^{\{3..5\}}$ \\\hline |
|
169 |
$[]$ &&&&&&& \\\hline |
|
170 |
\texttt{a} &&&&&&& \\\hline |
|
171 |
\texttt{aa} &&&&&&& \\\hline |
|
172 |
\texttt{aaa} &&&&&&& \\\hline |
|
173 |
\texttt{aaaaa} &&&&&&& \\\hline |
|
174 |
\texttt{aaaaaa}&&&&&&& \\ |
|
492 | 175 |
\end{tabular} |
176 |
\end{center} |
|
177 |
||
178 |
\noindent |
|
718 | 179 |
Does your matcher produce the expected results? Make sure you |
180 |
also test corner-cases, like $a^{\{0\}}$! |
|
473 | 181 |
|
966 | 182 |
\subsection*{Question 2} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
183 |
|
494 | 184 |
As you can see, there are a number of explicit regular expressions |
185 |
that deal with single or several characters, for example: |
|
492 | 186 |
|
187 |
\begin{center} |
|
188 |
\begin{tabular}{ll} |
|
494 | 189 |
$c$ & matches a single character\\ |
190 |
$[c_1,c_2,\ldots,c_n]$ & matches a set of characters---for character ranges\\ |
|
191 |
$\textit{ALL}$ & matches any character |
|
492 | 192 |
\end{tabular} |
193 |
\end{center} |
|
194 |
||
195 |
\noindent |
|
567 | 196 |
The latter is useful for matching any string (for example |
494 | 197 |
by using $\textit{ALL}^*$). In order to avoid having an explicit constructor |
198 |
for each case, we can generalise all these cases and introduce a single |
|
492 | 199 |
constructor $\textit{CFUN}(f)$ where $f$ is a function from characters |
576 | 200 |
to booleans. In Scala code this would look as follows: |
201 |
||
202 |
\begin{lstlisting}[numbers=none] |
|
203 |
abstract class Rexp |
|
204 |
... |
|
205 |
case class CFUN(f: Char => Boolean) extends Rexp |
|
206 |
\end{lstlisting}\smallskip |
|
207 |
||
208 |
\noindent |
|
209 |
The idea is that the function $f$ determines which character(s) |
|
494 | 210 |
are matched, namely those where $f$ returns \texttt{true}. |
211 |
In this question implement \textit{CFUN} and define |
|
492 | 212 |
|
213 |
\begin{center} |
|
214 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
215 |
$\textit{nullable}(\textit{CFUN}(f))$ & $\dn$ & $?$\\ |
|
216 |
$\textit{der}\,c\,(\textit{CFUN}(f))$ & $\dn$ & $?$ |
|
217 |
\end{tabular} |
|
218 |
\end{center} |
|
219 |
||
494 | 220 |
\noindent in your matcher and then also give definitions for |
492 | 221 |
|
222 |
\begin{center} |
|
223 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
224 |
$c$ & $\dn$ & $\textit{CFUN}(?)$\\ |
|
225 |
$[c_1,c_2,\ldots,c_n]$ & $\dn$ & $\textit{CFUN}(?)$\\ |
|
226 |
$\textit{ALL}$ & $\dn$ & $\textit{CFUN}(?)$ |
|
227 |
\end{tabular} |
|
228 |
\end{center} |
|
229 |
||
567 | 230 |
\noindent |
231 |
You can either add the constructor $CFUN$ to your implementation in |
|
232 |
Question 3, or you can implement this questions first |
|
233 |
and then use $CFUN$ instead of \code{RANGE} and \code{CHAR} in Question 3. |
|
833 | 234 |
In an ideal world one would do this task first, but this might confuse |
235 |
you with what you need to do in the previous question. |
|
492 | 236 |
|
966 | 237 |
\subsection*{Question 3} |
492 | 238 |
|
239 |
Suppose $[a\mbox{-}z0\mbox{-}9\_\,.\mbox{-}]$ stands for the regular expression |
|
240 |
||
241 |
\[[a,b,c,\ldots,z,0,\dots,9,\_,.,\mbox{-}]\;.\] |
|
242 |
||
243 |
\noindent |
|
244 |
Define in your code the following regular expression for email addresses |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
245 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
246 |
\[ |
492 | 247 |
([a\mbox{-}z0\mbox{-}9\_\,.-]^+)\cdot @\cdot ([a\mbox{-}z0\mbox{-}9\,.-]^+)\cdot .\cdot ([a\mbox{-}z\,.]^{\{2,6\}}) |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
248 |
\] |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
249 |
|
567 | 250 |
\noindent and calculate the derivative according to your own email |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
251 |
address. When calculating the derivative, simplify all regular |
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
252 |
expressions as much as possible by applying the |
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
253 |
following 7 simplification rules: |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
254 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
255 |
\begin{center} |
272
1446bc47a294
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
260
diff
changeset
|
256 |
\begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}ll} |
439
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
257 |
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
258 |
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
259 |
$r \cdot \ONE$ & $\mapsto$ & $r$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
260 |
$\ONE \cdot r$ & $\mapsto$ & $r$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
261 |
$r + \ZERO$ & $\mapsto$ & $r$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
262 |
$\ZERO + r$ & $\mapsto$ & $r$\\ |
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
263 |
$r + r$ & $\mapsto$ & $r$\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
264 |
\end{tabular} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
265 |
\end{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
266 |
|
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
267 |
\noindent Write down your simplified derivative in a readable |
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
268 |
notation using parentheses where necessary. That means you |
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
269 |
should use the infix notation $+$, $\cdot$, $^*$ and so on, |
567 | 270 |
instead of raw code.\bigskip |
271 |
||
272 |
||
966 | 273 |
\subsection*{Question 4} |
567 | 274 |
|
492 | 275 |
Implement the simplification rules in your regular expression matcher. |
276 |
Consider the regular expression $/ \cdot * \cdot |
|
277 |
(\sim{}(\textit{ALL}^* \cdot * \cdot / \cdot \textit{ALL}^*)) \cdot * |
|
630 | 278 |
\cdot /$ and decide whether the following four strings are matched by |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
279 |
this regular expression. Answer yes or no. |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
280 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
281 |
\begin{enumerate} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
282 |
\item \texttt{"/**/"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
283 |
\item \texttt{"/*foobar*/"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
284 |
\item \texttt{"/*test*/test*/"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
285 |
\item \texttt{"/*test/*test*/"} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
286 |
\end{enumerate} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
287 |
|
966 | 288 |
\subsection*{Question 5} |
512 | 289 |
|
290 |
Let $r_1$ be the regular expression $a\cdot a\cdot a$ and $r_2$ be |
|
748 | 291 |
$(a^{\{19,19\}}) \cdot (a^?)$.\medskip |
292 |
||
293 |
\noindent |
|
294 |
Decide whether the following three |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
295 |
strings consisting of $a$s only can be matched by $(r_1^+)^+$. |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
296 |
Similarly test them with $(r_2^+)^+$. Again answer in all six cases |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
297 |
with yes or no. \medskip |
130
5c4998375c46
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
298 |
|
5c4998375c46
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
299 |
\noindent |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
300 |
These are strings are meant to be entirely made up of $a$s. Be careful |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
301 |
when copy-and-pasting the strings so as to not forgetting any $a$ and |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
302 |
to not introducing any other character. |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
303 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
304 |
\begin{enumerate} |
918 | 305 |
\setcounter{enumi}{0} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
306 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
307 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
308 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
309 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
310 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
311 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
312 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
313 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
314 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
315 |
\end{enumerate} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
316 |
|
918 | 317 |
\newpage |
318 |
\section*{Answers} |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
319 |
|
918 | 320 |
\mbox{} |
321 |
||
322 |
\noindent |
|
323 |
\textbf{Name:}\uline{\hfill}\bigskip |
|
324 |
||
325 |
\noindent |
|
326 |
\textbf{BSc / MSci \hspace{2cm} Year:\uline{\hspace{2cm}}}\bigskip |
|
327 |
||
328 |
\noindent |
|
329 |
\textbf{Programming Languages:}\uline{\hfill}\medskip |
|
330 |
||
331 |
\noindent |
|
332 |
\uline{\hfill}\medskip |
|
333 |
||
334 |
\noindent |
|
335 |
\uline{\hfill}\medskip |
|
336 |
||
337 |
\noindent |
|
338 |
\uline{\hfill}\bigskip\bigskip |
|
339 |
||
340 |
\noindent |
|
966 | 341 |
\textbf{Question 1:} |
918 | 342 |
|
343 |
\begin{center} |
|
344 |
\def\arraystretch{1.6} |
|
345 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
346 |
$\textit{nullable}([c_1,c_2,\ldots,c_n])$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
347 |
$\textit{nullable}(r^+)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
348 |
$\textit{nullable}(r^?)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
349 |
$\textit{nullable}(r_1 \,\&\, r_2)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
350 |
$\textit{nullable}(r^{\{n\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
351 |
& & \uline{\hspace{8cm}}\\ |
|
352 |
$\textit{nullable}(r^{\{..m\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
353 |
& & \uline{\hspace{8cm}}\\ |
|
354 |
$\textit{nullable}(r^{\{n..\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
355 |
& & \uline{\hspace{8cm}}\\ |
|
356 |
$\textit{nullable}(r^{\{n..m\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
357 |
& & \uline{\hspace{8cm}}\\ |
|
358 |
$\textit{nullable}(\sim{}r)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
359 |
% \\ |
|
360 |
\end{tabular} |
|
361 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
362 |
$der\, c\, ([c_1,c_2,\ldots,c_n])$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
363 |
$der\, c\, (r^+)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
364 |
$der\, c\, (r^?)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
365 |
$der\, c\, (r_1 \,\&\, r_2)$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
366 |
$der\, c\, (r^{\{n\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
367 |
& & \uline{\hspace{8cm}}\\ |
|
368 |
$der\, c\, (r^{\{..m\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
369 |
& & \uline{\hspace{8cm}}\\ |
|
370 |
$der\, c\, (r^{\{n..\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
371 |
& & \uline{\hspace{8cm}}\\ |
|
372 |
$der\, c\, (r^{\{n..m\}})$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
373 |
& & \uline{\hspace{8cm}}\\ |
|
374 |
& & \uline{\hspace{8cm}}\\ |
|
375 |
$der\, c\, (\sim{}r)$ & $\dn$ & \uline{\hspace{8cm}} |
|
376 |
\end{tabular} |
|
377 |
\end{center}\bigskip |
|
378 |
||
379 |
||
380 |
\noindent |
|
966 | 381 |
\textbf{Question 2:} |
918 | 382 |
|
383 |
\begin{center} |
|
384 |
\def\arraystretch{1.6} |
|
385 |
\begin{tabular}{@ {}r@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
386 |
$\textit{nullable}(CFUN(f))$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
387 |
\\ |
|
388 |
$der\, c\, (CFUN(f))$ & $\dn$ & \uline{\hspace{8cm}}\\ |
|
389 |
& & \uline{\hspace{8cm}}\\ |
|
390 |
\\ |
|
391 |
\\ |
|
392 |
$c$ & $\dn$ & \textit{CFUN}(\uline{\hspace{7cm}})\medskip\\ |
|
393 |
$[c_1,c_2,\ldots,c_n]$ & $\dn$ & \textit{CFUN}(\uline{\hspace{7cm}})\medskip\\ |
|
394 |
$\textit{ALL}$ & $\dn$ & \textit{CFUN}(\uline{\hspace{7cm}})\medskip\\ |
|
395 |
\end{tabular} |
|
396 |
\end{center} |
|
397 |
||
398 |
\newpage |
|
399 |
||
400 |
\noindent |
|
966 | 401 |
\textbf{Question 3 (`mathematical' notation):} |
918 | 402 |
|
403 |
\noindent |
|
404 |
\uline{\hfill}\medskip |
|
405 |
||
406 |
\noindent |
|
407 |
\uline{\hfill}\medskip |
|
408 |
||
409 |
\noindent |
|
410 |
\uline{\hfill}\medskip |
|
411 |
||
412 |
\noindent |
|
413 |
\uline{\hfill}\medskip |
|
414 |
||
415 |
\noindent |
|
416 |
\uline{\hfill}\bigskip\bigskip |
|
417 |
||
418 |
\noindent |
|
966 | 419 |
\textbf{Question 4:}\medskip |
918 | 420 |
|
421 |
\noindent |
|
422 |
\textbf{1)}\; Yes / No\qquad |
|
423 |
\textbf{2)}\; Yes / No\qquad |
|
424 |
\textbf{3)}\; Yes / No\qquad |
|
425 |
\textbf{4)}\; Yes / No\bigskip\bigskip |
|
426 |
||
427 |
||
428 |
\noindent |
|
966 | 429 |
\textbf{Question 5:}\medskip |
918 | 430 |
|
431 |
\def\arraystretch{1.5} |
|
432 |
\begin{tabular}{l|c|c} |
|
433 |
& $\quad{}r_1\quad$ & $\quad{}r_2\quad$\\\hline |
|
434 |
1. & & \\\hline |
|
435 |
2. & & \\\hline |
|
436 |
3. & & \\ |
|
437 |
\end{tabular} |
|
492 | 438 |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
439 |
\end{document} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
440 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
441 |
%%% Local Variables: |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
442 |
%%% mode: latex |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
443 |
%%% TeX-master: t |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
444 |
%%% End: |