6
|
1 |
\documentclass{article}
|
62
|
2 |
\usepackage{../style}
|
6
|
3 |
%%\usepackage{../langs}
|
|
4 |
|
|
5 |
\begin{document}
|
|
6 |
|
62
|
7 |
\section*{Coursework 8 (Scala, Regular Expressions}
|
6
|
8 |
|
62
|
9 |
This coursework is worth 10\% and is due on XXXX at
|
6
|
10 |
16:00. You are asked to implement a regular expression matcher.
|
|
11 |
|
62
|
12 |
Make sure the files
|
|
13 |
you submit can be processed by just calling \texttt{scala
|
|
14 |
<<filename.scala>>}.\bigskip
|
|
15 |
|
|
16 |
\noindent
|
|
17 |
\textbf{Important:} Do not use any mutable data structures in your
|
|
18 |
submissions! They are not needed. This excluded the use of
|
|
19 |
\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
|
|
20 |
code! It has a different meaning in Scala, than in Java.
|
|
21 |
Do not use \texttt{var}! This declares a mutable variable. Feel free to
|
|
22 |
copy any code you need from files \texttt{knight1.scala},
|
|
23 |
\texttt{knight2.scala} and \texttt{knight3.scala}. Make sure the
|
|
24 |
functions you submit are defined on the ``top-level'' of Scala, not
|
|
25 |
inside a class or object.
|
6
|
26 |
|
|
27 |
|
62
|
28 |
\subsection*{Disclaimer}
|
6
|
29 |
|
|
30 |
It should be understood that the work you submit represents
|
|
31 |
your own effort. You have not copied from anyone else. An
|
|
32 |
exception is the Scala code I showed during the lectures or
|
|
33 |
uploaded to KEATS, which you can freely use.\bigskip
|
|
34 |
|
|
35 |
|
|
36 |
\subsubsection*{Task}
|
|
37 |
|
|
38 |
The task is to implement a regular expression matcher based on
|
|
39 |
derivatives of regular expressions. The implementation should
|
|
40 |
be able to deal with the usual (basic) regular expressions
|
|
41 |
|
|
42 |
\begin{center}
|
|
43 |
\begin{tabular}{lcll}
|
|
44 |
$r$ & $::=$ & $\ZERO$ & cannot match anything\\
|
|
45 |
& $|$ & $\ONE$ & can only match the empty string\\
|
|
46 |
& $|$ & $c$ & can match a character $c$\\
|
|
47 |
& $|$ & $r_1 + r_2$ & can match either with $r_1$ or with $r_2$\\
|
|
48 |
& $|$ & $r_1 \cdot r_2$ & can match first with $r_1$ and then with $r_2$\\
|
|
49 |
& $|$ & $r^*$ & can match zero or more times $r$\\
|
|
50 |
& $|$ & $r^{\{\uparrow n\}}$ & can match zero upto $n$ times $r$\\
|
|
51 |
& $|$ & $r^{\{n\}}$ & can match exactly $n$ times $r$\\
|
|
52 |
\end{tabular}
|
|
53 |
\end{center}
|
|
54 |
|
|
55 |
\noindent
|
|
56 |
Implement a function called \textit{nullable} by recursion over
|
|
57 |
regular expressions:
|
|
58 |
|
|
59 |
\begin{center}
|
|
60 |
\begin{tabular}{lcl}
|
|
61 |
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\
|
|
62 |
$\textit{nullable}(\ONE)$ & $\dn$ & $\textit{true}$\\
|
|
63 |
$\textit{nullable}(c)$ & $\dn$ & $\textit{false}$\\
|
|
64 |
$\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\
|
|
65 |
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\
|
|
66 |
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\
|
|
67 |
$\textit{nullable}(r^{\{\uparrow n\}})$ & $\dn$ & $\textit{true}$\\
|
|
68 |
$\textit{nullable}(r^{\{n\}})$ & $\dn$ &
|
|
69 |
$\textit{if}\;n = 0\; \textit{then} \; \textit{true} \; \textit{else} \; \textit{nullable}(r)$\\
|
|
70 |
\end{tabular}
|
|
71 |
\end{center}
|
|
72 |
|
|
73 |
\begin{center}
|
|
74 |
\begin{tabular}{lcl}
|
|
75 |
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
|
|
76 |
$\textit{der}\;c\;(\ONE)$ & $\dn$ & $\ZERO$\\
|
|
77 |
$\textit{der}\;c\;(d)$ & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\
|
|
78 |
$\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\
|
|
79 |
$\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\
|
|
80 |
& & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\
|
|
81 |
& & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\
|
|
82 |
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
|
|
83 |
$\textit{der}\;c\;(r^{\{\uparrow n\}})$ & $\dn$ & $\textit{if}\;n = 0\;\textit{then}\;\ZERO\;\text{else}\;
|
|
84 |
(\textit{der}\;c\;r)\cdot (r^{\{\uparrow n-1\}})$\\
|
|
85 |
$\textit{der}\;c\;(r^{\{n\}})$ & $\dn$ &
|
|
86 |
$\textit{if}\;n = 0\; \textit{then} \; \ZERO\; \textit{else}\;$\\
|
|
87 |
& & $\textit{if} \;\textit{nullable}(r)\;\textit{then}\;(\textit{der}\;c\;r)\cdot (r^{\{\uparrow n-1\}})$\\
|
|
88 |
& & $\textit{else}\;(\textit{der}\;c\;r)\cdot (r^{\{n-1\}})$
|
|
89 |
\end{tabular}
|
|
90 |
\end{center}
|
|
91 |
|
|
92 |
|
|
93 |
Be careful that your implementation of \textit{nullable} and
|
|
94 |
\textit{der}\;c\; satisfies for every $r$ the following two
|
|
95 |
properties (see also Question 2):
|
|
96 |
|
|
97 |
\begin{itemize}
|
|
98 |
\item $\textit{nullable}(r)$ if and only if $[]\in L(r)$
|
|
99 |
\item $L(der\,c\,r) = Der\,c\,(L(r))$
|
|
100 |
\end{itemize}
|
|
101 |
|
|
102 |
\noindent {\bf Important!} Your implementation should have
|
|
103 |
explicit cases for the basic regular expressions, but also
|
|
104 |
explicit cases for the extended regular expressions. That
|
|
105 |
means do not treat the extended regular expressions by just
|
|
106 |
translating them into the basic ones. See also Question 2,
|
|
107 |
where you are asked to explicitly give the rules for
|
|
108 |
\textit{nullable} and \textit{der}\;c\; for the extended regular
|
|
109 |
expressions.
|
|
110 |
|
|
111 |
|
|
112 |
\subsection*{Question 1}
|
|
113 |
|
|
114 |
What is your King's email address (you will need it in
|
|
115 |
Question 3)?
|
|
116 |
|
|
117 |
\subsection*{Question 2}
|
|
118 |
|
|
119 |
This question does not require any implementation. From the
|
|
120 |
lectures you have seen the definitions for the functions
|
|
121 |
\textit{nullable} and \textit{der}\;c\; for the basic regular
|
|
122 |
expressions. Give the rules for the extended regular
|
|
123 |
expressions:
|
|
124 |
|
|
125 |
\begin{center}
|
|
126 |
\begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
|
|
127 |
$\textit{nullable}([c_1 c_2 \ldots c_n])$ & $\dn$ & $?$\\
|
|
128 |
$\textit{nullable}(r^+)$ & $\dn$ & $?$\\
|
|
129 |
$\textit{nullable}(r^?)$ & $\dn$ & $?$\\
|
|
130 |
$\textit{nullable}(r^{\{n,m\}})$ & $\dn$ & $?$\\
|
|
131 |
$\textit{nullable}(\sim{}r)$ & $\dn$ & $?$\medskip\\
|
|
132 |
$der\, c\, ([c_1 c_2 \ldots c_n])$ & $\dn$ & $?$\\
|
|
133 |
$der\, c\, (r^+)$ & $\dn$ & $?$\\
|
|
134 |
$der\, c\, (r^?)$ & $\dn$ & $?$\\
|
|
135 |
$der\, c\, (r^{\{n,m\}})$ & $\dn$ & $?$\\
|
|
136 |
$der\, c\, (\sim{}r)$ & $\dn$ & $?$\\
|
|
137 |
\end{tabular}
|
|
138 |
\end{center}
|
|
139 |
|
|
140 |
\noindent
|
|
141 |
Remember your definitions have to satisfy the two properties
|
|
142 |
|
|
143 |
\begin{itemize}
|
|
144 |
\item $\textit{nullable}(r)$ if and only if $[]\in L(r)$
|
|
145 |
\item $L(der\,c\,r)) = Der\,c\,(L(r))$
|
|
146 |
\end{itemize}
|
|
147 |
|
|
148 |
\subsection*{Question 3}
|
|
149 |
|
|
150 |
Implement the following regular expression for email addresses
|
|
151 |
|
|
152 |
\[
|
|
153 |
([a\mbox{-}z0\mbox{-}9\_\!\_\,.-]^+)\cdot @\cdot ([a\mbox{-}z0\mbox{-}9\,.-]^+)\cdot .\cdot ([a\mbox{-}z\,.]^{\{2,6\}})
|
|
154 |
\]
|
|
155 |
|
|
156 |
\noindent and calculate the derivative according to your email
|
|
157 |
address. When calculating the derivative, simplify all regular
|
|
158 |
expressions as much as possible by applying the
|
|
159 |
following 7 simplification rules:
|
|
160 |
|
|
161 |
\begin{center}
|
|
162 |
\begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}ll}
|
|
163 |
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\
|
|
164 |
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\
|
|
165 |
$r \cdot \ONE$ & $\mapsto$ & $r$\\
|
|
166 |
$\ONE \cdot r$ & $\mapsto$ & $r$\\
|
|
167 |
$r + \ZERO$ & $\mapsto$ & $r$\\
|
|
168 |
$\ZERO + r$ & $\mapsto$ & $r$\\
|
|
169 |
$r + r$ & $\mapsto$ & $r$\\
|
|
170 |
\end{tabular}
|
|
171 |
\end{center}
|
|
172 |
|
|
173 |
\noindent Write down your simplified derivative in a readable
|
|
174 |
notation using parentheses where necessary. That means you
|
|
175 |
should use the infix notation $+$, $\cdot$, $^*$ and so on,
|
|
176 |
instead of code.
|
|
177 |
|
|
178 |
\subsection*{Question 4}
|
|
179 |
|
|
180 |
Suppose \textit{[a-z]} stands for the range regular expression
|
|
181 |
$[a,b,c,\ldots,z]$. Consider the regular expression $/ \cdot * \cdot
|
|
182 |
(\sim{}([a\mbox{-}z]^* \cdot * \cdot / \cdot [a\mbox{-}z]^*)) \cdot *
|
|
183 |
\cdot /$ and decide wether the following four strings are matched by
|
|
184 |
this regular expression. Answer yes or no.
|
|
185 |
|
|
186 |
\begin{enumerate}
|
|
187 |
\item \texttt{"/**/"}
|
|
188 |
\item \texttt{"/*foobar*/"}
|
|
189 |
\item \texttt{"/*test*/test*/"}
|
|
190 |
\item \texttt{"/*test/*test*/"}
|
|
191 |
\end{enumerate}
|
|
192 |
|
|
193 |
\noindent
|
|
194 |
Also test your regular expression matcher with the regular
|
|
195 |
expression $a^{\{3,5\}}$ and the strings
|
|
196 |
|
|
197 |
\begin{enumerate}
|
|
198 |
\setcounter{enumi}{4}
|
|
199 |
\item \texttt{aa}
|
|
200 |
\item \texttt{aaa}
|
|
201 |
\item \texttt{aaaaa}
|
|
202 |
\item \texttt{aaaaaa}
|
|
203 |
\end{enumerate}
|
|
204 |
|
|
205 |
\noindent
|
|
206 |
Does your matcher produce the expected results?
|
|
207 |
|
|
208 |
\subsection*{Question 5}
|
|
209 |
|
|
210 |
Let $r_1$ be the regular expression $a\cdot a\cdot a$ and $r_2$ be
|
|
211 |
$(a^{\{19,19\}}) \cdot (a^?)$. Decide whether the following three
|
|
212 |
strings consisting of $a$s only can be matched by $(r_1^+)^+$.
|
|
213 |
Similarly test them with $(r_2^+)^+$. Again answer in all six cases
|
|
214 |
with yes or no. \medskip
|
|
215 |
|
|
216 |
\noindent
|
|
217 |
These are strings are meant to be entirely made up of $a$s. Be careful
|
|
218 |
when copy-and-pasting the strings so as to not forgetting any $a$ and
|
|
219 |
to not introducing any other character.
|
|
220 |
|
|
221 |
\begin{enumerate}
|
|
222 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
|
|
223 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
|
|
224 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
|
|
225 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
|
|
226 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
|
|
227 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
|
|
228 |
\item \texttt{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
|
|
229 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
|
|
230 |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
|
|
231 |
\end{enumerate}
|
|
232 |
|
|
233 |
|
|
234 |
\end{document}
|
|
235 |
|
|
236 |
%%% Local Variables:
|
|
237 |
%%% mode: latex
|
|
238 |
%%% TeX-master: t
|
|
239 |
%%% End:
|