532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
\chapter{Regular Expressions and POSIX Lexing} % Main chapter title
|
|
4 |
|
|
5 |
\label{Inj} % In chapter 2 \ref{Chapter2} we will introduce the concepts
|
|
6 |
%and notations we
|
564
|
7 |
% used for describing the lexing algorithm by Sulzmann and Lu,
|
|
8 |
%and then give the algorithm and its variant and discuss
|
532
|
9 |
%why more aggressive simplifications are needed.
|
|
10 |
|
538
|
11 |
In this chapter, we define the basic notions
|
|
12 |
for regular languages and regular expressions.
|
583
|
13 |
This is essentially a description in ``English''
|
637
|
14 |
the functions and datatypes of our formalisation in Isabelle/HOL.
|
|
15 |
We also define what $\POSIX$ lexing means,
|
638
|
16 |
followed by the first lexing algorithm by Sulzmanna and Lu \parencite{Sulzmann2014}
|
564
|
17 |
that produces the output conforming
|
622
|
18 |
to the $\POSIX$ standard\footnote{In what follows
|
583
|
19 |
we choose to use the Isabelle-style notation
|
564
|
20 |
for function applications, where
|
583
|
21 |
the parameters of a function are not enclosed
|
564
|
22 |
inside a pair of parentheses (e.g. $f \;x \;y$
|
|
23 |
instead of $f(x,\;y)$). This is mainly
|
622
|
24 |
to make the text visually more concise.}.
|
532
|
25 |
|
649
|
26 |
|
|
27 |
\section{Technical Overview}
|
|
28 |
|
|
29 |
Consider for example the regular expression $(a^*)^*\,b$ and
|
|
30 |
strings of the form $aa..a$. These strings cannot be matched by this regular
|
|
31 |
expression: Obviously the expected $b$ in the last
|
|
32 |
position is missing. One would assume that modern regular expression
|
|
33 |
matching engines can find this out very quickly. Surprisingly, if one tries
|
|
34 |
this example in JavaScript, Python or Java 8, even with small strings,
|
|
35 |
say of lenght of around 30 $a$'s,
|
|
36 |
the decision takes an absurd amount of time to finish (see graphs in figure \ref{fig:aStarStarb}).
|
|
37 |
The algorithms clearly show exponential behaviour, and as can be seen
|
|
38 |
is triggered by some relatively simple regular expressions.
|
|
39 |
Java 9 and newer
|
|
40 |
versions improve this behaviour somewhat, but are still slow compared
|
|
41 |
with the approach we are going to use in this thesis.
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
This superlinear blowup in regular expression engines
|
|
46 |
has caused grief in ``real life'' where it is
|
|
47 |
given the name ``catastrophic backtracking'' or ``evil'' regular expressions.
|
|
48 |
For example, on 20 July 2016 one evil
|
|
49 |
regular expression brought the webpage
|
|
50 |
\href{http://stackexchange.com}{Stack Exchange} to its
|
|
51 |
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}(Last accessed in 2019)}
|
|
52 |
In this instance, a regular expression intended to just trim white
|
|
53 |
spaces from the beginning and the end of a line actually consumed
|
|
54 |
massive amounts of CPU resources---causing the web servers to grind to a
|
|
55 |
halt. In this example, the time needed to process
|
|
56 |
the string was
|
|
57 |
$O(n^2)$ with respect to the string length $n$. This
|
|
58 |
quadratic overhead was enough for the homepage of Stack Exchange to
|
|
59 |
respond so slowly that the load balancer assumed a $\mathit{DoS}$
|
|
60 |
attack and therefore stopped the servers from responding to any
|
|
61 |
requests. This made the whole site become unavailable.
|
|
62 |
|
|
63 |
\begin{figure}[p]
|
|
64 |
\begin{center}
|
|
65 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{}}
|
|
66 |
\begin{tikzpicture}
|
|
67 |
\begin{axis}[
|
|
68 |
xlabel={$n$},
|
|
69 |
x label style={at={(1.05,-0.05)}},
|
|
70 |
ylabel={time in secs},
|
|
71 |
enlargelimits=false,
|
|
72 |
xtick={0,5,...,30},
|
|
73 |
xmax=33,
|
|
74 |
ymax=35,
|
|
75 |
ytick={0,5,...,30},
|
|
76 |
scaled ticks=false,
|
|
77 |
axis lines=left,
|
|
78 |
width=5cm,
|
|
79 |
height=4cm,
|
|
80 |
legend entries={JavaScript},
|
|
81 |
legend pos=north west,
|
|
82 |
legend cell align=left]
|
|
83 |
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
|
|
84 |
\end{axis}
|
|
85 |
\end{tikzpicture}
|
|
86 |
&
|
|
87 |
\begin{tikzpicture}
|
|
88 |
\begin{axis}[
|
|
89 |
xlabel={$n$},
|
|
90 |
x label style={at={(1.05,-0.05)}},
|
|
91 |
%ylabel={time in secs},
|
|
92 |
enlargelimits=false,
|
|
93 |
xtick={0,5,...,30},
|
|
94 |
xmax=33,
|
|
95 |
ymax=35,
|
|
96 |
ytick={0,5,...,30},
|
|
97 |
scaled ticks=false,
|
|
98 |
axis lines=left,
|
|
99 |
width=5cm,
|
|
100 |
height=4cm,
|
|
101 |
legend entries={Python},
|
|
102 |
legend pos=north west,
|
|
103 |
legend cell align=left]
|
|
104 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
|
|
105 |
\end{axis}
|
|
106 |
\end{tikzpicture}\\
|
|
107 |
\begin{tikzpicture}
|
|
108 |
\begin{axis}[
|
|
109 |
xlabel={$n$},
|
|
110 |
x label style={at={(1.05,-0.05)}},
|
|
111 |
ylabel={time in secs},
|
|
112 |
enlargelimits=false,
|
|
113 |
xtick={0,5,...,30},
|
|
114 |
xmax=33,
|
|
115 |
ymax=35,
|
|
116 |
ytick={0,5,...,30},
|
|
117 |
scaled ticks=false,
|
|
118 |
axis lines=left,
|
|
119 |
width=5cm,
|
|
120 |
height=4cm,
|
|
121 |
legend entries={Java 8},
|
|
122 |
legend pos=north west,
|
|
123 |
legend cell align=left]
|
|
124 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
|
|
125 |
\end{axis}
|
|
126 |
\end{tikzpicture}
|
|
127 |
&
|
|
128 |
\begin{tikzpicture}
|
|
129 |
\begin{axis}[
|
|
130 |
xlabel={$n$},
|
|
131 |
x label style={at={(1.05,-0.05)}},
|
|
132 |
%ylabel={time in secs},
|
|
133 |
enlargelimits=false,
|
|
134 |
xtick={0,5,...,30},
|
|
135 |
xmax=33,
|
|
136 |
ymax=35,
|
|
137 |
ytick={0,5,...,30},
|
|
138 |
scaled ticks=false,
|
|
139 |
axis lines=left,
|
|
140 |
width=5cm,
|
|
141 |
height=4cm,
|
|
142 |
legend entries={Dart},
|
|
143 |
legend pos=north west,
|
|
144 |
legend cell align=left]
|
|
145 |
\addplot[green,mark=*, mark options={fill=white}] table {re-dart.data};
|
|
146 |
\end{axis}
|
|
147 |
\end{tikzpicture}\\
|
|
148 |
\begin{tikzpicture}
|
|
149 |
\begin{axis}[
|
|
150 |
xlabel={$n$},
|
|
151 |
x label style={at={(1.05,-0.05)}},
|
|
152 |
ylabel={time in secs},
|
|
153 |
enlargelimits=false,
|
|
154 |
xtick={0,5,...,30},
|
|
155 |
xmax=33,
|
|
156 |
ymax=35,
|
|
157 |
ytick={0,5,...,30},
|
|
158 |
scaled ticks=false,
|
|
159 |
axis lines=left,
|
|
160 |
width=5cm,
|
|
161 |
height=4cm,
|
|
162 |
legend entries={Swift},
|
|
163 |
legend pos=north west,
|
|
164 |
legend cell align=left]
|
|
165 |
\addplot[purple,mark=*, mark options={fill=white}] table {re-swift.data};
|
|
166 |
\end{axis}
|
|
167 |
\end{tikzpicture}
|
|
168 |
&
|
|
169 |
\begin{tikzpicture}
|
|
170 |
\begin{axis}[
|
|
171 |
xlabel={$n$},
|
|
172 |
x label style={at={(1.05,-0.05)}},
|
|
173 |
%ylabel={time in secs},
|
|
174 |
enlargelimits=true,
|
|
175 |
%xtick={0,5000,...,40000},
|
|
176 |
%xmax=40000,
|
|
177 |
%ymax=35,
|
|
178 |
restrict x to domain*=0:40000,
|
|
179 |
restrict y to domain*=0:35,
|
|
180 |
%ytick={0,5,...,30},
|
|
181 |
%scaled ticks=false,
|
|
182 |
axis lines=left,
|
|
183 |
width=5cm,
|
|
184 |
height=4cm,
|
|
185 |
legend entries={Java9+},
|
|
186 |
legend pos=north west,
|
|
187 |
legend cell align=left]
|
|
188 |
\addplot[orange,mark=*, mark options={fill=white}] table {re-java9.data};
|
|
189 |
\end{axis}
|
|
190 |
\end{tikzpicture}\\
|
|
191 |
\multicolumn{2}{c}{Graphs}
|
|
192 |
\end{tabular}
|
|
193 |
\end{center}
|
|
194 |
\caption{Graphs showing runtime for matching $(a^*)^*\,b$ with strings
|
|
195 |
of the form $\protect\underbrace{aa..a}_{n}$ in various existing regular expression libraries.
|
|
196 |
The reason for their superlinear behaviour is that they do a depth-first-search
|
|
197 |
using NFAs.
|
|
198 |
If the string does not match, the regular expression matching
|
|
199 |
engine starts to explore all possibilities.
|
|
200 |
}\label{fig:aStarStarb}
|
|
201 |
\end{figure}\afterpage{\clearpage}
|
|
202 |
|
|
203 |
A more recent example is a global outage of all Cloudflare servers on 2 July
|
|
204 |
2019. A poorly written regular expression exhibited catastrophic backtracking
|
|
205 |
and exhausted CPUs that serve HTTP traffic. Although the outage
|
|
206 |
had several causes, at the heart was a regular expression that
|
|
207 |
was used to monitor network
|
|
208 |
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}(Last accessed in 2022)}
|
|
209 |
These problems with regular expressions
|
|
210 |
are not isolated events that happen
|
|
211 |
very rarely,
|
|
212 |
%but actually widespread.
|
|
213 |
%They occur so often that they have a
|
|
214 |
but they occur actually often enough that they have a
|
|
215 |
name: Regular-Expression-Denial-Of-Service (ReDoS)
|
|
216 |
attacks.
|
|
217 |
Davis et al. \cite{Davis18} detected more
|
|
218 |
than 1000 evil regular expressions
|
|
219 |
in Node.js, Python core libraries, npm and pypi.
|
|
220 |
They therefore concluded that evil regular expressions
|
|
221 |
are a real problem rather than just "a parlour trick".
|
|
222 |
|
|
223 |
The work in this thesis aims to address this issue
|
|
224 |
with the help of formal proofs.
|
|
225 |
We describe a lexing algorithm based
|
|
226 |
on Brzozowski derivatives with verified correctness
|
|
227 |
and a finiteness property for the size of derivatives
|
|
228 |
(which are all done in Isabelle/HOL).
|
|
229 |
Such properties %guarantee the absence of
|
|
230 |
are an important step in preventing
|
|
231 |
catastrophic backtracking once and for all.
|
|
232 |
We will give more details in the next sections
|
|
233 |
on (i) why the slow cases in graph \ref{fig:aStarStarb}
|
|
234 |
can occur in traditional regular expression engines
|
|
235 |
and (ii) why we choose our
|
|
236 |
approach based on Brzozowski derivatives and formal proofs.
|
|
237 |
|
|
238 |
|
|
239 |
\section{Preliminaries}%Regex, and the Problems with Regex Matchers}
|
|
240 |
Regular expressions and regular expression matchers
|
|
241 |
have clearly been studied for many, many years.
|
|
242 |
Theoretical results in automata theory state
|
|
243 |
that basic regular expression matching should be linear
|
|
244 |
w.r.t the input.
|
|
245 |
This assumes that the regular expression
|
|
246 |
$r$ was pre-processed and turned into a
|
|
247 |
deterministic finite automaton (DFA) before matching \cite{Sakarovitch2009}.
|
|
248 |
By basic we mean textbook definitions such as the one
|
|
249 |
below, involving only regular expressions for characters, alternatives,
|
|
250 |
sequences, and Kleene stars:
|
|
251 |
\[
|
|
252 |
r ::= c | r_1 + r_2 | r_1 \cdot r_2 | r^*
|
|
253 |
\]
|
|
254 |
Modern regular expression matchers used by programmers,
|
|
255 |
however,
|
|
256 |
support much richer constructs, such as bounded repetitions,
|
|
257 |
negations,
|
|
258 |
and back-references.
|
|
259 |
To differentiate, we use the word \emph{regex} to refer
|
|
260 |
to those expressions with richer constructs while reserving the
|
|
261 |
term \emph{regular expression}
|
|
262 |
for the more traditional meaning in formal languages theory.
|
|
263 |
We follow this convention
|
|
264 |
in this thesis.
|
|
265 |
In the future, we aim to support all the popular features of regexes,
|
|
266 |
but for this work we mainly look at basic regular expressions
|
|
267 |
and bounded repetitions.
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
%Most modern regex libraries
|
|
272 |
%the so-called PCRE standard (Peral Compatible Regular Expressions)
|
|
273 |
%has the back-references
|
|
274 |
Regexes come with a number of constructs
|
|
275 |
that make it more convenient for
|
|
276 |
programmers to write regular expressions.
|
|
277 |
Depending on the types of constructs
|
|
278 |
the task of matching and lexing with them
|
|
279 |
will have different levels of complexity.
|
|
280 |
Some of those constructs are syntactic sugars that are
|
|
281 |
simply short hand notations
|
|
282 |
that save the programmers a few keystrokes.
|
|
283 |
These will not cause problems for regex libraries.
|
|
284 |
For example the
|
|
285 |
non-binary alternative involving three or more choices just means:
|
|
286 |
\[
|
|
287 |
(a | b | c) \stackrel{means}{=} ((a + b)+ c)
|
|
288 |
\]
|
|
289 |
Similarly, the range operator
|
|
290 |
%used to express the alternative
|
|
291 |
%of all characters between its operands,
|
|
292 |
is just a concise way
|
|
293 |
of expressing an alternative of consecutive characters:
|
|
294 |
\[
|
|
295 |
[0~-9]\stackrel{means}{=} (0 | 1 | \ldots | 9 )
|
|
296 |
\]
|
|
297 |
for an alternative. The
|
|
298 |
wildcard character '$.$' is used to refer to any single character,
|
|
299 |
\[
|
|
300 |
. \stackrel{means}{=} [0-9a-zA-Z+-()*\&\ldots]
|
|
301 |
\]
|
|
302 |
except the newline.
|
|
303 |
|
|
304 |
\subsection{Bounded Repetitions}
|
|
305 |
More interesting are bounded repetitions, which can
|
|
306 |
make the regular expressions much
|
|
307 |
more compact.
|
|
308 |
Normally there are four kinds of bounded repetitions:
|
|
309 |
$r^{\{n\}}$, $r^{\{\ldots m\}}$, $r^{\{n\ldots \}}$ and $r^{\{n\ldots m\}}$
|
|
310 |
(where $n$ and $m$ are constant natural numbers).
|
|
311 |
Like the star regular expressions, the set of strings or language
|
|
312 |
a bounded regular expression can match
|
|
313 |
is defined using the power operation on sets:
|
|
314 |
\begin{center}
|
|
315 |
\begin{tabular}{lcl}
|
|
316 |
$L \; r^{\{n\}}$ & $\dn$ & $(L \; r)^n$\\
|
|
317 |
$L \; r^{\{\ldots m\}}$ & $\dn$ & $\bigcup_{0 \leq i \leq m}. (L \; r)^i$\\
|
|
318 |
$L \; r^{\{n\ldots \}}$ & $\dn$ & $\bigcup_{n \leq i}. (L \; r)^i$\\
|
|
319 |
$L \; r^{\{n \ldots m\}}$ & $\dn$ & $\bigcup_{n \leq i \leq m}. (L \; r)^i$
|
|
320 |
\end{tabular}
|
|
321 |
\end{center}
|
|
322 |
The attraction of bounded repetitions is that they can be
|
|
323 |
used to avoid a size blow up: for example $r^{\{n\}}$
|
|
324 |
is a shorthand for
|
|
325 |
the much longer regular expression:
|
|
326 |
\[
|
|
327 |
\underbrace{r\ldots r}_\text{n copies of r}.
|
|
328 |
\]
|
|
329 |
%Therefore, a naive algorithm that simply unfolds
|
|
330 |
%them into their desugared forms
|
|
331 |
%will suffer from at least an exponential runtime increase.
|
|
332 |
|
|
333 |
|
|
334 |
The problem with matching
|
|
335 |
such bounded repetitions
|
|
336 |
is that tools based on the classic notion of
|
|
337 |
automata need to expand $r^{\{n\}}$ into $n$ connected
|
|
338 |
copies of the automaton for $r$. This leads to very inefficient matching
|
|
339 |
algorithms or algorithms that consume large amounts of memory.
|
|
340 |
Implementations using $\DFA$s will
|
|
341 |
in such situations
|
|
342 |
either become excruciatingly slow
|
|
343 |
(for example Verbatim++ \cite{Verbatimpp}) or run
|
|
344 |
out of memory (for example $\mathit{LEX}$ and
|
|
345 |
$\mathit{JFLEX}$\footnote{LEX and JFLEX are lexer generators
|
|
346 |
in C and JAVA that generate $\mathit{DFA}$-based
|
|
347 |
lexers. The user provides a set of regular expressions
|
|
348 |
and configurations, and then
|
|
349 |
gets an output program encoding a minimized $\mathit{DFA}$
|
|
350 |
that can be compiled and run.
|
|
351 |
When given the above countdown regular expression,
|
|
352 |
a small $n$ (say 20) would result in a program representing a
|
|
353 |
DFA
|
|
354 |
with millions of states.}) for large counters.
|
|
355 |
A classic example for this phenomenon is the regular expression $(a+b)^* a (a+b)^{n}$
|
|
356 |
where the minimal DFA requires at least $2^{n+1}$ states.
|
|
357 |
For example, when $n$ is equal to 2,
|
|
358 |
the corresponding $\mathit{NFA}$ looks like:
|
|
359 |
\vspace{6mm}
|
|
360 |
\begin{center}
|
|
361 |
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
|
|
362 |
\node[state,initial] (q_0) {$q_0$};
|
|
363 |
\node[state, red] (q_1) [right=of q_0] {$q_1$};
|
|
364 |
\node[state, red] (q_2) [right=of q_1] {$q_2$};
|
|
365 |
\node[state, accepting, red](q_3) [right=of q_2] {$q_3$};
|
|
366 |
\path[->]
|
|
367 |
(q_0) edge node {a} (q_1)
|
|
368 |
edge [loop below] node {a,b} ()
|
|
369 |
(q_1) edge node {a,b} (q_2)
|
|
370 |
(q_2) edge node {a,b} (q_3);
|
|
371 |
\end{tikzpicture}
|
|
372 |
\end{center}
|
|
373 |
and when turned into a DFA by the subset construction
|
|
374 |
requires at least $2^3$ states.\footnote{The
|
|
375 |
red states are "countdown states" which count down
|
|
376 |
the number of characters needed in addition to the current
|
|
377 |
string to make a successful match.
|
|
378 |
For example, state $q_1$ indicates a match that has
|
|
379 |
gone past the $(a|b)^*$ part of $(a|b)^*a(a|b)^{\{2\}}$,
|
|
380 |
and just consumed the "delimiter" $a$ in the middle, and
|
|
381 |
needs to match 2 more iterations of $(a|b)$ to complete.
|
|
382 |
State $q_2$ on the other hand, can be viewed as a state
|
|
383 |
after $q_1$ has consumed 1 character, and just waits
|
|
384 |
for 1 more character to complete.
|
|
385 |
The state $q_3$ is the last (accepting) state, requiring 0
|
|
386 |
more characters.
|
|
387 |
Depending on the suffix of the
|
|
388 |
input string up to the current read location,
|
|
389 |
the states $q_1$ and $q_2$, $q_3$
|
|
390 |
may or may
|
|
391 |
not be active.
|
|
392 |
A $\mathit{DFA}$ for such an $\mathit{NFA}$ would
|
|
393 |
contain at least $2^3$ non-equivalent states that cannot be merged,
|
|
394 |
because the subset construction during determinisation will generate
|
|
395 |
all the elements in the power set $\mathit{Pow}\{q_1, q_2, q_3\}$.
|
|
396 |
Generalizing this to regular expressions with larger
|
|
397 |
bounded repetitions number, we have that
|
|
398 |
regexes shaped like $r^*ar^{\{n\}}$ when converted to $\mathit{DFA}$s
|
|
399 |
would require at least $2^{n+1}$ states, if $r$ itself contains
|
|
400 |
more than 1 string.
|
|
401 |
This is to represent all different
|
|
402 |
scenarios in which "countdown" states are active.}
|
|
403 |
|
|
404 |
|
|
405 |
Bounded repetitions are important because they
|
|
406 |
tend to occur frequently in practical use,
|
|
407 |
for example in the regex library RegExLib, in
|
|
408 |
the rules library of Snort \cite{Snort1999}\footnote{
|
|
409 |
Snort is a network intrusion detection (NID) tool
|
|
410 |
for monitoring network traffic.
|
|
411 |
The network security community curates a list
|
|
412 |
of malicious patterns written as regexes,
|
|
413 |
which is used by Snort's detection engine
|
|
414 |
to match against network traffic for any hostile
|
|
415 |
activities such as buffer overflow attacks.},
|
|
416 |
as well as in XML Schema definitions (XSDs).
|
|
417 |
According to Bj\"{o}rklund et al \cite{xml2015},
|
|
418 |
more than half of the
|
|
419 |
XSDs they found on the Maven.org central repository
|
|
420 |
have bounded regular expressions in them.
|
|
421 |
Often the counters are quite large, with the largest being
|
|
422 |
close to ten million.
|
|
423 |
A smaller sample XSD they gave
|
|
424 |
is:
|
|
425 |
\lstset{
|
|
426 |
basicstyle=\fontsize{8.5}{9}\ttfamily,
|
|
427 |
language=XML,
|
|
428 |
morekeywords={encoding,
|
|
429 |
xs:schema,xs:element,xs:complexType,xs:sequence,xs:attribute}
|
|
430 |
}
|
|
431 |
\begin{lstlisting}
|
|
432 |
<sequence minOccurs="0" maxOccurs="65535">
|
|
433 |
<element name="TimeIncr" type="mpeg7:MediaIncrDurationType"/>
|
|
434 |
<element name="MotionParams" type="float" minOccurs="2" maxOccurs="12"/>
|
|
435 |
</sequence>
|
|
436 |
\end{lstlisting}
|
|
437 |
This can be seen as the regex
|
|
438 |
$(ab^{2\ldots 12})^{0 \ldots 65535}$, where $a$ and $b$ are themselves
|
|
439 |
regular expressions
|
|
440 |
satisfying certain constraints (such as
|
|
441 |
satisfying the floating point number format).
|
|
442 |
It is therefore quite unsatisfying that
|
|
443 |
some regular expressions matching libraries
|
|
444 |
impose adhoc limits
|
|
445 |
for bounded regular expressions:
|
|
446 |
For example, in the regular expression matching library in the Go
|
|
447 |
language the regular expression $a^{1001}$ is not permitted, because no counter
|
|
448 |
can be above 1000, and in the built-in Rust regular expression library
|
|
449 |
expressions such as $a^{\{1000\}\{100\}\{5\}}$ give an error message
|
|
450 |
for being too big.
|
|
451 |
As Becchi and Crawley \cite{Becchi08} have pointed out,
|
|
452 |
the reason for these restrictions
|
|
453 |
is that they simulate a non-deterministic finite
|
|
454 |
automata (NFA) with a breadth-first search.
|
|
455 |
This way the number of active states could
|
|
456 |
be equal to the counter number.
|
|
457 |
When the counters are large,
|
|
458 |
the memory requirement could become
|
|
459 |
infeasible, and a regex engine
|
|
460 |
like in Go will reject this pattern straight away.
|
|
461 |
\begin{figure}[H]
|
|
462 |
\begin{center}
|
|
463 |
\begin{tikzpicture} [node distance = 2cm, on grid, auto]
|
|
464 |
|
|
465 |
\node (q0) [state, initial] {$0$};
|
|
466 |
\node (q1) [state, right = of q0] {$1$};
|
|
467 |
%\node (q2) [state, right = of q1] {$2$};
|
|
468 |
\node (qdots) [right = of q1] {$\ldots$};
|
|
469 |
\node (qn) [state, right = of qdots] {$n$};
|
|
470 |
\node (qn1) [state, right = of qn] {$n+1$};
|
|
471 |
\node (qn2) [state, right = of qn1] {$n+2$};
|
|
472 |
\node (qn3) [state, accepting, right = of qn2] {$n+3$};
|
|
473 |
|
|
474 |
\path [-stealth, thick]
|
|
475 |
(q0) edge [loop above] node {a} ()
|
|
476 |
(q0) edge node {a} (q1)
|
|
477 |
%(q1) edge node {.} (q2)
|
|
478 |
(q1) edge node {.} (qdots)
|
|
479 |
(qdots) edge node {.} (qn)
|
|
480 |
(qn) edge node {.} (qn1)
|
|
481 |
(qn1) edge node {b} (qn2)
|
|
482 |
(qn2) edge node {$c$} (qn3);
|
|
483 |
\end{tikzpicture}
|
|
484 |
%\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
|
|
485 |
% \node[state,initial] (q_0) {$0$};
|
|
486 |
% \node[state, ] (q_1) [right=of q_0] {$1$};
|
|
487 |
% \node[state, ] (q_2) [right=of q_1] {$2$};
|
|
488 |
% \node[state,
|
|
489 |
% \node[state, accepting, ](q_3) [right=of q_2] {$3$};
|
|
490 |
% \path[->]
|
|
491 |
% (q_0) edge node {a} (q_1)
|
|
492 |
% edge [loop below] node {a,b} ()
|
|
493 |
% (q_1) edge node {a,b} (q_2)
|
|
494 |
% (q_2) edge node {a,b} (q_3);
|
|
495 |
%\end{tikzpicture}
|
|
496 |
\end{center}
|
|
497 |
\caption{The example given by Becchi and Crawley
|
|
498 |
that NFA simulation can consume large
|
|
499 |
amounts of memory: $.^*a.^{\{n\}}bc$ matching
|
|
500 |
strings of the form $aaa\ldots aaaabc$.
|
|
501 |
When traversing in a breadth-first manner,
|
|
502 |
all states from 0 till $n+1$ will become active.}
|
|
503 |
\end{figure}
|
|
504 |
%Languages like $\mathit{Go}$ and $\mathit{Rust}$ use this
|
|
505 |
%type of $\mathit{NFA}$ simulation and guarantees a linear runtime
|
|
506 |
%in terms of input string length.
|
|
507 |
%TODO:try out these lexers
|
|
508 |
These problems can of course be solved in matching algorithms where
|
|
509 |
automata go beyond the classic notion and for instance include explicit
|
|
510 |
counters \cite{Turo_ov__2020}.
|
|
511 |
These solutions can be quite efficient,
|
|
512 |
with the ability to process
|
|
513 |
gigabits of strings input per second
|
|
514 |
even with large counters \cite{Becchi08}.
|
|
515 |
These practical solutions do not come with
|
|
516 |
formal guarantees, and as pointed out by
|
|
517 |
Kuklewicz \cite{KuklewiczHaskell}, can be error-prone.
|
|
518 |
%But formal reasoning about these automata especially in Isabelle
|
|
519 |
%can be challenging
|
|
520 |
%and un-intuitive.
|
|
521 |
%Therefore, we take correctness and runtime claims made about these solutions
|
|
522 |
%with a grain of salt.
|
|
523 |
|
|
524 |
In the work reported in \cite{FoSSaCS2023} and here,
|
|
525 |
we add better support using derivatives
|
|
526 |
for bounded regular expression $r^{\{n\}}$.
|
|
527 |
Our results
|
|
528 |
extend straightforwardly to
|
|
529 |
repetitions with intervals such as
|
|
530 |
$r^{\{n\ldots m\}}$.
|
|
531 |
The merit of Brzozowski derivatives (more on this later)
|
|
532 |
on this problem is that
|
|
533 |
it can be naturally extended to support bounded repetitions.
|
|
534 |
Moreover these extensions are still made up of only small
|
|
535 |
inductive datatypes and recursive functions,
|
|
536 |
making it handy to deal with them in theorem provers.
|
|
537 |
%The point here is that Brzozowski derivatives and the algorithms by Sulzmann and Lu can be
|
|
538 |
%straightforwardly extended to deal with bounded regular expressions
|
|
539 |
%and moreover the resulting code still consists of only simple
|
|
540 |
%recursive functions and inductive datatypes.
|
|
541 |
Finally, bounded regular expressions do not destroy our finite
|
|
542 |
boundedness property, which we shall prove later on.
|
|
543 |
|
|
544 |
|
|
545 |
|
|
546 |
|
|
547 |
|
|
548 |
\subsection{Back-References}
|
|
549 |
The other way to simulate an $\mathit{NFA}$ for matching is choosing
|
|
550 |
a single transition each time, keeping all the other options in
|
|
551 |
a queue or stack, and backtracking if that choice eventually
|
|
552 |
fails.
|
|
553 |
This method, often called a "depth-first-search",
|
|
554 |
is efficient in many cases, but could end up
|
|
555 |
with exponential run time.
|
|
556 |
The backtracking method is employed in regex libraries
|
|
557 |
that support \emph{back-references}, for example
|
|
558 |
in Java and Python.
|
|
559 |
%\section{Back-references and The Terminology Regex}
|
|
560 |
|
|
561 |
%When one constructs an $\NFA$ out of a regular expression
|
|
562 |
%there is often very little to be done in the first phase, one simply
|
|
563 |
%construct the $\NFA$ states based on the structure of the input regular expression.
|
|
564 |
|
|
565 |
%In the lexing phase, one can simulate the $\mathit{NFA}$ running in two ways:
|
|
566 |
%one by keeping track of all active states after consuming
|
|
567 |
%a character, and update that set of states iteratively.
|
|
568 |
%This can be viewed as a breadth-first-search of the $\mathit{NFA}$
|
|
569 |
%for a path terminating
|
|
570 |
%at an accepting state.
|
|
571 |
|
|
572 |
|
|
573 |
|
|
574 |
Consider the following regular expression where the sequence
|
|
575 |
operator is omitted for brevity:
|
|
576 |
\begin{center}
|
|
577 |
$r_1r_2r_3r_4$
|
|
578 |
\end{center}
|
|
579 |
In this example,
|
|
580 |
one could label sub-expressions of interest
|
|
581 |
by parenthesizing them and giving
|
|
582 |
them a number in the order in which their opening parentheses appear.
|
|
583 |
One possible way of parenthesizing and labelling is:
|
|
584 |
\begin{center}
|
|
585 |
$\underset{1}{(}r_1\underset{2}{(}r_2\underset{3}{(}r_3)\underset{4}{(}r_4)))$
|
|
586 |
\end{center}
|
|
587 |
The sub-expressions
|
|
588 |
$r_1r_2r_3r_4$, $r_1r_2r_3$, $r_3$ and $r_4$ are labelled
|
|
589 |
by 1 to 4, and can be ``referred back'' by their respective numbers.
|
|
590 |
%These sub-expressions are called "capturing groups".
|
|
591 |
To do so, one uses the syntax $\backslash i$
|
|
592 |
to denote that we want the sub-string
|
|
593 |
of the input matched by the i-th
|
|
594 |
sub-expression to appear again,
|
|
595 |
exactly the same as it first appeared:
|
|
596 |
\begin{center}
|
|
597 |
$\ldots\underset{\text{i-th lparen}}{(}{r_i})\ldots
|
|
598 |
\underset{s_i \text{ which just matched} \;r_i}{\backslash i} \ldots$
|
|
599 |
\end{center}
|
|
600 |
Once the sub-string $s_i$ for the sub-expression $r_i$
|
|
601 |
has been fixed, there is no variability on what the back-reference
|
|
602 |
label $\backslash i$ can be---it is tied to $s_i$.
|
|
603 |
The overall string will look like $\ldots s_i \ldots s_i \ldots $
|
|
604 |
%The backslash and number $i$ are the
|
|
605 |
%so-called "back-references".
|
|
606 |
%Let $e$ be an expression made of regular expressions
|
|
607 |
%and back-references. $e$ contains the expression $e_i$
|
|
608 |
%as its $i$-th capturing group.
|
|
609 |
%The semantics of back-reference can be recursively
|
|
610 |
%written as:
|
|
611 |
%\begin{center}
|
|
612 |
% \begin{tabular}{c}
|
|
613 |
% $L ( e \cdot \backslash i) = \{s @ s_i \mid s \in L (e)\quad s_i \in L(r_i)$\\
|
|
614 |
% $s_i\; \text{match of ($e$, $s$)'s $i$-th capturing group string}\}$
|
|
615 |
% \end{tabular}
|
|
616 |
%\end{center}
|
|
617 |
A concrete example
|
|
618 |
for back-references is
|
|
619 |
\begin{center}
|
|
620 |
$(.^*)\backslash 1$,
|
|
621 |
\end{center}
|
|
622 |
which matches
|
|
623 |
strings that can be split into two identical halves,
|
|
624 |
for example $\mathit{foofoo}$, $\mathit{ww}$ and so on.
|
|
625 |
Note that this is different from
|
|
626 |
repeating the sub-expression verbatim like
|
|
627 |
\begin{center}
|
|
628 |
$(.^*)(.^*)$,
|
|
629 |
\end{center}
|
|
630 |
which does not impose any restrictions on what strings the second
|
|
631 |
sub-expression $.^*$
|
|
632 |
might match.
|
|
633 |
Another example for back-references is
|
|
634 |
\begin{center}
|
|
635 |
$(.)(.)\backslash 2\backslash 1$
|
|
636 |
\end{center}
|
|
637 |
which matches four-character palindromes
|
|
638 |
like $abba$, $x??x$ and so on.
|
|
639 |
|
|
640 |
Back-references are a regex construct
|
|
641 |
that programmers find quite useful.
|
|
642 |
According to Becchi and Crawley \cite{Becchi08},
|
|
643 |
6\% of Snort rules (up until 2008) use them.
|
|
644 |
The most common use of back-references
|
|
645 |
is to express well-formed html files,
|
|
646 |
where back-references are convenient for matching
|
|
647 |
opening and closing tags like
|
|
648 |
\begin{center}
|
|
649 |
$\langle html \rangle \ldots \langle / html \rangle$
|
|
650 |
\end{center}
|
|
651 |
A regex describing such a format
|
|
652 |
is
|
|
653 |
\begin{center}
|
|
654 |
$\langle (.^+) \rangle \ldots \langle / \backslash 1 \rangle$
|
|
655 |
\end{center}
|
|
656 |
Despite being useful, the expressive power of regexes
|
|
657 |
go beyond regular languages
|
|
658 |
once back-references are included.
|
|
659 |
In fact, they allow the regex construct to express
|
|
660 |
languages that cannot be contained in context-free
|
|
661 |
languages either.
|
|
662 |
For example, the back-reference $(a^*)b\backslash1 b \backslash 1$
|
|
663 |
expresses the language $\{a^n b a^n b a^n\mid n \in \mathbb{N}\}$,
|
|
664 |
which cannot be expressed by
|
|
665 |
context-free grammars \cite{campeanu2003formal}.
|
|
666 |
Such a language is contained in the context-sensitive hierarchy
|
|
667 |
of formal languages.
|
|
668 |
Also solving the matching problem involving back-references
|
|
669 |
is known to be NP-complete \parencite{alfred2014algorithms}.
|
|
670 |
Regex libraries supporting back-references such as
|
|
671 |
PCRE \cite{pcre} therefore have to
|
|
672 |
revert to a depth-first search algorithm including backtracking.
|
|
673 |
What is unexpected is that even in the cases
|
|
674 |
not involving back-references, there is still
|
|
675 |
a (non-negligible) chance they might backtrack super-linearly,
|
|
676 |
as shown in the graphs in figure \ref{fig:aStarStarb}.
|
|
677 |
|
|
678 |
Summing up, we can categorise existing
|
|
679 |
practical regex libraries into two kinds:
|
|
680 |
(i) The ones with linear
|
|
681 |
time guarantees like Go and Rust. The downside with them is that
|
|
682 |
they impose restrictions
|
|
683 |
on the regular expressions (not allowing back-references,
|
|
684 |
bounded repetitions cannot exceed an ad hoc limit etc.).
|
|
685 |
And (ii) those
|
|
686 |
that allow large bounded regular expressions and back-references
|
|
687 |
at the expense of using backtracking algorithms.
|
|
688 |
They can potentially ``grind to a halt''
|
|
689 |
on some very simple cases, resulting
|
|
690 |
ReDoS attacks if exposed to the internet.
|
|
691 |
|
|
692 |
The problems with both approaches are the motivation for us
|
|
693 |
to look again at the regular expression matching problem.
|
|
694 |
Another motivation is that regular expression matching algorithms
|
|
695 |
that follow the POSIX standard often contain errors and bugs
|
|
696 |
as we shall explain next.
|
|
697 |
|
|
698 |
%We would like to have regex engines that can
|
|
699 |
%deal with the regular part (e.g.
|
|
700 |
%bounded repetitions) of regexes more
|
|
701 |
%efficiently.
|
|
702 |
%Also we want to make sure that they do it correctly.
|
|
703 |
%It turns out that such aim is not so easy to achieve.
|
|
704 |
%TODO: give examples such as RE2 GOLANG 1000 restriction, rust no repetitions
|
|
705 |
% For example, the Rust regex engine claims to be linear,
|
|
706 |
% but does not support lookarounds and back-references.
|
|
707 |
% The GoLang regex library does not support over 1000 repetitions.
|
|
708 |
% Java and Python both support back-references, but shows
|
|
709 |
%catastrophic backtracking behaviours on inputs without back-references(
|
|
710 |
%when the language is still regular).
|
|
711 |
%TODO: test performance of Rust on (((((a*a*)b*)b){20})*)c baabaabababaabaaaaaaaaababaaaababababaaaabaaabaaaaaabaabaabababaababaaaaaaaaababaaaababababaaaaaaaaaaaaac
|
|
712 |
%TODO: verify the fact Rust does not allow 1000+ reps
|
|
713 |
|
|
714 |
|
|
715 |
|
|
716 |
|
|
717 |
%The time cost of regex matching algorithms in general
|
|
718 |
%involve two different phases, and different things can go differently wrong on
|
|
719 |
%these phases.
|
|
720 |
%$\DFA$s usually have problems in the first (construction) phase
|
|
721 |
%, whereas $\NFA$s usually run into trouble
|
|
722 |
%on the second phase.
|
|
723 |
|
|
724 |
|
|
725 |
\section{Error-prone POSIX Implementations}
|
|
726 |
Very often there are multiple ways of matching a string
|
|
727 |
with a regular expression.
|
|
728 |
In such cases the regular expressions matcher needs to
|
|
729 |
disambiguate.
|
|
730 |
The more widely used strategy is called POSIX,
|
|
731 |
which roughly speaking always chooses the longest initial match.
|
|
732 |
The POSIX strategy is widely adopted in many regular expression matchers
|
|
733 |
because it is a natural strategy for lexers.
|
|
734 |
However, many implementations (including the C libraries
|
|
735 |
used by Linux and OS X distributions) contain bugs
|
|
736 |
or do not meet the specification they claim to adhere to.
|
|
737 |
Kuklewicz maintains a unit test repository which lists some
|
|
738 |
problems with existing regular expression engines \cite{KuklewiczHaskell}.
|
|
739 |
In some cases, they either fail to generate a
|
|
740 |
result when there exists a match,
|
|
741 |
or give results that are inconsistent with the POSIX standard.
|
|
742 |
A concrete example is the regex:
|
|
743 |
\begin{center}
|
|
744 |
$(aba + ab + a)^* \text{and the string} \; ababa$
|
|
745 |
\end{center}
|
|
746 |
The correct POSIX match for the above
|
|
747 |
involves the entire string $ababa$,
|
|
748 |
split into two Kleene star iterations, namely $[ab], [aba]$ at positions
|
|
749 |
$[0, 2), [2, 5)$
|
|
750 |
respectively.
|
|
751 |
But feeding this example to the different engines
|
|
752 |
listed at regex101 \footnote{
|
|
753 |
regex101 is an online regular expression matcher which
|
|
754 |
provides API for trying out regular expression
|
|
755 |
engines of multiple popular programming languages like
|
|
756 |
Java, Python, Go, etc.} \parencite{regex101}.
|
|
757 |
yields
|
|
758 |
only two incomplete matches: $[aba]$ at $[0, 3)$
|
|
759 |
and $a$ at $[4, 5)$.
|
|
760 |
Fowler \cite{fowler2003} and Kuklewicz \cite{KuklewiczHaskell}
|
|
761 |
commented that most regex libraries are not
|
|
762 |
correctly implementing the central POSIX
|
|
763 |
rule, called the maximum munch rule.
|
|
764 |
Grathwohl \parencite{grathwohl2014crash} wrote:
|
|
765 |
\begin{quote}\it
|
|
766 |
``The POSIX strategy is more complicated than the
|
|
767 |
greedy because of the dependence on information about
|
|
768 |
the length of matched strings in the various subexpressions.''
|
|
769 |
\end{quote}
|
|
770 |
%\noindent
|
|
771 |
People have recognised that the implementation complexity of POSIX rules also come from
|
|
772 |
the specification being not very precise.
|
|
773 |
The developers of the regexp package of Go
|
|
774 |
\footnote{\url{https://pkg.go.dev/regexp\#pkg-overview}}
|
|
775 |
commented that
|
|
776 |
\begin{quote}\it
|
|
777 |
``
|
|
778 |
The POSIX rule is computationally prohibitive
|
|
779 |
and not even well-defined.
|
|
780 |
``
|
|
781 |
\end{quote}
|
|
782 |
There are many informal summaries of this disambiguation
|
|
783 |
strategy, which are often quite long and delicate.
|
|
784 |
For example Kuklewicz \cite{KuklewiczHaskell}
|
|
785 |
described the POSIX rule as (section 1, last paragraph):
|
|
786 |
\begin{quote}
|
|
787 |
\begin{itemize}
|
|
788 |
\item
|
|
789 |
regular expressions (REs) take the leftmost starting match, and the longest match starting there
|
|
790 |
earlier subpatterns have leftmost-longest priority over later subpatterns\\
|
|
791 |
\item
|
|
792 |
higher-level subpatterns have leftmost-longest priority over their component subpatterns\\
|
|
793 |
\item
|
|
794 |
REs have right associative concatenation which can be changed with parenthesis\\
|
|
795 |
\item
|
|
796 |
parenthesized subexpressions return the match from their last usage\\
|
|
797 |
\item
|
|
798 |
text of component subexpressions must be contained in the text of the
|
|
799 |
higher-level subexpressions\\
|
|
800 |
\item
|
|
801 |
if "p" and "q" can never match the same text then "p|q" and "q|p" are equivalent, up to trivial renumbering of captured subexpressions\\
|
|
802 |
\item
|
|
803 |
if "p" in "p*" is used to capture non-empty text then additional repetitions of "p" will not capture an empty string\\
|
|
804 |
\end{itemize}
|
|
805 |
\end{quote}
|
|
806 |
%The text above
|
|
807 |
%is trying to capture something very precise,
|
|
808 |
%and is crying out for formalising.
|
|
809 |
Ribeiro and Du Bois \cite{RibeiroAgda2017} have
|
|
810 |
formalised the notion of bit-coded regular expressions
|
|
811 |
and proved their relations with simple regular expressions in
|
|
812 |
the dependently-typed proof assistant Agda.
|
|
813 |
They also proved the soundness and completeness of a matching algorithm
|
|
814 |
based on the bit-coded regular expressions.
|
|
815 |
Ausaf et al. \cite{AusafDyckhoffUrban2016}
|
|
816 |
are the first to
|
|
817 |
give a quite simple formalised POSIX
|
|
818 |
specification in Isabelle/HOL, and also prove
|
|
819 |
that their specification coincides with an earlier (unformalised)
|
|
820 |
POSIX specification given by Okui and Suzuki \cite{Okui10}.
|
|
821 |
They then formally proved the correctness of
|
|
822 |
a lexing algorithm by Sulzmann and Lu \cite{Sulzmann2014}
|
|
823 |
with regards to that specification.
|
|
824 |
They also found that the informal POSIX
|
|
825 |
specification by Sulzmann and Lu needs to be substantially
|
|
826 |
revised in order for the correctness proof to go through.
|
|
827 |
Their original specification and proof were unfixable
|
|
828 |
according to Ausaf et al.
|
|
829 |
|
|
830 |
|
|
831 |
In the next section, we will briefly
|
|
832 |
introduce Brzozowski derivatives and Sulzmann
|
|
833 |
and Lu's algorithm, which the main point of this thesis builds on.
|
|
834 |
%We give a taste of what they
|
|
835 |
%are like and why they are suitable for regular expression
|
|
836 |
%matching and lexing.
|
|
837 |
\section{Formal Specification of POSIX Matching
|
|
838 |
and Brzozowski Derivatives}
|
|
839 |
%Now we start with the central topic of the thesis: Brzozowski derivatives.
|
|
840 |
Brzozowski \cite{Brzozowski1964} first introduced the
|
|
841 |
concept of a \emph{derivative} of regular expression in 1964.
|
|
842 |
The derivative of a regular expression $r$
|
|
843 |
with respect to a character $c$, is written as $r \backslash c$.
|
|
844 |
This operation tells us what $r$ transforms into
|
|
845 |
if we ``chop'' off a particular character $c$
|
|
846 |
from all strings in the language of $r$ (defined
|
|
847 |
later as $L \; r$).
|
|
848 |
%To give a flavour of Brzozowski derivatives, we present
|
|
849 |
%two straightforward clauses from it:
|
|
850 |
%\begin{center}
|
|
851 |
% \begin{tabular}{lcl}
|
|
852 |
% $d \backslash c$ & $\dn$ &
|
|
853 |
% $\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
|
|
854 |
%$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
|
|
855 |
% \end{tabular}
|
|
856 |
%\end{center}
|
|
857 |
%\noindent
|
|
858 |
%The first clause says that for the regular expression
|
|
859 |
%denoting a singleton set consisting of a single-character string $\{ d \}$,
|
|
860 |
%we check the derivative character $c$ against $d$,
|
|
861 |
%returning a set containing only the empty string $\{ [] \}$
|
|
862 |
%if $c$ and $d$ are equal, and the empty set $\varnothing$ otherwise.
|
|
863 |
%The second clause states that to obtain the regular expression
|
|
864 |
%representing all strings' head character $c$ being chopped off
|
|
865 |
%from $r_1 + r_2$, one simply needs to recursively take derivative
|
|
866 |
%of $r_1$ and $r_2$ and then put them together.
|
|
867 |
Derivatives have the property
|
|
868 |
that $s \in L \; (r\backslash c)$ if and only if
|
|
869 |
$c::s \in L \; r$ where $::$ stands for list prepending.
|
|
870 |
%This property can be used on regular expressions
|
|
871 |
%matching and lexing--to test whether a string $s$ is in $L \; r$,
|
|
872 |
%one simply takes derivatives of $r$ successively with
|
|
873 |
%respect to the characters (in the correct order) in $s$,
|
|
874 |
%and then test whether the empty string is in the last regular expression.
|
|
875 |
With this property, derivatives can give a simple solution
|
|
876 |
to the problem of matching a string $s$ with a regular
|
|
877 |
expression $r$: if the derivative of $r$ w.r.t.\ (in
|
|
878 |
succession) all the characters of the string matches the empty string,
|
|
879 |
then $r$ matches $s$ (and {\em vice versa}).
|
|
880 |
%This makes formally reasoning about these properties such
|
|
881 |
%as correctness and complexity smooth and intuitive.
|
|
882 |
There are several mechanised proofs of this property in various theorem
|
|
883 |
provers,
|
|
884 |
for example one by Owens and Slind \cite{Owens2008} in HOL4,
|
|
885 |
another one by Krauss and Nipkow \cite{Nipkow98} in Isabelle/HOL, and
|
|
886 |
yet another in Coq by Coquand and Siles \cite{Coquand2012}.
|
|
887 |
|
|
888 |
In addition, one can extend derivatives to bounded repetitions
|
|
889 |
relatively straightforwardly. For example, the derivative for
|
|
890 |
this can be defined as:
|
|
891 |
\begin{center}
|
|
892 |
\begin{tabular}{lcl}
|
|
893 |
$r^{\{n\}} \backslash c$ & $\dn$ & $r \backslash c \cdot
|
|
894 |
r^{\{n-1\}} (\text{when} n > 0)$\\
|
|
895 |
\end{tabular}
|
|
896 |
\end{center}
|
|
897 |
\noindent
|
|
898 |
Experimental results suggest that unlike DFA-based solutions
|
|
899 |
for bounded regular expressions,
|
|
900 |
derivatives can cope
|
|
901 |
large counters
|
|
902 |
quite well.
|
|
903 |
|
|
904 |
There have also been
|
|
905 |
extensions of derivatives to other regex constructs.
|
|
906 |
For example, Owens et al include the derivatives
|
|
907 |
for the \emph{NOT} regular expression, which is
|
|
908 |
able to concisely express C-style comments of the form
|
|
909 |
$/* \ldots */$ (see \cite{Owens2008}).
|
|
910 |
Another extension for derivatives is
|
|
911 |
regular expressions with look-aheads, done
|
|
912 |
by Miyazaki and Minamide
|
|
913 |
\cite{Takayuki2019}.
|
|
914 |
%We therefore use Brzozowski derivatives on regular expressions
|
|
915 |
%lexing
|
|
916 |
|
|
917 |
|
|
918 |
|
|
919 |
Given the above definitions and properties of
|
|
920 |
Brzozowski derivatives, one quickly realises their potential
|
|
921 |
in generating a formally verified algorithm for lexing: the clauses and property
|
|
922 |
can be easily expressed in a functional programming language
|
|
923 |
or converted to theorem prover
|
|
924 |
code, with great ease.
|
|
925 |
Perhaps this is the reason why derivatives have sparked quite a bit of interest
|
|
926 |
in the functional programming and theorem prover communities in the last
|
|
927 |
fifteen or so years (
|
|
928 |
\cite{Almeidaetal10}, \cite{Berglund14}, \cite{Berglund18},
|
|
929 |
\cite{Chen12} and \cite{Coquand2012}
|
|
930 |
to name a few), despite being buried in the ``sands of time'' \cite{Owens2008}
|
|
931 |
after they were first published by Brzozowski.
|
|
932 |
|
|
933 |
|
|
934 |
However, there are two difficulties with derivative-based matchers:
|
|
935 |
First, Brzozowski's original matcher only generates a yes/no answer
|
|
936 |
for whether a regular expression matches a string or not. This is too
|
|
937 |
little information in the context of lexing where separate tokens must
|
|
938 |
be identified and also classified (for example as keywords
|
|
939 |
or identifiers).
|
|
940 |
Second, derivative-based matchers need to be more efficient in terms
|
|
941 |
of the sizes of derivatives.
|
|
942 |
Elegant and beautiful
|
|
943 |
as many implementations are,
|
|
944 |
they can be excruciatingly slow.
|
|
945 |
For example, Sulzmann and Lu
|
|
946 |
claim a linear running time of their proposed algorithm,
|
|
947 |
but that was falsified by our experiments. The running time
|
|
948 |
is actually $\Omega(2^n)$ in the worst case.
|
|
949 |
A similar claim about a theoretical runtime of $O(n^2)$
|
|
950 |
is made for the Verbatim \cite{Verbatim}
|
|
951 |
%TODO: give references
|
|
952 |
lexer, which calculates POSIX matches and is based on derivatives.
|
|
953 |
They formalized the correctness of the lexer, but not their complexity result.
|
|
954 |
In the performance evaluation section, they analyzed the run time
|
|
955 |
of matching $a$ with the string
|
|
956 |
\begin{center}
|
|
957 |
$\underbrace{a \ldots a}_{\text{n a's}}$.
|
|
958 |
\end{center}
|
|
959 |
\noindent
|
|
960 |
They concluded that the algorithm is quadratic in terms of
|
|
961 |
the length of the input string.
|
|
962 |
When we tried out their extracted OCaml code with the example $(a+aa)^*$,
|
|
963 |
the time it took to match a string of 40 $a$'s was approximately 5 minutes.
|
|
964 |
|
|
965 |
|
|
966 |
\subsection{Sulzmann and Lu's Algorithm}
|
|
967 |
Sulzmann and Lu~\cite{Sulzmann2014} overcame the first
|
|
968 |
problem with the yes/no answer
|
|
969 |
by cleverly extending Brzozowski's matching
|
|
970 |
algorithm. Their extended version generates additional information on
|
|
971 |
\emph{how} a regular expression matches a string following the POSIX
|
|
972 |
rules for regular expression matching. They achieve this by adding a
|
|
973 |
second ``phase'' to Brzozowski's algorithm involving an injection
|
|
974 |
function. This injection function in a sense undoes the ``damage''
|
|
975 |
of the derivatives chopping off characters.
|
|
976 |
In earlier work, Ausaf et al provided the formal
|
|
977 |
specification of what POSIX matching means and proved in Isabelle/HOL
|
|
978 |
the correctness
|
|
979 |
of this extended algorithm accordingly
|
|
980 |
\cite{AusafDyckhoffUrban2016}.
|
|
981 |
|
|
982 |
The version of the algorithm proven correct
|
|
983 |
suffers however heavily from a
|
|
984 |
second difficulty, where derivatives can
|
|
985 |
grow to arbitrarily big sizes.
|
|
986 |
For example if we start with the
|
|
987 |
regular expression $(a+aa)^*$ and take
|
|
988 |
successive derivatives according to the character $a$, we end up with
|
|
989 |
a sequence of ever-growing derivatives like
|
|
990 |
|
|
991 |
\def\ll{\stackrel{\_\backslash{} a}{\longrightarrow}}
|
|
992 |
\begin{center}
|
|
993 |
\begin{tabular}{rll}
|
|
994 |
$(a + aa)^*$ & $\ll$ & $(\ONE + \ONE{}a) \cdot (a + aa)^*$\\
|
|
995 |
& $\ll$ & $(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* \;+\; (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
|
|
996 |
& $\ll$ & $(\ZERO + \ZERO{}a + \ZERO) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^* \;+\; $\\
|
|
997 |
& & $\qquad(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
|
|
998 |
& $\ll$ & \ldots \hspace{15mm}(regular expressions of sizes 98, 169, 283, 468, 767, \ldots)
|
|
999 |
\end{tabular}
|
|
1000 |
\end{center}
|
|
1001 |
|
|
1002 |
\noindent where after around 35 steps we usually run out of memory on a
|
|
1003 |
typical computer. Clearly, the
|
|
1004 |
notation involving $\ZERO$s and $\ONE$s already suggests
|
|
1005 |
simplification rules that can be applied to regular regular
|
|
1006 |
expressions, for example $\ZERO{}\,r \Rightarrow \ZERO$, $\ONE{}\,r
|
|
1007 |
\Rightarrow r$, $\ZERO{} + r \Rightarrow r$ and $r + r \Rightarrow
|
|
1008 |
r$. While such simple-minded simplifications have been proved in
|
|
1009 |
the work by Ausaf et al. to preserve the correctness of Sulzmann and Lu's
|
|
1010 |
algorithm \cite{AusafDyckhoffUrban2016}, they unfortunately do
|
|
1011 |
\emph{not} help with limiting the growth of the derivatives shown
|
|
1012 |
above: the growth is slowed, but the derivatives can still grow rather
|
|
1013 |
quickly beyond any finite bound.
|
|
1014 |
|
|
1015 |
Therefore we want to look in this thesis at a second
|
|
1016 |
algorithm by Sulzmann and Lu where they
|
|
1017 |
overcame this ``growth problem'' \cite{Sulzmann2014}.
|
|
1018 |
In this version, POSIX values are
|
|
1019 |
represented as bit sequences and such sequences are incrementally generated
|
|
1020 |
when derivatives are calculated. The compact representation
|
|
1021 |
of bit sequences and regular expressions allows them to define a more
|
|
1022 |
``aggressive'' simplification method that keeps the size of the
|
|
1023 |
derivatives finite no matter what the length of the string is.
|
|
1024 |
They make some informal claims about the correctness and linear behaviour
|
|
1025 |
of this version, but do not provide any supporting proof arguments, not
|
|
1026 |
even ``pencil-and-paper'' arguments. They write about their bit-coded
|
|
1027 |
\emph{incremental parsing method} (that is the algorithm to be formalised
|
|
1028 |
in this dissertation)
|
|
1029 |
|
|
1030 |
|
|
1031 |
|
|
1032 |
\begin{quote}\it
|
|
1033 |
``Correctness Claim: We further claim that the incremental parsing
|
|
1034 |
method [..] in combination with the simplification steps [..]
|
|
1035 |
yields POSIX parse trees. We have tested this claim
|
|
1036 |
extensively [..] but yet
|
|
1037 |
have to work out all proof details.'' \cite[Page 14]{Sulzmann2014}
|
|
1038 |
\end{quote}
|
|
1039 |
Ausaf and Urban made some initial progress towards the
|
|
1040 |
full correctness proof but still had to leave out the optimisation
|
|
1041 |
Sulzmann and Lu proposed.
|
|
1042 |
Ausaf wrote \cite{Ausaf},
|
|
1043 |
\begin{quote}\it
|
|
1044 |
``The next step would be to implement a more aggressive simplification procedure on annotated regular expressions and then prove the corresponding algorithm generates the same values as blexer. Alas due to time constraints we are unable to do so here.''
|
|
1045 |
\end{quote}
|
|
1046 |
This thesis implements the aggressive simplifications envisioned
|
|
1047 |
by Ausaf and Urban,
|
|
1048 |
together with a formal proof of the correctness of those simplifications.
|
|
1049 |
|
|
1050 |
|
|
1051 |
One of the most recent work in the context of lexing
|
|
1052 |
%with this issue
|
|
1053 |
is the Verbatim lexer by Egolf, Lasser and Fisher~\cite{Verbatim}.
|
|
1054 |
This is relevant work for us and we will compare it later with
|
|
1055 |
our derivative-based matcher we are going to present.
|
|
1056 |
There is also some newer work called
|
|
1057 |
Verbatim++~\cite{Verbatimpp}, which does not use derivatives,
|
|
1058 |
but deterministic finite automaton instead.
|
|
1059 |
We will also study this work in a later section.
|
|
1060 |
%An example that gives problem to automaton approaches would be
|
|
1061 |
%the regular expression $(a|b)^*a(a|b)^{\{n\}}$.
|
|
1062 |
%It requires at least $2^{n+1}$ states to represent
|
|
1063 |
%as a DFA.
|
|
1064 |
|
|
1065 |
|
538
|
1066 |
\section{Basic Concepts}
|
622
|
1067 |
Formal language theory usually starts with an alphabet
|
538
|
1068 |
denoting a set of characters.
|
637
|
1069 |
Here we use the datatype of characters from Isabelle,
|
541
|
1070 |
which roughly corresponds to the ASCII characters.
|
564
|
1071 |
In what follows, we shall leave the information about the alphabet
|
541
|
1072 |
implicit.
|
|
1073 |
Then using the usual bracket notation for lists,
|
622
|
1074 |
we can define strings made up of characters as:
|
532
|
1075 |
\begin{center}
|
|
1076 |
\begin{tabular}{lcl}
|
541
|
1077 |
$\textit{s}$ & $\dn$ & $[] \; |\; c :: s$
|
532
|
1078 |
\end{tabular}
|
|
1079 |
\end{center}
|
583
|
1080 |
where $c$ is a variable ranging over characters.
|
622
|
1081 |
The $::$ stands for list cons and $[]$ for the empty
|
|
1082 |
list.
|
637
|
1083 |
For brevity, a singleton list is sometimes written as $[c]$.
|
541
|
1084 |
Strings can be concatenated to form longer strings in the same
|
637
|
1085 |
way we concatenate two lists, which we shall write as $s_1 @ s_2$.
|
541
|
1086 |
We omit the precise
|
538
|
1087 |
recursive definition here.
|
|
1088 |
We overload this concatenation operator for two sets of strings:
|
532
|
1089 |
\begin{center}
|
|
1090 |
\begin{tabular}{lcl}
|
541
|
1091 |
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A \land s_B \in B \}$\\
|
532
|
1092 |
\end{tabular}
|
|
1093 |
\end{center}
|
538
|
1094 |
We also call the above \emph{language concatenation}.
|
532
|
1095 |
The power of a language is defined recursively, using the
|
|
1096 |
concatenation operator $@$:
|
|
1097 |
\begin{center}
|
|
1098 |
\begin{tabular}{lcl}
|
|
1099 |
$A^0 $ & $\dn$ & $\{ [] \}$\\
|
541
|
1100 |
$A^{n+1}$ & $\dn$ & $A @ A^n$
|
532
|
1101 |
\end{tabular}
|
|
1102 |
\end{center}
|
564
|
1103 |
The union of all powers of a language
|
|
1104 |
can be used to define the Kleene star operator:
|
532
|
1105 |
\begin{center}
|
|
1106 |
\begin{tabular}{lcl}
|
536
|
1107 |
$A*$ & $\dn$ & $\bigcup_{i \geq 0} A^i$ \\
|
532
|
1108 |
\end{tabular}
|
|
1109 |
\end{center}
|
|
1110 |
|
538
|
1111 |
\noindent
|
564
|
1112 |
However, to obtain a more convenient induction principle
|
538
|
1113 |
in Isabelle/HOL,
|
536
|
1114 |
we instead define the Kleene star
|
532
|
1115 |
as an inductive set:
|
538
|
1116 |
|
532
|
1117 |
\begin{center}
|
538
|
1118 |
\begin{mathpar}
|
564
|
1119 |
\inferrule{\mbox{}}{[] \in A*\\}
|
538
|
1120 |
|
564
|
1121 |
\inferrule{s_1 \in A \;\; s_2 \in A*}{s_1 @ s_2 \in A*}
|
538
|
1122 |
\end{mathpar}
|
532
|
1123 |
\end{center}
|
564
|
1124 |
\noindent
|
541
|
1125 |
We also define an operation of "chopping off" a character from
|
|
1126 |
a language, which we call $\Der$, meaning \emph{Derivative} (for a language):
|
532
|
1127 |
\begin{center}
|
|
1128 |
\begin{tabular}{lcl}
|
|
1129 |
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\
|
|
1130 |
\end{tabular}
|
|
1131 |
\end{center}
|
538
|
1132 |
\noindent
|
583
|
1133 |
This can be generalised to ``chopping off'' a string
|
|
1134 |
from all strings within a set $A$,
|
541
|
1135 |
namely:
|
532
|
1136 |
\begin{center}
|
|
1137 |
\begin{tabular}{lcl}
|
541
|
1138 |
$\textit{Ders} \;s \;A$ & $\dn$ & $\{ s' \mid s@s' \in A \}$\\
|
532
|
1139 |
\end{tabular}
|
|
1140 |
\end{center}
|
538
|
1141 |
\noindent
|
541
|
1142 |
which is essentially the left quotient $A \backslash L$ of $A$ against
|
622
|
1143 |
the singleton language with $L = \{s\}$.
|
|
1144 |
However, for our purposes here, the $\textit{Ders}$ definition with
|
541
|
1145 |
a single string is sufficient.
|
532
|
1146 |
|
577
|
1147 |
The reason for defining derivatives
|
622
|
1148 |
is that they provide another approach
|
577
|
1149 |
to test membership of a string in
|
|
1150 |
a set of strings.
|
|
1151 |
For example, to test whether the string
|
638
|
1152 |
$bar$ is contained in the set $\{foo, bar, brak\}$, one can take derivative of the set with
|
577
|
1153 |
respect to the string $bar$:
|
|
1154 |
\begin{center}
|
622
|
1155 |
\begin{tabular}{lll}
|
577
|
1156 |
$S = \{foo, bar, brak\}$ & $ \stackrel{\backslash b}{\rightarrow }$ &
|
622
|
1157 |
$\{ar, rak\}$ \\
|
|
1158 |
& $\stackrel{\backslash a}{\rightarrow}$ & $\{r \}$\\
|
|
1159 |
& $\stackrel{\backslash r}{\rightarrow}$ & $\{[]\}$\\
|
|
1160 |
%& $\stackrel{[] \in S \backslash bar}{\longrightarrow}$ & $bar \in S$\\
|
577
|
1161 |
\end{tabular}
|
|
1162 |
\end{center}
|
|
1163 |
\noindent
|
637
|
1164 |
and in the end, test whether the set
|
638
|
1165 |
contains the empty string.\footnote{We use the infix notation $A\backslash c$
|
|
1166 |
instead of $\Der \; c \; A$ for brevity, as it will always be
|
|
1167 |
clear from the context that we are operating
|
622
|
1168 |
on languages rather than regular expressions.}
|
|
1169 |
|
|
1170 |
In general, if we have a language $S$,
|
|
1171 |
then we can test whether $s$ is in $S$
|
577
|
1172 |
by testing whether $[] \in S \backslash s$.
|
564
|
1173 |
With the sequencing, Kleene star, and $\textit{Der}$ operator on languages,
|
532
|
1174 |
we have a few properties of how the language derivative can be defined using
|
|
1175 |
sub-languages.
|
577
|
1176 |
For example, for the sequence operator, we have
|
622
|
1177 |
something similar to a ``chain rule'':
|
532
|
1178 |
\begin{lemma}
|
536
|
1179 |
\[
|
|
1180 |
\Der \; c \; (A @ B) =
|
|
1181 |
\begin{cases}
|
538
|
1182 |
((\Der \; c \; A) \, @ \, B ) \cup (\Der \; c\; B) , & \text{if} \; [] \in A \\
|
|
1183 |
(\Der \; c \; A) \, @ \, B, & \text{otherwise}
|
536
|
1184 |
\end{cases}
|
|
1185 |
\]
|
532
|
1186 |
\end{lemma}
|
|
1187 |
\noindent
|
|
1188 |
This lemma states that if $A$ contains the empty string, $\Der$ can "pierce" through it
|
|
1189 |
and get to $B$.
|
583
|
1190 |
The language derivative for $A*$ can be described using the language derivative
|
532
|
1191 |
of $A$:
|
|
1192 |
\begin{lemma}
|
538
|
1193 |
$\textit{Der} \;c \;(A*) = (\textit{Der}\; c A) @ (A*)$\\
|
532
|
1194 |
\end{lemma}
|
|
1195 |
\begin{proof}
|
583
|
1196 |
There are two inclusions to prove:
|
532
|
1197 |
\begin{itemize}
|
564
|
1198 |
\item{$\subseteq$}:\\
|
532
|
1199 |
The set
|
637
|
1200 |
\[ S_1 = \{s \mid c :: s \in A*\} \]
|
532
|
1201 |
is enclosed in the set
|
637
|
1202 |
\[ S_2 = \{s_1 @ s_2 \mid s_1 \, s_2.\; s_1 \in \{s \mid c :: s \in A\} \land s_2 \in A* \}. \]
|
|
1203 |
This is because for any string $c::s$ satisfying $c::s \in A*$,
|
|
1204 |
%whenever you have a string starting with a character
|
|
1205 |
%in the language of a Kleene star $A*$,
|
|
1206 |
%then that
|
|
1207 |
the character $c$, together with a prefix of $s$
|
|
1208 |
%immediately after $c$
|
|
1209 |
forms the first iteration of $A*$,
|
|
1210 |
and the rest of the $s$ is also $A*$.
|
|
1211 |
This coincides with the definition of $S_2$.
|
564
|
1212 |
\item{$\supseteq$}:\\
|
532
|
1213 |
Note that
|
538
|
1214 |
\[ \Der \; c \; (A*) = \Der \; c \; (\{ [] \} \cup (A @ A*) ) \]
|
583
|
1215 |
holds.
|
|
1216 |
Also the following holds:
|
536
|
1217 |
\[ \Der \; c \; (\{ [] \} \cup (A @ A*) ) = \Der\; c \; (A @ A*) \]
|
564
|
1218 |
where the $\textit{RHS}$ can be rewritten
|
|
1219 |
as \[ (\Der \; c\; A) @ A* \cup (\Der \; c \; (A*)) \]
|
|
1220 |
which of course contains $\Der \; c \; A @ A*$.
|
532
|
1221 |
\end{itemize}
|
|
1222 |
\end{proof}
|
538
|
1223 |
|
|
1224 |
\noindent
|
622
|
1225 |
The clever idea of Brzozowski was to find counterparts of $\Der$ and $\Ders$
|
|
1226 |
for regular expressions.
|
|
1227 |
To introduce them, we need to first give definitions for regular expressions,
|
|
1228 |
which we shall do next.
|
532
|
1229 |
|
536
|
1230 |
\subsection{Regular Expressions and Their Meaning}
|
564
|
1231 |
The \emph{basic regular expressions} are defined inductively
|
532
|
1232 |
by the following grammar:
|
|
1233 |
\[ r ::= \ZERO \mid \ONE
|
|
1234 |
\mid c
|
|
1235 |
\mid r_1 \cdot r_2
|
|
1236 |
\mid r_1 + r_2
|
|
1237 |
\mid r^*
|
|
1238 |
\]
|
538
|
1239 |
\noindent
|
564
|
1240 |
We call them basic because we will introduce
|
637
|
1241 |
additional constructors in later chapters, such as negation
|
538
|
1242 |
and bounded repetitions.
|
564
|
1243 |
We use $\ZERO$ for the regular expression that
|
|
1244 |
matches no string, and $\ONE$ for the regular
|
622
|
1245 |
expression that matches only the empty string.\footnote{
|
|
1246 |
Some authors
|
564
|
1247 |
also use $\phi$ and $\epsilon$ for $\ZERO$ and $\ONE$
|
638
|
1248 |
but we prefer this notation.}
|
564
|
1249 |
The sequence regular expression is written $r_1\cdot r_2$
|
|
1250 |
and sometimes we omit the dot if it is clear which
|
|
1251 |
regular expression is meant; the alternative
|
|
1252 |
is written $r_1 + r_2$.
|
|
1253 |
The \emph{language} or meaning of
|
|
1254 |
a regular expression is defined recursively as
|
|
1255 |
a set of strings:
|
532
|
1256 |
%TODO: FILL in the other defs
|
|
1257 |
\begin{center}
|
|
1258 |
\begin{tabular}{lcl}
|
564
|
1259 |
$L \; \ZERO$ & $\dn$ & $\phi$\\
|
|
1260 |
$L \; \ONE$ & $\dn$ & $\{[]\}$\\
|
|
1261 |
$L \; c$ & $\dn$ & $\{[c]\}$\\
|
622
|
1262 |
$L \; (r_1 + r_2)$ & $\dn$ & $ L \; r_1 \cup L \; r_2$\\
|
|
1263 |
$L \; (r_1 \cdot r_2)$ & $\dn$ & $ L \; r_1 @ L \; r_2$\\
|
|
1264 |
$L \; (r^*)$ & $\dn$ & $ (L\;r)*$
|
532
|
1265 |
\end{tabular}
|
|
1266 |
\end{center}
|
536
|
1267 |
\noindent
|
622
|
1268 |
%Now with language derivatives of a language and regular expressions and
|
|
1269 |
%their language interpretations in place, we are ready to define derivatives on regular expressions.
|
637
|
1270 |
With $L$, we are ready to introduce Brzozowski derivatives on regular expressions.
|
638
|
1271 |
We do so by first introducing what properties they should satisfy.
|
622
|
1272 |
|
532
|
1273 |
\subsection{Brzozowski Derivatives and a Regular Expression Matcher}
|
564
|
1274 |
%Recall, the language derivative acts on a set of strings
|
|
1275 |
%and essentially chops off a particular character from
|
|
1276 |
%all strings in that set, Brzozowski defined a derivative operation on regular expressions
|
|
1277 |
%so that after derivative $L(r\backslash c)$
|
|
1278 |
%will look as if it was obtained by doing a language derivative on $L(r)$:
|
622
|
1279 |
%Recall that the language derivative acts on a
|
|
1280 |
%language (set of strings).
|
|
1281 |
%One can decide whether a string $s$ belongs
|
|
1282 |
%to a language $S$ by taking derivative with respect to
|
|
1283 |
%that string and then checking whether the empty
|
|
1284 |
%string is in the derivative:
|
|
1285 |
%\begin{center}
|
|
1286 |
%\parskip \baselineskip
|
|
1287 |
%\def\myupbracefill#1{\rotatebox{90}{\stretchto{\{}{#1}}}
|
|
1288 |
%\def\rlwd{.5pt}
|
|
1289 |
%\newcommand\notate[3]{%
|
|
1290 |
% \unskip\def\useanchorwidth{T}%
|
|
1291 |
% \setbox0=\hbox{#1}%
|
|
1292 |
% \def\stackalignment{c}\stackunder[-6pt]{%
|
|
1293 |
% \def\stackalignment{c}\stackunder[-1.5pt]{%
|
|
1294 |
% \stackunder[-2pt]{\strut #1}{\myupbracefill{\wd0}}}{%
|
|
1295 |
% \rule{\rlwd}{#2\baselineskip}}}{%
|
|
1296 |
% \strut\kern7pt$\hookrightarrow$\rlap{ \footnotesize#3}}\ignorespaces%
|
|
1297 |
%}
|
|
1298 |
%\Longstack{
|
|
1299 |
%\notate{$\{ \ldots ,\;$
|
|
1300 |
% \notate{s}{1}{$(c_1 :: s_1)$}
|
|
1301 |
% $, \; \ldots \}$
|
|
1302 |
%}{1}{$S_{start}$}
|
|
1303 |
%}
|
|
1304 |
%\Longstack{
|
|
1305 |
% $\stackrel{\backslash c_1}{\longrightarrow}$
|
|
1306 |
%}
|
|
1307 |
%\Longstack{
|
|
1308 |
% $\{ \ldots,\;$ \notate{$s_1$}{1}{$(c_2::s_2)$}
|
|
1309 |
% $,\; \ldots \}$
|
|
1310 |
%}
|
|
1311 |
%\Longstack{
|
|
1312 |
% $\stackrel{\backslash c_2}{\longrightarrow}$
|
|
1313 |
%}
|
|
1314 |
%\Longstack{
|
|
1315 |
% $\{ \ldots,\; s_2
|
|
1316 |
% ,\; \ldots \}$
|
|
1317 |
%}
|
|
1318 |
%\Longstack{
|
|
1319 |
% $ \xdashrightarrow{\backslash c_3\ldots\ldots} $
|
|
1320 |
%}
|
|
1321 |
%\Longstack{
|
|
1322 |
% \notate{$\{\ldots, [], \ldots\}$}{1}{$S_{end} =
|
|
1323 |
% S_{start}\backslash s$}
|
|
1324 |
%}
|
|
1325 |
%\end{center}
|
|
1326 |
%\begin{center}
|
|
1327 |
% $s \in S_{start} \iff [] \in S_{end}$
|
|
1328 |
%\end{center}
|
|
1329 |
%\noindent
|
|
1330 |
Brzozowski noticed that $\Der$
|
579
|
1331 |
can be ``mirrored'' on regular expressions which
|
564
|
1332 |
he calls the derivative of a regular expression $r$
|
|
1333 |
with respect to a character $c$, written
|
577
|
1334 |
$r \backslash c$. This infix operator
|
638
|
1335 |
takes regular expression $r$ as input
|
|
1336 |
and a character as a right operand.
|
577
|
1337 |
The derivative operation on regular expression
|
|
1338 |
is defined such that the language of the derivative result
|
|
1339 |
coincides with the language of the original
|
579
|
1340 |
regular expression being taken
|
638
|
1341 |
derivative with respect to the same characters, namely
|
579
|
1342 |
\begin{property}
|
|
1343 |
|
|
1344 |
\[
|
|
1345 |
L \; (r \backslash c) = \Der \; c \; (L \; r)
|
|
1346 |
\]
|
|
1347 |
\end{property}
|
|
1348 |
\noindent
|
637
|
1349 |
Next, we give the recursive definition of derivative on
|
|
1350 |
regular expressions so that it satisfies the properties above.
|
638
|
1351 |
%The derivative function, written $r\backslash c$,
|
|
1352 |
%takes a regular expression $r$ and character $c$, and
|
|
1353 |
%returns a new regular expression representing
|
|
1354 |
%the original regular expression's language $L \; r$
|
|
1355 |
%being taken the language derivative with respect to $c$.
|
626
|
1356 |
\begin{table}
|
|
1357 |
\begin{center}
|
579
|
1358 |
\begin{tabular}{lcl}
|
|
1359 |
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\
|
|
1360 |
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\
|
|
1361 |
$d \backslash c$ & $\dn$ &
|
|
1362 |
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
|
|
1363 |
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
|
|
1364 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\
|
|
1365 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
|
|
1366 |
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
|
|
1367 |
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\
|
|
1368 |
\end{tabular}
|
626
|
1369 |
\end{center}
|
|
1370 |
\caption{Derivative on Regular Expressions}
|
|
1371 |
\label{table:der}
|
|
1372 |
\end{table}
|
564
|
1373 |
\noindent
|
579
|
1374 |
The most involved cases are the sequence case
|
|
1375 |
and the star case.
|
|
1376 |
The sequence case says that if the first regular expression
|
|
1377 |
contains an empty string, then the second component of the sequence
|
|
1378 |
needs to be considered, as its derivative will contribute to the
|
|
1379 |
result of this derivative:
|
|
1380 |
\begin{center}
|
|
1381 |
\begin{tabular}{lcl}
|
583
|
1382 |
$(r_1 \cdot r_2 ) \backslash c$ & $\dn$ &
|
|
1383 |
$\textit{if}\;\,([] \in L(r_1))\;
|
638
|
1384 |
\textit{then} \; (r_1 \backslash c) \cdot r_2 + r_2 \backslash c$ \\
|
579
|
1385 |
& & $\textit{else} \; (r_1 \backslash c) \cdot r_2$
|
|
1386 |
\end{tabular}
|
|
1387 |
\end{center}
|
|
1388 |
\noindent
|
|
1389 |
Notice how this closely resembles
|
|
1390 |
the language derivative operation $\Der$:
|
564
|
1391 |
\begin{center}
|
|
1392 |
\begin{tabular}{lcl}
|
|
1393 |
$\Der \; c \; (A @ B)$ & $\dn$ &
|
|
1394 |
$ \textit{if} \;\, [] \in A \;
|
|
1395 |
\textit{then} \;\, ((\Der \; c \; A) @ B ) \cup
|
|
1396 |
\Der \; c\; B$\\
|
|
1397 |
& & $\textit{else}\; (\Der \; c \; A) @ B$\\
|
|
1398 |
\end{tabular}
|
|
1399 |
\end{center}
|
|
1400 |
\noindent
|
583
|
1401 |
The derivative of the star regular expression $r^*$
|
579
|
1402 |
unwraps one iteration of $r$, turns it into $r\backslash c$,
|
|
1403 |
and attaches the original $r^*$
|
|
1404 |
after $r\backslash c$, so that
|
|
1405 |
we can further unfold it as many times as needed:
|
|
1406 |
\[
|
|
1407 |
(r^*) \backslash c \dn (r \backslash c)\cdot r^*.
|
|
1408 |
\]
|
|
1409 |
Again,
|
637
|
1410 |
the structure is the same as the language derivative of the Kleene star:
|
532
|
1411 |
\[
|
564
|
1412 |
\textit{Der} \;c \;(A*) \dn (\textit{Der}\; c A) @ (A*)
|
532
|
1413 |
\]
|
564
|
1414 |
In the above definition of $(r_1\cdot r_2) \backslash c$,
|
|
1415 |
the $\textit{if}$ clause's
|
|
1416 |
boolean condition
|
|
1417 |
$[] \in L(r_1)$ needs to be
|
|
1418 |
somehow recursively computed.
|
|
1419 |
We call such a function that checks
|
|
1420 |
whether the empty string $[]$ is
|
|
1421 |
in the language of a regular expression $\nullable$:
|
|
1422 |
\begin{center}
|
|
1423 |
\begin{tabular}{lcl}
|
|
1424 |
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\
|
|
1425 |
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\
|
|
1426 |
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\
|
|
1427 |
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
|
|
1428 |
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
|
|
1429 |
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\
|
|
1430 |
\end{tabular}
|
|
1431 |
\end{center}
|
|
1432 |
\noindent
|
|
1433 |
The $\ZERO$ regular expression
|
|
1434 |
does not contain any string and
|
|
1435 |
therefore is not \emph{nullable}.
|
|
1436 |
$\ONE$ is \emph{nullable}
|
|
1437 |
by definition.
|
|
1438 |
The character regular expression $c$
|
|
1439 |
corresponds to the singleton set $\{c\}$,
|
|
1440 |
and therefore does not contain the empty string.
|
|
1441 |
The alternative regular expression is nullable
|
|
1442 |
if at least one of its children is nullable.
|
|
1443 |
The sequence regular expression
|
|
1444 |
would require both children to have the empty string
|
|
1445 |
to compose an empty string, and the Kleene star
|
|
1446 |
is always nullable because it naturally
|
579
|
1447 |
contains the empty string.
|
532
|
1448 |
\noindent
|
638
|
1449 |
We have the following two correspondences between
|
564
|
1450 |
derivatives on regular expressions and
|
|
1451 |
derivatives on a set of strings:
|
|
1452 |
\begin{lemma}\label{derDer}
|
608
|
1453 |
\mbox{}
|
579
|
1454 |
\begin{itemize}
|
|
1455 |
\item
|
532
|
1456 |
$\textit{Der} \; c \; L(r) = L (r\backslash c)$
|
579
|
1457 |
\item
|
|
1458 |
$c\!::\!s \in L(r)$ \textit{iff} $s \in L(r\backslash c)$.
|
|
1459 |
\end{itemize}
|
532
|
1460 |
\end{lemma}
|
579
|
1461 |
\begin{proof}
|
|
1462 |
By induction on $r$.
|
|
1463 |
\end{proof}
|
532
|
1464 |
\noindent
|
638
|
1465 |
which are the main properties of derivatives
|
|
1466 |
that enables us later to reason about the correctness of
|
622
|
1467 |
derivative-based matching.
|
532
|
1468 |
We can generalise the derivative operation shown above for single characters
|
|
1469 |
to strings as follows:
|
|
1470 |
|
|
1471 |
\begin{center}
|
|
1472 |
\begin{tabular}{lcl}
|
|
1473 |
$r \backslash_s (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash_s s$ \\
|
583
|
1474 |
$r \backslash_s [\,] $ & $\dn$ & $r$
|
532
|
1475 |
\end{tabular}
|
|
1476 |
\end{center}
|
|
1477 |
|
|
1478 |
\noindent
|
564
|
1479 |
When there is no ambiguity, we will
|
|
1480 |
omit the subscript and use $\backslash$ instead
|
583
|
1481 |
of $\backslash_s$ to denote
|
532
|
1482 |
string derivatives for brevity.
|
622
|
1483 |
Brzozowski's derivative-based
|
|
1484 |
regular-expression matching algorithm can then be described as:
|
532
|
1485 |
|
|
1486 |
\begin{definition}
|
564
|
1487 |
$\textit{match}\;s\;r \;\dn\; \nullable \; (r\backslash s)$
|
532
|
1488 |
\end{definition}
|
|
1489 |
|
|
1490 |
\noindent
|
637
|
1491 |
Assuming the string is given as a sequence of characters, say $c_0c_1 \ldots c_n$,
|
|
1492 |
this algorithm, presented graphically, is as follows:
|
532
|
1493 |
|
601
|
1494 |
\begin{equation}\label{matcher}
|
532
|
1495 |
\begin{tikzcd}
|
583
|
1496 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] &
|
|
1497 |
r_2 \arrow[r, dashed] & r_n \arrow[r,"\textit{nullable}?"] &
|
|
1498 |
\;\textrm{true}/\textrm{false}
|
532
|
1499 |
\end{tikzcd}
|
|
1500 |
\end{equation}
|
|
1501 |
|
|
1502 |
\noindent
|
622
|
1503 |
It is relatively
|
|
1504 |
easy to show that this matcher is correct, namely
|
539
|
1505 |
\begin{lemma}
|
564
|
1506 |
$\textit{match} \; s\; r = \textit{true} \; \textit{iff} \; s \in L(r)$
|
539
|
1507 |
\end{lemma}
|
|
1508 |
\begin{proof}
|
637
|
1509 |
By induction on $s$ using the property of derivatives:
|
583
|
1510 |
lemma \ref{derDer}.
|
539
|
1511 |
\end{proof}
|
601
|
1512 |
\begin{figure}
|
564
|
1513 |
\begin{center}
|
539
|
1514 |
\begin{tikzpicture}
|
|
1515 |
\begin{axis}[
|
|
1516 |
xlabel={$n$},
|
|
1517 |
ylabel={time in secs},
|
601
|
1518 |
%ymode = log,
|
539
|
1519 |
legend entries={Naive Matcher},
|
|
1520 |
legend pos=north west,
|
|
1521 |
legend cell align=left]
|
|
1522 |
\addplot[red,mark=*, mark options={fill=white}] table {NaiveMatcher.data};
|
|
1523 |
\end{axis}
|
|
1524 |
\end{tikzpicture}
|
583
|
1525 |
\caption{Matching the regular expression $(a^*)^*b$ against strings of the form
|
|
1526 |
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}
|
|
1527 |
$ using Brzozowski's original algorithm}\label{NaiveMatcher}
|
601
|
1528 |
\end{center}
|
539
|
1529 |
\end{figure}
|
|
1530 |
\noindent
|
564
|
1531 |
If we implement the above algorithm naively, however,
|
|
1532 |
the algorithm can be excruciatingly slow, as shown in
|
|
1533 |
\ref{NaiveMatcher}.
|
|
1534 |
Note that both axes are in logarithmic scale.
|
637
|
1535 |
Around two dozen characters
|
638
|
1536 |
this algorithm already ``explodes'' with the regular expression
|
564
|
1537 |
$(a^*)^*b$.
|
622
|
1538 |
To improve this situation, we need to introduce simplification
|
|
1539 |
rules for the intermediate results,
|
638
|
1540 |
such as $r + r \rightarrow r$ or $\ONE \cdot r \rightarrow r$,
|
539
|
1541 |
and make sure those rules do not change the
|
|
1542 |
language of the regular expression.
|
638
|
1543 |
One simple-minded simplification function
|
622
|
1544 |
that achieves these requirements
|
|
1545 |
is given below (see Ausaf et al. \cite{AusafDyckhoffUrban2016}):
|
564
|
1546 |
\begin{center}
|
|
1547 |
\begin{tabular}{lcl}
|
|
1548 |
$\simp \; r_1 \cdot r_2 $ & $ \dn$ &
|
|
1549 |
$(\simp \; r_1, \simp \; r_2) \; \textit{match}$\\
|
|
1550 |
& & $\quad \case \; (\ZERO, \_) \Rightarrow \ZERO$\\
|
|
1551 |
& & $\quad \case \; (\_, \ZERO) \Rightarrow \ZERO$\\
|
|
1552 |
& & $\quad \case \; (\ONE, r_2') \Rightarrow r_2'$\\
|
|
1553 |
& & $\quad \case \; (r_1', \ONE) \Rightarrow r_1'$\\
|
|
1554 |
& & $\quad \case \; (r_1', r_2') \Rightarrow r_1'\cdot r_2'$\\
|
|
1555 |
$\simp \; r_1 + r_2$ & $\dn$ & $(\simp \; r_1, \simp \; r_2) \textit{match}$\\
|
|
1556 |
& & $\quad \; \case \; (\ZERO, r_2') \Rightarrow r_2'$\\
|
|
1557 |
& & $\quad \; \case \; (r_1', \ZERO) \Rightarrow r_1'$\\
|
|
1558 |
& & $\quad \; \case \; (r_1', r_2') \Rightarrow r_1' + r_2'$\\
|
583
|
1559 |
$\simp \; r$ & $\dn$ & $r\quad\quad (otherwise)$
|
564
|
1560 |
|
|
1561 |
\end{tabular}
|
|
1562 |
\end{center}
|
|
1563 |
If we repeatedly apply this simplification
|
|
1564 |
function during the matching algorithm,
|
|
1565 |
we have a matcher with simplification:
|
|
1566 |
\begin{center}
|
|
1567 |
\begin{tabular}{lcl}
|
|
1568 |
$\derssimp \; [] \; r$ & $\dn$ & $r$\\
|
|
1569 |
$\derssimp \; c :: cs \; r$ & $\dn$ & $\derssimp \; cs \; (\simp \; (r \backslash c))$\\
|
|
1570 |
$\textit{matcher}_{simp}\; s \; r $ & $\dn$ & $\nullable \; (\derssimp \; s\;r)$
|
|
1571 |
\end{tabular}
|
|
1572 |
\end{center}
|
|
1573 |
\begin{figure}
|
539
|
1574 |
\begin{tikzpicture}
|
|
1575 |
\begin{axis}[
|
|
1576 |
xlabel={$n$},
|
|
1577 |
ylabel={time in secs},
|
601
|
1578 |
%ymode = log,
|
|
1579 |
%xmode = log,
|
564
|
1580 |
grid = both,
|
539
|
1581 |
legend entries={Matcher With Simp},
|
|
1582 |
legend pos=north west,
|
|
1583 |
legend cell align=left]
|
|
1584 |
\addplot[red,mark=*, mark options={fill=white}] table {BetterMatcher.data};
|
|
1585 |
\end{axis}
|
564
|
1586 |
\end{tikzpicture}
|
|
1587 |
\caption{$(a^*)^*b$
|
|
1588 |
against
|
|
1589 |
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$ Using $\textit{matcher}_{simp}$}\label{BetterMatcher}
|
|
1590 |
\end{figure}
|
|
1591 |
\noindent
|
|
1592 |
The running time of $\textit{ders}\_\textit{simp}$
|
583
|
1593 |
on the same example of Figure \ref{NaiveMatcher}
|
|
1594 |
is now ``tame'' in terms of the length of inputs,
|
|
1595 |
as shown in Figure \ref{BetterMatcher}.
|
539
|
1596 |
|
637
|
1597 |
So far, the story is use Brzozowski derivatives and
|
|
1598 |
simplify as much as possible, and at the end test
|
622
|
1599 |
whether the empty string is recognised
|
|
1600 |
by the final derivative.
|
583
|
1601 |
But what if we want to
|
|
1602 |
do lexing instead of just getting a true/false answer?
|
622
|
1603 |
Sulzmann and Lu \cite{Sulzmann2014} first came up with a nice and
|
|
1604 |
elegant (arguably as beautiful as the definition of the
|
|
1605 |
Brzozowski derivative) solution for this.
|
538
|
1606 |
|
539
|
1607 |
\section{Values and the Lexing Algorithm by Sulzmann and Lu}
|
564
|
1608 |
In this section, we present a two-phase regular expression lexing
|
|
1609 |
algorithm.
|
|
1610 |
The first phase takes successive derivatives with
|
|
1611 |
respect to the input string,
|
|
1612 |
and the second phase does the reverse, \emph{injecting} back
|
|
1613 |
characters, in the meantime constructing a lexing result.
|
|
1614 |
We will introduce the injection phase in detail slightly
|
|
1615 |
later, but as a preliminary we have to first define
|
|
1616 |
the datatype for lexing results,
|
|
1617 |
called \emph{value} or
|
608
|
1618 |
sometimes also \emph{lexical value}.
|
|
1619 |
Values and regular
|
|
1620 |
expressions correspond to each other
|
|
1621 |
as illustrated in the following
|
538
|
1622 |
table:
|
|
1623 |
|
|
1624 |
\begin{center}
|
|
1625 |
\begin{tabular}{c@{\hspace{20mm}}c}
|
|
1626 |
\begin{tabular}{@{}rrl@{}}
|
|
1627 |
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
|
|
1628 |
$r$ & $::=$ & $\ZERO$\\
|
|
1629 |
& $\mid$ & $\ONE$ \\
|
|
1630 |
& $\mid$ & $c$ \\
|
|
1631 |
& $\mid$ & $r_1 \cdot r_2$\\
|
|
1632 |
& $\mid$ & $r_1 + r_2$ \\
|
|
1633 |
\\
|
|
1634 |
& $\mid$ & $r^*$ \\
|
|
1635 |
\end{tabular}
|
|
1636 |
&
|
|
1637 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}}
|
|
1638 |
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
|
|
1639 |
$v$ & $::=$ & \\
|
|
1640 |
& & $\Empty$ \\
|
591
|
1641 |
& $\mid$ & $\Char \; c$ \\
|
538
|
1642 |
& $\mid$ & $\Seq\,v_1\, v_2$\\
|
591
|
1643 |
& $\mid$ & $\Left \;v$ \\
|
|
1644 |
& $\mid$ & $\Right\;v$ \\
|
538
|
1645 |
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
|
|
1646 |
\end{tabular}
|
|
1647 |
\end{tabular}
|
|
1648 |
\end{center}
|
|
1649 |
\noindent
|
564
|
1650 |
A value has an underlying string, which
|
|
1651 |
can be calculated by the ``flatten" function $|\_|$:
|
|
1652 |
\begin{center}
|
|
1653 |
\begin{tabular}{lcl}
|
|
1654 |
$|\Empty|$ & $\dn$ & $[]$\\
|
|
1655 |
$|\Char \; c|$ & $ \dn$ & $ [c]$\\
|
591
|
1656 |
$|\Seq \; v_1, \;v_2|$ & $ \dn$ & $ v_1| @ |v_2|$\\
|
|
1657 |
$|\Left \; v|$ & $ \dn$ & $ |v|$\\
|
|
1658 |
$|\Right \; v|$ & $ \dn$ & $ |v|$\\
|
|
1659 |
$|\Stars \; []|$ & $\dn$ & $[]$\\
|
|
1660 |
$|\Stars \; v::vs|$ & $\dn$ & $ |v| @ |\Stars(vs)|$
|
564
|
1661 |
\end{tabular}
|
|
1662 |
\end{center}
|
|
1663 |
Sulzmann and Lu used a binary predicate, written $\vdash v:r $,
|
|
1664 |
to indicate that a value $v$ could be generated from a lexing algorithm
|
622
|
1665 |
with input $r$. They call it the value inhabitation relation,
|
|
1666 |
defined by the rules.
|
628
|
1667 |
\begin{figure}[H]
|
538
|
1668 |
\begin{mathpar}
|
591
|
1669 |
\inferrule{\mbox{}}{\vdash \Char \; c : \mathbf{c}} \hspace{2em}
|
564
|
1670 |
|
|
1671 |
\inferrule{\mbox{}}{\vdash \Empty : \ONE} \hspace{2em}
|
|
1672 |
|
591
|
1673 |
\inferrule{\vdash v_1 : r_1 \;\; \vdash v_2 : r_2 }{\vdash \Seq \; v_1,\; v_2 : (r_1 \cdot r_2)}
|
564
|
1674 |
|
591
|
1675 |
\inferrule{\vdash v_1 : r_1}{\vdash \Left \; v_1 : r_1+r_2}
|
564
|
1676 |
|
591
|
1677 |
\inferrule{\vdash v_2 : r_2}{\vdash \Right \; v_2:r_1 + r_2}
|
538
|
1678 |
|
591
|
1679 |
\inferrule{\forall v \in vs. \vdash v:r \land |v| \neq []}{\vdash \Stars \; vs : r^*}
|
564
|
1680 |
\end{mathpar}
|
628
|
1681 |
\caption{The inhabitation relation for values and regular expressions}\label{fig:inhab}
|
626
|
1682 |
\end{figure}
|
564
|
1683 |
\noindent
|
|
1684 |
The condition $|v| \neq []$ in the premise of star's rule
|
|
1685 |
is to make sure that for a given pair of regular
|
|
1686 |
expression $r$ and string $s$, the number of values
|
|
1687 |
satisfying $|v| = s$ and $\vdash v:r$ is finite.
|
601
|
1688 |
This additional condition was
|
|
1689 |
imposed by Ausaf and Urban to make their proofs easier.
|
622
|
1690 |
Given a string and a regular expression, there can be
|
564
|
1691 |
multiple values for it. For example, both
|
|
1692 |
$\vdash \Seq(\Left \; ab)(\Right \; c):(ab+a)(bc+c)$ and
|
|
1693 |
$\vdash \Seq(\Right\; a)(\Left \; bc ):(ab+a)(bc+c)$ hold
|
|
1694 |
and the values both flatten to $abc$.
|
|
1695 |
Lexers therefore have to disambiguate and choose only
|
637
|
1696 |
one of the values to be generated. $\POSIX$ is one of the
|
564
|
1697 |
disambiguation strategies that is widely adopted.
|
|
1698 |
|
638
|
1699 |
Ausaf et al. \cite{AusafDyckhoffUrban2016}
|
564
|
1700 |
formalised the property
|
|
1701 |
as a ternary relation.
|
|
1702 |
The $\POSIX$ value $v$ for a regular expression
|
538
|
1703 |
$r$ and string $s$, denoted as $(s, r) \rightarrow v$, can be specified
|
622
|
1704 |
in the following rules\footnote{The names of the rules are used
|
|
1705 |
as they were originally given in \cite{AusafDyckhoffUrban2016}.}:
|
|
1706 |
\begin{figure}[p]
|
564
|
1707 |
\begin{mathpar}
|
|
1708 |
\inferrule[P1]{\mbox{}}{([], \ONE) \rightarrow \Empty}
|
|
1709 |
|
|
1710 |
\inferrule[PC]{\mbox{}}{([c], c) \rightarrow \Char \; c}
|
|
1711 |
|
|
1712 |
\inferrule[P+L]{(s,r_1)\rightarrow v_1}{(s, r_1+r_2)\rightarrow \Left \; v_1}
|
|
1713 |
|
|
1714 |
\inferrule[P+R]{(s,r_2)\rightarrow v_2\\ s \notin L \; r_1}{(s, r_1+r_2)\rightarrow \Right \; v_2}
|
|
1715 |
|
|
1716 |
\inferrule[PS]{(s_1, v_1) \rightarrow r_1 \\ (s_2, v_2)\rightarrow r_2\\
|
|
1717 |
\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land
|
|
1718 |
s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2}{(s_1 @ s_2, r_1\cdot r_2) \rightarrow
|
|
1719 |
\Seq \; v_1 \; v_2}
|
|
1720 |
|
|
1721 |
\inferrule[P{[]}]{\mbox{}}{([], r^*) \rightarrow \Stars([])}
|
|
1722 |
|
|
1723 |
\inferrule[P*]{(s_1, v) \rightarrow v \\ (s_2, r^*) \rightarrow \Stars \; vs \\
|
|
1724 |
|v| \neq []\\ \nexists s_3 \; s_4. s_3 \neq [] \land s_3@s_4 = s_2 \land
|
|
1725 |
s_1@s_3 \in L \; r \land s_4 \in L \; r^*}{(s_1@s_2, r^*)\rightarrow \Stars \;
|
|
1726 |
(v::vs)}
|
|
1727 |
\end{mathpar}
|
622
|
1728 |
\caption{The inductive POSIX rules given by Ausaf et al.
|
|
1729 |
\cite{AusafDyckhoffUrban2016}.
|
|
1730 |
This ternary relation, written $(s, r) \rightarrow v$,
|
|
1731 |
formalises the POSIX constraints on the
|
601
|
1732 |
value $v$ given a string $s$ and
|
|
1733 |
regular expression $r$.
|
|
1734 |
}
|
646
|
1735 |
\label{fig:POSIXDef}
|
622
|
1736 |
\end{figure}\afterpage{\clearpage}
|
538
|
1737 |
\noindent
|
579
|
1738 |
|
622
|
1739 |
%\begin{figure}
|
|
1740 |
%\begin{tikzpicture}[]
|
|
1741 |
% \node [minimum width = 6cm, rectangle split, rectangle split horizontal,
|
|
1742 |
% rectangle split parts=2, rectangle split part fill={red!30,blue!20}, style={draw, rounded corners, inner sep=10pt}]
|
|
1743 |
% (node1)
|
|
1744 |
% {$r_{token1}$
|
|
1745 |
% \nodepart{two} $\;\;\; \quad r_{token2}\;\;\;\quad$ };
|
|
1746 |
% %\node [left = 6.0cm of node1] (start1) {hi};
|
|
1747 |
% \node [left = 0.2cm of node1] (middle) {$v.s.$};
|
|
1748 |
% \node [minimum width = 6cm, left = 0.2cm of middle, rectangle split, rectangle split horizontal,
|
|
1749 |
% rectangle split parts=2, rectangle split part fill={red!30,blue!20}, style={draw, rounded corners, inner sep=10pt}]
|
|
1750 |
% (node2)
|
|
1751 |
% {$\quad\;\;\;r_{token1}\quad\;\;\;$
|
|
1752 |
% \nodepart{two} $r_{token2}$ };
|
|
1753 |
% \node [below = 0.1cm of node2] (text1) {\checkmark preferred by POSIX};
|
|
1754 |
% \node [above = 1.5cm of middle, minimum width = 6cm,
|
|
1755 |
% rectangle, style={draw, rounded corners, inner sep=10pt}]
|
|
1756 |
% (topNode) {$s$};
|
|
1757 |
% \path[->,draw]
|
|
1758 |
% (topNode) edge node {split $A$} (node2)
|
|
1759 |
% (topNode) edge node {split $B$} (node1)
|
|
1760 |
% ;
|
|
1761 |
%
|
|
1762 |
%
|
|
1763 |
%\end{tikzpicture}
|
|
1764 |
%\caption{Maximum munch example: $s$ matches $r_{token1} \cdot r_{token2}$}\label{munch}
|
|
1765 |
%\end{figure}
|
637
|
1766 |
The above $\POSIX$ rules follow the intuition described below:
|
538
|
1767 |
\begin{itemize}
|
564
|
1768 |
\item (Left Priority)\\
|
637
|
1769 |
Match the leftmost regular expression when multiple options for matching
|
622
|
1770 |
are available. See P+L and P+R where in P+R $s$ cannot
|
|
1771 |
be in the language of $L \; r_1$.
|
564
|
1772 |
\item (Maximum munch)\\
|
|
1773 |
Always match a subpart as much as possible before proceeding
|
622
|
1774 |
to the next part of the string.
|
579
|
1775 |
For example, when the string $s$ matches
|
622
|
1776 |
$r_{part1}\cdot r_{part2}$, and we have two ways $s$ can be split:
|
|
1777 |
Then the split that matches a longer string for the first part
|
|
1778 |
$r_{part1}$ is preferred by this maximum munch rule.
|
637
|
1779 |
The side-condition
|
622
|
1780 |
\begin{center}
|
|
1781 |
$\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land
|
|
1782 |
s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2$
|
|
1783 |
\end{center}
|
637
|
1784 |
in PS causes this.
|
622
|
1785 |
%(See
|
|
1786 |
%\ref{munch} for an illustration).
|
538
|
1787 |
\end{itemize}
|
564
|
1788 |
\noindent
|
|
1789 |
These disambiguation strategies can be
|
|
1790 |
quite practical.
|
538
|
1791 |
For instance, when lexing a code snippet
|
564
|
1792 |
\[
|
|
1793 |
\textit{iffoo} = 3
|
|
1794 |
\]
|
622
|
1795 |
using a regular expression
|
|
1796 |
for keywords and
|
|
1797 |
identifiers:
|
|
1798 |
%(for example, keyword is a nonempty string starting with letters
|
|
1799 |
%followed by alphanumeric characters or underscores):
|
564
|
1800 |
\[
|
622
|
1801 |
r_{keyword} + r_{identifier}.
|
564
|
1802 |
\]
|
622
|
1803 |
If we want $\textit{iffoo}$ to be recognized
|
|
1804 |
as an identifier
|
|
1805 |
where identifiers are defined as usual (letters
|
|
1806 |
followed by letters, numbers or underscores),
|
|
1807 |
then a match with a keyword (if)
|
564
|
1808 |
followed by
|
622
|
1809 |
an identifier (foo) would be incorrect.
|
638
|
1810 |
POSIX lexing generates what is included by lexing.
|
564
|
1811 |
|
622
|
1812 |
\noindent
|
|
1813 |
We know that a POSIX
|
|
1814 |
value for regular expression $r$ is inhabited by $r$.
|
541
|
1815 |
\begin{lemma}
|
|
1816 |
$(r, s) \rightarrow v \implies \vdash v: r$
|
|
1817 |
\end{lemma}
|
|
1818 |
\noindent
|
622
|
1819 |
The main property about a $\POSIX$ value is that
|
538
|
1820 |
given the same regular expression $r$ and string $s$,
|
|
1821 |
one can always uniquely determine the $\POSIX$ value for it:
|
|
1822 |
\begin{lemma}
|
|
1823 |
$\textit{if} \,(s, r) \rightarrow v_1 \land (s, r) \rightarrow v_2\quad \textit{then} \; v_1 = v_2$
|
|
1824 |
\end{lemma}
|
539
|
1825 |
\begin{proof}
|
564
|
1826 |
By induction on $s$, $r$ and $v_1$. The inductive cases
|
|
1827 |
are all the POSIX rules.
|
|
1828 |
Probably the most cumbersome cases are
|
|
1829 |
the sequence and star with non-empty iterations.
|
567
|
1830 |
We shall give the details for proving the sequence case here.
|
539
|
1831 |
|
567
|
1832 |
When we have
|
|
1833 |
\[
|
|
1834 |
(s_1, r_1) \rightarrow v_1 \;\, and \;\,
|
601
|
1835 |
(s_2, r_2) \rightarrow v_2 \;\, and \;\,
|
567
|
1836 |
\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land
|
|
1837 |
s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2
|
|
1838 |
\]
|
|
1839 |
we know that the last condition
|
|
1840 |
excludes the possibility of a
|
|
1841 |
string $s_1'$ longer than $s_1$ such that
|
|
1842 |
\[
|
|
1843 |
(s_1', r_1) \rightarrow v_1' \;\;
|
|
1844 |
and\;\; (s_2', r_2) \rightarrow v_2'\;\; and \;\;s_1' @s_2' = s
|
|
1845 |
\]
|
|
1846 |
hold.
|
|
1847 |
A shorter string $s_1''$ with $s_2''$ satisfying
|
|
1848 |
\[
|
|
1849 |
(s_1'', r_1) \rightarrow v_1''
|
|
1850 |
\;\;and\;\; (s_2'', r_2) \rightarrow v_2'' \;\;and \;\;s_1'' @s_2'' = s
|
|
1851 |
\]
|
|
1852 |
cannot possibly form a $\POSIX$ value either, because
|
637
|
1853 |
by definition, there is a candidate
|
|
1854 |
with a longer initial string
|
567
|
1855 |
$s_1$. Therefore, we know that the POSIX
|
|
1856 |
value $\Seq \; a \; b$ for $r_1 \cdot r_2$ matching
|
|
1857 |
$s$ must have the
|
|
1858 |
property that
|
|
1859 |
\[
|
|
1860 |
|a| = s_1 \;\; and \;\; |b| = s_2.
|
|
1861 |
\]
|
|
1862 |
The goal is to prove that $a = v_1 $ and $b = v_2$.
|
|
1863 |
If we have some other POSIX values $v_{10}$ and $v_{20}$ such that
|
|
1864 |
$(s_1, r_1) \rightarrow v_{10}$ and $(s_2, r_2) \rightarrow v_{20}$ hold,
|
|
1865 |
then by induction hypothesis $v_{10} = v_1$ and $v_{20}= v_2$,
|
|
1866 |
which means this "other" $\POSIX$ value $\Seq(v_{10}, v_{20})$
|
539
|
1867 |
is the same as $\Seq(v_1, v_2)$.
|
|
1868 |
\end{proof}
|
567
|
1869 |
\noindent
|
637
|
1870 |
We have now defined what a POSIX value is and shown that it is unique.
|
|
1871 |
The problem is to generate
|
567
|
1872 |
such a value in a lexing algorithm using derivatives.
|
538
|
1873 |
|
|
1874 |
\subsection{Sulzmann and Lu's Injection-based Lexing Algorithm}
|
|
1875 |
|
567
|
1876 |
Sulzmann and Lu extended Brzozowski's
|
|
1877 |
derivative-based matching
|
622
|
1878 |
to a lexing algorithm by a second phase
|
567
|
1879 |
after the initial phase of successive derivatives.
|
|
1880 |
This second phase generates a POSIX value
|
|
1881 |
if the regular expression matches the string.
|
638
|
1882 |
The algorithm uses two functions called $\inj$ and $\mkeps$.
|
622
|
1883 |
The function $\mkeps$ constructs a POSIX value from the last
|
567
|
1884 |
derivative $r_n$:
|
538
|
1885 |
\begin{ceqn}
|
|
1886 |
\begin{equation}\label{graph:mkeps}
|
|
1887 |
\begin{tikzcd}
|
567
|
1888 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed, "\ldots"] & r_n \arrow[d, "mkeps" description] \\
|
538
|
1889 |
& & & v_n
|
|
1890 |
\end{tikzcd}
|
|
1891 |
\end{equation}
|
|
1892 |
\end{ceqn}
|
567
|
1893 |
\noindent
|
|
1894 |
In the above diagram, again we assume that
|
|
1895 |
the input string $s$ is made of $n$ characters
|
622
|
1896 |
$c_0c_1 \ldots c_{n-1}$
|
|
1897 |
The last derivative operation
|
|
1898 |
$\backslash c_{n-1}$ generates the derivative $r_n$, for which
|
|
1899 |
$\mkeps$ produces the value $v_n$. This value
|
|
1900 |
tells us how the empty string is matched by the (nullable)
|
|
1901 |
regular expression $r_n$, in a POSIX way.
|
567
|
1902 |
The definition of $\mkeps$ is
|
538
|
1903 |
\begin{center}
|
|
1904 |
\begin{tabular}{lcl}
|
564
|
1905 |
$\mkeps \; \ONE$ & $\dn$ & $\Empty$ \\
|
567
|
1906 |
$\mkeps \; (r_{1}+r_{2})$ & $\dn$
|
|
1907 |
& $\textit{if}\; (\nullable \; r_{1}) \;\,
|
|
1908 |
\textit{then}\;\, \Left \; (\mkeps \; r_{1})$\\
|
|
1909 |
& & $\phantom{if}\; \textit{else}\;\, \Right \;(\mkeps \; r_{2})$\\
|
|
1910 |
$\mkeps \; (r_1 \cdot r_2)$ & $\dn$ & $\Seq\;(\mkeps\;r_1)\;(\mkeps \; r_2)$\\
|
564
|
1911 |
$\mkeps \; r^* $ & $\dn$ & $\Stars\;[]$
|
538
|
1912 |
\end{tabular}
|
|
1913 |
\end{center}
|
|
1914 |
|
|
1915 |
|
|
1916 |
\noindent
|
622
|
1917 |
The function prefers the left child $r_1$ of $r_1 + r_2$
|
567
|
1918 |
to match an empty string if there is a choice.
|
622
|
1919 |
When there is a star to match the empty string,
|
538
|
1920 |
we give the $\Stars$ constructor an empty list, meaning
|
567
|
1921 |
no iteration is taken.
|
622
|
1922 |
The result of $\mkeps$ on a $\nullable$ $r$
|
|
1923 |
is a POSIX value for $r$ and the empty string:
|
567
|
1924 |
\begin{lemma}\label{mePosix}
|
638
|
1925 |
$\nullable\; r \implies (r, []) \rightarrow (\mkeps\; r)$
|
567
|
1926 |
\end{lemma}
|
|
1927 |
\begin{proof}
|
622
|
1928 |
By induction on $r$.
|
567
|
1929 |
\end{proof}
|
|
1930 |
\noindent
|
622
|
1931 |
After the $\mkeps$-call, Sulzmann and Lu inject back the characters one by one
|
567
|
1932 |
in reverse order as they were chopped off in the derivative phase.
|
637
|
1933 |
The function for this is called $\inj$. This function
|
622
|
1934 |
operates on values, unlike $\backslash$ which operates on regular expressions.
|
567
|
1935 |
In the diagram below, $v_i$ stands for the (POSIX) value
|
|
1936 |
for how the regular expression
|
|
1937 |
$r_i$ matches the string $s_i$ consisting of the last $n-i$ characters
|
|
1938 |
of $s$ (i.e. $s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
|
538
|
1939 |
After injecting back $n$ characters, we get the lexical value for how $r_0$
|
|
1940 |
matches $s$.
|
601
|
1941 |
\begin{figure}[H]
|
|
1942 |
\begin{center}
|
538
|
1943 |
\begin{ceqn}
|
|
1944 |
\begin{tikzcd}
|
567
|
1945 |
r_0 \arrow[r, dashed] \arrow[d]& r_i \arrow[r, "\backslash c_i"] \arrow[d] & r_{i+1} \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
|
|
1946 |
v_0 \arrow[u] & v_i \arrow[l, dashed] & v_{i+1} \arrow[l,"inj_{r_i} c_i"] & v_n \arrow[l, dashed]
|
538
|
1947 |
\end{tikzcd}
|
|
1948 |
\end{ceqn}
|
601
|
1949 |
\end{center}
|
|
1950 |
\caption{The two-phase lexing algorithm by Sulzmann and Lu \cite{AusafDyckhoffUrban2016},
|
|
1951 |
matching the regular expression $r_0$ and string of the form $[c_0, c_1, \ldots, c_{n-1}]$.
|
|
1952 |
The first phase involves taking successive derivatives w.r.t the characters $c_0$,
|
|
1953 |
$c_1$, and so on. These are the same operations as they have appeared in the matcher
|
|
1954 |
\ref{matcher}. When the final derivative regular expression is nullable (contains the empty string),
|
637
|
1955 |
then the second phase starts. First, $\mkeps$ generates a POSIX value which tells us how $r_n$ matches
|
|
1956 |
the empty string, by always selecting the leftmost
|
|
1957 |
nullable regular expression. After that, $\inj$ ``injects'' back the character in reverse order as they
|
601
|
1958 |
appeared in the string, always preserving POSIXness.}\label{graph:inj}
|
|
1959 |
\end{figure}
|
538
|
1960 |
\noindent
|
623
|
1961 |
The function $\textit{inj}$ as defined by Sulzmann and Lu
|
|
1962 |
takes three arguments: a regular
|
567
|
1963 |
expression ${r_{i}}$, before the character is chopped off,
|
623
|
1964 |
a character ${c_{i}}$ (the character we want to inject back) and
|
567
|
1965 |
the third argument $v_{i+1}$ the value we want to inject into.
|
568
|
1966 |
The result of an application
|
|
1967 |
$\inj \; r_i \; c_i \; v_{i+1}$ is a new value $v_i$ such that
|
|
1968 |
\[
|
|
1969 |
(s_i, r_i) \rightarrow v_i
|
|
1970 |
\]
|
|
1971 |
holds.
|
567
|
1972 |
The definition of $\textit{inj}$ is as follows:
|
538
|
1973 |
\begin{center}
|
568
|
1974 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{5mm}}l}
|
|
1975 |
$\textit{inj}\;(c)\;c\,Empty$ & $\dn$ & $\Char\,c$\\
|
|
1976 |
$\textit{inj}\;(r_1 + r_2)\;c\; (\Left\; v)$ & $\dn$ & $\Left \; (\textit{inj}\; r_1 \; c\,v)$\\
|
|
1977 |
$\textit{inj}\;(r_1 + r_2)\,c\; (\Right\;v)$ & $\dn$ & $\Right \; (\textit{inj}\;r_2\;c \; v)$\\
|
|
1978 |
$\textit{inj}\;(r_1 \cdot r_2)\; c\;(\Seq \; v_1 \; v_2)$ & $\dn$ &
|
|
1979 |
$\Seq \; (\textit{inj}\;r_1\;c\;v_1) \; v_2$\\
|
|
1980 |
$\textit{inj}\;(r_1 \cdot r_2)\; c\;(\Left \; (\Seq \; v_1\;v_2) )$ &
|
|
1981 |
$\dn$ & $\Seq \; (\textit{inj}\,r_1\,c\,v_1)\; v_2$\\
|
|
1982 |
$\textit{inj}\;(r_1 \cdot r_2)\; c\; (\Right\; v)$ & $\dn$ & $\Seq\; (\textit{mkeps}\; r_1) \; (\textit{inj} \; r_2\;c\;v)$\\
|
|
1983 |
$\textit{inj}\;(r^*)\; c \; (\Seq \; v\; (\Stars\;vs))$ & $\dn$ & $\Stars\;\,((\textit{inj}\;r\;c\;v)\,::\,vs)$\\
|
538
|
1984 |
\end{tabular}
|
|
1985 |
\end{center}
|
|
1986 |
|
|
1987 |
\noindent
|
623
|
1988 |
The function recurses on
|
568
|
1989 |
the shape of regular
|
637
|
1990 |
expressions and values.
|
568
|
1991 |
Intuitively, each clause analyses
|
|
1992 |
how $r_i$ could have transformed when being
|
|
1993 |
derived by $c$, identifying which subpart
|
|
1994 |
of $v_{i+1}$ has the ``hole''
|
|
1995 |
to inject the character back into.
|
|
1996 |
Once the character is
|
|
1997 |
injected back to that sub-value;
|
637
|
1998 |
$\inj$ assembles all parts
|
568
|
1999 |
to form a new value.
|
|
2000 |
|
|
2001 |
For instance, the last clause is an
|
|
2002 |
injection into a sequence value $v_{i+1}$
|
|
2003 |
whose second child
|
637
|
2004 |
value is a star and the shape of the
|
568
|
2005 |
regular expression $r_i$ before injection
|
|
2006 |
is a star.
|
|
2007 |
We therefore know
|
|
2008 |
the derivative
|
|
2009 |
starts on a star and ends as a sequence:
|
|
2010 |
\[
|
|
2011 |
(r^*) \backslash c \longrightarrow r\backslash c \cdot r^*
|
|
2012 |
\]
|
|
2013 |
during which an iteration of the star
|
|
2014 |
had just been unfolded, giving the below
|
|
2015 |
value inhabitation relation:
|
|
2016 |
\[
|
|
2017 |
\vdash \Seq \; v \; (\Stars \; vs) : (r\backslash c) \cdot r^*.
|
|
2018 |
\]
|
|
2019 |
The value list $vs$ corresponds to
|
|
2020 |
matched star iterations,
|
|
2021 |
and the ``hole'' lies in $v$ because
|
|
2022 |
\[
|
|
2023 |
\vdash v: r\backslash c.
|
|
2024 |
\]
|
|
2025 |
Finally,
|
|
2026 |
$\inj \; r \;c \; v$ is prepended
|
637
|
2027 |
to the previous list of iterations and then
|
568
|
2028 |
wrapped under the $\Stars$
|
|
2029 |
constructor, giving us $\Stars \; ((\inj \; r \; c \; v) ::vs)$.
|
538
|
2030 |
|
568
|
2031 |
Recall that lemma
|
623
|
2032 |
\ref{mePosix} tells us that
|
|
2033 |
$\mkeps$ always generates the POSIX value.
|
|
2034 |
The function $\inj$ preserves the POSIXness, provided
|
|
2035 |
the value before injection is POSIX, namely
|
568
|
2036 |
\begin{lemma}\label{injPosix}
|
623
|
2037 |
If$(r \backslash c, s) \rightarrow v $,
|
|
2038 |
then $(r, c :: s) \rightarrow (\inj r \; c\; v)$.
|
568
|
2039 |
\end{lemma}
|
|
2040 |
\begin{proof}
|
|
2041 |
By induction on $r$.
|
623
|
2042 |
The non-trivial cases are sequence and star.
|
|
2043 |
When $r = a \cdot b$, there can be
|
568
|
2044 |
three cases for the value $v$ satisfying $\vdash v:a\backslash c$.
|
|
2045 |
We give the reasoning why $\inj \; r \; c \; v$ is POSIX in each
|
|
2046 |
case.
|
|
2047 |
\begin{itemize}
|
|
2048 |
\item
|
|
2049 |
$v = \Seq \; v_a \; v_b$.\\
|
|
2050 |
The ``not nullable'' clause of the $\inj$ function is taken:
|
|
2051 |
\begin{center}
|
|
2052 |
\begin{tabular}{lcl}
|
|
2053 |
$\inj \; r \; c \; v$ & $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; (\Seq \; v_a \; v_b) $\\
|
|
2054 |
& $=$ & $\Seq \; (\inj \;a \; c \; v_a) \; v_b$
|
|
2055 |
\end{tabular}
|
|
2056 |
\end{center}
|
|
2057 |
We know that there exists a unique pair of
|
637
|
2058 |
$s_a$ and $s_b$ satisfying
|
568
|
2059 |
$(a \backslash c, s_a) \rightarrow v_a$,
|
|
2060 |
$(b , s_b) \rightarrow v_b$, and
|
|
2061 |
$\nexists s_3 \; s_4. s_3 \neq [] \land s_a @ s_3 \in
|
|
2062 |
L \; (a\backslash c) \land
|
|
2063 |
s_4 \in L \; b$.
|
|
2064 |
The last condition gives us
|
|
2065 |
$\nexists s_3 \; s_4. s_3 \neq [] \land (c :: s_a )@ s_3 \in
|
|
2066 |
L \; a \land
|
|
2067 |
s_4 \in L \; b$.
|
|
2068 |
By induction hypothesis, $(a, c::s_a) \rightarrow \inj \; a \; c \; v_a $ holds,
|
|
2069 |
and this gives us
|
|
2070 |
\[
|
|
2071 |
(a\cdot b, (c::s_a)@s_b) \rightarrow \Seq \; (\inj \; a\;c \;v_a) \; v_b.
|
|
2072 |
\]
|
|
2073 |
\item
|
|
2074 |
$v = \Left \; (\Seq \; v_a \; v_b)$\\
|
|
2075 |
The argument is almost identical to the above case,
|
|
2076 |
except that a different clause of $\inj$ is taken:
|
|
2077 |
\begin{center}
|
|
2078 |
\begin{tabular}{lcl}
|
|
2079 |
$\inj \; r \; c \; v$ & $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; (\Left \; (\Seq \; v_a \; v_b)) $\\
|
|
2080 |
& $=$ & $\Seq \; (\inj \;a \; c \; v_a) \; v_b$
|
|
2081 |
\end{tabular}
|
|
2082 |
\end{center}
|
637
|
2083 |
With similar reasoning,
|
538
|
2084 |
|
568
|
2085 |
\[
|
|
2086 |
(a\cdot b, (c::s_a)@s_b) \rightarrow \Seq \; (\inj \; a\;c \;v_a) \; v_b.
|
|
2087 |
\]
|
|
2088 |
again holds.
|
|
2089 |
\item
|
|
2090 |
$v = \Right \; v_b$\\
|
|
2091 |
Again the injection result would be
|
|
2092 |
\begin{center}
|
|
2093 |
\begin{tabular}{lcl}
|
|
2094 |
$\inj \; r \; c \; v$ & $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; \Right \; (v_b) $\\
|
|
2095 |
& $=$ & $\Seq \; (\mkeps \; a) \; (\inj \;b \; c\; v_b)$
|
|
2096 |
\end{tabular}
|
|
2097 |
\end{center}
|
|
2098 |
We know that $a$ must be nullable,
|
|
2099 |
allowing us to call $\mkeps$ and get
|
|
2100 |
\[
|
|
2101 |
(a, []) \rightarrow \mkeps \; a.
|
|
2102 |
\]
|
637
|
2103 |
Also, by inductive hypothesis
|
568
|
2104 |
\[
|
|
2105 |
(b, c::s) \rightarrow \inj\; b \; c \; v_b
|
|
2106 |
\]
|
|
2107 |
holds.
|
|
2108 |
In addition, as
|
|
2109 |
$\Right \;v_b$ instead of $\Left \ldots$ is
|
|
2110 |
the POSIX value for $v$, it must be the case
|
|
2111 |
that $s \notin L \;( (a\backslash c)\cdot b)$.
|
|
2112 |
This tells us that
|
|
2113 |
\[
|
|
2114 |
\nexists s_3 \; s_4.
|
|
2115 |
s_3 @s_4 = s \land s_3 \in L \; (a\backslash c)
|
|
2116 |
\land s_4 \in L \; b
|
|
2117 |
\]
|
|
2118 |
which translates to
|
|
2119 |
\[
|
|
2120 |
\nexists s_3 \; s_4. \; s_3 \neq [] \land
|
|
2121 |
s_3 @s_4 = c::s \land s_3 \in L \; a
|
|
2122 |
\land s_4 \in L \; b.
|
|
2123 |
\]
|
637
|
2124 |
(Which says there cannot be a longer
|
568
|
2125 |
initial split for $s$ other than the empty string.)
|
|
2126 |
Therefore we have $\Seq \; (\mkeps \; a) \;(\inj \;b \; c\; v_b)$
|
|
2127 |
as the POSIX value for $a\cdot b$.
|
|
2128 |
\end{itemize}
|
|
2129 |
The star case can be proven similarly.
|
|
2130 |
\end{proof}
|
|
2131 |
\noindent
|
623
|
2132 |
Putting all together, Sulzmann and Lu obtained the following algorithm
|
|
2133 |
outlined in the diagram \ref{graph:inj}:
|
538
|
2134 |
\begin{center}
|
539
|
2135 |
\begin{tabular}{lcl}
|
568
|
2136 |
$\lexer \; r \; [] $ & $=$ & $\textit{if} \; (\nullable \; r)\; \textit{then}\; \Some(\mkeps \; r) \; \textit{else} \; \None$\\
|
|
2137 |
$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer \; (r\backslash c) \; s) \;\textit{of}\; $\\
|
|
2138 |
& & $\quad \phantom{\mid}\; \None \implies \None$\\
|
|
2139 |
& & $\quad \mid \Some(v) \implies \Some(\inj \; r\; c\; v)$
|
538
|
2140 |
\end{tabular}
|
|
2141 |
\end{center}
|
568
|
2142 |
\noindent
|
623
|
2143 |
The central property of the $\lexer$ is that it gives the correct result
|
|
2144 |
according to
|
|
2145 |
POSIX rules.
|
573
|
2146 |
\begin{theorem}\label{lexerCorrectness}
|
568
|
2147 |
The $\lexer$ based on derivatives and injections is correct:
|
|
2148 |
\begin{center}
|
|
2149 |
\begin{tabular}{lcl}
|
|
2150 |
$\lexer \; r \; s = \Some(v)$ & $ \Longleftrightarrow$ & $ (r, \; s) \rightarrow v$\\
|
|
2151 |
$\lexer \;r \; s = \None $ & $\Longleftrightarrow$ & $ \neg(\exists v. (r, s) \rightarrow v)$
|
|
2152 |
\end{tabular}
|
|
2153 |
\end{center}
|
|
2154 |
\end{theorem}
|
|
2155 |
\begin{proof}
|
623
|
2156 |
By induction on $s$. $r$ generalising over an arbitrary regular expression.
|
637
|
2157 |
The $[]$ case is proven by an application of lemma \ref{mePosix}, and the inductive case
|
568
|
2158 |
by lemma \ref{injPosix}.
|
|
2159 |
\end{proof}
|
538
|
2160 |
\noindent
|
623
|
2161 |
As we did earlier in this chapter with the matcher, one can
|
|
2162 |
introduce simplification on the regular expression in each derivative step.
|
637
|
2163 |
However, due to lexing, one needs to do a backward phase (w.r.t the forward derivative phase)
|
|
2164 |
and ensure that
|
|
2165 |
the values align with the regular expression at each step.
|
539
|
2166 |
Therefore one has to
|
538
|
2167 |
be careful not to break the correctness, as the injection
|
623
|
2168 |
function heavily relies on the structure of
|
|
2169 |
the regular expressions and values being aligned.
|
|
2170 |
This can be achieved by recording some extra rectification functions
|
637
|
2171 |
during the derivatives step and applying these rectifications in
|
538
|
2172 |
each run during the injection phase.
|
568
|
2173 |
With extra care
|
623
|
2174 |
one can show that POSIXness will not be affected
|
|
2175 |
by the simplifications listed here \cite{AusafDyckhoffUrban2016}.
|
|
2176 |
\begin{center}
|
|
2177 |
\begin{tabular}{lcl}
|
|
2178 |
$\simp \; r_1 \cdot r_2 $ & $ \dn$ &
|
|
2179 |
$(\simp \; r_1, \simp \; r_2) \; \textit{match}$\\
|
|
2180 |
& & $\quad \case \; (\ZERO, \_) \Rightarrow \ZERO$\\
|
|
2181 |
& & $\quad \case \; (\_, \ZERO) \Rightarrow \ZERO$\\
|
|
2182 |
& & $\quad \case \; (\ONE, r_2') \Rightarrow r_2'$\\
|
|
2183 |
& & $\quad \case \; (r_1', \ONE) \Rightarrow r_1'$\\
|
|
2184 |
& & $\quad \case \; (r_1', r_2') \Rightarrow r_1'\cdot r_2'$\\
|
|
2185 |
$\simp \; r_1 + r_2$ & $\dn$ & $(\simp \; r_1, \simp \; r_2) \textit{match}$\\
|
|
2186 |
& & $\quad \; \case \; (\ZERO, r_2') \Rightarrow r_2'$\\
|
|
2187 |
& & $\quad \; \case \; (r_1', \ZERO) \Rightarrow r_1'$\\
|
|
2188 |
& & $\quad \; \case \; (r_1', r_2') \Rightarrow r_1' + r_2'$\\
|
|
2189 |
$\simp \; r$ & $\dn$ & $r\quad\quad (otherwise)$
|
|
2190 |
|
|
2191 |
\end{tabular}
|
|
2192 |
\end{center}
|
538
|
2193 |
|
637
|
2194 |
However, one can still end up
|
|
2195 |
with exploding derivatives,
|
|
2196 |
even with the simple-minded simplification rules allowed
|
|
2197 |
in an injection-based lexer.
|
|
2198 |
\section{A Case Requiring More Aggressive Simplifications}
|
539
|
2199 |
For example, when starting with the regular
|
585
|
2200 |
expression $(a^* \cdot a^*)^*$ and building just over
|
|
2201 |
a dozen successive derivatives
|
539
|
2202 |
w.r.t.~the character $a$, one obtains a derivative regular expression
|
585
|
2203 |
with millions of nodes (when viewed as a tree)
|
637
|
2204 |
even with the mentioned simplifications.
|
585
|
2205 |
\begin{figure}[H]
|
601
|
2206 |
\begin{center}
|
539
|
2207 |
\begin{tikzpicture}
|
|
2208 |
\begin{axis}[
|
|
2209 |
xlabel={$n$},
|
|
2210 |
ylabel={size},
|
585
|
2211 |
legend entries={Simple-Minded Simp, Naive Matcher},
|
539
|
2212 |
legend pos=north west,
|
|
2213 |
legend cell align=left]
|
|
2214 |
\addplot[red,mark=*, mark options={fill=white}] table {BetterWaterloo.data};
|
585
|
2215 |
\addplot[blue,mark=*, mark options={fill=white}] table {BetterWaterloo1.data};
|
539
|
2216 |
\end{axis}
|
|
2217 |
\end{tikzpicture}
|
601
|
2218 |
\end{center}
|
539
|
2219 |
\caption{Size of $(a^*\cdot a^*)^*$ against $\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$}
|
|
2220 |
\end{figure}\label{fig:BetterWaterloo}
|
|
2221 |
|
568
|
2222 |
That is because Sulzmann and Lu's
|
|
2223 |
injection-based lexing algorithm keeps a lot of
|
541
|
2224 |
"useless" values that will not be used.
|
539
|
2225 |
These different ways of matching will grow exponentially with the string length.
|
623
|
2226 |
Consider the case
|
568
|
2227 |
\[
|
|
2228 |
r= (a^*\cdot a^*)^* \quad and \quad
|
|
2229 |
s=\underbrace{aa\ldots a}_\text{n \textit{a}s}
|
|
2230 |
\]
|
|
2231 |
as an example.
|
|
2232 |
This is a highly ambiguous regular expression, with
|
|
2233 |
many ways to split up the string into multiple segments for
|
637
|
2234 |
different star iterations,
|
573
|
2235 |
and for each segment
|
|
2236 |
multiple ways of splitting between
|
568
|
2237 |
the two $a^*$ sub-expressions.
|
573
|
2238 |
When $n$ is equal to $1$, there are two lexical values for
|
|
2239 |
the match:
|
|
2240 |
\[
|
623
|
2241 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [])] \quad (v1)
|
573
|
2242 |
\]
|
|
2243 |
and
|
|
2244 |
\[
|
623
|
2245 |
\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])] \quad (v2)
|
573
|
2246 |
\]
|
|
2247 |
The derivative of $\derssimp \;s \; r$ is
|
|
2248 |
\[
|
|
2249 |
(a^*a^* + a^*)\cdot(a^*a^*)^*.
|
|
2250 |
\]
|
|
2251 |
The $a^*a^*$ and $a^*$ in the first child of the above sequence
|
|
2252 |
correspond to value 1 and value 2, respectively.
|
|
2253 |
When $n=2$, the number goes up to 7:
|
|
2254 |
\[
|
|
2255 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a, \Char \; a])\; (\Stars \; [])]
|
|
2256 |
\]
|
623
|
2257 |
|
573
|
2258 |
\[
|
|
2259 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [\Char \; a])]
|
|
2260 |
\]
|
623
|
2261 |
|
573
|
2262 |
\[
|
|
2263 |
\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a, \Char \; a])]
|
|
2264 |
\]
|
623
|
2265 |
|
573
|
2266 |
\[
|
|
2267 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; []), \Seq \; (\Stars \; [\Char\;a])\; (\Stars\; []) ]
|
|
2268 |
\]
|
623
|
2269 |
|
573
|
2270 |
\[
|
|
2271 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; []),
|
|
2272 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])
|
|
2273 |
]
|
|
2274 |
\]
|
623
|
2275 |
|
573
|
2276 |
\[
|
|
2277 |
\Stars \; [
|
|
2278 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a]),
|
|
2279 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])
|
|
2280 |
]
|
|
2281 |
\]
|
|
2282 |
and
|
|
2283 |
\[
|
|
2284 |
\Stars \; [
|
|
2285 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a]),
|
|
2286 |
\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [])
|
|
2287 |
]
|
|
2288 |
\]
|
638
|
2289 |
And $\derssimp \; aa \; (a^*a^*)^*$ is
|
573
|
2290 |
\[
|
|
2291 |
((a^*a^* + a^*)+a^*)\cdot(a^*a^*)^* +
|
|
2292 |
(a^*a^* + a^*)\cdot(a^*a^*)^*.
|
|
2293 |
\]
|
|
2294 |
which removes two out of the seven terms corresponding to the
|
|
2295 |
seven distinct lexical values.
|
|
2296 |
|
|
2297 |
It is not surprising that there are exponentially many
|
|
2298 |
distinct lexical values that cannot be eliminated by
|
|
2299 |
the simple-minded simplification of $\derssimp$.
|
568
|
2300 |
A lexer without a good enough strategy to
|
|
2301 |
deduplicate will naturally
|
623
|
2302 |
have an exponential runtime on highly
|
637
|
2303 |
ambiguous regular expressions because there
|
623
|
2304 |
are exponentially many matches.
|
|
2305 |
For this particular example, it seems
|
|
2306 |
that the number of distinct matches growth
|
|
2307 |
speed is proportional to $(2n)!/(n!(n+1)!)$ ($n$ being the input length).
|
538
|
2308 |
|
573
|
2309 |
On the other hand, the
|
|
2310 |
$\POSIX$ value for $r= (a^*\cdot a^*)^*$ and
|
|
2311 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$ is
|
|
2312 |
\[
|
|
2313 |
\Stars\,
|
637
|
2314 |
[\Seq \; (\Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}]), \Stars\,[]].
|
573
|
2315 |
\]
|
637
|
2316 |
At any moment, the subterms in a regular expression
|
|
2317 |
that will potentially result in a POSIX value is only
|
573
|
2318 |
a minority among the many other terms,
|
638
|
2319 |
and one can remove the ones that are not possible to
|
573
|
2320 |
be POSIX.
|
|
2321 |
In the above example,
|
579
|
2322 |
\begin{equation}\label{eqn:growth2}
|
573
|
2323 |
((a^*a^* + \underbrace{a^*}_\text{A})+\underbrace{a^*}_\text{duplicate of A})\cdot(a^*a^*)^* +
|
|
2324 |
\underbrace{(a^*a^* + a^*)\cdot(a^*a^*)^*}_\text{further simp removes this}.
|
579
|
2325 |
\end{equation}
|
573
|
2326 |
can be further simplified by
|
|
2327 |
removing the underlined term first,
|
|
2328 |
which would open up possibilities
|
|
2329 |
of further simplification that removes the
|
|
2330 |
underbraced part.
|
|
2331 |
The result would be
|
|
2332 |
\[
|
|
2333 |
(\underbrace{a^*a^*}_\text{term 1} + \underbrace{a^*}_\text{term 2})\cdot(a^*a^*)^*.
|
|
2334 |
\]
|
|
2335 |
with corresponding values
|
|
2336 |
\begin{center}
|
|
2337 |
\begin{tabular}{lr}
|
|
2338 |
$\Stars \; [\Seq \; (\Stars \; [\Char \; a, \Char \; a])\; (\Stars \; [])]$ & $(\text{term 1})$\\
|
|
2339 |
$\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [\Char \; a])] $ & $(\text{term 2})$
|
|
2340 |
\end{tabular}
|
|
2341 |
\end{center}
|
637
|
2342 |
Other terms with an underlying value, such as
|
573
|
2343 |
\[
|
|
2344 |
\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a, \Char \; a])]
|
|
2345 |
\]
|
638
|
2346 |
do not to contribute a POSIX lexical value,
|
|
2347 |
and therefore can be thrown away.
|
538
|
2348 |
|
623
|
2349 |
Ausaf et al. \cite{AusafDyckhoffUrban2016}
|
639
|
2350 |
have come up with some simplification steps,
|
|
2351 |
and incorporated the simplification into $\lexer$.
|
|
2352 |
They call this lexer $\textit{lexer}_{simp}$ and proved that
|
|
2353 |
\[
|
|
2354 |
\lexer \; r\; s = \textit{lexer}_{simp} \; r \; s
|
|
2355 |
\]
|
|
2356 |
The function $\textit{lexer}_{simp}$
|
|
2357 |
involves some fiddly manipulation of value rectification,
|
|
2358 |
which we omit here.
|
|
2359 |
however those steps
|
638
|
2360 |
are not yet sufficiently strong, to achieve the above effects.
|
637
|
2361 |
And even with these relatively mild simplifications, the proof
|
|
2362 |
is already quite a bit more complicated than the theorem \ref{lexerCorrectness}.
|
638
|
2363 |
One would need to prove something like this:
|
573
|
2364 |
\[
|
|
2365 |
\textit{If}\; (\textit{snd} \; (\textit{simp} \; r\backslash c), s) \rightarrow v \;\;
|
|
2366 |
\textit{then}\;\; (r, c::s) \rightarrow
|
637
|
2367 |
\inj\;\, r\, \;c \;\, ((\textit{fst} \; (\textit{simp} \; r \backslash c))\; v).
|
573
|
2368 |
\]
|
|
2369 |
instead of the simple lemma \ref{injPosix}, where now $\textit{simp}$
|
|
2370 |
not only has to return a simplified regular expression,
|
|
2371 |
but also what specific simplifications
|
638
|
2372 |
have been done as a function on values
|
573
|
2373 |
showing how one can transform the value
|
|
2374 |
underlying the simplified regular expression
|
|
2375 |
to the unsimplified one.
|
|
2376 |
|
623
|
2377 |
We therefore choose a slightly different approach
|
|
2378 |
also described by Sulzmann and Lu to
|
573
|
2379 |
get better simplifications, which uses
|
|
2380 |
some augmented data structures compared to
|
|
2381 |
plain regular expressions.
|
|
2382 |
We call them \emph{annotated}
|
|
2383 |
regular expressions.
|
|
2384 |
With annotated regular expressions,
|
|
2385 |
we can avoid creating the intermediate values $v_1,\ldots v_n$ and a
|
|
2386 |
second phase altogether.
|
|
2387 |
We introduce this new datatype and the
|
|
2388 |
corresponding algorithm in the next chapter.
|
538
|
2389 |
|
|
2390 |
|
|
2391 |
|
|
2392 |
|