author | Christian Urban <christian.urban@kcl.ac.uk> |
Thu, 01 Dec 2022 12:51:49 +0000 | |
changeset 899 | 569a3adb6e01 |
parent 888 | fc812b8f120f |
child 907 | 8160c55991b9 |
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} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
492 | 6 |
\usepackage{array} |
7 |
||
888 | 8 |
%TEST |
492 | 9 |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
\begin{document} |
492 | 11 |
\newcolumntype{C}[1]{>{\centering}m{#1}} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
748 | 13 |
\section*{Coursework 1} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
877 | 15 |
This coursework is worth 5\% and is due on \cwONE{} at 16:00. You are |
748 | 16 |
asked to implement a regular expression matcher and submit a document |
17 |
containing the answers for the questions below. You can do the |
|
18 |
implementation in any programming language you like, but you need to |
|
19 |
submit the source code with which you answered the questions, |
|
20 |
otherwise a mark of 0\% will be awarded. You can submit your answers |
|
21 |
in a txt-file or pdf. Code send as code. Please package everything |
|
22 |
inside a zip-file that creates a directory with the name |
|
23 |
\[\texttt{YournameYourfamilyname}\] |
|
24 |
||
25 |
\noindent on my end. Thanks! |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
351
diff
changeset
|
26 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
351
diff
changeset
|
27 |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
28 |
|
750 | 29 |
\subsubsection*{Disclaimer\alert} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
|
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
31 |
It should be understood that the work you submit represents |
886 | 32 |
your \textbf{own} effort. You have not copied from anyone else. An |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
33 |
exception is the Scala code I showed during the lectures or |
886 | 34 |
uploaded to KEATS, which you can freely use. Do not |
35 |
be tempted to ask Github Copilot for help or do any other |
|
36 |
shenanigans like this!\bigskip |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
37 |
|
512 | 38 |
\noindent |
39 |
If you have any questions, please send me an email in \textbf{good} |
|
40 |
time.\bigskip |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
41 |
|
492 | 42 |
\subsection*{Task} |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
43 |
|
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
44 |
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
|
45 |
derivatives of regular expressions. The implementation should |
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
46 |
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
|
47 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
\[ |
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
49 |
\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
|
50 |
\] |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
\noindent |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
53 |
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
|
54 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
\begin{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
\begin{tabular}{ll} |
492 | 57 |
$[c_1,c_2,\ldots,c_n]$ & a set of characters---for character ranges\\ |
58 |
$r^+$ & one or more times $r$\\ |
|
59 |
$r^?$ & optional $r$\\ |
|
60 |
$r^{\{n\}}$ & exactly $n$-times\\ |
|
61 |
$r^{\{..m\}}$ & zero or more times $r$ but no more than $m$-times\\ |
|
62 |
$r^{\{n..\}}$ & at least $n$-times $r$\\ |
|
63 |
$r^{\{n..m\}}$ & at least $n$-times $r$ but no more than $m$-times\\ |
|
64 |
$\sim{}r$ & not-regular-expression of $r$\\ |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
\end{tabular} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
\end{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
492 | 68 |
\noindent You can assume that $n$ and $m$ are greater or equal than |
69 |
$0$. In the case of $r^{\{n,m\}}$ you can also assume $0 \le n \le m$.\bigskip |
|
70 |
||
71 |
\noindent {\bf Important!} Your implementation should have explicit |
|
567 | 72 |
case classes for the basic regular expressions, but also explicit case |
73 |
classes for |
|
74 |
the extended regular expressions.\footnote{Please call them |
|
75 |
\code{RANGE}, \code{PLUS}, \code{OPTIONAL}, \code{NTIMES}, |
|
630 | 76 |
\code{UPTO}, \code{FROM} and \code{BETWEEN}.} |
77 |
That means do not treat the extended regular expressions |
|
567 | 78 |
by just translating them into the basic ones. See also Question 3, |
79 |
where you are asked to explicitly give the rules for \textit{nullable} |
|
748 | 80 |
and \textit{der} for the extended regular expressions. Something like |
81 |
||
82 |
\[der\,c\,(r^+) \dn der\,c\,(r\cdot r^*)\] |
|
83 |
||
84 |
\noindent is \emph{not} allowed as answer in Question 3 and \emph{not} |
|
85 |
allowed in your code.\medskip |
|
492 | 86 |
|
87 |
\noindent |
|
88 |
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
|
89 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
\begin{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
\begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l} |
492 | 92 |
$L([c_1,c_2,\ldots,c_n])$ & $\dn$ & $\{[c_1], [c_2], \ldots, [c_n]\}$\\ |
93 |
$L(r^+)$ & $\dn$ & $\bigcup_{1\le i}.\;L(r)^i$\\ |
|
94 |
$L(r^?)$ & $\dn$ & $L(r) \cup \{[]\}$\\ |
|
95 |
$L(r^{\{n\}})$ & $\dn$ & $L(r)^n$\\ |
|
96 |
$L(r^{\{..m\}})$ & $\dn$ & $\bigcup_{0\le i \le m}.\;L(r)^i$\\ |
|
97 |
$L(r^{\{n..\}})$ & $\dn$ & $\bigcup_{n\le i}.\;L(r)^i$\\ |
|
98 |
$L(r^{\{n..m\}})$ & $\dn$ & $\bigcup_{n\le i \le m}.\;L(r)^i$\\ |
|
99 |
$L(\sim{}r)$ & $\dn$ & $\Sigma^* - L(r)$ |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
\end{tabular} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
\end{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
|
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
103 |
\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
|
104 |
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
|
105 |
(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
|
106 |
represented by, say, the type \pcode{Char}). So $\sim{}r$ |
492 | 107 |
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
|
108 |
|
492 | 109 |
\noindent |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
110 |
Be careful that your implementation of \textit{nullable} and |
492 | 111 |
\textit{der} satisfies for every regular expression $r$ the following |
545 | 112 |
two properties (see also Question 3): |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
\begin{itemize} |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
115 |
\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
|
116 |
\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
|
117 |
\end{itemize} |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
118 |
|
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
119 |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
|
512 | 121 |
\subsection*{Question 1 (Unmarked)} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
|
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
123 |
What is your King's email address (you will need it in |
772 | 124 |
Question 5)? Also could you please let me know from where you will be mainly |
833 | 125 |
studying? (online / in-person, in London / somewhere else) Thanks! |
545 | 126 |
|
127 |
\subsection*{Question 2 (Unmarked)} |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
128 |
|
833 | 129 |
Can you please also list all programming languages in which you have |
748 | 130 |
already written programs (include only instances where you have spent |
131 |
at least a good working day fiddling with a program)? This is just |
|
132 |
for my curiosity to estimate what your background is. |
|
545 | 133 |
|
134 |
\subsection*{Question 3} |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
135 |
|
473 | 136 |
From the |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
137 |
lectures you have seen the definitions for the functions |
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
138 |
\textit{nullable} and \textit{der} for the basic regular |
718 | 139 |
expressions. Implement and write down the rules for the extended |
567 | 140 |
regular expressions: |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
142 |
\begin{center} |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
143 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
492 | 144 |
$\textit{nullable}([c_1,c_2,\ldots,c_n])$ & $\dn$ & $?$\\ |
145 |
$\textit{nullable}(r^+)$ & $\dn$ & $?$\\ |
|
146 |
$\textit{nullable}(r^?)$ & $\dn$ & $?$\\ |
|
147 |
$\textit{nullable}(r^{\{n\}})$ & $\dn$ & $?$\\ |
|
148 |
$\textit{nullable}(r^{\{..m\}})$ & $\dn$ & $?$\\ |
|
149 |
$\textit{nullable}(r^{\{n..\}})$ & $\dn$ & $?$\\ |
|
150 |
$\textit{nullable}(r^{\{n..m\}})$ & $\dn$ & $?$\\ |
|
151 |
$\textit{nullable}(\sim{}r)$ & $\dn$ & $?$ |
|
152 |
\end{tabular} |
|
153 |
\end{center} |
|
154 |
||
155 |
\begin{center} |
|
156 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
157 |
$der\, c\, ([c_1,c_2,\ldots,c_n])$ & $\dn$ & $?$\\ |
|
158 |
$der\, c\, (r^+)$ & $\dn$ & $?$\\ |
|
159 |
$der\, c\, (r^?)$ & $\dn$ & $?$\\ |
|
160 |
$der\, c\, (r^{\{n\}})$ & $\dn$ & $?$\\ |
|
161 |
$der\, c\, (r^{\{..m\}})$ & $\dn$ & $?$\\ |
|
162 |
$der\, c\, (r^{\{n..\}})$ & $\dn$ & $?$\\ |
|
163 |
$der\, c\, (r^{\{n..m\}})$ & $\dn$ & $?$\\ |
|
164 |
$der\, c\, (\sim{}r)$ & $\dn$ & $?$\\ |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
165 |
\end{tabular} |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
166 |
\end{center} |
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
167 |
|
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
168 |
\noindent |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
169 |
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
|
170 |
|
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
171 |
\begin{itemize} |
395
e57d3d92b856
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
172 |
\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
|
173 |
\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
|
174 |
\end{itemize} |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
175 |
|
473 | 176 |
\noindent |
492 | 177 |
Given the definitions of \textit{nullable} and \textit{der}, it is |
178 |
easy to implement a regular expression matcher. Test your regular |
|
179 |
expression matcher with (at least) the examples: |
|
180 |
||
181 |
||
182 |
\begin{center} |
|
183 |
\def\arraystretch{1.2} |
|
748 | 184 |
\begin{tabular}{@{}r|m{3mm}|m{6mm}|m{6mm}|m{10mm}|m{6mm}|m{10mm}|m{10mm}|m{10mm}} |
718 | 185 |
string & $a^?$ & $\sim{}a$ & $a^{\{3\}}$ & $(a^?)^{\{3\}}$ & $a^{\{..3\}}$ & |
186 |
$(a^?)^{\{..3\}}$ & $a^{\{3..5\}}$ & $(a^?)^{\{3..5\}}$ \\\hline |
|
187 |
$[]$ &&&&&&& \\\hline |
|
188 |
\texttt{a} &&&&&&& \\\hline |
|
189 |
\texttt{aa} &&&&&&& \\\hline |
|
190 |
\texttt{aaa} &&&&&&& \\\hline |
|
191 |
\texttt{aaaaa} &&&&&&& \\\hline |
|
192 |
\texttt{aaaaaa}&&&&&&& \\ |
|
492 | 193 |
\end{tabular} |
194 |
\end{center} |
|
195 |
||
196 |
\noindent |
|
718 | 197 |
Does your matcher produce the expected results? Make sure you |
198 |
also test corner-cases, like $a^{\{0\}}$! |
|
473 | 199 |
|
545 | 200 |
\subsection*{Question 4} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
|
494 | 202 |
As you can see, there are a number of explicit regular expressions |
203 |
that deal with single or several characters, for example: |
|
492 | 204 |
|
205 |
\begin{center} |
|
206 |
\begin{tabular}{ll} |
|
494 | 207 |
$c$ & matches a single character\\ |
208 |
$[c_1,c_2,\ldots,c_n]$ & matches a set of characters---for character ranges\\ |
|
209 |
$\textit{ALL}$ & matches any character |
|
492 | 210 |
\end{tabular} |
211 |
\end{center} |
|
212 |
||
213 |
\noindent |
|
567 | 214 |
The latter is useful for matching any string (for example |
494 | 215 |
by using $\textit{ALL}^*$). In order to avoid having an explicit constructor |
216 |
for each case, we can generalise all these cases and introduce a single |
|
492 | 217 |
constructor $\textit{CFUN}(f)$ where $f$ is a function from characters |
576 | 218 |
to booleans. In Scala code this would look as follows: |
219 |
||
220 |
\begin{lstlisting}[numbers=none] |
|
221 |
abstract class Rexp |
|
222 |
... |
|
223 |
case class CFUN(f: Char => Boolean) extends Rexp |
|
224 |
\end{lstlisting}\smallskip |
|
225 |
||
226 |
\noindent |
|
227 |
The idea is that the function $f$ determines which character(s) |
|
494 | 228 |
are matched, namely those where $f$ returns \texttt{true}. |
229 |
In this question implement \textit{CFUN} and define |
|
492 | 230 |
|
231 |
\begin{center} |
|
232 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
233 |
$\textit{nullable}(\textit{CFUN}(f))$ & $\dn$ & $?$\\ |
|
234 |
$\textit{der}\,c\,(\textit{CFUN}(f))$ & $\dn$ & $?$ |
|
235 |
\end{tabular} |
|
236 |
\end{center} |
|
237 |
||
494 | 238 |
\noindent in your matcher and then also give definitions for |
492 | 239 |
|
240 |
\begin{center} |
|
241 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}} |
|
242 |
$c$ & $\dn$ & $\textit{CFUN}(?)$\\ |
|
243 |
$[c_1,c_2,\ldots,c_n]$ & $\dn$ & $\textit{CFUN}(?)$\\ |
|
244 |
$\textit{ALL}$ & $\dn$ & $\textit{CFUN}(?)$ |
|
245 |
\end{tabular} |
|
246 |
\end{center} |
|
247 |
||
567 | 248 |
\noindent |
249 |
You can either add the constructor $CFUN$ to your implementation in |
|
250 |
Question 3, or you can implement this questions first |
|
251 |
and then use $CFUN$ instead of \code{RANGE} and \code{CHAR} in Question 3. |
|
833 | 252 |
In an ideal world one would do this task first, but this might confuse |
253 |
you with what you need to do in the previous question. |
|
492 | 254 |
|
545 | 255 |
\subsection*{Question 5} |
492 | 256 |
|
257 |
Suppose $[a\mbox{-}z0\mbox{-}9\_\,.\mbox{-}]$ stands for the regular expression |
|
258 |
||
259 |
\[[a,b,c,\ldots,z,0,\dots,9,\_,.,\mbox{-}]\;.\] |
|
260 |
||
261 |
\noindent |
|
262 |
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
|
263 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
264 |
\[ |
492 | 265 |
([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
|
266 |
\] |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
267 |
|
567 | 268 |
\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
|
269 |
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
|
270 |
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
|
271 |
following 7 simplification rules: |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
272 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
273 |
\begin{center} |
272
1446bc47a294
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
260
diff
changeset
|
274 |
\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
|
275 |
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
276 |
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
277 |
$r \cdot \ONE$ & $\mapsto$ & $r$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
278 |
$\ONE \cdot r$ & $\mapsto$ & $r$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
279 |
$r + \ZERO$ & $\mapsto$ & $r$\\ |
7611ace6a93b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
418
diff
changeset
|
280 |
$\ZERO + r$ & $\mapsto$ & $r$\\ |
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
281 |
$r + r$ & $\mapsto$ & $r$\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
282 |
\end{tabular} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
283 |
\end{center} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
284 |
|
418
010c5a03dca2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
395
diff
changeset
|
285 |
\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
|
286 |
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
|
287 |
should use the infix notation $+$, $\cdot$, $^*$ and so on, |
567 | 288 |
instead of raw code.\bigskip |
289 |
||
290 |
||
291 |
\subsection*{Question 6} |
|
292 |
||
492 | 293 |
Implement the simplification rules in your regular expression matcher. |
294 |
Consider the regular expression $/ \cdot * \cdot |
|
295 |
(\sim{}(\textit{ALL}^* \cdot * \cdot / \cdot \textit{ALL}^*)) \cdot * |
|
630 | 296 |
\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
|
297 |
this regular expression. Answer yes or no. |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
298 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
299 |
\begin{enumerate} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
300 |
\item \texttt{"/**/"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
301 |
\item \texttt{"/*foobar*/"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
302 |
\item \texttt{"/*test*/test*/"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
303 |
\item \texttt{"/*test/*test*/"} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
304 |
\end{enumerate} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
305 |
|
567 | 306 |
\subsection*{Question 7} |
512 | 307 |
|
308 |
Let $r_1$ be the regular expression $a\cdot a\cdot a$ and $r_2$ be |
|
748 | 309 |
$(a^{\{19,19\}}) \cdot (a^?)$.\medskip |
310 |
||
311 |
\noindent |
|
312 |
Decide whether the following three |
|
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
313 |
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
|
314 |
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
|
315 |
with yes or no. \medskip |
130
5c4998375c46
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
316 |
|
5c4998375c46
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
317 |
\noindent |
259
e5f4b8ff23b8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
253
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
to not introducing any other character. |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
321 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
322 |
\begin{enumerate} |
492 | 323 |
\setcounter{enumi}{4} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
324 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
325 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
326 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
327 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
328 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
329 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} |
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
330 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
331 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
133
diff
changeset
|
332 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} |
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
333 |
\end{enumerate} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
334 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
335 |
|
492 | 336 |
|
127
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
337 |
\end{document} |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
338 |
|
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
339 |
%%% Local Variables: |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
340 |
%%% mode: latex |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
341 |
%%% TeX-master: t |
41ef073ac6c4
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
342 |
%%% End: |