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