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