94
|
1 |
\documentclass[a4paper,UKenglish]{lipics}
|
|
2 |
\usepackage{graphic}
|
|
3 |
\usepackage{data}
|
|
4 |
\usepackage{tikz-cd}
|
|
5 |
%\usepackage{algorithm}
|
|
6 |
\usepackage{amsmath}
|
|
7 |
\usepackage[noend]{algpseudocode}
|
|
8 |
\usepackage{enumitem}
|
|
9 |
\usepackage{nccmath}
|
|
10 |
|
|
11 |
\definecolor{darkblue}{rgb}{0,0,0.6}
|
|
12 |
\hypersetup{colorlinks=true,allcolors=darkblue}
|
|
13 |
\newcommand{\comment}[1]%
|
|
14 |
{{\color{red}$\Rightarrow$}\marginpar{\raggedright\small{\bf\color{red}#1}}}
|
|
15 |
|
|
16 |
% \documentclass{article}
|
|
17 |
%\usepackage[utf8]{inputenc}
|
|
18 |
%\usepackage[english]{babel}
|
|
19 |
%\usepackage{listings}
|
|
20 |
% \usepackage{amsthm}
|
|
21 |
%\usepackage{hyperref}
|
|
22 |
% \usepackage[margin=0.5in]{geometry}
|
|
23 |
%\usepackage{pmboxdraw}
|
|
24 |
|
|
25 |
\title{POSIX Regular Expression Matching and Lexing}
|
|
26 |
\author{Chengsong Tan}
|
|
27 |
\affil{King's College London\\
|
|
28 |
London, UK\\
|
|
29 |
\texttt{chengsong.tan@kcl.ac.uk}}
|
|
30 |
\authorrunning{Chengsong Tan}
|
|
31 |
\Copyright{Chengsong Tan}
|
|
32 |
|
|
33 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
|
|
34 |
\newcommand{\ZERO}{\mbox{\bf 0}}
|
|
35 |
\newcommand{\ONE}{\mbox{\bf 1}}
|
101
|
36 |
\def\erase{\textit{erase}}
|
94
|
37 |
\def\bders{\textit{bders}}
|
|
38 |
\def\lexer{\mathit{lexer}}
|
|
39 |
\def\blexer{\textit{blexer}}
|
|
40 |
\def\blexers{\mathit{blexer\_simp}}
|
95
|
41 |
\def\simp{\mathit{simp}}
|
94
|
42 |
\def\mkeps{\mathit{mkeps}}
|
|
43 |
\def\bmkeps{\textit{bmkeps}}
|
|
44 |
\def\inj{\mathit{inj}}
|
|
45 |
\def\Empty{\mathit{Empty}}
|
|
46 |
\def\Left{\mathit{Left}}
|
|
47 |
\def\Right{\mathit{Right}}
|
|
48 |
\def\Stars{\mathit{Stars}}
|
|
49 |
\def\Char{\mathit{Char}}
|
|
50 |
\def\Seq{\mathit{Seq}}
|
|
51 |
\def\Der{\mathit{Der}}
|
|
52 |
\def\nullable{\mathit{nullable}}
|
|
53 |
\def\Z{\mathit{Z}}
|
|
54 |
\def\S{\mathit{S}}
|
|
55 |
\def\flex{\textit{flex}}
|
|
56 |
\def\rup{r^\uparrow}
|
|
57 |
\def\retrieve{\textit{retrieve}}
|
|
58 |
\def\AALTS{\textit{AALTS}}
|
|
59 |
\def\AONE{\textit{AONE}}
|
|
60 |
%\theoremstyle{theorem}
|
|
61 |
%\newtheorem{theorem}{Theorem}
|
|
62 |
%\theoremstyle{lemma}
|
|
63 |
%\newtheorem{lemma}{Lemma}
|
|
64 |
%\newcommand{\lemmaautorefname}{Lemma}
|
|
65 |
%\theoremstyle{definition}
|
|
66 |
%\newtheorem{definition}{Definition}
|
|
67 |
\algnewcommand\algorithmicswitch{\textbf{switch}}
|
|
68 |
\algnewcommand\algorithmiccase{\textbf{case}}
|
|
69 |
\algnewcommand\algorithmicassert{\texttt{assert}}
|
|
70 |
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}%
|
|
71 |
% New "environments"
|
|
72 |
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
|
|
73 |
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
|
|
74 |
\algtext*{EndSwitch}%
|
|
75 |
\algtext*{EndCase}%
|
|
76 |
|
|
77 |
|
|
78 |
\begin{document}
|
|
79 |
|
|
80 |
\maketitle
|
|
81 |
|
|
82 |
\begin{abstract}
|
|
83 |
Brzozowski introduced in 1964 a beautifully simple algorithm for
|
|
84 |
regular expression matching based on the notion of derivatives of
|
|
85 |
regular expressions. In 2014, Sulzmann and Lu extended this
|
|
86 |
algorithm to not just give a YES/NO answer for whether or not a
|
|
87 |
regular expression matches a string, but in case it does also
|
|
88 |
answers with \emph{how} it matches the string. This is important for
|
|
89 |
applications such as lexing (tokenising a string). The problem is to
|
|
90 |
make the algorithm by Sulzmann and Lu fast on all inputs without
|
100
|
91 |
breaking its correctness. Being fast depends on a complete set of
|
|
92 |
simplification rules, some of which
|
|
93 |
have been put forward by Sulzmann and Lu. We have extended their
|
|
94 |
rules in order to obtain a tight bound on size of regular expressions.
|
|
95 |
We have tested the correctness of these extended rules, but have not
|
|
96 |
formally established their correctness. We also have not yet looked
|
|
97 |
at extended regular expressions, such as bounded repetitions,
|
94
|
98 |
negation and back-references.
|
|
99 |
\end{abstract}
|
|
100 |
|
|
101 |
\section{Introduction}
|
|
102 |
|
100
|
103 |
While we believe derivatives of regular expressions is a beautiful
|
|
104 |
concept (interms of ease to implementing them in functional programming
|
|
105 |
language and ease to reason about them formally), they have one major
|
|
106 |
drawback: every derivative step can make regular expressions grow
|
|
107 |
drastically in size. This in turn has negative effects on the runtime of
|
|
108 |
the corresponding lexing algorithms. Consider for example the regular
|
|
109 |
expression $(a+aa)^*$ and the short string $aaaaaaaaaaaa$. The size of
|
|
110 |
the corresponding derivative is already 8668 node assuming the derivatives
|
|
111 |
is seen as a tree. The reason for the poor runtime of the lexing algorithms is
|
|
112 |
that they need to traverse such trees over and over again. The solution is to
|
|
113 |
find a complete set of simplification rules that keep the sizes of derivatives
|
|
114 |
uniformly small.
|
94
|
115 |
|
100
|
116 |
For reasons beyond this report, it turns out that a complete set of
|
|
117 |
simplification rules depend on values being encoded as bitsequences.
|
|
118 |
(Vlue are the results of the lexing algorithms generate; they encode how
|
|
119 |
a regular expression matched a string.) We already know that the lexing
|
|
120 |
algorithm \emph{without} simplification is correct. Therefore in the
|
|
121 |
past 6 months we were trying to prove that the algorithm using bitsequences plus
|
|
122 |
our simplification rules is correct. Formally this amounts to show that
|
|
123 |
|
|
124 |
\begin{equation}\label{mainthm}
|
|
125 |
\blexers \; r \; s = \blexer \;r\;s
|
|
126 |
\end{equation}
|
|
127 |
|
94
|
128 |
\noindent
|
100
|
129 |
whereby $\blexers$ simplifies (makes derivatives smaller) in each step,
|
|
130 |
whereas with $\blexer$ the size can grow exponentially. This would be an
|
|
131 |
important milestone, because we already have a very good idea how to
|
|
132 |
establish that our set our simplification rules keeps the size below a
|
|
133 |
relatively tight bound.
|
|
134 |
|
|
135 |
In order to prove the main theorem \eqref{mainthm}, we need to prove the
|
|
136 |
two functions produce the same output. The definition of these functions
|
|
137 |
is shown below.
|
|
138 |
|
94
|
139 |
\begin{center}
|
|
140 |
\begin{tabular}{lcl}
|
|
141 |
$\textit{blexer}\;r\,s$ & $\dn$ &
|
|
142 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\
|
|
143 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
144 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
145 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
146 |
\end{tabular}
|
|
147 |
\end{center}
|
|
148 |
|
100
|
149 |
\begin{center}
|
94
|
150 |
\begin{tabular}{lcl}
|
100
|
151 |
$\blexers \; r \, s$ &$\dn$ &
|
|
152 |
$\textit{let} \; a = (r^\uparrow)\backslash_{simp}\, s\; \textit{in}$\\
|
|
153 |
& & $\; \; \textit{if} \; \textit{bnullable}(a)$\\
|
|
154 |
& & $\; \; \textit{then} \; \textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
155 |
& & $\;\; \textit{else}\;\textit{None}$
|
94
|
156 |
\end{tabular}
|
|
157 |
\end{center}
|
|
158 |
\noindent
|
100
|
159 |
In these definitions $(r^\uparrow)$ is a kind of coding function that is the
|
|
160 |
same in each case, similarly the decode and the \textit{bmkeps}
|
|
161 |
functions. Our main theorem \eqref{mainthm} therefore boils down to
|
|
162 |
proving the following two propositions (depending on which branch the
|
|
163 |
if-else clause takes). It establishes how the derivatives \emph{with}
|
|
164 |
simplification do not change the computed result:
|
94
|
165 |
|
|
166 |
\begin{itemize}
|
100
|
167 |
\item{} If a string $s$ is in the language of $L(r)$, then \\
|
|
168 |
$\textit{bmkeps} (r^\uparrow)\backslash_{simp}\,s = \textit{bmkeps} (r^\uparrow)\backslash s$,\\
|
|
169 |
\item{} If a string $s$ is in the language $L(r)$, then
|
|
170 |
$\rup \backslash_{simp} \,s$ is not nullable.
|
94
|
171 |
\end{itemize}
|
100
|
172 |
|
94
|
173 |
\noindent
|
100
|
174 |
We have already proved in Isabelle the second part. This is actually not
|
|
175 |
too difficult because we can show that simplification does not change
|
|
176 |
the language of regular expressions. If we can prove the first case,
|
|
177 |
that is the bitsequence algorithm with simplification produces the same
|
|
178 |
result as the one without simplification, then we are done.
|
|
179 |
Unfortunately that part requires more effort, because simplification does not
|
|
180 |
only.need to \emph{not} change the language, but also not change
|
|
181 |
the value (computed result).
|
|
182 |
|
|
183 |
\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
|
|
184 |
Do you want to keep this? You essentially want to say that the old
|
|
185 |
method used retrieve, which unfortunately cannot be adopted to
|
|
186 |
the simplification rules. You could just say that and give an example.
|
|
187 |
However you have to think about how you give the example....nobody knows
|
|
188 |
about AZERO etc yet. Maybe it might be better to use normal regexes
|
|
189 |
like $a + aa$, but annotate bitsequences as subscript like $_1(_0a + _1aa)$.
|
|
190 |
|
|
191 |
\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
|
|
192 |
REPLY:\\
|
|
193 |
Yes, I am essentially saying that the old method
|
|
194 |
cannot be adopted without adjustments.
|
|
195 |
But this does not mean we should skip
|
|
196 |
the proof of the bit-coded algorithm
|
|
197 |
as it is still the main direction we are looking into
|
|
198 |
to prove things. We are trying to modify
|
|
199 |
the old proof to suit our needs, but not give
|
|
200 |
up it totally, that is why i believe the old
|
|
201 |
proof is fundamental in understanding
|
|
202 |
what we are doing in the past 6 months.
|
|
203 |
|
|
204 |
\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
|
|
205 |
|
94
|
206 |
The correctness proof of
|
|
207 |
\begin{center}
|
|
208 |
$\blexer \; r^\uparrow s = \lexer \;r \;s$
|
|
209 |
\end{center}
|
|
210 |
\noindent
|
|
211 |
might provide us insight into proving
|
|
212 |
\begin{center}
|
|
213 |
$\blexer \; r^\uparrow \;s = \blexers \; r^\uparrow \;s$
|
|
214 |
\end{center}
|
|
215 |
\noindent
|
|
216 |
(that is also
|
|
217 |
why we say the new proof builds on the older one).
|
|
218 |
The proof defined the function $\flex$ as another way of
|
|
219 |
expressing the $\lexer$ function:
|
|
220 |
\begin{center}
|
|
221 |
$\lexer \;r\; s = \flex \;\textit{id} \; r\;s \;(\mkeps \; r\backslash s)$
|
|
222 |
\end{center}.
|
|
223 |
\noindent
|
|
224 |
(proof for the above equality will be explained later)
|
|
225 |
The definition of $flex$ is as follows:
|
|
226 |
\begin{center}
|
|
227 |
\begin{tabular}{lcl}
|
|
228 |
$\textit{flex} \;r\; f\; (c\!::\!s) $ & $\dn$ & $\textit{flex} \; (r\backslash c) \;(\lambda v. f (inj \; r \; c \; v)) \;s$ \\
|
|
229 |
$\textit{flex} \;r\; f\; [\,] $ & $\dn$ & $f$
|
|
230 |
\end{tabular}
|
|
231 |
\end{center}
|
|
232 |
\noindent
|
|
233 |
here $\flex$ essentially does lexing by
|
|
234 |
stacking up injection functions while doing derivatives,
|
|
235 |
explicitly showing the order of characters being
|
|
236 |
injected back in each step.
|
|
237 |
With $\flex$ we can write $\lexer$ this way:
|
|
238 |
\begin{center}
|
|
239 |
$\lexer \;r\; s = \flex \;id \; r\;s \;(\mkeps r\backslash s)$
|
|
240 |
\end{center}
|
|
241 |
\noindent
|
|
242 |
$\flex$ focuses on
|
|
243 |
the injections instead
|
|
244 |
of the derivatives ,
|
|
245 |
compared
|
|
246 |
to the original definition of $\lexer$,
|
|
247 |
which puts equal amount of emphasis on
|
|
248 |
injection and derivative with respect to each character:
|
|
249 |
\begin{center}
|
|
250 |
\begin{tabular}{lcl}
|
|
251 |
$\textit{lexer} \; r\; (c\!::\!s) $ & $\dn$ & $\textit{case} \; \lexer \; (r\backslash c) \;s \; \textit{of}$ \\
|
|
252 |
& & $\textit{None} \; \Longrightarrow \; \textit{None}$\\
|
|
253 |
& & $\textbar \; v \; \Longrightarrow \; \inj \; r\;c\;v$\\
|
|
254 |
$\textit{lexer} \; r\; [\,] $ & $\dn$ & $\textit{if} \; \nullable (r) \; \textit{then} \; \mkeps (r) \; \textit{else} \;None$
|
|
255 |
\end{tabular}
|
|
256 |
\end{center}
|
|
257 |
\noindent
|
|
258 |
Using this feature of $\flex$ we can rewrite the lexing
|
|
259 |
$w.r.t \; s @ [c]$ in term of lexing
|
|
260 |
$w.r.t \; s$:
|
|
261 |
\begin{center}
|
|
262 |
$\flex \; r \; id \; (s@[c]) \; v = \flex \; r \; id \; s \; (inj \; (r\backslash s) \; c\; v)$.
|
|
263 |
\end{center}
|
|
264 |
\noindent
|
|
265 |
this allows us to use
|
|
266 |
the inductive hypothesis to get
|
|
267 |
\begin{center}
|
|
268 |
$ \flex \; r\; id\; (s@[c])\; v = \textit{decode} \;( \textit{retrieve}\; (\rup \backslash s) \; (\inj \; (r\backslash s) \;c\;v)\;) r$
|
|
269 |
\end{center}
|
|
270 |
\noindent
|
|
271 |
By using a property of retrieve we have the $\textit{RHS}$ of the above equality is
|
|
272 |
$decode (retrieve (r^\uparrow \backslash(s @ [c])) v) r$, and this gives the
|
|
273 |
main lemma result:
|
|
274 |
\begin{center}
|
|
275 |
$ \flex \;r\; id \; (s@[c]) \; v =\textit{decode}(\textit{retrieve} (\rup \backslash (s@[c])) \;v) r$
|
|
276 |
\end{center}
|
|
277 |
\noindent
|
|
278 |
To use this lemma result for our
|
|
279 |
correctness proof, simply replace the $v$ in the
|
|
280 |
$\textit{RHS}$ of the above equality with
|
|
281 |
$\mkeps\;(r\backslash (s@[c]))$, and apply the lemma that
|
|
282 |
|
|
283 |
\begin{center}
|
|
284 |
$\textit{decode} \; \bmkeps \; \rup \; r = \textit{decode} \; (\textit{retrieve} \; \rup \; \mkeps(r)) \;r$
|
|
285 |
\end{center}
|
|
286 |
\noindent
|
|
287 |
We get the correctness of our bit-coded algorithm:
|
|
288 |
\begin{center}
|
|
289 |
$\flex \;r\; id \; s \; (\mkeps \; r\backslash s) = \textit{decode} \; \bmkeps \; \rup\backslash s \; r$
|
|
290 |
\end{center}
|
|
291 |
\noindent
|
|
292 |
The bridge between the above chain of equalities
|
|
293 |
is the use of $\retrieve$,
|
|
294 |
if we want to use a similar technique for the
|
|
295 |
simplified version of algorithm,
|
|
296 |
we face the problem that in the above
|
|
297 |
equalities,
|
|
298 |
$\retrieve \; a \; v$ is not always defined.
|
|
299 |
for example,
|
100
|
300 |
$\retrieve \; _0(_1a+_0a) \; \Left(\Empty)$
|
101
|
301 |
is defined, but not $\retrieve \; (_{01}a) \;\Left(\Empty)$,
|
94
|
302 |
though we can extract the same POSIX
|
|
303 |
bits from the two annotated regular expressions.
|
95
|
304 |
The latter might occur when we try to retrieve from
|
|
305 |
a simplified regular expression using the same value
|
|
306 |
as the unsimplified one.
|
|
307 |
This is because $\Left(\Empty)$ corresponds to
|
101
|
308 |
the regular expression structure $\ONE+r_2$ instead of
|
|
309 |
$\ONE$.
|
94
|
310 |
That means, if we
|
|
311 |
want to prove that
|
|
312 |
\begin{center}
|
|
313 |
$\textit{decode} \; \bmkeps \; \rup\backslash s \; r = \textit{decode} \; \bmkeps \; \rup\backslash_{simp} s \; r$
|
|
314 |
\end{center}
|
|
315 |
\noindent
|
|
316 |
holds by using $\retrieve$,
|
|
317 |
we probably need to prove an equality like below:
|
|
318 |
\begin{center}
|
|
319 |
%$\retrieve \; \rup\backslash_{simp} s \; \mkeps(r\backslash_{simp} s)=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
|
101
|
320 |
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(f(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
|
94
|
321 |
\end{center}
|
|
322 |
\noindent
|
101
|
323 |
$f$ rectifies $r\backslash s$ so the value $\mkeps(f(r\backslash s))$ becomes
|
|
324 |
something simpler
|
94
|
325 |
to make the retrieve function defined.\\
|
95
|
326 |
One way to do this is to prove the following:
|
|
327 |
\begin{center}
|
|
328 |
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(\simp(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
|
|
329 |
\end{center}
|
|
330 |
\noindent
|
101
|
331 |
The reason why we choose $\simp$ as $f$ is because
|
|
332 |
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash \, s)$
|
|
333 |
have the same shape:
|
|
334 |
\begin{center}
|
|
335 |
$\erase (\rup\backslash_{simp} \, s) = \erase(\simp(\rup\backslash s))$
|
|
336 |
\end{center}
|
|
337 |
|
|
338 |
\noindent
|
|
339 |
$\erase$ in the above equality means to remove the bit-codes
|
|
340 |
in an annotated regular expression and only keep the original
|
|
341 |
regular expression(just like "erasing" the bits). Its definition is omitted.
|
|
342 |
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash s)$
|
|
343 |
are very closely related, but not identical.
|
|
344 |
For example, let $r$ be the regular expression
|
|
345 |
$(a+b)(a+a*)$ and $s$ be the string $aa$, then
|
|
346 |
\begin{center}
|
|
347 |
$\rup\backslash_{simp} \, s$ is equal to $_0(_0\ONE +_{11}a^*)$
|
|
348 |
\end{center}
|
|
349 |
\noindent
|
|
350 |
whereas
|
|
351 |
\begin{center}
|
|
352 |
$\rup\backslash \, s$ is equal to $(_{00}\ONE +_{011}a^*)$
|
|
353 |
\end{center}
|
|
354 |
\noindent
|
|
355 |
Two "rules" might be inferred from the above example.
|
|
356 |
First, after erasing the bits the two regular expressions
|
|
357 |
are exactly the same: both become $1+a^*$. Here the
|
|
358 |
function $\simp$ exhibits the "once equals many times"
|
|
359 |
property: one simplification in the end causes the
|
|
360 |
same regular expression structure as
|
|
361 |
successive simplifications done alongside derivatives.
|
|
362 |
Second, the bit-codes are different, but they are essentially
|
|
363 |
the same: if we push the outmost bits ${\bf_0}(_0\ONE +_{11}a^*)$ of $\rup\backslash_{simp} \, s$
|
|
364 |
inside then we get $(_{00}\ONE +_{011}a^*)$, exactly the
|
|
365 |
same as that of $\rup\backslash \, s$. And this difference
|
|
366 |
does not matter when we try to apply $\bmkeps$ or $\retrieve$
|
|
367 |
to it.\\
|
|
368 |
If we look into the difference above, we could see why:
|
|
369 |
|
|
370 |
that is to say, despite the bits are being moved around on the regular expression
|
|
371 |
(difference in bits), the structure of the (unannotated)regular expression
|
|
372 |
after one simplification is exactly the same after the
|
|
373 |
same sequence of derivative operations
|
|
374 |
regardless of whether we did simplification
|
|
375 |
along the way.
|
|
376 |
However, without erase the above equality does not hold:
|
|
377 |
for the regular expression
|
|
378 |
$(a+b)(a+a*)$,
|
|
379 |
if we do derivative with respect to string $aa$,
|
|
380 |
we get
|
|
381 |
%TODO
|
|
382 |
sdddddr does not equal sdsdsdsr sometimes.\\
|
|
383 |
For example,
|
|
384 |
|
|
385 |
This equicalence class method might still have the potential of proving this,
|
|
386 |
but not yet
|
|
387 |
i parallelly tried another method of using retrieve\\
|
|
388 |
|
|
389 |
|
|
390 |
|
94
|
391 |
%HERE CONSTRUCTION SITE
|
|
392 |
The vsimp function, defined as follows
|
|
393 |
tries to simplify the value in lockstep with
|
|
394 |
regular expression:\\
|
|
395 |
|
|
396 |
|
|
397 |
The problem here is that
|
|
398 |
|
|
399 |
we used retrieve for the key induction:
|
|
400 |
$decode (retrieve (r\backslash (s @ [c])) v) r $
|
|
401 |
$decode (retrieve (r\backslash s) (inj (r\backslash s) c v)) r$
|
|
402 |
Here, decode recovers a value that corresponds to a match(possibly partial)
|
|
403 |
from bits, and the bits are extracted by retrieve,
|
|
404 |
and the key value $v$ that guides retrieve is
|
|
405 |
$mkeps r\backslash s$, $inj r c (mkeps r\backslash s)$, $inj (inj (v))$, ......
|
|
406 |
if we can
|
|
407 |
the problem is that
|
|
408 |
need vsiimp to make a value that is suitable for decoding
|
|
409 |
$Some(flex rid(s@[c])v) = Some(flex rids(inj (r\backslash s)cv))$
|
|
410 |
another way that christian came up with that might circumvent the
|
|
411 |
prblem of finding suitable value is by not stating the visimp
|
|
412 |
function but include all possible value in a set that a regex is able to produce,
|
|
413 |
and proving that both r and sr are able to produce the bits that correspond the POSIX value
|
|
414 |
|
|
415 |
produced by feeding the same initial regular expression $r$ and string $s$ to the
|
|
416 |
two functions $ders$ and $ders\_simp$.
|
|
417 |
The reason why
|
|
418 |
Namely, if $bmkeps( r_1) = bmkeps(r_2)$, then we
|
|
419 |
|
|
420 |
|
|
421 |
If we define the equivalence relation $\sim_{m\epsilon}$ between two regular expressions
|
|
422 |
$r_1$ and $r_2$as follows:
|
|
423 |
$r_1 \sim_{m\epsilon} r_2 \iff bmkeps(r_1)= bmkeps(r_2)$
|
|
424 |
(in other words, they $r1$ and $r2$ produce the same output under the function $bmkeps$.)
|
|
425 |
Then the first goal
|
|
426 |
might be restated as
|
|
427 |
$(r^\uparrow)\backslash_{simp}\, s \sim_{m\epsilon} (r^\uparrow)\backslash s$.
|
|
428 |
I tried to establish an equivalence relation between the regular experssions
|
|
429 |
like dddr dddsr,.....
|
|
430 |
but right now i am only able to establish dsr and dr, using structural induction on r.
|
|
431 |
Those involve multiple derivative operations are harder to prove.
|
|
432 |
Two attempts have been made:
|
|
433 |
(1)induction on the number of der operations(or in other words, the length of the string s),
|
|
434 |
the inductive hypothesis was initially specified as
|
|
435 |
"For an arbitrary regular expression r,
|
|
436 |
For all string s in the language of r whose length do not exceed
|
|
437 |
the number n, ders s r me derssimp s r"
|
|
438 |
and the proof goal may be stated as
|
|
439 |
"For an arbitrary regular expression r,
|
|
440 |
For all string s in the language of r whose length do not exceed
|
|
441 |
the number n+1, ders s r me derssimp s r"
|
|
442 |
the problem here is that although we can easily break down
|
|
443 |
a string s of length n+1 into s1@list(c), it is not that easy
|
|
444 |
to use the i.h. as a stepping stone to prove anything because s1 may well be not
|
|
445 |
in the language L(r). This inhibits us from obtaining the fact that
|
|
446 |
ders s1 r me derssimps s1 r.
|
|
447 |
Further exploration is needed to amend this hypothesis so it includes the
|
|
448 |
situation when s1 is not nullable.
|
|
449 |
For example, what information(bits?
|
|
450 |
values?) can be extracted
|
|
451 |
from the regular expression ders(s1,r) so that we can compute or predict the possible
|
|
452 |
result of bmkeps after another derivative operation. What function f can used to
|
|
453 |
carry out the task? The possible way of exploration can be
|
|
454 |
more directly perceived throught the graph below:
|
|
455 |
find a function
|
|
456 |
f
|
|
457 |
such that
|
|
458 |
f(bders s1 r)
|
|
459 |
= re1
|
|
460 |
f(bderss s1 r)
|
|
461 |
= re2
|
|
462 |
bmkeps(bders s r) = g(re1,c)
|
|
463 |
bmkeps(bderssimp s r) = g(re2,c)
|
|
464 |
and g(re1,c) = g(re2,c)
|
|
465 |
The inductive hypothesis would be
|
|
466 |
"For all strings s1 of length <= n,
|
|
467 |
f(bders s1 r)
|
|
468 |
= re1
|
|
469 |
f(bderss s1 r)
|
|
470 |
= re2"
|
|
471 |
proving this would be a lemma for the main proof:
|
|
472 |
the main proof would be
|
|
473 |
"
|
|
474 |
bmkeps(bders s r) = g(re1,c)
|
|
475 |
bmkeps(bderssimp s r) = g(re2,c)
|
|
476 |
for s = s1@c
|
|
477 |
"
|
|
478 |
and f need to be a recursive property for the lemma to be proved:
|
|
479 |
it needs to store not only the "after one char nullable info",
|
|
480 |
but also the "after two char nullable info",
|
|
481 |
and so on so that it is able to predict what f will compute after a derivative operation,
|
|
482 |
in other words, it needs to be "infinitely recursive"\\
|
|
483 |
To prove the lemma, in other words, to get
|
|
484 |
"For all strings s1 of length <= n+1,
|
|
485 |
f(bders s1 r)
|
|
486 |
= re3
|
|
487 |
f(bderss s1 r)
|
|
488 |
= re4"\\
|
|
489 |
from\\
|
|
490 |
"For all strings s1 of length <= n,
|
|
491 |
f(bders s1 r)
|
|
492 |
= re1
|
|
493 |
f(bderss s1 r)
|
|
494 |
= re2"\\
|
|
495 |
it might be best to construct an auxiliary function h such that\\
|
|
496 |
h(re1, c) = re3\\
|
|
497 |
h(re2, c) = re4\\
|
|
498 |
and re3 = f(bder c (bders s1 r))\\
|
|
499 |
re4 = f(simp(bder c (bderss s1 r)))
|
|
500 |
The key point here is that we are not satisfied with what bders s r will produce under
|
|
501 |
bmkeps, but also how it will perform after a derivative operation and then bmkeps, and two
|
|
502 |
derivative operations and so on. In essence, we are preserving the regular expression
|
|
503 |
itself under the function f, in a less compact way than the regluar expression: we are
|
|
504 |
not just recording but also interpreting what the regular expression matches.
|
|
505 |
In other words, we need to prove the properties of bderss s r beyond the bmkeps result,
|
|
506 |
i.e., not just the nullable ones, but also those containing remaining characters.\\
|
|
507 |
(2)we observed the fact that
|
|
508 |
erase sdddddr= erase sdsdsdsr
|
|
509 |
that is to say, despite the bits are being moved around on the regular expression
|
|
510 |
(difference in bits), the structure of the (unannotated)regular expression
|
|
511 |
after one simplification is exactly the same after the
|
|
512 |
same sequence of derivative operations
|
|
513 |
regardless of whether we did simplification
|
|
514 |
along the way.
|
|
515 |
However, without erase the above equality does not hold:
|
|
516 |
for the regular expression
|
|
517 |
$(a+b)(a+a*)$,
|
|
518 |
if we do derivative with respect to string $aa$,
|
|
519 |
we get
|
|
520 |
%TODO
|
|
521 |
sdddddr does not equal sdsdsdsr sometimes.\\
|
|
522 |
For example,
|
|
523 |
|
|
524 |
This equicalence class method might still have the potential of proving this,
|
|
525 |
but not yet
|
|
526 |
i parallelly tried another method of using retrieve\\
|
|
527 |
|
|
528 |
|
|
529 |
|
|
530 |
\noindent\rule[0.5ex]{\linewidth}{1pt}
|
|
531 |
|
|
532 |
This PhD-project is about regular expression matching and
|
|
533 |
lexing. Given the maturity of this topic, the reader might wonder:
|
|
534 |
Surely, regular expressions must have already been studied to death?
|
|
535 |
What could possibly be \emph{not} known in this area? And surely all
|
|
536 |
implemented algorithms for regular expression matching are blindingly
|
|
537 |
fast?
|
|
538 |
|
|
539 |
Unfortunately these preconceptions are not supported by evidence: Take
|
|
540 |
for example the regular expression $(a^*)^*\,b$ and ask whether
|
|
541 |
strings of the form $aa..a$ match this regular
|
|
542 |
expression. Obviously this is not the case---the expected $b$ in the last
|
|
543 |
position is missing. One would expect that modern regular expression
|
|
544 |
matching engines can find this out very quickly. Alas, if one tries
|
|
545 |
this example in JavaScript, Python or Java 8 with strings like 28
|
|
546 |
$a$'s, one discovers that this decision takes around 30 seconds and
|
|
547 |
takes considerably longer when adding a few more $a$'s, as the graphs
|
|
548 |
below show:
|
|
549 |
|
|
550 |
\begin{center}
|
|
551 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
552 |
\begin{tikzpicture}
|
|
553 |
\begin{axis}[
|
|
554 |
xlabel={$n$},
|
|
555 |
x label style={at={(1.05,-0.05)}},
|
|
556 |
ylabel={time in secs},
|
|
557 |
enlargelimits=false,
|
|
558 |
xtick={0,5,...,30},
|
|
559 |
xmax=33,
|
|
560 |
ymax=35,
|
|
561 |
ytick={0,5,...,30},
|
|
562 |
scaled ticks=false,
|
|
563 |
axis lines=left,
|
|
564 |
width=5cm,
|
|
565 |
height=4cm,
|
|
566 |
legend entries={JavaScript},
|
|
567 |
legend pos=north west,
|
|
568 |
legend cell align=left]
|
|
569 |
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
|
|
570 |
\end{axis}
|
|
571 |
\end{tikzpicture}
|
|
572 |
&
|
|
573 |
\begin{tikzpicture}
|
|
574 |
\begin{axis}[
|
|
575 |
xlabel={$n$},
|
|
576 |
x label style={at={(1.05,-0.05)}},
|
|
577 |
%ylabel={time in secs},
|
|
578 |
enlargelimits=false,
|
|
579 |
xtick={0,5,...,30},
|
|
580 |
xmax=33,
|
|
581 |
ymax=35,
|
|
582 |
ytick={0,5,...,30},
|
|
583 |
scaled ticks=false,
|
|
584 |
axis lines=left,
|
|
585 |
width=5cm,
|
|
586 |
height=4cm,
|
|
587 |
legend entries={Python},
|
|
588 |
legend pos=north west,
|
|
589 |
legend cell align=left]
|
|
590 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
|
|
591 |
\end{axis}
|
|
592 |
\end{tikzpicture}
|
|
593 |
&
|
|
594 |
\begin{tikzpicture}
|
|
595 |
\begin{axis}[
|
|
596 |
xlabel={$n$},
|
|
597 |
x label style={at={(1.05,-0.05)}},
|
|
598 |
%ylabel={time in secs},
|
|
599 |
enlargelimits=false,
|
|
600 |
xtick={0,5,...,30},
|
|
601 |
xmax=33,
|
|
602 |
ymax=35,
|
|
603 |
ytick={0,5,...,30},
|
|
604 |
scaled ticks=false,
|
|
605 |
axis lines=left,
|
|
606 |
width=5cm,
|
|
607 |
height=4cm,
|
|
608 |
legend entries={Java 8},
|
|
609 |
legend pos=north west,
|
|
610 |
legend cell align=left]
|
|
611 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
|
|
612 |
\end{axis}
|
|
613 |
\end{tikzpicture}\\
|
|
614 |
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings
|
|
615 |
of the form $\underbrace{aa..a}_{n}$.}
|
|
616 |
\end{tabular}
|
|
617 |
\end{center}
|
|
618 |
|
|
619 |
\noindent These are clearly abysmal and possibly surprising results. One
|
|
620 |
would expect these systems to do much better than that---after all,
|
|
621 |
given a DFA and a string, deciding whether a string is matched by this
|
|
622 |
DFA should be linear in terms of the size of the regular expression and
|
|
623 |
the string?
|
|
624 |
|
|
625 |
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to
|
|
626 |
exhibit this super-linear behaviour. But unfortunately, such regular
|
|
627 |
expressions are not just a few outliers. They are actually
|
|
628 |
frequent enough to have a separate name created for
|
|
629 |
them---\emph{evil regular expressions}. In empiric work, Davis et al
|
|
630 |
report that they have found thousands of such evil regular expressions
|
|
631 |
in the JavaScript and Python ecosystems \cite{Davis18}. Static analysis
|
|
632 |
approach that is both sound and complete exists\cite{17Bir}, but the running
|
|
633 |
time on certain examples in the RegExLib and Snort regular expressions
|
|
634 |
libraries is unacceptable. Therefore the problem of efficiency still remains.
|
|
635 |
|
|
636 |
This superlinear blowup in matching algorithms sometimes causes
|
|
637 |
considerable grief in real life: for example on 20 July 2016 one evil
|
|
638 |
regular expression brought the webpage
|
|
639 |
\href{http://stackexchange.com}{Stack Exchange} to its
|
|
640 |
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}}
|
|
641 |
In this instance, a regular expression intended to just trim white
|
|
642 |
spaces from the beginning and the end of a line actually consumed
|
|
643 |
massive amounts of CPU-resources---causing web servers to grind to a
|
|
644 |
halt. This happened when a post with 20,000 white spaces was submitted,
|
|
645 |
but importantly the white spaces were neither at the beginning nor at
|
|
646 |
the end. As a result, the regular expression matching engine needed to
|
|
647 |
backtrack over many choices. In this example, the time needed to process
|
|
648 |
the string was $O(n^2)$ with respect to the string length. This
|
|
649 |
quadratic overhead was enough for the homepage of Stack Exchange to
|
|
650 |
respond so slowly that the load balancer assumed there must be some
|
|
651 |
attack and therefore stopped the servers from responding to any
|
|
652 |
requests. This made the whole site become unavailable. Another very
|
|
653 |
recent example is a global outage of all Cloudflare servers on 2 July
|
|
654 |
2019. A poorly written regular expression exhibited exponential
|
|
655 |
behaviour and exhausted CPUs that serve HTTP traffic. Although the
|
|
656 |
outage had several causes, at the heart was a regular expression that
|
|
657 |
was used to monitor network
|
|
658 |
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}}
|
|
659 |
|
|
660 |
The underlying problem is that many ``real life'' regular expression
|
|
661 |
matching engines do not use DFAs for matching. This is because they
|
|
662 |
support regular expressions that are not covered by the classical
|
|
663 |
automata theory, and in this more general setting there are quite a few
|
|
664 |
research questions still unanswered and fast algorithms still need to be
|
|
665 |
developed (for example how to treat efficiently bounded repetitions, negation and
|
|
666 |
back-references).
|
|
667 |
%question: dfa can have exponential states. isn't this the actual reason why they do not use dfas?
|
|
668 |
%how do they avoid dfas exponential states if they use them for fast matching?
|
|
669 |
|
|
670 |
There is also another under-researched problem to do with regular
|
|
671 |
expressions and lexing, i.e.~the process of breaking up strings into
|
|
672 |
sequences of tokens according to some regular expressions. In this
|
|
673 |
setting one is not just interested in whether or not a regular
|
|
674 |
expression matches a string, but also in \emph{how}. Consider for
|
|
675 |
example a regular expression $r_{key}$ for recognising keywords such as
|
|
676 |
\textit{if}, \textit{then} and so on; and a regular expression $r_{id}$
|
|
677 |
for recognising identifiers (say, a single character followed by
|
|
678 |
characters or numbers). One can then form the compound regular
|
|
679 |
expression $(r_{key} + r_{id})^*$ and use it to tokenise strings. But
|
|
680 |
then how should the string \textit{iffoo} be tokenised? It could be
|
|
681 |
tokenised as a keyword followed by an identifier, or the entire string
|
|
682 |
as a single identifier. Similarly, how should the string \textit{if} be
|
|
683 |
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would
|
|
684 |
``fire''---so is it an identifier or a keyword? While in applications
|
|
685 |
there is a well-known strategy to decide these questions, called POSIX
|
|
686 |
matching, only relatively recently precise definitions of what POSIX
|
|
687 |
matching actually means have been formalised
|
|
688 |
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. Such a
|
|
689 |
definition has also been given by Sulzmann and Lu \cite{Sulzmann2014},
|
|
690 |
but the corresponding correctness proof turned out to be faulty
|
|
691 |
\cite{AusafDyckhoffUrban2016}. Roughly, POSIX matching means matching
|
|
692 |
the longest initial substring. In the case of a tie, the initial
|
|
693 |
sub-match is chosen according to some priorities attached to the regular
|
|
694 |
expressions (e.g.~keywords have a higher priority than identifiers).
|
|
695 |
This sounds rather simple, but according to Grathwohl et al \cite[Page
|
|
696 |
36]{CrashCourse2014} this is not the case. They wrote:
|
|
697 |
|
|
698 |
\begin{quote}
|
|
699 |
\it{}``The POSIX strategy is more complicated than the greedy because of
|
|
700 |
the dependence on information about the length of matched strings in the
|
|
701 |
various subexpressions.''
|
|
702 |
\end{quote}
|
|
703 |
|
|
704 |
\noindent
|
|
705 |
This is also supported by evidence collected by Kuklewicz
|
|
706 |
\cite{Kuklewicz} who noticed that a number of POSIX regular expression
|
|
707 |
matchers calculate incorrect results.
|
|
708 |
|
|
709 |
Our focus in this project is on an algorithm introduced by Sulzmann and
|
|
710 |
Lu in 2014 for regular expression matching according to the POSIX
|
|
711 |
strategy \cite{Sulzmann2014}. Their algorithm is based on an older
|
|
712 |
algorithm by Brzozowski from 1964 where he introduced the notion of
|
|
713 |
derivatives of regular expressions~\cite{Brzozowski1964}. We shall
|
|
714 |
briefly explain this algorithm next.
|
|
715 |
|
|
716 |
\section{The Algorithm by Brzozowski based on Derivatives of Regular
|
|
717 |
Expressions}
|
|
718 |
|
|
719 |
Suppose (basic) regular expressions are given by the following grammar:
|
|
720 |
\[ r ::= \ZERO \mid \ONE
|
|
721 |
\mid c
|
|
722 |
\mid r_1 \cdot r_2
|
|
723 |
\mid r_1 + r_2
|
|
724 |
\mid r^*
|
|
725 |
\]
|
|
726 |
|
|
727 |
\noindent
|
|
728 |
The intended meaning of the constructors is as follows: $\ZERO$
|
|
729 |
cannot match any string, $\ONE$ can match the empty string, the
|
|
730 |
character regular expression $c$ can match the character $c$, and so
|
|
731 |
on.
|
|
732 |
|
|
733 |
The ingenious contribution by Brzozowski is the notion of
|
|
734 |
\emph{derivatives} of regular expressions. The idea behind this
|
|
735 |
notion is as follows: suppose a regular expression $r$ can match a
|
|
736 |
string of the form $c\!::\! s$ (that is a list of characters starting
|
|
737 |
with $c$), what does the regular expression look like that can match
|
|
738 |
just $s$? Brzozowski gave a neat answer to this question. He started
|
|
739 |
with the definition of $nullable$:
|
|
740 |
\begin{center}
|
|
741 |
\begin{tabular}{lcl}
|
|
742 |
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\
|
|
743 |
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\
|
|
744 |
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\
|
|
745 |
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
|
|
746 |
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
|
|
747 |
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\
|
|
748 |
\end{tabular}
|
|
749 |
\end{center}
|
|
750 |
This function simply tests whether the empty string is in $L(r)$.
|
|
751 |
He then defined
|
|
752 |
the following operation on regular expressions, written
|
|
753 |
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$):
|
|
754 |
|
|
755 |
\begin{center}
|
|
756 |
\begin{tabular}{lcl}
|
|
757 |
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\
|
|
758 |
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\
|
|
759 |
$d \backslash c$ & $\dn$ &
|
|
760 |
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
|
|
761 |
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
|
|
762 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
|
|
763 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
|
|
764 |
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
|
|
765 |
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\
|
|
766 |
\end{tabular}
|
|
767 |
\end{center}
|
|
768 |
|
|
769 |
%Assuming the classic notion of a
|
|
770 |
%\emph{language} of a regular expression, written $L(\_)$, t
|
|
771 |
|
|
772 |
\noindent
|
|
773 |
The main property of the derivative operation is that
|
|
774 |
|
|
775 |
\begin{center}
|
|
776 |
$c\!::\!s \in L(r)$ holds
|
|
777 |
if and only if $s \in L(r\backslash c)$.
|
|
778 |
\end{center}
|
|
779 |
|
|
780 |
\noindent
|
|
781 |
For us the main advantage is that derivatives can be
|
|
782 |
straightforwardly implemented in any functional programming language,
|
|
783 |
and are easily definable and reasoned about in theorem provers---the
|
|
784 |
definitions just consist of inductive datatypes and simple recursive
|
|
785 |
functions. Moreover, the notion of derivatives can be easily
|
|
786 |
generalised to cover extended regular expression constructors such as
|
|
787 |
the not-regular expression, written $\neg\,r$, or bounded repetitions
|
|
788 |
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so
|
|
789 |
straightforwardly realised within the classic automata approach.
|
|
790 |
For the moment however, we focus only on the usual basic regular expressions.
|
|
791 |
|
|
792 |
|
|
793 |
Now if we want to find out whether a string $s$ matches with a regular
|
|
794 |
expression $r$, we can build the derivatives of $r$ w.r.t.\ (in succession)
|
|
795 |
all the characters of the string $s$. Finally, test whether the
|
|
796 |
resulting regular expression can match the empty string. If yes, then
|
|
797 |
$r$ matches $s$, and no in the negative case. To implement this idea
|
|
798 |
we can generalise the derivative operation to strings like this:
|
|
799 |
|
|
800 |
\begin{center}
|
|
801 |
\begin{tabular}{lcl}
|
|
802 |
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
|
|
803 |
$r \backslash [\,] $ & $\dn$ & $r$
|
|
804 |
\end{tabular}
|
|
805 |
\end{center}
|
|
806 |
|
|
807 |
\noindent
|
|
808 |
and then define as regular-expression matching algorithm:
|
|
809 |
\[
|
|
810 |
match\;s\;r \;\dn\; nullable(r\backslash s)
|
|
811 |
\]
|
|
812 |
|
|
813 |
\noindent
|
|
814 |
This algorithm looks graphically as follows:
|
|
815 |
\begin{equation}\label{graph:*}
|
|
816 |
\begin{tikzcd}
|
|
817 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed] & r_n \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO}
|
|
818 |
\end{tikzcd}
|
|
819 |
\end{equation}
|
|
820 |
|
|
821 |
\noindent
|
|
822 |
where we start with a regular expression $r_0$, build successive
|
|
823 |
derivatives until we exhaust the string and then use \textit{nullable}
|
|
824 |
to test whether the result can match the empty string. It can be
|
|
825 |
relatively easily shown that this matcher is correct (that is given
|
|
826 |
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
|
|
827 |
|
|
828 |
|
|
829 |
\section{Values and the Algorithm by Sulzmann and Lu}
|
|
830 |
|
|
831 |
One limitation of Brzozowski's algorithm is that it only produces a
|
|
832 |
YES/NO answer for whether a string is being matched by a regular
|
|
833 |
expression. Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm
|
|
834 |
to allow generation of an actual matching, called a \emph{value} or
|
|
835 |
sometimes also \emph{lexical value}. These values and regular
|
|
836 |
expressions correspond to each other as illustrated in the following
|
|
837 |
table:
|
|
838 |
|
|
839 |
|
|
840 |
\begin{center}
|
|
841 |
\begin{tabular}{c@{\hspace{20mm}}c}
|
|
842 |
\begin{tabular}{@{}rrl@{}}
|
|
843 |
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
|
|
844 |
$r$ & $::=$ & $\ZERO$\\
|
|
845 |
& $\mid$ & $\ONE$ \\
|
|
846 |
& $\mid$ & $c$ \\
|
|
847 |
& $\mid$ & $r_1 \cdot r_2$\\
|
|
848 |
& $\mid$ & $r_1 + r_2$ \\
|
|
849 |
\\
|
|
850 |
& $\mid$ & $r^*$ \\
|
|
851 |
\end{tabular}
|
|
852 |
&
|
|
853 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}}
|
|
854 |
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
|
|
855 |
$v$ & $::=$ & \\
|
|
856 |
& & $\Empty$ \\
|
|
857 |
& $\mid$ & $\Char(c)$ \\
|
|
858 |
& $\mid$ & $\Seq\,v_1\, v_2$\\
|
|
859 |
& $\mid$ & $\Left(v)$ \\
|
|
860 |
& $\mid$ & $\Right(v)$ \\
|
|
861 |
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
|
|
862 |
\end{tabular}
|
|
863 |
\end{tabular}
|
|
864 |
\end{center}
|
|
865 |
|
|
866 |
\noindent
|
|
867 |
No value corresponds to $\ZERO$; $\Empty$ corresponds to $\ONE$;
|
|
868 |
$\Char$ to the character regular expression; $\Seq$ to the sequence
|
|
869 |
regular expression and so on. The idea of values is to encode a kind of
|
|
870 |
lexical value for how the sub-parts of a regular expression match the
|
|
871 |
sub-parts of a string. To see this, suppose a \emph{flatten} operation,
|
|
872 |
written $|v|$ for values. We can use this function to extract the
|
|
873 |
underlying string of a value $v$. For example, $|\mathit{Seq} \,
|
|
874 |
(\textit{Char x}) \, (\textit{Char y})|$ is the string $xy$. Using
|
|
875 |
flatten, we can describe how values encode lexical values: $\Seq\,v_1\,
|
|
876 |
v_2$ encodes a tree with two children nodes that tells how the string
|
|
877 |
$|v_1| @ |v_2|$ matches the regex $r_1 \cdot r_2$ whereby $r_1$ matches
|
|
878 |
the substring $|v_1|$ and, respectively, $r_2$ matches the substring
|
|
879 |
$|v_2|$. Exactly how these two are matched is contained in the children
|
|
880 |
nodes $v_1$ and $v_2$ of parent $\textit{Seq}$.
|
|
881 |
|
|
882 |
To give a concrete example of how values work, consider the string $xy$
|
|
883 |
and the regular expression $(x + (y + xy))^*$. We can view this regular
|
|
884 |
expression as a tree and if the string $xy$ is matched by two Star
|
|
885 |
``iterations'', then the $x$ is matched by the left-most alternative in
|
|
886 |
this tree and the $y$ by the right-left alternative. This suggests to
|
|
887 |
record this matching as
|
|
888 |
|
|
889 |
\begin{center}
|
|
890 |
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$
|
|
891 |
\end{center}
|
|
892 |
|
|
893 |
\noindent
|
|
894 |
where $\Stars \; [\ldots]$ records all the
|
|
895 |
iterations; and $\Left$, respectively $\Right$, which
|
|
896 |
alternative is used. The value for
|
|
897 |
matching $xy$ in a single ``iteration'', i.e.~the POSIX value,
|
|
898 |
would look as follows
|
|
899 |
|
|
900 |
\begin{center}
|
|
901 |
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$
|
|
902 |
\end{center}
|
|
903 |
|
|
904 |
\noindent
|
|
905 |
where $\Stars$ has only a single-element list for the single iteration
|
|
906 |
and $\Seq$ indicates that $xy$ is matched by a sequence regular
|
|
907 |
expression.
|
|
908 |
|
|
909 |
The contribution of Sulzmann and Lu is an extension of Brzozowski's
|
|
910 |
algorithm by a second phase (the first phase being building successive
|
|
911 |
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value
|
|
912 |
is generated in case the regular expression matches the string.
|
|
913 |
Pictorially, the Sulzmann and Lu algorithm is as follows:
|
|
914 |
|
|
915 |
\begin{ceqn}
|
|
916 |
\begin{equation}\label{graph:2}
|
|
917 |
\begin{tikzcd}
|
|
918 |
r_0 \arrow[r, "\backslash c_0"] \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
|
|
919 |
v_0 & v_1 \arrow[l,"inj_{r_0} c_0"] & v_2 \arrow[l, "inj_{r_1} c_1"] & v_n \arrow[l, dashed]
|
|
920 |
\end{tikzcd}
|
|
921 |
\end{equation}
|
|
922 |
\end{ceqn}
|
|
923 |
|
|
924 |
\noindent
|
|
925 |
For convenience, we shall employ the following notations: the regular
|
|
926 |
expression we start with is $r_0$, and the given string $s$ is composed
|
|
927 |
of characters $c_0 c_1 \ldots c_{n-1}$. In the first phase from the
|
|
928 |
left to right, we build the derivatives $r_1$, $r_2$, \ldots according
|
|
929 |
to the characters $c_0$, $c_1$ until we exhaust the string and obtain
|
|
930 |
the derivative $r_n$. We test whether this derivative is
|
|
931 |
$\textit{nullable}$ or not. If not, we know the string does not match
|
|
932 |
$r$ and no value needs to be generated. If yes, we start building the
|
|
933 |
values incrementally by \emph{injecting} back the characters into the
|
|
934 |
earlier values $v_n, \ldots, v_0$. This is the second phase of the
|
|
935 |
algorithm from the right to left. For the first value $v_n$, we call the
|
|
936 |
function $\textit{mkeps}$, which builds the lexical value
|
|
937 |
for how the empty string has been matched by the (nullable) regular
|
|
938 |
expression $r_n$. This function is defined as
|
|
939 |
|
|
940 |
\begin{center}
|
|
941 |
\begin{tabular}{lcl}
|
|
942 |
$\mkeps(\ONE)$ & $\dn$ & $\Empty$ \\
|
|
943 |
$\mkeps(r_{1}+r_{2})$ & $\dn$
|
|
944 |
& \textit{if} $\nullable(r_{1})$\\
|
|
945 |
& & \textit{then} $\Left(\mkeps(r_{1}))$\\
|
|
946 |
& & \textit{else} $\Right(\mkeps(r_{2}))$\\
|
|
947 |
$\mkeps(r_1\cdot r_2)$ & $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
|
|
948 |
$mkeps(r^*)$ & $\dn$ & $\Stars\,[]$
|
|
949 |
\end{tabular}
|
|
950 |
\end{center}
|
|
951 |
|
|
952 |
|
|
953 |
\noindent There are no cases for $\ZERO$ and $c$, since
|
|
954 |
these regular expression cannot match the empty string. Note
|
|
955 |
also that in case of alternatives we give preference to the
|
|
956 |
regular expression on the left-hand side. This will become
|
|
957 |
important later on about what value is calculated.
|
|
958 |
|
|
959 |
After the $\mkeps$-call, we inject back the characters one by one in order to build
|
|
960 |
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
|
|
961 |
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
|
|
962 |
After injecting back $n$ characters, we get the lexical value for how $r_0$
|
|
963 |
matches $s$. For this Sulzmann and Lu defined a function that reverses
|
|
964 |
the ``chopping off'' of characters during the derivative phase. The
|
|
965 |
corresponding function is called \emph{injection}, written
|
|
966 |
$\textit{inj}$; it takes three arguments: the first one is a regular
|
|
967 |
expression ${r_{i-1}}$, before the character is chopped off, the second
|
|
968 |
is a character ${c_{i-1}}$, the character we want to inject and the
|
|
969 |
third argument is the value ${v_i}$, into which one wants to inject the
|
|
970 |
character (it corresponds to the regular expression after the character
|
|
971 |
has been chopped off). The result of this function is a new value. The
|
|
972 |
definition of $\textit{inj}$ is as follows:
|
|
973 |
|
|
974 |
\begin{center}
|
|
975 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
|
|
976 |
$\textit{inj}\,(c)\,c\,Empty$ & $\dn$ & $Char\,c$\\
|
|
977 |
$\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
|
|
978 |
$\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
|
|
979 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
|
|
980 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
|
|
981 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$ & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
|
|
982 |
$\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$ & $\dn$ & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
|
|
983 |
\end{tabular}
|
|
984 |
\end{center}
|
|
985 |
|
|
986 |
\noindent This definition is by recursion on the ``shape'' of regular
|
|
987 |
expressions and values. To understands this definition better consider
|
|
988 |
the situation when we build the derivative on regular expression $r_{i-1}$.
|
|
989 |
For this we chop off a character from $r_{i-1}$ to form $r_i$. This leaves a
|
|
990 |
``hole'' in $r_i$ and its corresponding value $v_i$.
|
|
991 |
To calculate $v_{i-1}$, we need to
|
|
992 |
locate where that hole is and fill it.
|
|
993 |
We can find this location by
|
|
994 |
comparing $r_{i-1}$ and $v_i$. For instance, if $r_{i-1}$ is of shape
|
|
995 |
$r_a \cdot r_b$, and $v_i$ is of shape $\Left(Seq(v_1,v_2))$, we know immediately that
|
|
996 |
%
|
|
997 |
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b \,+\, r_b\backslash c,\]
|
|
998 |
|
|
999 |
\noindent
|
|
1000 |
otherwise if $r_a$ is not nullable,
|
|
1001 |
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b,\]
|
|
1002 |
|
|
1003 |
\noindent
|
|
1004 |
the value $v_i$ should be $\Seq(\ldots)$, contradicting the fact that
|
|
1005 |
$v_i$ is actually of shape $\Left(\ldots)$. Furthermore, since $v_i$ is of shape
|
|
1006 |
$\Left(\ldots)$ instead of $\Right(\ldots)$, we know that the left
|
|
1007 |
branch of \[ (r_a \cdot r_b)\backslash c =
|
|
1008 |
\bold{\underline{ (r_a\backslash c) \cdot r_b} }\,+\, r_b\backslash c,\](underlined)
|
|
1009 |
is taken instead of the right one. This means $c$ is chopped off
|
|
1010 |
from $r_a$ rather than $r_b$.
|
|
1011 |
We have therefore found out
|
|
1012 |
that the hole will be on $r_a$. So we recursively call $\inj\,
|
|
1013 |
r_a\,c\,v_a$ to fill that hole in $v_a$. After injection, the value
|
|
1014 |
$v_i$ for $r_i = r_a \cdot r_b$ should be $\Seq\,(\inj\,r_a\,c\,v_a)\,v_b$.
|
|
1015 |
Other clauses can be understood in a similar way.
|
|
1016 |
|
|
1017 |
%\comment{Other word: insight?}
|
|
1018 |
The following example gives an insight of $\textit{inj}$'s effect and
|
|
1019 |
how Sulzmann and Lu's algorithm works as a whole. Suppose we have a
|
|
1020 |
regular expression $((((a+b)+ab)+c)+abc)^*$, and want to match it
|
|
1021 |
against the string $abc$ (when $abc$ is written as a regular expression,
|
|
1022 |
the standard way of expressing it is $a \cdot (b \cdot c)$. But we
|
|
1023 |
usually omit the parentheses and dots here for better readability. This
|
|
1024 |
algorithm returns a POSIX value, which means it will produce the longest
|
|
1025 |
matching. Consequently, it matches the string $abc$ in one star
|
|
1026 |
iteration, using the longest alternative $abc$ in the sub-expression (we shall use $r$ to denote this
|
|
1027 |
sub-expression for conciseness):
|
|
1028 |
|
|
1029 |
\[((((a+b)+ab)+c)+\underbrace{abc}_r)\]
|
|
1030 |
|
|
1031 |
\noindent
|
|
1032 |
Before $\textit{inj}$ is called, our lexer first builds derivative using
|
|
1033 |
string $abc$ (we simplified some regular expressions like $\ZERO \cdot
|
|
1034 |
b$ to $\ZERO$ for conciseness; we also omit parentheses if they are
|
|
1035 |
clear from the context):
|
|
1036 |
|
|
1037 |
%Similarly, we allow
|
|
1038 |
%$\textit{ALT}$ to take a list of regular expressions as an argument
|
|
1039 |
%instead of just 2 operands to reduce the nested depth of
|
|
1040 |
%$\textit{ALT}$
|
|
1041 |
|
|
1042 |
\begin{center}
|
|
1043 |
\begin{tabular}{lcl}
|
|
1044 |
$r^*$ & $\xrightarrow{\backslash a}$ & $r_1 = (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r^*$\\
|
|
1045 |
& $\xrightarrow{\backslash b}$ & $r_2 = (\ZERO+\ZERO+\ONE \cdot \ONE + \ZERO + \ONE \cdot \ONE \cdot c) \cdot r^* +(\ZERO+\ONE+\ZERO + \ZERO + \ZERO) \cdot r^*$\\
|
|
1046 |
& $\xrightarrow{\backslash c}$ & $r_3 = ((\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot \ONE) \cdot r^* + (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^*) + $\\
|
|
1047 |
& & $\phantom{r_3 = (} ((\ZERO+\ONE+\ZERO + \ZERO + \ZERO) \cdot r^* + (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^* )$
|
|
1048 |
\end{tabular}
|
|
1049 |
\end{center}
|
|
1050 |
|
|
1051 |
\noindent
|
|
1052 |
In case $r_3$ is nullable, we can call $\textit{mkeps}$
|
|
1053 |
to construct a lexical value for how $r_3$ matched the string $abc$.
|
|
1054 |
This function gives the following value $v_3$:
|
|
1055 |
|
|
1056 |
|
|
1057 |
\begin{center}
|
|
1058 |
$\Left(\Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty,\Empty))), \Stars [])))$
|
|
1059 |
\end{center}
|
|
1060 |
The outer $\Left(\Left(\ldots))$ tells us the leftmost nullable part of $r_3$(underlined):
|
|
1061 |
|
|
1062 |
\begin{center}
|
|
1063 |
\begin{tabular}{l@{\hspace{2mm}}l}
|
|
1064 |
& $\big(\underline{(\ZERO+\ZERO+\ZERO+ \ZERO+ \ONE \cdot \ONE \cdot \ONE) \cdot r^*}
|
|
1065 |
\;+\; (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^*\big)$ \smallskip\\
|
|
1066 |
$+$ & $\big((\ZERO+\ONE+\ZERO + \ZERO + \ZERO) \cdot r^*
|
|
1067 |
\;+\; (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^* \big)$
|
|
1068 |
\end{tabular}
|
|
1069 |
\end{center}
|
|
1070 |
|
|
1071 |
\noindent
|
|
1072 |
Note that the leftmost location of term $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot
|
|
1073 |
\ONE) \cdot r^*$ (which corresponds to the initial sub-match $abc$) allows
|
|
1074 |
$\textit{mkeps}$ to pick it up because $\textit{mkeps}$ is defined to always choose the
|
|
1075 |
left one when it is nullable. In the case of this example, $abc$ is
|
|
1076 |
preferred over $a$ or $ab$. This $\Left(\Left(\ldots))$ location is
|
|
1077 |
generated by two applications of the splitting clause
|
|
1078 |
|
|
1079 |
\begin{center}
|
|
1080 |
$(r_1 \cdot r_2)\backslash c \;\;(when \; r_1 \; nullable) \, = (r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c.$
|
|
1081 |
\end{center}
|
|
1082 |
|
|
1083 |
\noindent
|
|
1084 |
By this clause, we put $r_1 \backslash c \cdot r_2 $ at the
|
|
1085 |
$\textit{front}$ and $r_2 \backslash c$ at the $\textit{back}$. This
|
|
1086 |
allows $\textit{mkeps}$ to always pick up among two matches the one with a longer
|
|
1087 |
initial sub-match. Removing the outside $\Left(\Left(...))$, the inside
|
|
1088 |
sub-value
|
|
1089 |
|
|
1090 |
\begin{center}
|
|
1091 |
$\Seq(\Right(\Seq(\Empty, \Seq(\Empty, \Empty))), \Stars [])$
|
|
1092 |
\end{center}
|
|
1093 |
|
|
1094 |
\noindent
|
|
1095 |
tells us how the empty string $[]$ is matched with $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot
|
|
1096 |
\ONE \cdot \ONE) \cdot r^*$. We match $[]$ by a sequence of two nullable regular
|
|
1097 |
expressions. The first one is an alternative, we take the rightmost
|
|
1098 |
alternative---whose language contains the empty string. The second
|
|
1099 |
nullable regular expression is a Kleene star. $\Stars$ tells us how it
|
|
1100 |
generates the nullable regular expression: by 0 iterations to form
|
|
1101 |
$\ONE$. Now $\textit{inj}$ injects characters back and incrementally
|
|
1102 |
builds a lexical value based on $v_3$. Using the value $v_3$, the character
|
|
1103 |
c, and the regular expression $r_2$, we can recover how $r_2$ matched
|
|
1104 |
the string $[c]$ : $\textit{inj} \; r_2 \; c \; v_3$ gives us
|
|
1105 |
\begin{center}
|
|
1106 |
$v_2 = \Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty, c))), \Stars [])),$
|
|
1107 |
\end{center}
|
|
1108 |
which tells us how $r_2$ matched $[c]$. After this we inject back the character $b$, and get
|
|
1109 |
\begin{center}
|
|
1110 |
$v_1 = \Seq(\Right(\Seq(\Empty, \Seq(b, c))), \Stars [])$
|
|
1111 |
\end{center}
|
|
1112 |
for how
|
|
1113 |
\begin{center}
|
|
1114 |
$r_1= (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r*$
|
|
1115 |
\end{center}
|
|
1116 |
matched the string $bc$ before it split into two substrings.
|
|
1117 |
Finally, after injecting character $a$ back to $v_1$,
|
|
1118 |
we get the lexical value tree
|
|
1119 |
\begin{center}
|
|
1120 |
$v_0= \Stars [\Right(\Seq(a, \Seq(b, c)))]$
|
|
1121 |
\end{center}
|
|
1122 |
for how $r$ matched $abc$. This completes the algorithm.
|
|
1123 |
|
|
1124 |
%We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}.
|
|
1125 |
Readers might have noticed that the lexical value information is actually
|
|
1126 |
already available when doing derivatives. For example, immediately after
|
|
1127 |
the operation $\backslash a$ we know that if we want to match a string
|
|
1128 |
that starts with $a$, we can either take the initial match to be
|
|
1129 |
|
|
1130 |
\begin{center}
|
|
1131 |
\begin{enumerate}
|
|
1132 |
\item[1)] just $a$ or
|
|
1133 |
\item[2)] string $ab$ or
|
|
1134 |
\item[3)] string $abc$.
|
|
1135 |
\end{enumerate}
|
|
1136 |
\end{center}
|
|
1137 |
|
|
1138 |
\noindent
|
|
1139 |
In order to differentiate between these choices, we just need to
|
|
1140 |
remember their positions---$a$ is on the left, $ab$ is in the middle ,
|
|
1141 |
and $abc$ is on the right. Which of these alternatives is chosen
|
|
1142 |
later does not affect their relative position because the algorithm does
|
|
1143 |
not change this order. If this parsing information can be determined and
|
|
1144 |
does not change because of later derivatives, there is no point in
|
|
1145 |
traversing this information twice. This leads to an optimisation---if we
|
|
1146 |
store the information for lexical values inside the regular expression,
|
|
1147 |
update it when we do derivative on them, and collect the information
|
|
1148 |
when finished with derivatives and call $\textit{mkeps}$ for deciding which
|
|
1149 |
branch is POSIX, we can generate the lexical value in one pass, instead of
|
|
1150 |
doing the rest $n$ injections. This leads to Sulzmann and Lu's novel
|
|
1151 |
idea of using bitcodes in derivatives.
|
|
1152 |
|
|
1153 |
In the next section, we shall focus on the bitcoded algorithm and the
|
|
1154 |
process of simplification of regular expressions. This is needed in
|
|
1155 |
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann
|
|
1156 |
and Lu's algorithms. This is where the PhD-project aims to advance the
|
|
1157 |
state-of-the-art.
|
|
1158 |
|
|
1159 |
|
|
1160 |
\section{Simplification of Regular Expressions}
|
|
1161 |
|
|
1162 |
Using bitcodes to guide parsing is not a novel idea. It was applied to
|
|
1163 |
context free grammars and then adapted by Henglein and Nielson for
|
|
1164 |
efficient regular expression lexing using DFAs~\cite{nielson11bcre}.
|
|
1165 |
Sulzmann and Lu took this idea of bitcodes a step further by integrating
|
|
1166 |
bitcodes into derivatives. The reason why we want to use bitcodes in
|
|
1167 |
this project is that we want to introduce more aggressive simplification
|
|
1168 |
rules in order to keep the size of derivatives small throughout. This is
|
|
1169 |
because the main drawback of building successive derivatives according
|
|
1170 |
to Brzozowski's definition is that they can grow very quickly in size.
|
|
1171 |
This is mainly due to the fact that the derivative operation generates
|
|
1172 |
often ``useless'' $\ZERO$s and $\ONE$s in derivatives. As a result, if
|
|
1173 |
implemented naively both algorithms by Brzozowski and by Sulzmann and Lu
|
|
1174 |
are excruciatingly slow. For example when starting with the regular
|
|
1175 |
expression $(a + aa)^*$ and building 12 successive derivatives
|
|
1176 |
w.r.t.~the character $a$, one obtains a derivative regular expression
|
|
1177 |
with more than 8000 nodes (when viewed as a tree). Operations like
|
|
1178 |
$\textit{der}$ and $\nullable$ need to traverse such trees and
|
|
1179 |
consequently the bigger the size of the derivative the slower the
|
|
1180 |
algorithm.
|
|
1181 |
|
|
1182 |
Fortunately, one can simplify regular expressions after each derivative
|
|
1183 |
step. Various simplifications of regular expressions are possible, such
|
|
1184 |
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r
|
|
1185 |
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not
|
|
1186 |
affect the answer for whether a regular expression matches a string or
|
|
1187 |
not, but fortunately also do not affect the POSIX strategy of how
|
|
1188 |
regular expressions match strings---although the latter is much harder
|
|
1189 |
to establish. Some initial results in this regard have been
|
|
1190 |
obtained in \cite{AusafDyckhoffUrban2016}.
|
|
1191 |
|
|
1192 |
Unfortunately, the simplification rules outlined above are not
|
|
1193 |
sufficient to prevent a size explosion in all cases. We
|
|
1194 |
believe a tighter bound can be achieved that prevents an explosion in
|
|
1195 |
\emph{all} cases. Such a tighter bound is suggested by work of Antimirov who
|
|
1196 |
proved that (partial) derivatives can be bound by the number of
|
|
1197 |
characters contained in the initial regular expression
|
|
1198 |
\cite{Antimirov95}. He defined the \emph{partial derivatives} of regular
|
|
1199 |
expressions as follows:
|
|
1200 |
|
|
1201 |
\begin{center}
|
|
1202 |
\begin{tabular}{lcl}
|
|
1203 |
$\textit{pder} \; c \; \ZERO$ & $\dn$ & $\emptyset$\\
|
|
1204 |
$\textit{pder} \; c \; \ONE$ & $\dn$ & $\emptyset$ \\
|
|
1205 |
$\textit{pder} \; c \; d$ & $\dn$ & $\textit{if} \; c \,=\, d \; \{ \ONE \} \; \textit{else} \; \emptyset$ \\
|
|
1206 |
$\textit{pder} \; c \; r_1+r_2$ & $\dn$ & $pder \; c \; r_1 \cup pder \; c \; r_2$ \\
|
|
1207 |
$\textit{pder} \; c \; r_1 \cdot r_2$ & $\dn$ & $\textit{if} \; nullable \; r_1 $\\
|
|
1208 |
& & $\textit{then} \; \{ r \cdot r_2 \mid r \in pder \; c \; r_1 \} \cup pder \; c \; r_2 \;$\\
|
|
1209 |
& & $\textit{else} \; \{ r \cdot r_2 \mid r \in pder \; c \; r_1 \} $ \\
|
|
1210 |
$\textit{pder} \; c \; r^*$ & $\dn$ & $ \{ r' \cdot r^* \mid r' \in pder \; c \; r \} $ \\
|
|
1211 |
\end{tabular}
|
|
1212 |
\end{center}
|
|
1213 |
|
|
1214 |
\noindent
|
|
1215 |
A partial derivative of a regular expression $r$ is essentially a set of
|
|
1216 |
regular expressions that are either $r$'s children expressions or a
|
|
1217 |
concatenation of them. Antimirov has proved a tight bound of the sum of
|
|
1218 |
the size of \emph{all} partial derivatives no matter what the string
|
|
1219 |
looks like. Roughly speaking the size sum will be at most cubic in the
|
|
1220 |
size of the regular expression.
|
|
1221 |
|
|
1222 |
If we want the size of derivatives in Sulzmann and Lu's algorithm to
|
|
1223 |
stay below this bound, we would need more aggressive simplifications.
|
|
1224 |
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
|
|
1225 |
deleting duplicates whenever possible. For example, the parentheses in
|
|
1226 |
$(a+b) \cdot c + bc$ can be opened up to get $a\cdot c + b \cdot c + b
|
|
1227 |
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
|
|
1228 |
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
|
|
1229 |
$a^*+a+\ONE$. Adding these more aggressive simplification rules helps us
|
|
1230 |
to achieve the same size bound as that of the partial derivatives.
|
|
1231 |
|
|
1232 |
In order to implement the idea of ``spilling out alternatives'' and to
|
|
1233 |
make them compatible with the $\text{inj}$-mechanism, we use
|
|
1234 |
\emph{bitcodes}. Bits and bitcodes (lists of bits) are just:
|
|
1235 |
|
|
1236 |
%This allows us to prove a tight
|
|
1237 |
%bound on the size of regular expression during the running time of the
|
|
1238 |
%algorithm if we can establish the connection between our simplification
|
|
1239 |
%rules and partial derivatives.
|
|
1240 |
|
|
1241 |
%We believe, and have generated test
|
|
1242 |
%data, that a similar bound can be obtained for the derivatives in
|
|
1243 |
%Sulzmann and Lu's algorithm. Let us give some details about this next.
|
|
1244 |
|
|
1245 |
|
|
1246 |
\begin{center}
|
|
1247 |
$b ::= S \mid Z \qquad
|
|
1248 |
bs ::= [] \mid b:bs
|
|
1249 |
$
|
|
1250 |
\end{center}
|
|
1251 |
|
|
1252 |
\noindent
|
|
1253 |
The $S$ and $Z$ are arbitrary names for the bits in order to avoid
|
|
1254 |
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
|
|
1255 |
bit-lists) can be used to encode values (or incomplete values) in a
|
|
1256 |
compact form. This can be straightforwardly seen in the following
|
|
1257 |
coding function from values to bitcodes:
|
|
1258 |
|
|
1259 |
\begin{center}
|
|
1260 |
\begin{tabular}{lcl}
|
|
1261 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\
|
|
1262 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
|
|
1263 |
$\textit{code}(\Left\,v)$ & $\dn$ & $\Z :: code(v)$\\
|
|
1264 |
$\textit{code}(\Right\,v)$ & $\dn$ & $\S :: code(v)$\\
|
|
1265 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
|
|
1266 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[\Z]$\\
|
|
1267 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $\S :: code(v) \;@\;
|
|
1268 |
code(\Stars\,vs)$
|
|
1269 |
\end{tabular}
|
|
1270 |
\end{center}
|
|
1271 |
|
|
1272 |
\noindent
|
|
1273 |
Here $\textit{code}$ encodes a value into a bitcodes by converting
|
|
1274 |
$\Left$ into $\Z$, $\Right$ into $\S$, the start point of a non-empty
|
|
1275 |
star iteration into $\S$, and the border where a local star terminates
|
|
1276 |
into $\Z$. This coding is lossy, as it throws away the information about
|
|
1277 |
characters, and also does not encode the ``boundary'' between two
|
|
1278 |
sequence values. Moreover, with only the bitcode we cannot even tell
|
|
1279 |
whether the $\S$s and $\Z$s are for $\Left/\Right$ or $\Stars$. The
|
|
1280 |
reason for choosing this compact way of storing information is that the
|
|
1281 |
relatively small size of bits can be easily manipulated and ``moved
|
|
1282 |
around'' in a regular expression. In order to recover values, we will
|
|
1283 |
need the corresponding regular expression as an extra information. This
|
|
1284 |
means the decoding function is defined as:
|
|
1285 |
|
|
1286 |
|
|
1287 |
%\begin{definition}[Bitdecoding of Values]\mbox{}
|
|
1288 |
\begin{center}
|
|
1289 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
|
|
1290 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
|
|
1291 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
|
|
1292 |
$\textit{decode}'\,(\Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
|
1293 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
|
|
1294 |
(\Left\,v, bs_1)$\\
|
|
1295 |
$\textit{decode}'\,(\S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
|
1296 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
|
|
1297 |
(\Right\,v, bs_1)$\\
|
|
1298 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
|
|
1299 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
|
|
1300 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
|
|
1301 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
|
|
1302 |
$\textit{decode}'\,(\Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
|
|
1303 |
$\textit{decode}'\,(\S\!::\!bs)\,(r^*)$ & $\dn$ &
|
|
1304 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
1305 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
|
|
1306 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
|
|
1307 |
|
|
1308 |
$\textit{decode}\,bs\,r$ & $\dn$ &
|
|
1309 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
1310 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
|
|
1311 |
\textit{else}\;\textit{None}$
|
|
1312 |
\end{tabular}
|
|
1313 |
\end{center}
|
|
1314 |
%\end{definition}
|
|
1315 |
|
|
1316 |
Sulzmann and Lu's integrated the bitcodes into regular expressions to
|
|
1317 |
create annotated regular expressions \cite{Sulzmann2014}.
|
|
1318 |
\emph{Annotated regular expressions} are defined by the following
|
|
1319 |
grammar:%\comment{ALTS should have an $as$ in the definitions, not just $a_1$ and $a_2$}
|
|
1320 |
|
|
1321 |
\begin{center}
|
|
1322 |
\begin{tabular}{lcl}
|
|
1323 |
$\textit{a}$ & $::=$ & $\textit{ZERO}$\\
|
|
1324 |
& $\mid$ & $\textit{ONE}\;\;bs$\\
|
|
1325 |
& $\mid$ & $\textit{CHAR}\;\;bs\,c$\\
|
|
1326 |
& $\mid$ & $\textit{ALTS}\;\;bs\,as$\\
|
|
1327 |
& $\mid$ & $\textit{SEQ}\;\;bs\,a_1\,a_2$\\
|
|
1328 |
& $\mid$ & $\textit{STAR}\;\;bs\,a$
|
|
1329 |
\end{tabular}
|
|
1330 |
\end{center}
|
|
1331 |
%(in \textit{ALTS})
|
|
1332 |
|
|
1333 |
\noindent
|
|
1334 |
where $bs$ stands for bitcodes, $a$ for $\bold{a}$nnotated regular
|
|
1335 |
expressions and $as$ for a list of annotated regular expressions.
|
|
1336 |
The alternative constructor($\textit{ALTS}$) has been generalized to
|
|
1337 |
accept a list of annotated regular expressions rather than just 2.
|
|
1338 |
We will show that these bitcodes encode information about
|
|
1339 |
the (POSIX) value that should be generated by the Sulzmann and Lu
|
|
1340 |
algorithm.
|
|
1341 |
|
|
1342 |
|
|
1343 |
To do lexing using annotated regular expressions, we shall first
|
|
1344 |
transform the usual (un-annotated) regular expressions into annotated
|
|
1345 |
regular expressions. This operation is called \emph{internalisation} and
|
|
1346 |
defined as follows:
|
|
1347 |
|
|
1348 |
%\begin{definition}
|
|
1349 |
\begin{center}
|
|
1350 |
\begin{tabular}{lcl}
|
|
1351 |
$(\ZERO)^\uparrow$ & $\dn$ & $\textit{ZERO}$\\
|
|
1352 |
$(\ONE)^\uparrow$ & $\dn$ & $\textit{ONE}\,[]$\\
|
|
1353 |
$(c)^\uparrow$ & $\dn$ & $\textit{CHAR}\,[]\,c$\\
|
|
1354 |
$(r_1 + r_2)^\uparrow$ & $\dn$ &
|
|
1355 |
$\textit{ALTS}\;[]\,List((\textit{fuse}\,[\Z]\,r_1^\uparrow),\,
|
|
1356 |
(\textit{fuse}\,[\S]\,r_2^\uparrow))$\\
|
|
1357 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ &
|
|
1358 |
$\textit{SEQ}\;[]\,r_1^\uparrow\,r_2^\uparrow$\\
|
|
1359 |
$(r^*)^\uparrow$ & $\dn$ &
|
|
1360 |
$\textit{STAR}\;[]\,r^\uparrow$\\
|
|
1361 |
\end{tabular}
|
|
1362 |
\end{center}
|
|
1363 |
%\end{definition}
|
|
1364 |
|
|
1365 |
\noindent
|
|
1366 |
We use up arrows here to indicate that the basic un-annotated regular
|
|
1367 |
expressions are ``lifted up'' into something slightly more complex. In the
|
|
1368 |
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
|
|
1369 |
attach bits to the front of an annotated regular expression. Its
|
|
1370 |
definition is as follows:
|
|
1371 |
|
|
1372 |
\begin{center}
|
|
1373 |
\begin{tabular}{lcl}
|
|
1374 |
$\textit{fuse}\;bs\,(\textit{ZERO})$ & $\dn$ & $\textit{ZERO}$\\
|
|
1375 |
$\textit{fuse}\;bs\,(\textit{ONE}\,bs')$ & $\dn$ &
|
|
1376 |
$\textit{ONE}\,(bs\,@\,bs')$\\
|
|
1377 |
$\textit{fuse}\;bs\,(\textit{CHAR}\,bs'\,c)$ & $\dn$ &
|
|
1378 |
$\textit{CHAR}\,(bs\,@\,bs')\,c$\\
|
|
1379 |
$\textit{fuse}\;bs\,(\textit{ALTS}\,bs'\,as)$ & $\dn$ &
|
|
1380 |
$\textit{ALTS}\,(bs\,@\,bs')\,as$\\
|
|
1381 |
$\textit{fuse}\;bs\,(\textit{SEQ}\,bs'\,a_1\,a_2)$ & $\dn$ &
|
|
1382 |
$\textit{SEQ}\,(bs\,@\,bs')\,a_1\,a_2$\\
|
|
1383 |
$\textit{fuse}\;bs\,(\textit{STAR}\,bs'\,a)$ & $\dn$ &
|
|
1384 |
$\textit{STAR}\,(bs\,@\,bs')\,a$
|
|
1385 |
\end{tabular}
|
|
1386 |
\end{center}
|
|
1387 |
|
|
1388 |
\noindent
|
|
1389 |
After internalising the regular expression, we perform successive
|
|
1390 |
derivative operations on the annotated regular expressions. This
|
|
1391 |
derivative operation is the same as what we had previously for the
|
|
1392 |
basic regular expressions, except that we beed to take care of
|
|
1393 |
the bitcodes:
|
|
1394 |
|
|
1395 |
%\begin{definition}{bder}
|
|
1396 |
\begin{center}
|
|
1397 |
\begin{tabular}{@{}lcl@{}}
|
|
1398 |
$(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\
|
|
1399 |
$(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\
|
|
1400 |
$(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
|
|
1401 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
1402 |
\textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\
|
|
1403 |
$(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
|
|
1404 |
$\textit{ALTS}\;bs\,(as.map(\backslash c))$\\
|
|
1405 |
$(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
|
|
1406 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
1407 |
& &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
|
|
1408 |
& &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
|
|
1409 |
& &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
|
|
1410 |
$(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
|
|
1411 |
$\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
|
|
1412 |
(\textit{STAR}\,[]\,r)$
|
|
1413 |
\end{tabular}
|
|
1414 |
\end{center}
|
|
1415 |
%\end{definition}
|
|
1416 |
|
|
1417 |
\noindent
|
|
1418 |
For instance, when we unfold $\textit{STAR} \; bs \; a$ into a sequence,
|
|
1419 |
we need to attach an additional bit $Z$ to the front of $r \backslash c$
|
|
1420 |
to indicate that there is one more star iteration. Also the $SEQ$ clause
|
|
1421 |
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
|
|
1422 |
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
|
|
1423 |
that it is for annotated regular expressions, therefore we omit the
|
|
1424 |
definition). Assume that $bmkeps$ correctly extracts the bitcode for how
|
|
1425 |
$a_1$ matches the string prior to character $c$ (more on this later),
|
|
1426 |
then the right branch of $ALTS$, which is $fuse \; bmkeps \; a_1 (a_2
|
|
1427 |
\backslash c)$ will collapse the regular expression $a_1$(as it has
|
|
1428 |
already been fully matched) and store the parsing information at the
|
|
1429 |
head of the regular expression $a_2 \backslash c$ by fusing to it. The
|
|
1430 |
bitsequence $bs$, which was initially attached to the head of $SEQ$, has
|
|
1431 |
now been elevated to the top-level of $ALTS$, as this information will be
|
|
1432 |
needed whichever way the $SEQ$ is matched---no matter whether $c$ belongs
|
|
1433 |
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
|
|
1434 |
the lexing information, we complete the lexing by collecting the
|
|
1435 |
bitcodes using a generalised version of the $\textit{mkeps}$ function
|
|
1436 |
for annotated regular expressions, called $\textit{bmkeps}$:
|
|
1437 |
|
|
1438 |
|
|
1439 |
%\begin{definition}[\textit{bmkeps}]\mbox{}
|
|
1440 |
\begin{center}
|
|
1441 |
\begin{tabular}{lcl}
|
|
1442 |
$\textit{bmkeps}\,(\textit{ONE}\;bs)$ & $\dn$ & $bs$\\
|
|
1443 |
$\textit{bmkeps}\,(\textit{ALTS}\;bs\,a::as)$ & $\dn$ &
|
|
1444 |
$\textit{if}\;\textit{bnullable}\,a$\\
|
|
1445 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
|
|
1446 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(\textit{ALTS}\;bs\,as)$\\
|
|
1447 |
$\textit{bmkeps}\,(\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ &
|
|
1448 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
|
|
1449 |
$\textit{bmkeps}\,(\textit{STAR}\;bs\,a)$ & $\dn$ &
|
|
1450 |
$bs \,@\, [\S]$
|
|
1451 |
\end{tabular}
|
|
1452 |
\end{center}
|
|
1453 |
%\end{definition}
|
|
1454 |
|
|
1455 |
\noindent
|
|
1456 |
This function completes the value information by travelling along the
|
|
1457 |
path of the regular expression that corresponds to a POSIX value and
|
|
1458 |
collecting all the bitcodes, and using $S$ to indicate the end of star
|
|
1459 |
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
|
|
1460 |
decode them, we get the value we expect. The corresponding lexing
|
|
1461 |
algorithm looks as follows:
|
|
1462 |
|
|
1463 |
\begin{center}
|
|
1464 |
\begin{tabular}{lcl}
|
|
1465 |
$\textit{blexer}\;r\,s$ & $\dn$ &
|
|
1466 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\
|
|
1467 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
1468 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
1469 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
1470 |
\end{tabular}
|
|
1471 |
\end{center}
|
|
1472 |
|
|
1473 |
\noindent
|
|
1474 |
In this definition $\_\backslash s$ is the generalisation of the derivative
|
|
1475 |
operation from characters to strings (just like the derivatives for un-annotated
|
|
1476 |
regular expressions).
|
|
1477 |
|
|
1478 |
The main point of the bitcodes and annotated regular expressions is that
|
|
1479 |
we can apply rather aggressive (in terms of size) simplification rules
|
|
1480 |
in order to keep derivatives small. We have developed such
|
|
1481 |
``aggressive'' simplification rules and generated test data that show
|
|
1482 |
that the expected bound can be achieved. Obviously we could only
|
|
1483 |
partially cover the search space as there are infinitely many regular
|
|
1484 |
expressions and strings.
|
|
1485 |
|
|
1486 |
One modification we introduced is to allow a list of annotated regular
|
|
1487 |
expressions in the \textit{ALTS} constructor. This allows us to not just
|
|
1488 |
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
|
|
1489 |
also unnecessary ``copies'' of regular expressions (very similar to
|
|
1490 |
simplifying $r + r$ to just $r$, but in a more general setting). Another
|
|
1491 |
modification is that we use simplification rules inspired by Antimirov's
|
|
1492 |
work on partial derivatives. They maintain the idea that only the first
|
|
1493 |
``copy'' of a regular expression in an alternative contributes to the
|
|
1494 |
calculation of a POSIX value. All subsequent copies can be pruned away from
|
|
1495 |
the regular expression. A recursive definition of our simplification function
|
|
1496 |
that looks somewhat similar to our Scala code is given below:
|
|
1497 |
%\comment{Use $\ZERO$, $\ONE$ and so on.
|
|
1498 |
%Is it $ALTS$ or $ALTS$?}\\
|
|
1499 |
|
|
1500 |
\begin{center}
|
|
1501 |
\begin{tabular}{@{}lcl@{}}
|
|
1502 |
|
|
1503 |
$\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp} \; a_2) \; \textit{match} $ \\
|
|
1504 |
&&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow \ZERO$ \\
|
|
1505 |
&&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow \ZERO$ \\
|
|
1506 |
&&$\quad\textit{case} \; (\ONE, a_2') \Rightarrow \textit{fuse} \; bs \; a_2'$ \\
|
|
1507 |
&&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow \textit{fuse} \; bs \; a_1'$ \\
|
|
1508 |
&&$\quad\textit{case} \; (a_1', a_2') \Rightarrow \textit{SEQ} \; bs \; a_1' \; a_2'$ \\
|
|
1509 |
|
|
1510 |
$\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
|
|
1511 |
&&$\quad\textit{case} \; [] \Rightarrow \ZERO$ \\
|
|
1512 |
&&$\quad\textit{case} \; a :: [] \Rightarrow \textit{fuse bs a}$ \\
|
|
1513 |
&&$\quad\textit{case} \; as' \Rightarrow \textit{ALTS}\;bs\;as'$\\
|
|
1514 |
|
|
1515 |
$\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$
|
|
1516 |
\end{tabular}
|
|
1517 |
\end{center}
|
|
1518 |
|
|
1519 |
\noindent
|
|
1520 |
The simplification does a pattern matching on the regular expression.
|
|
1521 |
When it detected that the regular expression is an alternative or
|
|
1522 |
sequence, it will try to simplify its children regular expressions
|
|
1523 |
recursively and then see if one of the children turn into $\ZERO$ or
|
|
1524 |
$\ONE$, which might trigger further simplification at the current level.
|
|
1525 |
The most involved part is the $\textit{ALTS}$ clause, where we use two
|
|
1526 |
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
|
|
1527 |
$\textit{ALTS}$ and reduce as many duplicates as possible. Function
|
|
1528 |
$\textit{distinct}$ keeps the first occurring copy only and remove all later ones
|
|
1529 |
when detected duplicates. Function $\textit{flatten}$ opens up nested \textit{ALTS}.
|
|
1530 |
Its recursive definition is given below:
|
|
1531 |
|
|
1532 |
\begin{center}
|
|
1533 |
\begin{tabular}{@{}lcl@{}}
|
|
1534 |
$\textit{flatten} \; (\textit{ALTS}\;bs\,as) :: as'$ & $\dn$ & $(\textit{map} \;
|
|
1535 |
(\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
|
|
1536 |
$\textit{flatten} \; \textit{ZERO} :: as'$ & $\dn$ & $ \textit{flatten} \; as' $ \\
|
|
1537 |
$\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; as'$ \quad(otherwise)
|
|
1538 |
\end{tabular}
|
|
1539 |
\end{center}
|
|
1540 |
|
|
1541 |
\noindent
|
|
1542 |
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
|
|
1543 |
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
|
|
1544 |
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
|
|
1545 |
|
|
1546 |
Suppose we apply simplification after each derivative step, and view
|
|
1547 |
these two operations as an atomic one: $a \backslash_{simp}\,c \dn
|
|
1548 |
\textit{simp}(a \backslash c)$. Then we can use the previous natural
|
|
1549 |
extension from derivative w.r.t.~character to derivative
|
|
1550 |
w.r.t.~string:%\comment{simp in the [] case?}
|
|
1551 |
|
|
1552 |
\begin{center}
|
|
1553 |
\begin{tabular}{lcl}
|
|
1554 |
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
|
|
1555 |
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
|
|
1556 |
\end{tabular}
|
|
1557 |
\end{center}
|
|
1558 |
|
|
1559 |
\noindent
|
|
1560 |
we obtain an optimised version of the algorithm:
|
|
1561 |
|
|
1562 |
\begin{center}
|
|
1563 |
\begin{tabular}{lcl}
|
|
1564 |
$\textit{blexer\_simp}\;r\,s$ & $\dn$ &
|
|
1565 |
$\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\
|
|
1566 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
1567 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
1568 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
1569 |
\end{tabular}
|
|
1570 |
\end{center}
|
|
1571 |
|
|
1572 |
\noindent
|
|
1573 |
This algorithm keeps the regular expression size small, for example,
|
|
1574 |
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
|
|
1575 |
will be reduced to just 6 and stays constant, no matter how long the
|
|
1576 |
input string is.
|
|
1577 |
|
|
1578 |
|
|
1579 |
|
|
1580 |
\section{Current Work}
|
|
1581 |
|
|
1582 |
We are currently engaged in two tasks related to this algorithm. The
|
|
1583 |
first task is proving that our simplification rules actually do not
|
|
1584 |
affect the POSIX value that should be generated by the algorithm
|
|
1585 |
according to the specification of a POSIX value and furthermore obtain a
|
|
1586 |
much tighter bound on the sizes of derivatives. The result is that our
|
|
1587 |
algorithm should be correct and faster on all inputs. The original
|
|
1588 |
blow-up, as observed in JavaScript, Python and Java, would be excluded
|
|
1589 |
from happening in our algorithm. For this proof we use the theorem prover
|
|
1590 |
Isabelle. Once completed, this result will advance the state-of-the-art:
|
|
1591 |
Sulzmann and Lu wrote in their paper~\cite{Sulzmann2014} about the
|
|
1592 |
bitcoded ``incremental parsing method'' (that is the lexing algorithm
|
|
1593 |
outlined in this section):
|
|
1594 |
|
|
1595 |
\begin{quote}\it
|
|
1596 |
``Correctness Claim: We further claim that the incremental parsing
|
|
1597 |
method in Figure~5 in combination with the simplification steps in
|
|
1598 |
Figure 6 yields POSIX parse tree [our lexical values]. We have tested this claim
|
|
1599 |
extensively by using the method in Figure~3 as a reference but yet
|
|
1600 |
have to work out all proof details.''
|
|
1601 |
\end{quote}
|
|
1602 |
|
|
1603 |
\noindent
|
|
1604 |
We like to settle this correctness claim. It is relatively
|
|
1605 |
straightforward to establish that after one simplification step, the part of a
|
|
1606 |
nullable derivative that corresponds to a POSIX value remains intact and can
|
|
1607 |
still be collected, in other words, we can show that
|
|
1608 |
%\comment{Double-check....I
|
|
1609 |
%think this is not the case}
|
|
1610 |
%\comment{If i remember correctly, you have proved this lemma.
|
|
1611 |
%I feel this is indeed not true because you might place arbitrary
|
|
1612 |
%bits on the regex r, however if this is the case, did i remember wrongly that
|
|
1613 |
%you proved something like simplification does not affect $\textit{bmkeps}$ results?
|
|
1614 |
%Anyway, i have amended this a little bit so it does not allow arbitrary bits attached
|
|
1615 |
%to a regex. Maybe it works now.}
|
|
1616 |
|
|
1617 |
\begin{center}
|
|
1618 |
$\textit{bmkeps} \; a = \textit{bmkeps} \; \textit{bsimp} \; a\;($\textit{provided}$ \; a\; is \; \textit{bnullable} )$
|
|
1619 |
\end{center}
|
|
1620 |
|
|
1621 |
\noindent
|
|
1622 |
as this basically comes down to proving actions like removing the
|
|
1623 |
additional $r$ in $r+r$ does not delete important POSIX information in
|
|
1624 |
a regular expression. The hard part of this proof is to establish that
|
|
1625 |
|
|
1626 |
\begin{center}
|
|
1627 |
$ \textit{blexer}\_{simp}(r, \; s) = \textit{blexer}(r, \; s)$
|
|
1628 |
\end{center}
|
|
1629 |
%comment{This is not true either...look at the definion blexer/blexer-simp}
|
|
1630 |
|
|
1631 |
\noindent That is, if we do derivative on regular expression $r$ and then
|
|
1632 |
simplify it, and repeat this process until we exhaust the string, we get a
|
|
1633 |
regular expression $r''$($\textit{LHS}$) that provides the POSIX matching
|
|
1634 |
information, which is exactly the same as the result $r'$($\textit{RHS}$ of the
|
|
1635 |
normal derivative algorithm that only does derivative repeatedly and has no
|
|
1636 |
simplification at all. This might seem at first glance very unintuitive, as
|
|
1637 |
the $r'$ could be exponentially larger than $r''$, but can be explained in the
|
|
1638 |
following way: we are pruning away the possible matches that are not POSIX.
|
|
1639 |
Since there could be exponentially many
|
|
1640 |
non-POSIX matchings and only 1 POSIX matching, it
|
|
1641 |
is understandable that our $r''$ can be a lot smaller. we can still provide
|
|
1642 |
the same POSIX value if there is one. This is not as straightforward as the
|
|
1643 |
previous proposition, as the two regular expressions $r'$ and $r''$ might have
|
|
1644 |
become very different. The crucial point is to find the
|
|
1645 |
$\textit{POSIX}$ information of a regular expression and how it is modified,
|
|
1646 |
augmented and propagated
|
|
1647 |
during simplification in parallel with the regular expression that
|
|
1648 |
has not been simplified in the subsequent derivative operations. To aid this,
|
|
1649 |
we use the helper function retrieve described by Sulzmann and Lu:
|
|
1650 |
\begin{center}
|
|
1651 |
\begin{tabular}{@{}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
|
|
1652 |
$\textit{retrieve}\,(\textit{ONE}\,bs)\,\Empty$ & $\dn$ & $bs$\\
|
|
1653 |
$\textit{retrieve}\,(\textit{CHAR}\,bs\,c)\,(\Char\,d)$ & $\dn$ & $bs$\\
|
|
1654 |
$\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Left\,v)$ & $\dn$ &
|
|
1655 |
$bs \,@\, \textit{retrieve}\,a\,v$\\
|
|
1656 |
$\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Right\,v)$ & $\dn$ &
|
|
1657 |
$bs \,@\, \textit{retrieve}\,(\textit{ALTS}\,bs\,as)\,v$\\
|
|
1658 |
$\textit{retrieve}\,(\textit{SEQ}\,bs\,a_1\,a_2)\,(\Seq\,v_1\,v_2)$ & $\dn$ &
|
|
1659 |
$bs \,@\,\textit{retrieve}\,a_1\,v_1\,@\, \textit{retrieve}\,a_2\,v_2$\\
|
|
1660 |
$\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,[])$ & $\dn$ &
|
|
1661 |
$bs \,@\, [\S]$\\
|
|
1662 |
$\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,(v\!::\!vs))$ & $\dn$ &\\
|
|
1663 |
\multicolumn{3}{l}{
|
|
1664 |
\hspace{3cm}$bs \,@\, [\Z] \,@\, \textit{retrieve}\,a\,v\,@\,
|
|
1665 |
\textit{retrieve}\,(\textit{STAR}\,[]\,a)\,(\Stars\,vs)$}\\
|
|
1666 |
\end{tabular}
|
|
1667 |
\end{center}
|
|
1668 |
%\comment{Did not read further}\\
|
|
1669 |
This function assembles the bitcode
|
|
1670 |
%that corresponds to a lexical value for how
|
|
1671 |
%the current derivative matches the suffix of the string(the characters that
|
|
1672 |
%have not yet appeared, but will appear as the successive derivatives go on.
|
|
1673 |
%How do we get this "future" information? By the value $v$, which is
|
|
1674 |
%computed by a pass of the algorithm that uses
|
|
1675 |
%$inj$ as described in the previous section).
|
|
1676 |
using information from both the derivative regular expression and the
|
|
1677 |
value. Sulzmann and Lu poroposed this function, but did not prove
|
|
1678 |
anything about it. Ausaf and Urban used it to connect the bitcoded
|
|
1679 |
algorithm to the older algorithm by the following equation:
|
|
1680 |
|
|
1681 |
\begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\;
|
|
1682 |
(r^\uparrow)\backslash_{simp} \,c)\,v)$
|
|
1683 |
\end{center}
|
|
1684 |
|
|
1685 |
\noindent
|
|
1686 |
whereby $r^\uparrow$ stands for the internalised version of $r$. Ausaf
|
|
1687 |
and Urban also used this fact to prove the correctness of bitcoded
|
|
1688 |
algorithm without simplification. Our purpose of using this, however,
|
|
1689 |
is to establish
|
|
1690 |
|
|
1691 |
\begin{center}
|
|
1692 |
$ \textit{retrieve} \;
|
|
1693 |
a \; v \;=\; \textit{retrieve} \; (\textit{simp}\,a) \; v'.$
|
|
1694 |
\end{center}
|
|
1695 |
The idea is that using $v'$, a simplified version of $v$ that had gone
|
|
1696 |
through the same simplification step as $\textit{simp}(a)$, we are able
|
|
1697 |
to extract the bitcode that gives the same parsing information as the
|
|
1698 |
unsimplified one. However, we noticed that constructing such a $v'$
|
|
1699 |
from $v$ is not so straightforward. The point of this is that we might
|
|
1700 |
be able to finally bridge the gap by proving
|
|
1701 |
|
|
1702 |
\begin{center}
|
|
1703 |
$\textit{retrieve} \; (r^\uparrow \backslash s) \; v = \;\textit{retrieve} \;
|
|
1704 |
(\textit{simp}(r^\uparrow) \backslash s) \; v'$
|
|
1705 |
\end{center}
|
|
1706 |
|
|
1707 |
\noindent
|
|
1708 |
and subsequently
|
|
1709 |
|
|
1710 |
\begin{center}
|
|
1711 |
$\textit{retrieve} \; (r^\uparrow \backslash s) \; v\; = \; \textit{retrieve} \;
|
|
1712 |
(r^\uparrow \backslash_{simp} \, s) \; v'$.
|
|
1713 |
\end{center}
|
|
1714 |
|
|
1715 |
\noindent
|
|
1716 |
The $\textit{LHS}$ of the above equation is the bitcode we want. This
|
|
1717 |
would prove that our simplified version of regular expression still
|
|
1718 |
contains all the bitcodes needed. The task here is to find a way to
|
|
1719 |
compute the correct $v'$.
|
|
1720 |
|
|
1721 |
The second task is to speed up the more aggressive simplification. Currently
|
|
1722 |
it is slower than the original naive simplification by Ausaf and Urban (the
|
|
1723 |
naive version as implemented by Ausaf and Urban of course can ``explode'' in
|
|
1724 |
some cases). It is therefore not surprising that the speed is also much slower
|
|
1725 |
than regular expression engines in popular programming languages such as Java
|
|
1726 |
and Python on most inputs that are linear. For example, just by rewriting the
|
|
1727 |
example regular expression in the beginning of this report $(a^*)^*\,b$ into
|
|
1728 |
$a^*\,b$ would eliminate the ambiguity in the matching and make the time
|
|
1729 |
for matching linear with respect to the input string size. This allows the
|
|
1730 |
DFA approach to become blindingly fast, and dwarf the speed of our current
|
|
1731 |
implementation. For example, here is a comparison of Java regex engine
|
|
1732 |
and our implementation on this example.
|
|
1733 |
|
|
1734 |
\begin{center}
|
|
1735 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
1736 |
\begin{tikzpicture}
|
|
1737 |
\begin{axis}[
|
|
1738 |
xlabel={$n*1000$},
|
|
1739 |
x label style={at={(1.05,-0.05)}},
|
|
1740 |
ylabel={time in secs},
|
|
1741 |
enlargelimits=false,
|
|
1742 |
xtick={0,5,...,30},
|
|
1743 |
xmax=33,
|
|
1744 |
ymax=9,
|
|
1745 |
scaled ticks=true,
|
|
1746 |
axis lines=left,
|
|
1747 |
width=5cm,
|
|
1748 |
height=4cm,
|
|
1749 |
legend entries={Bitcoded Algorithm},
|
|
1750 |
legend pos=north west,
|
|
1751 |
legend cell align=left]
|
|
1752 |
\addplot[red,mark=*, mark options={fill=white}] table {bad-scala.data};
|
|
1753 |
\end{axis}
|
|
1754 |
\end{tikzpicture}
|
|
1755 |
&
|
|
1756 |
\begin{tikzpicture}
|
|
1757 |
\begin{axis}[
|
|
1758 |
xlabel={$n*1000$},
|
|
1759 |
x label style={at={(1.05,-0.05)}},
|
|
1760 |
%ylabel={time in secs},
|
|
1761 |
enlargelimits=false,
|
|
1762 |
xtick={0,5,...,30},
|
|
1763 |
xmax=33,
|
|
1764 |
ymax=9,
|
|
1765 |
scaled ticks=false,
|
|
1766 |
axis lines=left,
|
|
1767 |
width=5cm,
|
|
1768 |
height=4cm,
|
|
1769 |
legend entries={Java},
|
|
1770 |
legend pos=north west,
|
|
1771 |
legend cell align=left]
|
|
1772 |
\addplot[cyan,mark=*, mark options={fill=white}] table {good-java.data};
|
|
1773 |
\end{axis}
|
|
1774 |
\end{tikzpicture}\\
|
|
1775 |
\multicolumn{3}{c}{Graphs: Runtime for matching $a^*\,b$ with strings
|
|
1776 |
of the form $\underbrace{aa..a}_{n}$.}
|
|
1777 |
\end{tabular}
|
|
1778 |
\end{center}
|
|
1779 |
|
|
1780 |
|
|
1781 |
Java regex engine can match string of thousands of characters in a few milliseconds,
|
|
1782 |
whereas our current algorithm gets excruciatingly slow on input of this size.
|
|
1783 |
The running time in theory is linear, however it does not appear to be the
|
|
1784 |
case in an actual implementation. So it needs to be explored how to
|
|
1785 |
make our algorithm faster on all inputs. It could be the recursive calls that are
|
|
1786 |
needed to manipulate bits that are causing the slow down. A possible solution
|
|
1787 |
is to write recursive functions into tail-recusive form.
|
|
1788 |
Another possibility would be to explore
|
|
1789 |
again the connection to DFAs to speed up the algorithm on
|
|
1790 |
subcalls that are small enough. This is very much work in progress.
|
|
1791 |
|
|
1792 |
\section{Conclusion}
|
|
1793 |
|
|
1794 |
In this PhD-project we are interested in fast algorithms for regular
|
|
1795 |
expression matching. While this seems to be a ``settled'' area, in
|
|
1796 |
fact interesting research questions are popping up as soon as one steps
|
|
1797 |
outside the classic automata theory (for example in terms of what kind
|
|
1798 |
of regular expressions are supported). The reason why it is
|
|
1799 |
interesting for us to look at the derivative approach introduced by
|
|
1800 |
Brzozowski for regular expression matching, and then much further
|
|
1801 |
developed by Sulzmann and Lu, is that derivatives can elegantly deal
|
|
1802 |
with some of the regular expressions that are of interest in ``real
|
|
1803 |
life''. This includes the not-regular expression, written $\neg\,r$
|
|
1804 |
(that is all strings that are not recognised by $r$), but also bounded
|
|
1805 |
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is
|
|
1806 |
also hope that the derivatives can provide another angle for how to
|
|
1807 |
deal more efficiently with back-references, which are one of the
|
|
1808 |
reasons why regular expression engines in JavaScript, Python and Java
|
|
1809 |
choose to not implement the classic automata approach of transforming
|
|
1810 |
regular expressions into NFAs and then DFAs---because we simply do not
|
|
1811 |
know how such back-references can be represented by DFAs.
|
|
1812 |
We also plan to implement the bitcoded algorithm
|
|
1813 |
in some imperative language like C to see if the inefficiency of the
|
|
1814 |
Scala implementation
|
|
1815 |
is language specific. To make this research more comprehensive we also plan
|
|
1816 |
to contrast our (faster) version of bitcoded algorithm with the
|
|
1817 |
Symbolic Regex Matcher, the RE2, the Rust Regex Engine, and the static
|
|
1818 |
analysis approach by implementing them in the same language and then compare
|
|
1819 |
their performance.
|
|
1820 |
|
|
1821 |
\bibliographystyle{plain}
|
|
1822 |
\bibliography{root}
|
|
1823 |
|
|
1824 |
|
|
1825 |
\end{document}
|