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