34
+ − 1
\documentclass[a4paper,UKenglish]{lipics}
30
+ − 2
\usepackage{graphic}
+ − 3
\usepackage{data}
+ − 4
\usepackage{tikz-cd}
35
+ − 5
\usepackage{algorithm}
+ − 6
\usepackage{amsmath}
+ − 7
\usepackage[noend]{algpseudocode}
30
+ − 8
% \documentclass{article}
+ − 9
%\usepackage[utf8]{inputenc}
+ − 10
%\usepackage[english]{babel}
+ − 11
%\usepackage{listings}
+ − 12
% \usepackage{amsthm}
+ − 13
% \usepackage{hyperref}
+ − 14
% \usepackage[margin=0.5in]{geometry}
+ − 15
%\usepackage{pmboxdraw}
+ − 16
+ − 17
\title{POSIX Regular Expression Matching and Lexing}
+ − 18
\author{Chengsong Tan}
+ − 19
\affil{King's College London\\
+ − 20
London, UK\\
+ − 21
\texttt{chengsong.tan@kcl.ac.uk}}
+ − 22
\authorrunning{Chengsong Tan}
+ − 23
\Copyright{Chengsong Tan}
+ − 24
+ − 25
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
+ − 26
\newcommand{\ZERO}{\mbox{\bf 0}}
+ − 27
\newcommand{\ONE}{\mbox{\bf 1}}
+ − 28
\def\lexer{\mathit{lexer}}
+ − 29
\def\mkeps{\mathit{mkeps}}
+ − 30
\def\inj{\mathit{inj}}
+ − 31
\def\Empty{\mathit{Empty}}
+ − 32
\def\Left{\mathit{Left}}
+ − 33
\def\Right{\mathit{Right}}
+ − 34
\def\Stars{\mathit{Stars}}
+ − 35
\def\Char{\mathit{Char}}
+ − 36
\def\Seq{\mathit{Seq}}
+ − 37
\def\Der{\mathit{Der}}
+ − 38
\def\nullable{\mathit{nullable}}
+ − 39
\def\Z{\mathit{Z}}
+ − 40
\def\S{\mathit{S}}
+ − 41
+ − 42
%\theoremstyle{theorem}
+ − 43
%\newtheorem{theorem}{Theorem}
+ − 44
%\theoremstyle{lemma}
+ − 45
%\newtheorem{lemma}{Lemma}
+ − 46
%\newcommand{\lemmaautorefname}{Lemma}
+ − 47
%\theoremstyle{definition}
+ − 48
%\newtheorem{definition}{Definition}
35
+ − 49
\algnewcommand\algorithmicswitch{\textbf{switch}}
+ − 50
\algnewcommand\algorithmiccase{\textbf{case}}
+ − 51
\algnewcommand\algorithmicassert{\texttt{assert}}
+ − 52
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}%
+ − 53
% New "environments"
+ − 54
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
+ − 55
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
+ − 56
\algtext*{EndSwitch}%
+ − 57
\algtext*{EndCase}%
30
+ − 58
+ − 59
+ − 60
\begin{document}
+ − 61
+ − 62
\maketitle
+ − 63
+ − 64
\begin{abstract}
+ − 65
Brzozowski introduced in 1964 a beautifully simple algorithm for
+ − 66
regular expression matching based on the notion of derivatives of
+ − 67
regular expressions. In 2014, Sulzmann and Lu extended this
+ − 68
algorithm to not just give a YES/NO answer for whether or not a regular
+ − 69
expression matches a string, but in case it matches also \emph{how}
+ − 70
it matches the string. This is important for applications such as
+ − 71
lexing (tokenising a string). The problem is to make the algorithm
+ − 72
by Sulzmann and Lu fast on all inputs without breaking its
+ − 73
correctness.
+ − 74
\end{abstract}
+ − 75
+ − 76
\section{Introduction}
+ − 77
+ − 78
This PhD-project is about regular expression matching and
+ − 79
lexing. Given the maturity of this topic, the reader might wonder:
+ − 80
Surely, regular expressions must have already been studied to death?
+ − 81
What could possibly be \emph{not} known in this area? And surely all
+ − 82
implemented algorithms for regular expression matching are blindingly
+ − 83
fast?
+ − 84
+ − 85
+ − 86
+ − 87
Unfortunately these preconceptions are not supported by evidence: Take
+ − 88
for example the regular expression $(a^*)^*\,b$ and ask whether
+ − 89
strings of the form $aa..a$ match this regular
+ − 90
expression. Obviously they do not match---the expected $b$ in the last
+ − 91
position is missing. One would expect that modern regular expression
+ − 92
matching engines can find this out very quickly. Alas, if one tries
+ − 93
this example in JavaScript, Python or Java 8 with strings like 28
+ − 94
$a$'s, one discovers that this decision takes around 30 seconds and
+ − 95
takes considerably longer when adding a few more $a$'s, as the graphs
+ − 96
below show:
+ − 97
+ − 98
\begin{center}
+ − 99
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
+ − 100
\begin{tikzpicture}
+ − 101
\begin{axis}[
+ − 102
xlabel={$n$},
+ − 103
x label style={at={(1.05,-0.05)}},
+ − 104
ylabel={time in secs},
+ − 105
enlargelimits=false,
+ − 106
xtick={0,5,...,30},
+ − 107
xmax=33,
+ − 108
ymax=35,
+ − 109
ytick={0,5,...,30},
+ − 110
scaled ticks=false,
+ − 111
axis lines=left,
+ − 112
width=5cm,
+ − 113
height=4cm,
+ − 114
legend entries={JavaScript},
+ − 115
legend pos=north west,
+ − 116
legend cell align=left]
+ − 117
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
+ − 118
\end{axis}
+ − 119
\end{tikzpicture}
+ − 120
&
+ − 121
\begin{tikzpicture}
+ − 122
\begin{axis}[
+ − 123
xlabel={$n$},
+ − 124
x label style={at={(1.05,-0.05)}},
+ − 125
%ylabel={time in secs},
+ − 126
enlargelimits=false,
+ − 127
xtick={0,5,...,30},
+ − 128
xmax=33,
+ − 129
ymax=35,
+ − 130
ytick={0,5,...,30},
+ − 131
scaled ticks=false,
+ − 132
axis lines=left,
+ − 133
width=5cm,
+ − 134
height=4cm,
+ − 135
legend entries={Python},
+ − 136
legend pos=north west,
+ − 137
legend cell align=left]
+ − 138
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
+ − 139
\end{axis}
+ − 140
\end{tikzpicture}
+ − 141
&
+ − 142
\begin{tikzpicture}
+ − 143
\begin{axis}[
+ − 144
xlabel={$n$},
+ − 145
x label style={at={(1.05,-0.05)}},
+ − 146
%ylabel={time in secs},
+ − 147
enlargelimits=false,
+ − 148
xtick={0,5,...,30},
+ − 149
xmax=33,
+ − 150
ymax=35,
+ − 151
ytick={0,5,...,30},
+ − 152
scaled ticks=false,
+ − 153
axis lines=left,
+ − 154
width=5cm,
+ − 155
height=4cm,
+ − 156
legend entries={Java 8},
+ − 157
legend pos=north west,
+ − 158
legend cell align=left]
+ − 159
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
+ − 160
\end{axis}
+ − 161
\end{tikzpicture}\\
+ − 162
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings
+ − 163
of the form $\underbrace{aa..a}_{n}$.}
+ − 164
\end{tabular}
+ − 165
\end{center}
+ − 166
+ − 167
\noindent These are clearly abysmal and possibly surprising results.
+ − 168
One would expect these systems doing much better than that---after
+ − 169
all, given a DFA and a string, whether a string is matched by this DFA
+ − 170
should be linear.
+ − 171
+ − 172
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to
+ − 173
exhibit this ``exponential behaviour''. Unfortunately, such regular
+ − 174
expressions are not just a few ``outliers'', but actually they are
+ − 175
frequent enough that a separate name has been created for
+ − 176
them---\emph{evil regular expressions}. In empiric work, Davis et al
+ − 177
report that they have found thousands of such evil regular expressions
+ − 178
in the JavaScript and Python ecosystems \cite{Davis18}.
+ − 179
+ − 180
This exponential blowup sometimes causes real pain in real life:
+ − 181
for example on 20 July 2016 one evil regular expression brought the
+ − 182
webpage \href{http://stackexchange.com}{Stack Exchange} to its knees \footnote{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}.
+ − 183
In this instance, a regular expression intended to just trim white
+ − 184
spaces from the beginning and the end of a line actually consumed
+ − 185
massive amounts of CPU-resources and because of this the web servers
+ − 186
ground to a halt. This happened when a post with 20,000 white spaces
+ − 187
was submitted, but importantly the white spaces were neither at the
+ − 188
beginning nor at the end. As a result, the regular expression matching
+ − 189
engine needed to backtrack over many choices.
+ − 190
+ − 191
The underlying problem is that many ``real life'' regular expression
+ − 192
matching engines do not use DFAs for matching. This is because they
+ − 193
support regular expressions that are not covered by the classical
+ − 194
automata theory, and in this more general setting there are quite a
+ − 195
few research questions still unanswered and fast algorithms still need
+ − 196
to be developed.
+ − 197
+ − 198
There is also another under-researched problem to do with regular
+ − 199
expressions and lexing, i.e.~the process of breaking up strings into
+ − 200
sequences of tokens according to some regular expressions. In this
+ − 201
setting one is not just interested in whether or not a regular
+ − 202
expression matches a string, but if it matches also in \emph{how} it
+ − 203
matches the string. Consider for example a regular expression
+ − 204
$r_{key}$ for recognising keywords such as \textit{if}, \textit{then}
+ − 205
and so on; and a regular expression $r_{id}$ for recognising
+ − 206
identifiers (say, a single character followed by characters or
+ − 207
numbers). One can then form the compound regular expression
+ − 208
$(r_{key} + r_{id})^*$ and use it to tokenise strings. But then how
+ − 209
should the string \textit{iffoo} be tokenised? It could be tokenised
+ − 210
as a keyword followed by an identifier, or the entire string as a
+ − 211
single identifier. Similarly, how should the string \textit{if} be
+ − 212
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would
+ − 213
``fire''---so is it an identifier or a keyword? While in applications
+ − 214
there is a well-known strategy to decide these questions, called POSIX
+ − 215
matching, only relatively recently precise definitions of what POSIX
+ − 216
matching actually means have been formalised
+ − 217
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. Roughly,
+ − 218
POSIX matching means matching the longest initial substring and
+ − 219
in the case of a tie, the initial submatch is chosen according to some priorities attached to the
+ − 220
regular expressions (e.g.~keywords have a higher priority than
+ − 221
identifiers). This sounds rather simple, but according to Grathwohl et
+ − 222
al \cite[Page 36]{CrashCourse2014} this is not the case. They wrote:
+ − 223
+ − 224
\begin{quote}
+ − 225
\it{}``The POSIX strategy is more complicated than the greedy because of
+ − 226
the dependence on information about the length of matched strings in the
+ − 227
various subexpressions.''
+ − 228
\end{quote}
+ − 229
+ − 230
\noindent
+ − 231
This is also supported by evidence collected by Kuklewicz
+ − 232
\cite{Kuklewicz} who noticed that a number of POSIX regular expression
+ − 233
matchers calculate incorrect results.
+ − 234
+ − 235
Our focus is on an algorithm introduced by Sulzmann and Lu in 2014 for
+ − 236
regular expression matching according to the POSIX strategy
+ − 237
\cite{Sulzmann2014}. Their algorithm is based on an older algorithm by
+ − 238
Brzozowski from 1964 where he introduced the notion of derivatives of
+ − 239
regular expressions \cite{Brzozowski1964}. We shall briefly explain
+ − 240
the algorithms next.
+ − 241
+ − 242
\section{The Algorithms by Brzozowski, and Sulzmann and Lu}
+ − 243
+ − 244
Suppose regular expressions are given by the following grammar:
+ − 245
+ − 246
+ − 247
\begin{center}
35
+ − 248
%TODO
30
+ − 249
\begin{tabular}{@{}rrl@{}}
+ − 250
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
+ − 251
$r$ & $::=$ & $\ZERO$\\
+ − 252
& $\mid$ & $\ONE$ \\
+ − 253
& $\mid$ & $c$ \\
+ − 254
& $\mid$ & $r_1 \cdot r_2$\\
+ − 255
& $\mid$ & $r_1 + r_2$ \\
35
+ − 256
+ − 257
& $\mid$ & $r^*$
30
+ − 258
\end{tabular}
+ − 259
+ − 260
\end{center}
+ − 261
+ − 262
\noindent
+ − 263
The intended meaning of the regular expressions is as usual: $\ZERO$
+ − 264
cannot match any string, $\ONE$ can match the empty string, the
+ − 265
character regular expression $c$ can match the character $c$, and so
+ − 266
on. The brilliant contribution by Brzozowski is the notion of
+ − 267
\emph{derivatives} of regular expressions. The idea behind this
+ − 268
notion is as follows: suppose a regular expression $r$ can match a
+ − 269
string of the form $c\!::\! s$ (that is a list of characters starting
+ − 270
with $c$), what does the regular expression look like that can match
+ − 271
just $s$? Brzozowski gave a neat answer to this question. He defined
+ − 272
the following operation on regular expressions, written
+ − 273
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$):
+ − 274
+ − 275
\begin{center}
+ − 276
\begin{tabular}{lcl}
+ − 277
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\
+ − 278
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\
+ − 279
$d \backslash c$ & $\dn$ &
+ − 280
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
+ − 281
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
+ − 282
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, \epsilon \in L(r_1)$\\
+ − 283
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
+ − 284
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
+ − 285
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\
+ − 286
\end{tabular}
+ − 287
\end{center}
+ − 288
+ − 289
\noindent
+ − 290
The $\mathit{if}$ condition in the definition of $(r_1 \cdot r_2) \backslash c$ involves a membership testing: $\epsilon \overset{?}{\in} L(r_1)$.
+ − 291
Such testing is easily implemented by the following simple recursive function $\nullable(\_)$:
+ − 292
+ − 293
+ − 294
\begin{center}
+ − 295
\begin{tabular}{lcl}
+ − 296
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\
+ − 297
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\
+ − 298
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\
+ − 299
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
+ − 300
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
+ − 301
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\
+ − 302
\end{tabular}
+ − 303
\end{center}
+ − 304
+ − 305
%Assuming the classic notion of a
+ − 306
%\emph{language} of a regular expression, written $L(\_)$, t
+ − 307
The main
+ − 308
property of the derivative operation is that
+ − 309
+ − 310
\begin{center}
+ − 311
$c\!::\!s \in L(r)$ holds
+ − 312
if and only if $s \in L(r\backslash c)$.
+ − 313
\end{center}
+ − 314
+ − 315
\noindent
+ − 316
So if we want to find out whether a string $s$
+ − 317
matches with a regular expression $r$, build the derivatives of $r$
+ − 318
w.r.t.\ (in succession) all the characters of the string $s$. Finally,
+ − 319
test whether the resulting regular expression can match the empty
+ − 320
string. If yes, then $r$ matches $s$, and no in the negative
+ − 321
case.\\
+ − 322
If we define the successive derivative operation to be like this:
+ − 323
\begin{center}
+ − 324
\begin{tabular}{lcl}
+ − 325
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
+ − 326
$r \backslash \epsilon $ & $\dn$ & $r$
+ − 327
\end{tabular}
+ − 328
\end{center}
+ − 329
35
+ − 330
For us the main advantage is that derivatives can be
+ − 331
straightforwardly implemented in any functional programming language,
+ − 332
and are easily definable and reasoned about in theorem provers---the
+ − 333
definitions just consist of inductive datatypes and simple recursive
+ − 334
functions. Moreover, the notion of derivatives can be easily
+ − 335
generalised to cover extended regular expression constructors such as
+ − 336
the not-regular expression, written $\neg\,r$, or bounded repetitions
+ − 337
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so
+ − 338
straightforwardly realised within the classic automata approach.
+ − 339
30
+ − 340
+ − 341
We obtain a simple and elegant regular
+ − 342
expression matching algorithm:
+ − 343
\begin{definition}{matcher}
+ − 344
\[
+ − 345
match\;s\;r \;\dn\; nullable(r\backslash s)
+ − 346
\]
+ − 347
\end{definition}
+ − 348
+ − 349
+ − 350
+ − 351
One limitation, however, of Brzozowski's algorithm is that it only
+ − 352
produces a YES/NO answer for whether a string is being matched by a
+ − 353
regular expression. Sulzmann and Lu~\cite{Sulzmann2014} extended this
+ − 354
algorithm to allow generation of an actual matching, called a
+ − 355
\emph{value}.
+ − 356
+ − 357
\begin{center}
+ − 358
\begin{tabular}{c@{\hspace{20mm}}c}
+ − 359
\begin{tabular}{@{}rrl@{}}
+ − 360
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
+ − 361
$r$ & $::=$ & $\ZERO$\\
+ − 362
& $\mid$ & $\ONE$ \\
+ − 363
& $\mid$ & $c$ \\
+ − 364
& $\mid$ & $r_1 \cdot r_2$\\
+ − 365
& $\mid$ & $r_1 + r_2$ \\
+ − 366
\\
+ − 367
& $\mid$ & $r^*$ \\
+ − 368
\end{tabular}
+ − 369
&
+ − 370
\begin{tabular}{@{\hspace{0mm}}rrl@{}}
+ − 371
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
+ − 372
$v$ & $::=$ & \\
+ − 373
& & $\Empty$ \\
+ − 374
& $\mid$ & $\Char(c)$ \\
+ − 375
& $\mid$ & $\Seq\,v_1\, v_2$\\
+ − 376
& $\mid$ & $\Left(v)$ \\
+ − 377
& $\mid$ & $\Right(v)$ \\
+ − 378
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
+ − 379
\end{tabular}
+ − 380
\end{tabular}
+ − 381
\end{center}
+ − 382
+ − 383
\noindent
35
+ − 384
Here we put the regular expression and values of the same shape on the same level to illustrate the corresponding relation between them.
+ − 385
+ − 386
Values are a way of expressing parse trees(the tree structure that tells how a sub-regex matches a substring). For example, $\Seq\,v_1\, v_2$ tells us how the string $|v_1| \cdot |v_2|$ matches the regex $r_1 \cdot r_2$: $r_1$ matches $|v_1|$ and $r_2$ matches $|v_2|$. Exactly how these two are matched are contained in the sub-structure of $v_1$ and $v_2$. The flatten notation $| v |$ means extracting the characters in the value $v$ to form a string. For example, $|\mathit{Seq} \, \mathit{Char(c)} \, \mathit{Char(d)}|$ = $cd$.
30
+ − 387
+ − 388
To give a concrete example of how value works, consider the string $xy$ and the
+ − 389
regular expression $(x + (y + xy))^*$. We can view this regular
+ − 390
expression as a tree and if the string $xy$ is matched by two Star
+ − 391
``iterations'', then the $x$ is matched by the left-most alternative
+ − 392
in this tree and the $y$ by the right-left alternative. This suggests
+ − 393
to record this matching as
+ − 394
+ − 395
\begin{center}
+ − 396
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$
+ − 397
\end{center}
+ − 398
+ − 399
\noindent
+ − 400
where $\Stars$ records how many
+ − 401
iterations were used; and $\Left$, respectively $\Right$, which
+ − 402
alternative is used. The value for
+ − 403
matching $xy$ in a single ``iteration'', i.e.~the POSIX value,
+ − 404
would look as follows
+ − 405
+ − 406
\begin{center}
+ − 407
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$
+ − 408
\end{center}
+ − 409
+ − 410
\noindent
+ − 411
where $\Stars$ has only a single-element list for the single iteration
+ − 412
and $\Seq$ indicates that $xy$ is matched by a sequence regular
+ − 413
expression.
+ − 414
+ − 415
The contribution of Sulzmann and Lu is an extension of Brzozowski's
+ − 416
algorithm by a second phase (the first phase being building successive
+ − 417
derivatives). In this second phase, for every successful match the
35
+ − 418
corresponding POSIX value is computed. The whole process looks like the following diagram(the working flow of the simple matching algorithm that just gives a $YES/NO$ answer is provided on the right for the purpose of comparison):\\
30
+ − 419
\begin{tikzcd}
+ − 420
r_0 \arrow[r, "c_0"] \arrow[d] & r_1 \arrow[r, "c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
+ − 421
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]
+ − 422
\end{tikzcd}
35
+ − 423
30
+ − 424
We shall briefly explain this interesting process.\\ For the convenience of explanation, we have the following notations: the regular expression $r$ used for matching is also called $r_0$ and the string $s$ is composed of $n$ characters $c_0 c_1 ... c_{n-1}$.
+ − 425
First, we do the derivative operation on $r_0$, $r_1$, ..., using characters $c_0$, $c_1$, ... until we get the final derivative $r_n$.We test whether it is $nullable$ or not. If no we know immediately the string does not match the regex. If yes, we start building the parse tree incrementally. We first call $mkeps$(which stands for make epsilon--make the parse tree for the empty word epsilon) to construct the parse tree $v_n$ for how the final derivative $r_n$ matches the empty string:
+ − 426
+ − 427
After this, we inject back the characters one by one, in reverse order as they were chopped off, to build the parse tree $v_i$ for how the regex $r_i$ matches the string $s_i$($s_i$ means the string s with the first $i$ characters being chopped off) from the previous parse tree. After $n$ transformations, we get the parse tree for how $r_0$ matches $s$, exactly as we wanted.
+ − 428
An inductive proof can be routinely established.
+ − 429
We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}. Rather, we shall focus next on the
+ − 430
process of simplification of regular expressions, which is needed in
+ − 431
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann
+ − 432
and Lu's algorithms. This is where the PhD-project hopes to advance
+ − 433
the state-of-the-art.
+ − 434
+ − 435
+ − 436
\section{Simplification of Regular Expressions}
+ − 437
+ − 438
The main drawback of building successive derivatives according to
+ − 439
Brzozowski's definition is that they can grow very quickly in size.
+ − 440
This is mainly due to the fact that the derivative operation generates
+ − 441
often ``useless'' $\ZERO$s and $\ONE$s in derivatives. As a result,
+ − 442
if implemented naively both algorithms by Brzozowski and by Sulzmann
+ − 443
and Lu are excruciatingly slow. For example when starting with the
+ − 444
regular expression $(a + aa)^*$ and building 12 successive derivatives
+ − 445
w.r.t.~the character $a$, one obtains a derivative regular expression
+ − 446
with more than 8000 nodes (when viewed as a tree). Operations like
+ − 447
derivative and $\nullable$ need to traverse such trees and
+ − 448
consequently the bigger the size of the derivative the slower the
+ − 449
algorithm. Fortunately, one can simplify regular expressions after
+ − 450
each derivative step. Various simplifications of regular expressions
+ − 451
are possible, such as the simplifications of $\ZERO + r$,
+ − 452
$r + \ZERO$, $\ONE\cdot r$, $r \cdot \ONE$, and $r + r$ to just
+ − 453
$r$. These simplifications do not affect the answer for whether a
+ − 454
regular expression matches a string or not, but fortunately also do
+ − 455
not affect the POSIX strategy of how regular expressions match
+ − 456
strings---although the latter is much harder to establish. Some
+ − 457
initial results in this regard have been obtained in
+ − 458
\cite{AusafDyckhoffUrban2016}. However, what has not been achieved yet
+ − 459
is a very tight bound for the size. Such a tight bound is suggested by
+ − 460
work of Antimirov who proved that (partial) derivatives can be bound
+ − 461
by the number of characters contained in the initial regular
35
+ − 462
expression \cite{Antimirov95}.
+ − 463
+ − 464
Antimirov defined the "partial derivatives" of regular expressions to be this:
+ − 465
%TODO definition of partial derivatives
+ − 466
+ − 467
it is essentially a set of regular expressions that come from the sub-structure of the original regular expression.
+ − 468
Antimirov has proved a nice size bound of the size of partial derivatives. Roughly speaking the size will not exceed the fourth power of the number of nodes in that regular expression. Interestingly, we observed from experiment that after the simplification step, our regular expression has the same size or is smaller than the partial derivatives. This allows us to prove a tight bound on the size of regular expression during the running time of the algorithm if we can establish the connection between our simplification rules and partial derivatives.
+ − 469
+ − 470
%We believe, and have generated test
+ − 471
%data, that a similar bound can be obtained for the derivatives in
+ − 472
%Sulzmann and Lu's algorithm. Let us give some details about this next.
30
+ − 473
+ − 474
We first followed Sulzmann and Lu's idea of introducing
+ − 475
\emph{annotated regular expressions}~\cite{Sulzmann2014}. They are
+ − 476
defined by the following grammar:
+ − 477
+ − 478
\begin{center}
+ − 479
\begin{tabular}{lcl}
+ − 480
$\textit{a}$ & $::=$ & $\textit{ZERO}$\\
+ − 481
& $\mid$ & $\textit{ONE}\;\;bs$\\
+ − 482
& $\mid$ & $\textit{CHAR}\;\;bs\,c$\\
+ − 483
& $\mid$ & $\textit{ALTS}\;\;bs\,as$\\
+ − 484
& $\mid$ & $\textit{SEQ}\;\;bs\,a_1\,a_2$\\
+ − 485
& $\mid$ & $\textit{STAR}\;\;bs\,a$
+ − 486
\end{tabular}
+ − 487
\end{center}
+ − 488
+ − 489
\noindent
+ − 490
where $bs$ stands for bitsequences, and $as$ (in \textit{ALTS}) for a
+ − 491
list of annotated regular expressions. These bitsequences encode
+ − 492
information about the (POSIX) value that should be generated by the
+ − 493
Sulzmann and Lu algorithm. Bitcodes are essentially incomplete values.
+ − 494
This can be straightforwardly seen in the following transformation:
+ − 495
\begin{center}
+ − 496
\begin{tabular}{lcl}
+ − 497
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\
+ − 498
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
+ − 499
$\textit{code}(\Left\,v)$ & $\dn$ & $\Z :: code(v)$\\
+ − 500
$\textit{code}(\Right\,v)$ & $\dn$ & $\S :: code(v)$\\
+ − 501
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
+ − 502
$\textit{code}(\Stars\,[])$ & $\dn$ & $[\S]$\\
+ − 503
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $\Z :: code(v) \;@\;
+ − 504
code(\Stars\,vs)$
+ − 505
\end{tabular}
+ − 506
\end{center}
+ − 507
where $\Z$ and $\S$ are arbitrary names for the bits in the
+ − 508
bitsequences.
+ − 509
Here code encodes a value into a bitsequence by converting Left into $\Z$, Right into $\S$, the start point of a non-empty star iteration into $\S$, and the border where a local star terminates into $\Z$. This conversion is apparently lossy, as it throws away the character information, and does not decode the boundary between the two operands of the sequence constructor. Moreover, with only the bitcode we cannot even tell whether the $\S$s and $\Z$s are for $Left/Right$ or $Stars$. The reason for choosing this compact way of storing information is that the relatively small size of bits can be easily moved around during the lexing process. In order to recover the bitcode back into values, we will need the regular expression as the extra information and decode them back into value:\\
+ − 510
TODO: definition of decode
+ − 511
\\
+ − 512
+ − 513
To do lexing using annotated regular expressions, we shall first transform the
+ − 514
usual (un-annotated) regular expressions into annotated regular
+ − 515
expressions:\\
+ − 516
TODO: definition of internalise
+ − 517
\\
+ − 518
Then we do successive derivative operations on the annotated regular expression. This derivative operation is the same as what we previously have for the simple regular expressions, except that we take special care of the bits to store the parse tree information:\\
+ − 519
TODO: bder
+ − 520
\\
+ − 521
This way, we do not have to use an injection function and a second phase, but instead only need to collect the bits while running $mkeps$:
+ − 522
TODO: mkepsBC
+ − 523
\\
+ − 524
and then decode the bits using the regular expression. The whole process looks like this:\\
+ − 525
r
+ − 526
\\
+ − 527
+ − 528
The main point of the bitsequences and annotated regular expressions
+ − 529
is that we can apply rather aggressive (in terms of size)
+ − 530
simplification rules in order to keep derivatives small.
+ − 531
+ − 532
We have
+ − 533
developed such ``aggressive'' simplification rules and generated test
+ − 534
data that show that the expected bound can be achieved. Obviously we
+ − 535
could only partially cover the search space as there are infinitely
+ − 536
many regular expressions and strings. One modification we introduced
+ − 537
is to allow a list of annotated regular expressions in the
+ − 538
\textit{ALTS} constructor. This allows us to not just delete
+ − 539
unnecessary $\ZERO$s and $\ONE$s from regular expressions, but also
+ − 540
unnecessary ``copies'' of regular expressions (very similar to
+ − 541
simplifying $r + r$ to just $r$, but in a more general
35
+ − 542
setting).
+ − 543
A psuedocode version of our algorithm is given below:\\
+ − 544
+ − 545
\begin{algorithm}
+ − 546
\caption{simplification of annotated regular expression}\label{euclid}
+ − 547
\begin{algorithmic}[1]
+ − 548
\Procedure{$Simp$}{$areg$}
+ − 549
\Switch{$areg$}
+ − 550
\Case{$ALTS(bs, rs)$}
+ − 551
\For{\textit{rs[i] in array rs}}
+ − 552
\State $\textit{rs[i]} \gets$ \textit{Simp(rs[i])}
+ − 553
\EndFor
+ − 554
\For{\textit{rs[i] in array rs}}
+ − 555
\If{$rs[i] == ALTS(bs', rs')$}
+ − 556
\State $rs'' \gets$ attach bits $bs'$ to all elements in $rs'$
+ − 557
\State Insert $rs''$ into $rs$ at position $i$ ($rs[i]$ is destroyed, replaced by its list of children regular expressions)
+ − 558
\EndIf
+ − 559
\EndFor
+ − 560
\State Remove all duplicates in $rs$, only keeping the first copy for multiple occurrences of the same regular expression
+ − 561
\State Remove all $0$s in $rs$
+ − 562
\If{$ rs.length == 0$} \Return $ZERO$ \EndIf
+ − 563
\If {$ rs.length == 1$} \Return$ rs[0] $\EndIf
+ − 564
\EndCase
+ − 565
\Case{$SEQ(bs, r_1, r_2)$}
+ − 566
\If{$ r_1$ or $r_2$ is $ZERO$} \Return ZERO \EndIf
+ − 567
\State update $r_1$ and $r_2$ by attaching $bs$ to their front
+ − 568
\If {$r_1$ or $r_2$ is $ONE(bs')$} \Return $r_2$ or $r_1$ \EndIf
+ − 569
\EndCase
+ − 570
\Case{$Others$}
+ − 571
\Return $areg$ as it is
+ − 572
\EndCase
+ − 573
\EndSwitch
+ − 574
\EndProcedure
+ − 575
\end{algorithmic}
+ − 576
\end{algorithm}
+ − 577
+ − 578
+ − 579
Another modification is that we use simplification rules
30
+ − 580
inspired by Antimirov's work on partial derivatives. They maintain the
+ − 581
idea that only the first ``copy'' of a regular expression in an
+ − 582
alternative contributes to the calculation of a POSIX value. All
+ − 583
subsequent copies can be pruned from the regular expression.
+ − 584
35
+ − 585
30
+ − 586
We are currently engaged with proving that our simplification rules
+ − 587
actually do not affect the POSIX value that should be generated by the
+ − 588
algorithm according to the specification of a POSIX value and
+ − 589
furthermore that our derivatives stay small for all derivatives. For
+ − 590
this proof we use the theorem prover Isabelle. Once completed, this
+ − 591
result will advance the state-of-the-art: Sulzmann and Lu wrote in
+ − 592
their paper \cite{Sulzmann2014} about the bitcoded ``incremental
+ − 593
parsing method'' (that is the matching algorithm outlined in this
+ − 594
section):
+ − 595
+ − 596
\begin{quote}\it
+ − 597
``Correctness Claim: We further claim that the incremental parsing
+ − 598
method in Figure~5 in combination with the simplification steps in
+ − 599
Figure 6 yields POSIX parse trees. We have tested this claim
+ − 600
extensively by using the method in Figure~3 as a reference but yet
+ − 601
have to work out all proof details.''
+ − 602
\end{quote}
+ − 603
+ − 604
\noindent
+ − 605
We would settle the correctness claim and furthermore obtain a much
+ − 606
tighter bound on the sizes of derivatives. The result is that our
+ − 607
algorithm should be correct and faster on all inputs. The original
+ − 608
blow-up, as observed in JavaScript, Python and Java, would be excluded
+ − 609
from happening in our algorithm.
+ − 610
+ − 611
\section{Conclusion}
+ − 612
+ − 613
In this PhD-project we are interested in fast algorithms for regular
+ − 614
expression matching. While this seems to be a ``settled'' area, in
+ − 615
fact interesting research questions are popping up as soon as one steps
+ − 616
outside the classic automata theory (for example in terms of what kind
+ − 617
of regular expressions are supported). The reason why it is
+ − 618
interesting for us to look at the derivative approach introduced by
+ − 619
Brzozowski for regular expression matching, and then much further
+ − 620
developed by Sulzmann and Lu, is that derivatives can elegantly deal
+ − 621
with some of the regular expressions that are of interest in ``real
+ − 622
life''. This includes the not-regular expression, written $\neg\,r$
+ − 623
(that is all strings that are not recognised by $r$), but also bounded
+ − 624
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is
+ − 625
also hope that the derivatives can provide another angle for how to
+ − 626
deal more efficiently with back-references, which are one of the
+ − 627
reasons why regular expression engines in JavaScript, Python and Java
+ − 628
choose to not implement the classic automata approach of transforming
+ − 629
regular expressions into NFAs and then DFAs---because we simply do not
+ − 630
know how such back-references can be represented by DFAs.
+ − 631
+ − 632
+ − 633
\bibliographystyle{plain}
+ − 634
\bibliography{root}
+ − 635
+ − 636
+ − 637
\end{document}