author | Chengsong |
Tue, 23 Jul 2019 09:26:22 +0100 | |
changeset 80 | d9d61a648292 |
parent 79 | 481c8000de6d |
child 81 | a0df84875788 |
permissions | -rw-r--r-- |
45 | 1 |
\documentclass[a4paper,UKenglish]{lipics} |
30 | 2 |
\usepackage{graphic} |
3 |
\usepackage{data} |
|
4 |
\usepackage{tikz-cd} |
|
63 | 5 |
%\usepackage{algorithm} |
35 | 6 |
\usepackage{amsmath} |
7 |
\usepackage[noend]{algpseudocode} |
|
42 | 8 |
\usepackage{enumitem} |
70 | 9 |
\usepackage{nccmath} |
63 | 10 |
|
11 |
\definecolor{darkblue}{rgb}{0,0,0.6} |
|
12 |
\hypersetup{colorlinks=true,allcolors=darkblue} |
|
70 | 13 |
\newcommand{\comment}[1]% |
14 |
{{\color{red}$\Rightarrow$}\marginpar{\raggedright\small{\bf\color{red}#1}}} |
|
15 |
||
30 | 16 |
% \documentclass{article} |
17 |
%\usepackage[utf8]{inputenc} |
|
18 |
%\usepackage[english]{babel} |
|
19 |
%\usepackage{listings} |
|
20 |
% \usepackage{amsthm} |
|
63 | 21 |
%\usepackage{hyperref} |
30 | 22 |
% \usepackage[margin=0.5in]{geometry} |
23 |
%\usepackage{pmboxdraw} |
|
24 |
||
25 |
\title{POSIX Regular Expression Matching and Lexing} |
|
26 |
\author{Chengsong Tan} |
|
27 |
\affil{King's College London\\ |
|
28 |
London, UK\\ |
|
29 |
\texttt{chengsong.tan@kcl.ac.uk}} |
|
30 |
\authorrunning{Chengsong Tan} |
|
31 |
\Copyright{Chengsong Tan} |
|
32 |
||
33 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% |
|
34 |
\newcommand{\ZERO}{\mbox{\bf 0}} |
|
35 |
\newcommand{\ONE}{\mbox{\bf 1}} |
|
36 |
\def\lexer{\mathit{lexer}} |
|
37 |
\def\mkeps{\mathit{mkeps}} |
|
38 |
\def\inj{\mathit{inj}} |
|
39 |
\def\Empty{\mathit{Empty}} |
|
40 |
\def\Left{\mathit{Left}} |
|
41 |
\def\Right{\mathit{Right}} |
|
42 |
\def\Stars{\mathit{Stars}} |
|
43 |
\def\Char{\mathit{Char}} |
|
44 |
\def\Seq{\mathit{Seq}} |
|
45 |
\def\Der{\mathit{Der}} |
|
46 |
\def\nullable{\mathit{nullable}} |
|
47 |
\def\Z{\mathit{Z}} |
|
48 |
\def\S{\mathit{S}} |
|
49 |
||
50 |
%\theoremstyle{theorem} |
|
51 |
%\newtheorem{theorem}{Theorem} |
|
52 |
%\theoremstyle{lemma} |
|
53 |
%\newtheorem{lemma}{Lemma} |
|
54 |
%\newcommand{\lemmaautorefname}{Lemma} |
|
55 |
%\theoremstyle{definition} |
|
56 |
%\newtheorem{definition}{Definition} |
|
35 | 57 |
\algnewcommand\algorithmicswitch{\textbf{switch}} |
58 |
\algnewcommand\algorithmiccase{\textbf{case}} |
|
59 |
\algnewcommand\algorithmicassert{\texttt{assert}} |
|
60 |
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}% |
|
61 |
% New "environments" |
|
62 |
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}% |
|
63 |
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}% |
|
64 |
\algtext*{EndSwitch}% |
|
65 |
\algtext*{EndCase}% |
|
30 | 66 |
|
67 |
||
68 |
\begin{document} |
|
69 |
||
70 |
\maketitle |
|
71 |
||
72 |
\begin{abstract} |
|
73 |
Brzozowski introduced in 1964 a beautifully simple algorithm for |
|
74 |
regular expression matching based on the notion of derivatives of |
|
75 |
regular expressions. In 2014, Sulzmann and Lu extended this |
|
40 | 76 |
algorithm to not just give a YES/NO answer for whether or not a |
64 | 77 |
regular expression matches a string, but in case it does also |
58 | 78 |
answers with \emph{how} it matches the string. This is important for |
40 | 79 |
applications such as lexing (tokenising a string). The problem is to |
80 |
make the algorithm by Sulzmann and Lu fast on all inputs without |
|
81 |
breaking its correctness. We have already developed some |
|
59 | 82 |
simplification rules for this, but have not yet proved that they |
40 | 83 |
preserve the correctness of the algorithm. We also have not yet |
84 |
looked at extended regular expressions, such as bounded repetitions, |
|
85 |
negation and back-references. |
|
30 | 86 |
\end{abstract} |
87 |
||
88 |
\section{Introduction} |
|
89 |
||
70 | 90 |
|
30 | 91 |
This PhD-project is about regular expression matching and |
92 |
lexing. Given the maturity of this topic, the reader might wonder: |
|
93 |
Surely, regular expressions must have already been studied to death? |
|
94 |
What could possibly be \emph{not} known in this area? And surely all |
|
95 |
implemented algorithms for regular expression matching are blindingly |
|
96 |
fast? |
|
97 |
||
98 |
Unfortunately these preconceptions are not supported by evidence: Take |
|
99 |
for example the regular expression $(a^*)^*\,b$ and ask whether |
|
100 |
strings of the form $aa..a$ match this regular |
|
64 | 101 |
expression. Obviously not---the expected $b$ in the last |
30 | 102 |
position is missing. One would expect that modern regular expression |
103 |
matching engines can find this out very quickly. Alas, if one tries |
|
104 |
this example in JavaScript, Python or Java 8 with strings like 28 |
|
105 |
$a$'s, one discovers that this decision takes around 30 seconds and |
|
106 |
takes considerably longer when adding a few more $a$'s, as the graphs |
|
107 |
below show: |
|
108 |
||
109 |
\begin{center} |
|
110 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}} |
|
111 |
\begin{tikzpicture} |
|
112 |
\begin{axis}[ |
|
113 |
xlabel={$n$}, |
|
114 |
x label style={at={(1.05,-0.05)}}, |
|
115 |
ylabel={time in secs}, |
|
116 |
enlargelimits=false, |
|
117 |
xtick={0,5,...,30}, |
|
118 |
xmax=33, |
|
119 |
ymax=35, |
|
120 |
ytick={0,5,...,30}, |
|
121 |
scaled ticks=false, |
|
122 |
axis lines=left, |
|
123 |
width=5cm, |
|
124 |
height=4cm, |
|
125 |
legend entries={JavaScript}, |
|
126 |
legend pos=north west, |
|
127 |
legend cell align=left] |
|
128 |
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data}; |
|
129 |
\end{axis} |
|
130 |
\end{tikzpicture} |
|
131 |
& |
|
132 |
\begin{tikzpicture} |
|
133 |
\begin{axis}[ |
|
134 |
xlabel={$n$}, |
|
135 |
x label style={at={(1.05,-0.05)}}, |
|
136 |
%ylabel={time in secs}, |
|
137 |
enlargelimits=false, |
|
138 |
xtick={0,5,...,30}, |
|
139 |
xmax=33, |
|
140 |
ymax=35, |
|
141 |
ytick={0,5,...,30}, |
|
142 |
scaled ticks=false, |
|
143 |
axis lines=left, |
|
144 |
width=5cm, |
|
145 |
height=4cm, |
|
146 |
legend entries={Python}, |
|
147 |
legend pos=north west, |
|
148 |
legend cell align=left] |
|
149 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data}; |
|
150 |
\end{axis} |
|
151 |
\end{tikzpicture} |
|
152 |
& |
|
153 |
\begin{tikzpicture} |
|
154 |
\begin{axis}[ |
|
155 |
xlabel={$n$}, |
|
156 |
x label style={at={(1.05,-0.05)}}, |
|
157 |
%ylabel={time in secs}, |
|
158 |
enlargelimits=false, |
|
159 |
xtick={0,5,...,30}, |
|
160 |
xmax=33, |
|
161 |
ymax=35, |
|
162 |
ytick={0,5,...,30}, |
|
163 |
scaled ticks=false, |
|
164 |
axis lines=left, |
|
165 |
width=5cm, |
|
166 |
height=4cm, |
|
167 |
legend entries={Java 8}, |
|
168 |
legend pos=north west, |
|
169 |
legend cell align=left] |
|
170 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data}; |
|
171 |
\end{axis} |
|
172 |
\end{tikzpicture}\\ |
|
173 |
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings |
|
174 |
of the form $\underbrace{aa..a}_{n}$.} |
|
175 |
\end{tabular} |
|
176 |
\end{center} |
|
177 |
||
58 | 178 |
\noindent These are clearly abysmal and possibly surprising results. One |
64 | 179 |
would expect these systems to do much better than that---after all, |
58 | 180 |
given a DFA and a string, deciding whether a string is matched by this |
77 | 181 |
DFA should be linear? |
30 | 182 |
|
183 |
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to |
|
77 | 184 |
exhibit this exponential behaviour. But unfortunately, such regular |
64 | 185 |
expressions are not just a few outliers. They are actually |
186 |
frequent enough to have a separate name created for |
|
30 | 187 |
them---\emph{evil regular expressions}. In empiric work, Davis et al |
188 |
report that they have found thousands of such evil regular expressions |
|
189 |
in the JavaScript and Python ecosystems \cite{Davis18}. |
|
190 |
||
58 | 191 |
This exponential blowup in matching algorithms sometimes causes |
63 | 192 |
considerable grief in real life: for example on 20 July 2016 one evil |
58 | 193 |
regular expression brought the webpage |
194 |
\href{http://stackexchange.com}{Stack Exchange} to its |
|
63 | 195 |
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}} |
30 | 196 |
In this instance, a regular expression intended to just trim white |
197 |
spaces from the beginning and the end of a line actually consumed |
|
64 | 198 |
massive amounts of CPU-resources---causing web servers to |
199 |
grind to a halt. This happened when a post with 20,000 white spaces was |
|
58 | 200 |
submitted, but importantly the white spaces were neither at the |
75 | 201 |
beginning nor at the end. |
202 |
As a result, the regular expression matching |
|
203 |
engine needed to backtrack over many choices. |
|
204 |
In this example, the time needed to process the string is not |
|
205 |
exactly the classical exponential case, but rather $O(n^2)$ |
|
206 |
with respect to the string length. But this is enough for the |
|
77 | 207 |
home page of Stack Exchange to respond not fast enough to |
75 | 208 |
the load balancer, which thought that there must be some |
209 |
attack and therefore stopped the servers from responding to |
|
77 | 210 |
requests. This made the whole site become unavailable. |
211 |
Another very recent example is a global outage of all Cloudflare servers |
|
212 |
on 2 July 2019. A poorly written regular expression exhibited |
|
213 |
exponential behaviour and exhausted CPUs that serve HTTP traffic. |
|
214 |
Although the outage had several causes, at the heart was a regular |
|
215 |
expression that was used to monitor network |
|
216 |
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}} |
|
217 |
||
218 |
The underlying problem is that many ``real life'' regular expression |
|
219 |
matching engines do not use DFAs for matching. This is because they |
|
220 |
support regular expressions that are not covered by the classical |
|
221 |
automata theory, and in this more general setting there are quite a few |
|
222 |
research questions still unanswered and fast algorithms still need to be |
|
223 |
developed (for example how to treat bounded repetitions, negation and |
|
224 |
back-references efficiently). |
|
64 | 225 |
%question: dfa can have exponential states. isn't this the actual reason why they do not use dfas? |
226 |
%how do they avoid dfas exponential states if they use them for fast matching? |
|
77 | 227 |
|
30 | 228 |
There is also another under-researched problem to do with regular |
229 |
expressions and lexing, i.e.~the process of breaking up strings into |
|
230 |
sequences of tokens according to some regular expressions. In this |
|
231 |
setting one is not just interested in whether or not a regular |
|
64 | 232 |
expression matches a string, but also in \emph{how}. Consider for example a regular expression |
30 | 233 |
$r_{key}$ for recognising keywords such as \textit{if}, \textit{then} |
234 |
and so on; and a regular expression $r_{id}$ for recognising |
|
235 |
identifiers (say, a single character followed by characters or |
|
236 |
numbers). One can then form the compound regular expression |
|
237 |
$(r_{key} + r_{id})^*$ and use it to tokenise strings. But then how |
|
238 |
should the string \textit{iffoo} be tokenised? It could be tokenised |
|
239 |
as a keyword followed by an identifier, or the entire string as a |
|
240 |
single identifier. Similarly, how should the string \textit{if} be |
|
241 |
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would |
|
242 |
``fire''---so is it an identifier or a keyword? While in applications |
|
243 |
there is a well-known strategy to decide these questions, called POSIX |
|
244 |
matching, only relatively recently precise definitions of what POSIX |
|
64 | 245 |
matching actually means has been formalised |
46 | 246 |
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. |
247 |
Such a definition has also been given by Sulzmann and Lu \cite{Sulzmann2014}, but the |
|
248 |
corresponding correctness proof turned out to be faulty \cite{AusafDyckhoffUrban2016}. |
|
249 |
Roughly, POSIX matching means matching the longest initial substring. |
|
64 | 250 |
In the case of a tie, the initial sub-match is chosen according to some priorities attached to the |
30 | 251 |
regular expressions (e.g.~keywords have a higher priority than |
252 |
identifiers). This sounds rather simple, but according to Grathwohl et |
|
253 |
al \cite[Page 36]{CrashCourse2014} this is not the case. They wrote: |
|
254 |
||
255 |
\begin{quote} |
|
256 |
\it{}``The POSIX strategy is more complicated than the greedy because of |
|
257 |
the dependence on information about the length of matched strings in the |
|
258 |
various subexpressions.'' |
|
259 |
\end{quote} |
|
260 |
||
261 |
\noindent |
|
262 |
This is also supported by evidence collected by Kuklewicz |
|
263 |
\cite{Kuklewicz} who noticed that a number of POSIX regular expression |
|
264 |
matchers calculate incorrect results. |
|
265 |
||
77 | 266 |
Our focus in this project is on an algorithm introduced by Sulzmann and |
267 |
Lu in 2014 for regular expression matching according to the POSIX |
|
268 |
strategy \cite{Sulzmann2014}. Their algorithm is based on an older |
|
269 |
algorithm by Brzozowski from 1964 where he introduced the notion of |
|
270 |
derivatives of regular expressions~\cite{Brzozowski1964}. We shall |
|
271 |
briefly explain this algorithm next. |
|
30 | 272 |
|
46 | 273 |
\section{The Algorithm by Brzozowski based on Derivatives of Regular |
274 |
Expressions} |
|
30 | 275 |
|
40 | 276 |
Suppose (basic) regular expressions are given by the following grammar: |
38 | 277 |
\[ r ::= \ZERO \mid \ONE |
278 |
\mid c |
|
279 |
\mid r_1 \cdot r_2 |
|
280 |
\mid r_1 + r_2 |
|
281 |
\mid r^* |
|
282 |
\] |
|
30 | 283 |
|
284 |
\noindent |
|
46 | 285 |
The intended meaning of the constructors is as follows: $\ZERO$ |
30 | 286 |
cannot match any string, $\ONE$ can match the empty string, the |
287 |
character regular expression $c$ can match the character $c$, and so |
|
40 | 288 |
on. |
289 |
||
58 | 290 |
The ingenious contribution by Brzozowski is the notion of |
30 | 291 |
\emph{derivatives} of regular expressions. The idea behind this |
292 |
notion is as follows: suppose a regular expression $r$ can match a |
|
293 |
string of the form $c\!::\! s$ (that is a list of characters starting |
|
294 |
with $c$), what does the regular expression look like that can match |
|
40 | 295 |
just $s$? Brzozowski gave a neat answer to this question. He started |
296 |
with the definition of $nullable$: |
|
36 | 297 |
\begin{center} |
298 |
\begin{tabular}{lcl} |
|
299 |
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\ |
|
300 |
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\ |
|
301 |
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\ |
|
302 |
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\ |
|
303 |
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\ |
|
304 |
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\ |
|
305 |
\end{tabular} |
|
306 |
\end{center} |
|
38 | 307 |
This function simply tests whether the empty string is in $L(r)$. |
36 | 308 |
He then defined |
30 | 309 |
the following operation on regular expressions, written |
310 |
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$): |
|
311 |
||
312 |
\begin{center} |
|
313 |
\begin{tabular}{lcl} |
|
314 |
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\ |
|
315 |
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\ |
|
316 |
$d \backslash c$ & $\dn$ & |
|
317 |
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\ |
|
318 |
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\ |
|
36 | 319 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\ |
30 | 320 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\ |
321 |
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\ |
|
322 |
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\ |
|
323 |
\end{tabular} |
|
324 |
\end{center} |
|
325 |
||
46 | 326 |
%Assuming the classic notion of a |
327 |
%\emph{language} of a regular expression, written $L(\_)$, t |
|
30 | 328 |
|
40 | 329 |
\noindent |
330 |
The main property of the derivative operation is that |
|
30 | 331 |
|
332 |
\begin{center} |
|
333 |
$c\!::\!s \in L(r)$ holds |
|
334 |
if and only if $s \in L(r\backslash c)$. |
|
335 |
\end{center} |
|
336 |
||
337 |
\noindent |
|
46 | 338 |
For us the main advantage is that derivatives can be |
38 | 339 |
straightforwardly implemented in any functional programming language, |
340 |
and are easily definable and reasoned about in theorem provers---the |
|
341 |
definitions just consist of inductive datatypes and simple recursive |
|
342 |
functions. Moreover, the notion of derivatives can be easily |
|
343 |
generalised to cover extended regular expression constructors such as |
|
344 |
the not-regular expression, written $\neg\,r$, or bounded repetitions |
|
345 |
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so |
|
346 |
straightforwardly realised within the classic automata approach. |
|
347 |
For the moment however, we focus only on the usual basic regular expressions. |
|
348 |
||
349 |
||
40 | 350 |
Now if we want to find out whether a string $s$ matches with a regular |
351 |
expression $r$, build the derivatives of $r$ w.r.t.\ (in succession) |
|
352 |
all the characters of the string $s$. Finally, test whether the |
|
353 |
resulting regular expression can match the empty string. If yes, then |
|
354 |
$r$ matches $s$, and no in the negative case. To implement this idea |
|
355 |
we can generalise the derivative operation to strings like this: |
|
46 | 356 |
|
30 | 357 |
\begin{center} |
358 |
\begin{tabular}{lcl} |
|
359 |
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\ |
|
40 | 360 |
$r \backslash [\,] $ & $\dn$ & $r$ |
30 | 361 |
\end{tabular} |
362 |
\end{center} |
|
40 | 363 |
|
37 | 364 |
\noindent |
46 | 365 |
and then define as regular-expression matching algorithm: |
30 | 366 |
\[ |
367 |
match\;s\;r \;\dn\; nullable(r\backslash s) |
|
368 |
\] |
|
40 | 369 |
|
370 |
\noindent |
|
64 | 371 |
This algorithm looks graphically as follows: |
46 | 372 |
\begin{equation}\label{graph:*} |
373 |
\begin{tikzcd} |
|
374 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed] & r_n \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO} |
|
38 | 375 |
\end{tikzcd} |
46 | 376 |
\end{equation} |
40 | 377 |
|
378 |
\noindent |
|
46 | 379 |
where we start with a regular expression $r_0$, build successive |
380 |
derivatives until we exhaust the string and then use \textit{nullable} |
|
381 |
to test whether the result can match the empty string. It can be |
|
382 |
relatively easily shown that this matcher is correct (that is given |
|
64 | 383 |
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$). |
46 | 384 |
|
385 |
||
386 |
\section{Values and the Algorithm by Sulzmann and Lu} |
|
38 | 387 |
|
77 | 388 |
One limitation of Brzozowski's algorithm is that it only produces a |
389 |
YES/NO answer for whether a string is being matched by a regular |
|
390 |
expression. Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm |
|
391 |
to allow generation of an actual matching, called a \emph{value} or |
|
392 |
sometimes also \emph{lexical values}. These values and regular |
|
393 |
expressions correspond to each other as illustrated in the following |
|
394 |
table: |
|
46 | 395 |
|
30 | 396 |
|
397 |
\begin{center} |
|
398 |
\begin{tabular}{c@{\hspace{20mm}}c} |
|
399 |
\begin{tabular}{@{}rrl@{}} |
|
400 |
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\ |
|
401 |
$r$ & $::=$ & $\ZERO$\\ |
|
402 |
& $\mid$ & $\ONE$ \\ |
|
403 |
& $\mid$ & $c$ \\ |
|
404 |
& $\mid$ & $r_1 \cdot r_2$\\ |
|
405 |
& $\mid$ & $r_1 + r_2$ \\ |
|
406 |
\\ |
|
407 |
& $\mid$ & $r^*$ \\ |
|
408 |
\end{tabular} |
|
409 |
& |
|
410 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}} |
|
411 |
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\ |
|
412 |
$v$ & $::=$ & \\ |
|
413 |
& & $\Empty$ \\ |
|
414 |
& $\mid$ & $\Char(c)$ \\ |
|
415 |
& $\mid$ & $\Seq\,v_1\, v_2$\\ |
|
416 |
& $\mid$ & $\Left(v)$ \\ |
|
417 |
& $\mid$ & $\Right(v)$ \\ |
|
418 |
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\ |
|
419 |
\end{tabular} |
|
420 |
\end{tabular} |
|
421 |
\end{center} |
|
422 |
||
423 |
\noindent |
|
64 | 424 |
No value corresponds to $\ZERO$; $\Empty$ corresponds to |
63 | 425 |
$\ONE$; $\Char$ to the character regular expression; $\Seq$ to the |
426 |
sequence regular expression and so on. The idea of values is to encode |
|
72 | 427 |
a kind of lexical value for how the sub-parts of a regular expression match |
64 | 428 |
the sub-parts of a string. To see this, suppose a \emph{flatten} operation, written |
72 | 429 |
$|v|$ for values. We can use this function to extract the underlying string of a value |
63 | 430 |
$v$. For example, $|\mathit{Seq} \, (\textit{Char x}) \, (\textit{Char |
431 |
y})|$ is the string $xy$. Using flatten, we can describe how values |
|
64 | 432 |
encode parse trees: $\Seq\,v_1\, v_2$ encodes a tree with 2 children nodes |
433 |
that tells how the string $|v_1| @ |
|
63 | 434 |
|v_2|$ matches the regex $r_1 \cdot r_2$ whereby $r_1$ matches the |
435 |
substring $|v_1|$ and, respectively, $r_2$ matches the substring |
|
436 |
$|v_2|$. Exactly how these two are matched is contained in the |
|
64 | 437 |
children nodes $v_1$ and $v_2$ of parent $\textit{Seq}$ . |
30 | 438 |
|
77 | 439 |
To give a concrete example of how values work, consider the string $xy$ |
46 | 440 |
and the regular expression $(x + (y + xy))^*$. We can view this regular |
30 | 441 |
expression as a tree and if the string $xy$ is matched by two Star |
46 | 442 |
``iterations'', then the $x$ is matched by the left-most alternative in |
443 |
this tree and the $y$ by the right-left alternative. This suggests to |
|
444 |
record this matching as |
|
30 | 445 |
|
446 |
\begin{center} |
|
447 |
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$ |
|
448 |
\end{center} |
|
449 |
||
450 |
\noindent |
|
72 | 451 |
where $\Stars \; [\ldots]$ records all the |
64 | 452 |
iterations; and $\Left$, respectively $\Right$, which |
30 | 453 |
alternative is used. The value for |
454 |
matching $xy$ in a single ``iteration'', i.e.~the POSIX value, |
|
455 |
would look as follows |
|
456 |
||
457 |
\begin{center} |
|
458 |
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$ |
|
459 |
\end{center} |
|
460 |
||
461 |
\noindent |
|
462 |
where $\Stars$ has only a single-element list for the single iteration |
|
463 |
and $\Seq$ indicates that $xy$ is matched by a sequence regular |
|
464 |
expression. |
|
465 |
||
466 |
The contribution of Sulzmann and Lu is an extension of Brzozowski's |
|
467 |
algorithm by a second phase (the first phase being building successive |
|
46 | 468 |
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value |
72 | 469 |
is generated in case the regular expression matches the string. |
470 |
Pictorially, the Sulzmann and Lu algorithm is as follows: |
|
46 | 471 |
|
70 | 472 |
\begin{ceqn} |
59 | 473 |
\begin{equation}\label{graph:2} |
30 | 474 |
\begin{tikzcd} |
36 | 475 |
r_0 \arrow[r, "\backslash c_0"] \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\ |
30 | 476 |
v_0 & v_1 \arrow[l,"inj_{r_0} c_0"] & v_2 \arrow[l, "inj_{r_1} c_1"] & v_n \arrow[l, dashed] |
477 |
\end{tikzcd} |
|
59 | 478 |
\end{equation} |
70 | 479 |
\end{ceqn} |
37 | 480 |
|
46 | 481 |
\noindent |
77 | 482 |
For convenience, we shall employ the following notations: the regular |
483 |
expression we start with is $r_0$, and the given string $s$ is composed |
|
484 |
of characters $c_0 c_1 \ldots c_{n-1}$. In the first phase from the |
|
485 |
left to right, we build the derivatives $r_1$, $r_2$, \ldots according |
|
486 |
to the characters $c_0$, $c_1$ until we exhaust the string and obtain |
|
487 |
the derivative $r_n$. We test whether this derivative is |
|
46 | 488 |
$\textit{nullable}$ or not. If not, we know the string does not match |
489 |
$r$ and no value needs to be generated. If yes, we start building the |
|
77 | 490 |
values incrementally by \emph{injecting} back the characters into the |
491 |
earlier values $v_n, \ldots, v_0$. This is the second phase of the |
|
492 |
algorithm from the right to left. For the first value $v_n$, we call the |
|
493 |
function $\textit{mkeps}$, which builds the parse tree for how the empty |
|
494 |
string has been matched by the (nullable) regular expression $r_n$. This |
|
495 |
function is defined as |
|
30 | 496 |
|
51
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
497 |
\begin{center} |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
498 |
\begin{tabular}{lcl} |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
499 |
$\mkeps(\ONE)$ & $\dn$ & $\Empty$ \\ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
500 |
$\mkeps(r_{1}+r_{2})$ & $\dn$ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
501 |
& \textit{if} $\nullable(r_{1})$\\ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
502 |
& & \textit{then} $\Left(\mkeps(r_{1}))$\\ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
503 |
& & \textit{else} $\Right(\mkeps(r_{2}))$\\ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
504 |
$\mkeps(r_1\cdot r_2)$ & $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
505 |
$mkeps(r^*)$ & $\dn$ & $\Stars\,[]$ |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
506 |
\end{tabular} |
5df7faf69238
added mkeps and pder, still have not proof read it
Chengsong
parents:
50
diff
changeset
|
507 |
\end{center} |
41 | 508 |
|
59 | 509 |
|
510 |
\noindent There are no cases for $\ZERO$ and $c$, since |
|
511 |
these regular expression cannot match the empty string. Note |
|
512 |
also that in case of alternatives we give preference to the |
|
513 |
regular expression on the left-hand side. This will become |
|
514 |
important later on. |
|
515 |
||
516 |
After this, we inject back the characters one by one in order to build |
|
63 | 517 |
the parse tree $v_i$ for how the regex $r_i$ matches the string $s_i$ |
77 | 518 |
($s_i = c_i \ldots c_{n-1}$ ) from the previous parse tree $v_{i+1}$. |
519 |
After injecting back $n$ characters, we get the parse tree for how $r_0$ |
|
63 | 520 |
matches $s$. For this Sulzmann and Lu defined a function that reverses |
521 |
the ``chopping off'' of characters during the derivative phase. The |
|
77 | 522 |
corresponding function is called \emph{injection}, written |
523 |
$\textit{inj}$; it takes three arguments: the first one is a regular |
|
524 |
expression ${r_{i-1}}$, before the character is chopped off, the second |
|
525 |
is a character ${c_{i-1}}$, the character we want to inject and the |
|
526 |
third argument is the value ${v_i}$, into which one wants to inject the |
|
527 |
character (it corresponds to the regular expression after the character |
|
528 |
has been chopped off). The result of this function is a new value. The |
|
529 |
definition of $\textit{inj}$ is as follows: |
|
59 | 530 |
|
531 |
\begin{center} |
|
532 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
|
533 |
$\textit{inj}\,(c)\,c\,Empty$ & $\dn$ & $Char\,c$\\ |
|
534 |
$\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\ |
|
535 |
$\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\ |
|
536 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\ |
|
537 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\ |
|
538 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$ & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\ |
|
539 |
$\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$ & $\dn$ & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\ |
|
540 |
\end{tabular} |
|
541 |
\end{center} |
|
542 |
||
63 | 543 |
\noindent This definition is by recursion on the ``shape'' of regular |
544 |
expressions and values. To understands this definition better consider |
|
545 |
the situation when we build the derivative on regular expression $r_{i-1}$. |
|
546 |
For this we chop off a character from $r_{i-1}$ to form $r_i$. This leaves a |
|
72 | 547 |
``hole'' in $r_i$ and its corresponding value $v_i$. |
548 |
To calculate $v_{i-1}$, we need to |
|
64 | 549 |
locate where that hole is and fill it. |
550 |
We can find this location by |
|
63 | 551 |
comparing $r_{i-1}$ and $v_i$. For instance, if $r_{i-1}$ is of shape |
64 | 552 |
$r_a \cdot r_b$, and $v_i$ is of shape $\Left(Seq(v_1,v_2))$, we know immediately that |
63 | 553 |
% |
554 |
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b \,+\, r_b\backslash c,\] |
|
555 |
||
556 |
\noindent |
|
59 | 557 |
otherwise if $r_a$ is not nullable, |
63 | 558 |
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b,\] |
559 |
||
560 |
\noindent |
|
64 | 561 |
the value $v_i$ should be $\Seq(\ldots)$, contradicting the fact that |
562 |
$v_i$ is actually of shape $\Left(\ldots)$. Furthermore, since $v_i$ is of shape |
|
63 | 563 |
$\Left(\ldots)$ instead of $\Right(\ldots)$, we know that the left |
64 | 564 |
branch of \[ (r_a \cdot r_b)\backslash c = |
565 |
\bold{\underline{ (r_a\backslash c) \cdot r_b} }\,+\, r_b\backslash c,\](underlined) |
|
566 |
is taken instead of the right one. This means $c$ is chopped off |
|
567 |
from $r_a$ rather than $r_b$. |
|
568 |
We have therefore found out |
|
63 | 569 |
that the hole will be on $r_a$. So we recursively call $\inj\, |
64 | 570 |
r_a\,c\,v_a$ to fill that hole in $v_a$. After injection, the value |
63 | 571 |
$v_i$ for $r_i = r_a \cdot r_b$ should be $\Seq\,(\inj\,r_a\,c\,v_a)\,v_b$. |
60
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
572 |
Other clauses can be understood in a similar way. |
59 | 573 |
|
71 | 574 |
%\comment{Other word: insight?} |
575 |
The following example gives an insight of $\textit{inj}$'s effect |
|
65 | 576 |
and how Sulzmann and Lu's algorithm works as a whole. |
577 |
Suppose we have a |
|
578 |
regular expression $((((a+b)+ab)+c)+abc)^*$, and want to match it against |
|
63 | 579 |
the string $abc$ (when $abc$ is written as a regular expression, the most |
580 |
standard way of expressing it should be $a \cdot (b \cdot c)$. We omit |
|
65 | 581 |
the parentheses and dots here for readability). |
582 |
This algorithm returns a POSIX value, which means it |
|
583 |
will go for the longest matching, i.e.~it should match the string |
|
584 |
$abc$ in one star iteration, using the longest alternative $abc$ in the |
|
585 |
sub-expression $((((a+b)+ab)+c)+abc)$ (we use $r$ to denote this sub-expression |
|
64 | 586 |
for conciseness). |
65 | 587 |
Before $\textit{inj}$ comes into play, |
588 |
our lexer first builds derivative using string $abc$ (we simplified some regular expressions like |
|
80 | 589 |
$\ZERO \cdot b$ to $\ZERO$ for conciseness; we also omit parentheses if |
63 | 590 |
they are clear from the context): |
60
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
591 |
%Similarly, we allow |
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
592 |
%$\textit{ALT}$ to take a list of regular expressions as an argument |
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
593 |
%instead of just 2 operands to reduce the nested depth of |
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
594 |
%$\textit{ALT}$ |
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
595 |
\begin{center} |
63 | 596 |
\begin{tabular}{lcl} |
72 | 597 |
$r^*$ & $\xrightarrow{\backslash a}$ & $r_1 = (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r^*$\\ |
598 |
& $\xrightarrow{\backslash b}$ & $r_2 = (\ZERO+\ZERO+\ONE \cdot \ONE + \ZERO + \ONE \cdot \ONE \cdot c) \cdot r^* +(\ZERO+\ONE+\ZERO + \ZERO + \ZERO) \cdot r^*$\\ |
|
599 |
& $\xrightarrow{\backslash c}$ & $r_3 = ((\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot \ONE) \cdot r^* + (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^*) + $\\ |
|
600 |
& & $\phantom{r_3 = (} ((\ZERO+\ONE+\ZERO + \ZERO + \ZERO) \cdot r^* + (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^* )$ |
|
63 | 601 |
\end{tabular} |
60
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
602 |
\end{center} |
63 | 603 |
|
604 |
\noindent |
|
72 | 605 |
In case $r_3$ is nullable, we can call $\textit{mkeps}$ |
60
c737a0259194
sorry not all done, need a few more mins for last few changes
Chengsong
parents:
59
diff
changeset
|
606 |
to construct a parse tree for how $r_3$ matched the string $abc$. |
72 | 607 |
$\textit{mkeps}$ gives the following value $v_3$: |
65 | 608 |
\begin{center} |
609 |
$\Left(\Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty,\Empty))), \Stars [])))$ |
|
610 |
\end{center} |
|
611 |
The outer $\Left(\Left(\ldots))$ tells us the leftmost nullable part of $r_3$(underlined): |
|
70 | 612 |
|
65 | 613 |
\begin{center} |
72 | 614 |
$( \underline{(\ZERO+\ZERO+\ZERO+ \ZERO+ \ONE \cdot \ONE \cdot \ONE) \cdot r^*} + (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) |
615 |
\cdot r^*) +((\ZERO+\ONE+\ZERO + \ZERO + \ZERO) \cdot r^*+(\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^* ).$ |
|
65 | 616 |
\end{center} |
70 | 617 |
|
618 |
\noindent |
|
72 | 619 |
Note that the leftmost location of term $((\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot |
620 |
\ONE) \cdot r^*$ (which corresponds to the initial sub-match $abc$) allows |
|
621 |
$\textit{mkeps}$ to pick it up because $\textit{mkeps}$ is defined to always choose the |
|
70 | 622 |
left one when it is nullable. In the case of this example, $abc$ is |
623 |
preferred over $a$ or $ab$. This $\Left(\Left(\ldots))$ location is |
|
624 |
naturally generated by two applications of the splitting clause |
|
625 |
||
626 |
\begin{center} |
|
65 | 627 |
$(r_1 \cdot r_2)\backslash c (when \; r_1 \; nullable) \, = (r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c.$ |
70 | 628 |
\end{center} |
629 |
||
630 |
\noindent |
|
631 |
By this clause, we put $r_1 \backslash c \cdot r_2 $ at the |
|
632 |
$\textit{front}$ and $r_2 \backslash c$ at the $\textit{back}$. This |
|
72 | 633 |
allows $\textit{mkeps}$ to always pick up among two matches the one with a longer |
70 | 634 |
initial sub-match. Removing the outside $\Left(\Left(...))$, the inside |
635 |
sub-value |
|
636 |
||
637 |
\begin{center} |
|
65 | 638 |
$\Seq(\Right(\Seq(\Empty, \Seq(\Empty, \Empty))), \Stars [])$ |
70 | 639 |
\end{center} |
640 |
||
641 |
\noindent |
|
72 | 642 |
tells us how the empty string $[]$ is matched with $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot |
643 |
\ONE \cdot \ONE) \cdot r^*$. We match $[]$ by a sequence of 2 nullable regular |
|
70 | 644 |
expressions. The first one is an alternative, we take the rightmost |
645 |
alternative---whose language contains the empty string. The second |
|
646 |
nullable regular expression is a Kleene star. $\Stars$ tells us how it |
|
647 |
generates the nullable regular expression: by 0 iterations to form |
|
72 | 648 |
$\ONE$. Now $\textit{inj}$ injects characters back and incrementally |
70 | 649 |
builds a parse tree based on $v_3$. Using the value $v_3$, the character |
650 |
c, and the regular expression $r_2$, we can recover how $r_2$ matched |
|
651 |
the string $[c]$ : $\textit{inj} \; r_2 \; c \; v_3$ gives us |
|
65 | 652 |
\begin{center} |
653 |
$v_2 = \Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty, c))), \Stars [])),$ |
|
654 |
\end{center} |
|
655 |
which tells us how $r_2$ matched $[c]$. After this we inject back the character $b$, and get |
|
656 |
\begin{center} |
|
657 |
$v_1 = \Seq(\Right(\Seq(\Empty, \Seq(b, c))), \Stars [])$ |
|
658 |
\end{center} |
|
61 | 659 |
for how |
65 | 660 |
\begin{center} |
72 | 661 |
$r_1= (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r*$ |
65 | 662 |
\end{center} |
61 | 663 |
matched the string $bc$ before it split into 2 pieces. |
664 |
Finally, after injecting character $a$ back to $v_1$, |
|
65 | 665 |
we get the parse tree |
666 |
\begin{center} |
|
667 |
$v_0= \Stars [\Right(\Seq(a, \Seq(b, c)))]$ |
|
668 |
\end{center} |
|
669 |
for how $r$ matched $abc$. This completes the algorithm. |
|
670 |
||
61 | 671 |
%We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}. |
672 |
Readers might have noticed that the parse tree information |
|
673 |
is actually already available when doing derivatives. |
|
674 |
For example, immediately after the operation $\backslash a$ we know that if we want to match a string that starts with $a$, |
|
675 |
we can either take the initial match to be |
|
65 | 676 |
\begin{center} |
42 | 677 |
\begin{enumerate} |
678 |
\item[1)] just $a$ or |
|
679 |
\item[2)] string $ab$ or |
|
680 |
\item[3)] string $abc$. |
|
681 |
\end{enumerate} |
|
65 | 682 |
\end{center} |
70 | 683 |
|
684 |
\noindent |
|
685 |
In order to differentiate between these choices, we just need to |
|
686 |
remember their positions--$a$ is on the left, $ab$ is in the middle , |
|
687 |
and $abc$ is on the right. Which one of these alternatives is chosen |
|
688 |
later does not affect their relative position because our algorithm does |
|
689 |
not change this order. If this parsing information can be determined and |
|
690 |
does not change because of later derivatives, there is no point in |
|
691 |
traversing this information twice. This leads to an optimisation---if we |
|
692 |
store the information for parse trees inside the regular expression, |
|
693 |
update it when we do derivative on them, and collect the information |
|
72 | 694 |
when finished with derivatives and call $\textit{mkeps}$ for deciding which |
70 | 695 |
branch is POSIX, we can generate the parse tree in one pass, instead of |
696 |
doing the rest $n$ injections. This leads to Sulzmann and Lu's novel |
|
71 | 697 |
idea of using bitcodes in derivatives. |
42 | 698 |
|
72 | 699 |
In the next section, we shall focus on the bitcoded algorithm and the |
63 | 700 |
process of simplification of regular expressions. This is needed in |
30 | 701 |
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann |
63 | 702 |
and Lu's algorithms. This is where the PhD-project aims to advance the |
703 |
state-of-the-art. |
|
30 | 704 |
|
705 |
||
706 |
\section{Simplification of Regular Expressions} |
|
63 | 707 |
|
70 | 708 |
Using bitcodes to guide parsing is not a novel idea. It was applied to |
63 | 709 |
context free grammars and then adapted by Henglein and Nielson for |
70 | 710 |
efficient regular expression parsing using DFAs~\cite{nielson11bcre}. |
711 |
Sulzmann and Lu took this idea of bitcodes a step further by integrating |
|
712 |
bitcodes into derivatives. The reason why we want to use bitcodes in |
|
713 |
this project is that we want to introduce more aggressive |
|
77 | 714 |
simplification rules in order to keep the size of derivatives small |
70 | 715 |
throughout. This is because the main drawback of building successive |
716 |
derivatives according to Brzozowski's definition is that they can grow |
|
717 |
very quickly in size. This is mainly due to the fact that the derivative |
|
718 |
operation generates often ``useless'' $\ZERO$s and $\ONE$s in |
|
63 | 719 |
derivatives. As a result, if implemented naively both algorithms by |
70 | 720 |
Brzozowski and by Sulzmann and Lu are excruciatingly slow. For example |
721 |
when starting with the regular expression $(a + aa)^*$ and building 12 |
|
63 | 722 |
successive derivatives w.r.t.~the character $a$, one obtains a |
723 |
derivative regular expression with more than 8000 nodes (when viewed as |
|
77 | 724 |
a tree). Operations like $\textit{der}$ and $\nullable$ need to traverse |
63 | 725 |
such trees and consequently the bigger the size of the derivative the |
66 | 726 |
slower the algorithm. |
35 | 727 |
|
70 | 728 |
Fortunately, one can simplify regular expressions after each derivative |
729 |
step. Various simplifications of regular expressions are possible, such |
|
77 | 730 |
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r |
70 | 731 |
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not |
732 |
affect the answer for whether a regular expression matches a string or |
|
733 |
not, but fortunately also do not affect the POSIX strategy of how |
|
734 |
regular expressions match strings---although the latter is much harder |
|
71 | 735 |
to establish. Some initial results in this regard have been |
70 | 736 |
obtained in \cite{AusafDyckhoffUrban2016}. |
737 |
||
738 |
Unfortunately, the simplification rules outlined above are not |
|
77 | 739 |
sufficient to prevent a size explosion in all cases. We |
70 | 740 |
believe a tighter bound can be achieved that prevents an explosion in |
77 | 741 |
\emph{all} cases. Such a tighter bound is suggested by work of Antimirov who |
70 | 742 |
proved that (partial) derivatives can be bound by the number of |
743 |
characters contained in the initial regular expression |
|
744 |
\cite{Antimirov95}. He defined the \emph{partial derivatives} of regular |
|
745 |
expressions as follows: |
|
746 |
||
52
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
747 |
\begin{center} |
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
748 |
\begin{tabular}{lcl} |
80 | 749 |
$\textit{pder} \; c \; \ZERO$ & $\dn$ & $\emptyset$\\ |
750 |
$\textit{pder} \; c \; \ONE$ & $\dn$ & $\emptyset$ \\ |
|
751 |
$\textit{pder} \; c \; d$ & $\dn$ & $\textit{if} \; c \,=\, d \; \{ \ONE \} \; \textit{else} \; \emptyset$ \\ |
|
52
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
752 |
$\textit{pder} \; c \; r_1+r_2$ & $\dn$ & $pder \; c \; r_1 \cup pder \; c \; r_2$ \\ |
70 | 753 |
$\textit{pder} \; c \; r_1 \cdot r_2$ & $\dn$ & $\textit{if} \; nullable \; r_1 $\\ |
754 |
& & $\textit{then} \; \{ r \cdot r_2 \mid r \in pder \; c \; r_1 \} \cup pder \; c \; r_2 \;$\\ |
|
755 |
& & $\textit{else} \; \{ r \cdot r_2 \mid r \in pder \; c \; r_1 \} $ \\ |
|
52
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
756 |
$\textit{pder} \; c \; r^*$ & $\dn$ & $ \{ r' \cdot r^* \mid r' \in pder \; c \; r \} $ \\ |
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
757 |
\end{tabular} |
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
758 |
\end{center} |
70 | 759 |
|
760 |
\noindent |
|
761 |
A partial derivative of a regular expression $r$ is essentially a set of |
|
762 |
regular expressions that are either $r$'s children expressions or a |
|
763 |
concatenation of them. Antimirov has proved a tight bound of the size of |
|
77 | 764 |
\emph{all} partial derivatives no matter what the string looks like. |
765 |
Roughly speaking the size will be quadruple in the size of the regular |
|
766 |
expression. If we want the size of derivatives in Sulzmann and Lu's |
|
767 |
algorithm to stay equal or below this bound, we would need more |
|
768 |
aggressive simplifications. Essentially we need to delete useless |
|
769 |
$\ZERO$s and $\ONE$s, as well as deleting duplicates whenever possible. |
|
71 | 770 |
For example, the parentheses in $(a+b) \cdot c + bc$ can be opened up to |
77 | 771 |
get $a\cdot c + b \cdot c + b \cdot c$, and then simplified to just $a \cdot |
772 |
c + b \cdot c$. Another example is simplifying $(a^*+a) + (a^*+ \ONE) + (a |
|
773 |
+\ONE)$ to just $a^*+a+\ONE$. Adding these more aggressive simplification |
|
774 |
rules helps us to achieve the same size bound as that of the partial |
|
775 |
derivatives. In order to implement the idea of ``spilling out alternatives'' |
|
776 |
and to make them compatible with the $\text{inj}$-mechanism, we use \emph{bitcodes}. |
|
777 |
Bits and bitcodes (lists of bits) are just: |
|
71 | 778 |
%This allows us to prove a tight |
779 |
%bound on the size of regular expression during the running time of the |
|
780 |
%algorithm if we can establish the connection between our simplification |
|
781 |
%rules and partial derivatives. |
|
35 | 782 |
|
783 |
%We believe, and have generated test |
|
784 |
%data, that a similar bound can be obtained for the derivatives in |
|
785 |
%Sulzmann and Lu's algorithm. Let us give some details about this next. |
|
30 | 786 |
|
72 | 787 |
|
67 | 788 |
\begin{center} |
77 | 789 |
$b ::= S \mid Z \qquad |
43 | 790 |
bs ::= [] \mid b:bs |
67 | 791 |
$ |
792 |
\end{center} |
|
77 | 793 |
|
794 |
\noindent |
|
795 |
The names $S$ and $Z$ here are quite arbitrary in order to avoid |
|
796 |
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or |
|
797 |
bit-lists) can be used to encode values (or incomplete values) in a |
|
798 |
compact form. This can be straightforwardly seen in the following |
|
799 |
coding function from values to bitcodes: |
|
800 |
||
30 | 801 |
\begin{center} |
802 |
\begin{tabular}{lcl} |
|
803 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\ |
|
804 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\ |
|
805 |
$\textit{code}(\Left\,v)$ & $\dn$ & $\Z :: code(v)$\\ |
|
806 |
$\textit{code}(\Right\,v)$ & $\dn$ & $\S :: code(v)$\\ |
|
807 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\ |
|
68 | 808 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[\Z]$\\ |
809 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $\S :: code(v) \;@\; |
|
30 | 810 |
code(\Stars\,vs)$ |
811 |
\end{tabular} |
|
812 |
\end{center} |
|
70 | 813 |
|
77 | 814 |
\noindent |
815 |
Here $\textit{code}$ encodes a value into a bitcodes by converting |
|
816 |
$\Left$ into $\Z$, $\Right$ into $\S$, the start point of a non-empty |
|
817 |
star iteration into $\S$, and the border where a local star terminates |
|
818 |
into $\Z$. This coding is lossy, as it throws away the information about |
|
819 |
characters, and also does not encode the ``boundary'' between two |
|
820 |
sequence values. Moreover, with only the bitcode we cannot even tell |
|
821 |
whether the $\S$s and $\Z$s are for $\Left/\Right$ or $\Stars$. The |
|
822 |
reason for choosing this compact way of storing information is that the |
|
823 |
relatively small size of bits can be easily manipulated and ``moved |
|
824 |
around'' in a regular expression. In order to recover values, we will |
|
825 |
need the corresponding regular expression as an extra information. This |
|
826 |
means the decoding function is defined as: |
|
70 | 827 |
|
828 |
||
37 | 829 |
%\begin{definition}[Bitdecoding of Values]\mbox{} |
36 | 830 |
\begin{center} |
831 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}} |
|
832 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\ |
|
833 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\ |
|
834 |
$\textit{decode}'\,(\Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
835 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\; |
|
836 |
(\Left\,v, bs_1)$\\ |
|
837 |
$\textit{decode}'\,(\S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
838 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\; |
|
839 |
(\Right\,v, bs_1)$\\ |
|
840 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ & |
|
841 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\ |
|
842 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\ |
|
843 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\ |
|
844 |
$\textit{decode}'\,(\Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\ |
|
845 |
$\textit{decode}'\,(\S\!::\!bs)\,(r^*)$ & $\dn$ & |
|
846 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
|
847 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\ |
|
848 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\ |
|
849 |
||
850 |
$\textit{decode}\,bs\,r$ & $\dn$ & |
|
851 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
|
852 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\; |
|
853 |
\textit{else}\;\textit{None}$ |
|
854 |
\end{tabular} |
|
855 |
\end{center} |
|
37 | 856 |
%\end{definition} |
30 | 857 |
|
77 | 858 |
Sulzmann and Lu's integrated the bitcodes into regular expressions to |
859 |
create annotated regular expressions \cite{Sulzmann2014}. |
|
860 |
\emph{Annotated regular expressions} are defined by the following |
|
70 | 861 |
grammar: |
43 | 862 |
|
863 |
\begin{center} |
|
864 |
\begin{tabular}{lcl} |
|
865 |
$\textit{a}$ & $::=$ & $\textit{ZERO}$\\ |
|
866 |
& $\mid$ & $\textit{ONE}\;\;bs$\\ |
|
867 |
& $\mid$ & $\textit{CHAR}\;\;bs\,c$\\ |
|
72 | 868 |
& $\mid$ & $\textit{ALT}\;\;bs\,a_1 \, a_2$\\ |
43 | 869 |
& $\mid$ & $\textit{SEQ}\;\;bs\,a_1\,a_2$\\ |
870 |
& $\mid$ & $\textit{STAR}\;\;bs\,a$ |
|
871 |
\end{tabular} |
|
872 |
\end{center} |
|
72 | 873 |
%(in \textit{ALT}) |
77 | 874 |
|
43 | 875 |
\noindent |
77 | 876 |
where $bs$ stands for bitcodes, and $a$ for $\bold{a}$nnotated regular |
877 |
expressions. We will show that these bitcodes encode information about |
|
878 |
the (POSIX) value that should be generated by the Sulzmann and Lu |
|
879 |
algorithm. |
|
880 |
||
43 | 881 |
|
70 | 882 |
To do lexing using annotated regular expressions, we shall first |
883 |
transform the usual (un-annotated) regular expressions into annotated |
|
884 |
regular expressions. This operation is called \emph{internalisation} and |
|
885 |
defined as follows: |
|
886 |
||
37 | 887 |
%\begin{definition} |
36 | 888 |
\begin{center} |
889 |
\begin{tabular}{lcl} |
|
890 |
$(\ZERO)^\uparrow$ & $\dn$ & $\textit{ZERO}$\\ |
|
891 |
$(\ONE)^\uparrow$ & $\dn$ & $\textit{ONE}\,[]$\\ |
|
892 |
$(c)^\uparrow$ & $\dn$ & $\textit{CHAR}\,[]\,c$\\ |
|
893 |
$(r_1 + r_2)^\uparrow$ & $\dn$ & |
|
894 |
$\textit{ALT}\;[]\,(\textit{fuse}\,[\Z]\,r_1^\uparrow)\, |
|
895 |
(\textit{fuse}\,[\S]\,r_2^\uparrow)$\\ |
|
896 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ & |
|
897 |
$\textit{SEQ}\;[]\,r_1^\uparrow\,r_2^\uparrow$\\ |
|
898 |
$(r^*)^\uparrow$ & $\dn$ & |
|
899 |
$\textit{STAR}\;[]\,r^\uparrow$\\ |
|
900 |
\end{tabular} |
|
901 |
\end{center} |
|
37 | 902 |
%\end{definition} |
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
903 |
|
70 | 904 |
\noindent |
77 | 905 |
We use up arrows here to indicate that the basic un-annotated regular |
906 |
expressions are ``lifted up'' into something slightly more complex. In the |
|
907 |
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to |
|
908 |
attach bits to the front of an annotated regular expression. Its |
|
909 |
definition is as follows: |
|
70 | 910 |
|
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
911 |
\begin{center} |
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
912 |
\begin{tabular}{lcl} |
77 | 913 |
$\textit{fuse}\;bs\,(\textit{ZERO})$ & $\dn$ & $\textit{ZERO}$\\ |
914 |
$\textit{fuse}\;bs\,(\textit{ONE}\,bs')$ & $\dn$ & |
|
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
915 |
$\textit{ONE}\,(bs\,@\,bs')$\\ |
77 | 916 |
$\textit{fuse}\;bs\,(\textit{CHAR}\,bs'\,c)$ & $\dn$ & |
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
917 |
$\textit{CHAR}\,(bs\,@\,bs')\,c$\\ |
77 | 918 |
$\textit{fuse}\;bs\,(\textit{ALT}\,bs'\,a_1\,a_2)$ & $\dn$ & |
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
919 |
$\textit{ALT}\,(bs\,@\,bs')\,a_1\,a_2$\\ |
77 | 920 |
$\textit{fuse}\;bs\,(\textit{SEQ}\,bs'\,a_1\,a_2)$ & $\dn$ & |
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
921 |
$\textit{SEQ}\,(bs\,@\,bs')\,a_1\,a_2$\\ |
77 | 922 |
$\textit{fuse}\;bs\,(\textit{STAR}\,bs'\,a)$ & $\dn$ & |
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
923 |
$\textit{STAR}\,(bs\,@\,bs')\,a$ |
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
924 |
\end{tabular} |
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
925 |
\end{center} |
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
926 |
|
70 | 927 |
\noindent |
928 |
After internalise we do successive derivative operations on the |
|
77 | 929 |
annotated regular expressions. This derivative operation is the same as |
930 |
what we previously have for the simple regular expressions, except that |
|
931 |
we beed to take special care of the bitcodes:\comment{You need to be consitent with ALTS and ALT; ALT is just |
|
80 | 932 |
an abbreviation; derivations and so on are defined for ALTS}\comment{no this is not the case, |
933 |
ALT for 2 regexes, ALTS for a list} |
|
70 | 934 |
|
935 |
%\begin{definition}{bder} |
|
36 | 936 |
\begin{center} |
937 |
\begin{tabular}{@{}lcl@{}} |
|
77 | 938 |
$(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\ |
939 |
$(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\ |
|
940 |
$(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ & |
|
36 | 941 |
$\textit{if}\;c=d\; \;\textit{then}\; |
942 |
\textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\ |
|
77 | 943 |
$(\textit{ALT}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ & |
944 |
$\textit{ALT}\;bs\,(a_1\,\backslash c)\,(a_2\,\backslash c)$\\ |
|
945 |
$(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ & |
|
36 | 946 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
77 | 947 |
& &$\textit{then}\;\textit{ALT}\,bs\,(\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2)$\\ |
948 |
& &$\phantom{\textit{then}\;\textit{ALT}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))$\\ |
|
949 |
& &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\ |
|
950 |
$(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ & |
|
951 |
$\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\, |
|
36 | 952 |
(\textit{STAR}\,[]\,r)$ |
953 |
\end{tabular} |
|
954 |
\end{center} |
|
37 | 955 |
%\end{definition} |
74
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
956 |
|
77 | 957 |
\noindent |
958 |
For instance, when we unfold $\textit{STAR} \; bs \; a$ into a sequence, |
|
959 |
we need to attach an additional bit $Z$ to the front of $r \backslash c$ |
|
960 |
to indicate that there is one more star iteration. Also the $SEQ$ clause |
|
961 |
is more subtle---when $a_1$ is $\textit{bnullable}$ (here |
|
962 |
\textit{bnullable} is exactly the same as $\textit{nullable}$, except |
|
963 |
that it is for annotated regular expressions, therefore we omit the |
|
964 |
definition). Assume that $bmkeps$ correctly extracts the bitcode for how |
|
965 |
$a_1$ matches the string prior to character $c$ (more on this later), |
|
966 |
then the right branch of $ALTS$, which is $fuse \; bmkeps \; a_1 (a_2 |
|
967 |
\backslash c)$ will collapse the regular expression $a_1$(as it has |
|
968 |
already been fully matched) and store the parsing information at the |
|
969 |
head of the regular expression $a_2 \backslash c$ by fusing to it. The |
|
970 |
bitsequence $bs$, which was initially attached to the head of $SEQ$, has |
|
971 |
now been elevated to the top-level of ALT, as this information will be |
|
972 |
needed whichever way the $SEQ$ is matched--no matter whether $c$ belongs |
|
973 |
to $a_1$ or $ a_2$. After building these derivatives and maintaining all |
|
974 |
the lexing information, we complete the lexing by collecting the |
|
975 |
bitcodes using a generalised version of the $\textit{mkeps}$ function |
|
976 |
for annotated regular expressions, called $\textit{bmkeps}$: |
|
44
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
977 |
|
4d674a971852
another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents:
43
diff
changeset
|
978 |
|
37 | 979 |
%\begin{definition}[\textit{bmkeps}]\mbox{} |
36 | 980 |
\begin{center} |
981 |
\begin{tabular}{lcl} |
|
77 | 982 |
$\textit{bmkeps}\,(\textit{ONE}\;bs)$ & $\dn$ & $bs$\\ |
983 |
$\textit{bmkeps}\,(\textit{ALT}\;bs\,a_1\,a_2)$ & $\dn$ & |
|
36 | 984 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
985 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a_1$\\ |
|
986 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,a_2$\\ |
|
77 | 987 |
$\textit{bmkeps}\,(\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & |
36 | 988 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\ |
77 | 989 |
$\textit{bmkeps}\,(\textit{STAR}\;bs\,a)$ & $\dn$ & |
36 | 990 |
$bs \,@\, [\S]$ |
991 |
\end{tabular} |
|
992 |
\end{center} |
|
37 | 993 |
%\end{definition} |
70 | 994 |
|
995 |
\noindent |
|
77 | 996 |
This function completes the value information by travelling along the |
997 |
path of the regular expression that corresponds to a POSIX value and |
|
998 |
collecting all the bitcodes, and using $S$ to indicate the end of star |
|
999 |
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and |
|
1000 |
decode them, we get the value we expect. The corresponding lexing |
|
1001 |
algorithm looks as follows: |
|
1002 |
||
37 | 1003 |
\begin{center} |
1004 |
\begin{tabular}{lcl} |
|
1005 |
$\textit{blexer}\;r\,s$ & $\dn$ & |
|
1006 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\ |
|
1007 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
1008 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
1009 |
& & $\;\;\textit{else}\;\textit{None}$ |
|
1010 |
\end{tabular} |
|
1011 |
\end{center} |
|
77 | 1012 |
|
1013 |
\noindent |
|
1014 |
In this definition $_\backslash s$ is the generalisation of the derivative |
|
1015 |
operation from characters to strings (just like the derivatives for un-annotated |
|
1016 |
regular expressions). |
|
30 | 1017 |
|
77 | 1018 |
The main point of the bitcodes and annotated regular expressions is that |
1019 |
we can apply rather aggressive (in terms of size) simplification rules |
|
1020 |
in order to keep derivatives small. We have developed such |
|
1021 |
``aggressive'' simplification rules and generated test data that show |
|
1022 |
that the expected bound can be achieved. Obviously we could only |
|
1023 |
partially cover the search space as there are infinitely many regular |
|
1024 |
expressions and strings. |
|
30 | 1025 |
|
77 | 1026 |
One modification we introduced is to allow a list of annotated regular |
1027 |
expressions in the \textit{ALTS} constructor. This allows us to not just |
|
1028 |
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but |
|
1029 |
also unnecessary ``copies'' of regular expressions (very similar to |
|
1030 |
simplifying $r + r$ to just $r$, but in a more general setting). Another |
|
1031 |
modification is that we use simplification rules inspired by Antimirov's |
|
1032 |
work on partial derivatives. They maintain the idea that only the first |
|
1033 |
``copy'' of a regular expression in an alternative contributes to the |
|
1034 |
calculation of a POSIX value. All subsequent copies can be pruned away from |
|
1035 |
the regular expression. A recursive definition of our simplification function |
|
1036 |
that looks somewhat similar to our Scala code is given below:\comment{Use $\ZERO$, $\ONE$ and so on. |
|
1037 |
Is it $ALT$ or $ALTS$?}\\ |
|
49 | 1038 |
|
52
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
1039 |
\begin{center} |
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
1040 |
\begin{tabular}{@{}lcl@{}} |
77 | 1041 |
|
1042 |
$\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp} \; a_2) \; \textit{match} $ \\ |
|
80 | 1043 |
&&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow \ZERO$ \\ |
1044 |
&&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow \ZERO$ \\ |
|
1045 |
&&$\quad\textit{case} \; (\ONE, a_2') \Rightarrow \textit{fuse} \; bs \; a_2'$ \\ |
|
1046 |
&&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow \textit{fuse} \; bs \; a_1'$ \\ |
|
77 | 1047 |
&&$\quad\textit{case} \; (a_1', a_2') \Rightarrow \textit{SEQ} \; bs \; a_1' \; a_2'$ \\ |
52
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
1048 |
|
77 | 1049 |
$\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\ |
80 | 1050 |
&&$\quad\textit{case} \; [] \Rightarrow \ZERO$ \\ |
77 | 1051 |
&&$\quad\textit{case} \; a :: [] \Rightarrow \textit{fuse bs a}$ \\ |
80 | 1052 |
&&$\quad\textit{case} \; as' \Rightarrow \textit{ALTS}\;bs\;as'$\\ |
77 | 1053 |
|
1054 |
$\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$ |
|
52
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
1055 |
\end{tabular} |
25bbbb8b0e90
just in case of some accidents from erasing my work
Chengsong
parents:
51
diff
changeset
|
1056 |
\end{center} |
47 | 1057 |
|
77 | 1058 |
\noindent |
1059 |
The simplification does a pattern matching on the regular expression. |
|
1060 |
When it detected that the regular expression is an alternative or |
|
1061 |
sequence, it will try to simplify its children regular expressions |
|
1062 |
recursively and then see if one of the children turn into $\ZERO$ or |
|
1063 |
$\ONE$, which might trigger further simplification at the current level. |
|
1064 |
The most involved part is the $\textit{ALTS}$ clause, where we use two |
|
1065 |
auxiliary functions flatten and distinct to open up nested |
|
1066 |
$\textit{ALTS}$ and reduce as many duplicates as possible. Function |
|
1067 |
distinct keeps the first occurring copy only and remove all later ones |
|
80 | 1068 |
when detected duplicates. Function flatten opens up nested \textit{ALTS}. |
77 | 1069 |
Its recursive definition is given below: |
1070 |
||
53 | 1071 |
\begin{center} |
1072 |
\begin{tabular}{@{}lcl@{}} |
|
80 | 1073 |
$\textit{flatten} \; (\textit{ALTS}\;bs\,as) :: as'$ & $\dn$ & $(\textit{map} \; |
70 | 1074 |
(\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\ |
53 | 1075 |
$\textit{flatten} \; \textit{ZERO} :: as'$ & $\dn$ & $ \textit{flatten} \; as' $ \\ |
70 | 1076 |
$\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; as'$ \quad(otherwise) |
53 | 1077 |
\end{tabular} |
1078 |
\end{center} |
|
1079 |
||
70 | 1080 |
\noindent |
77 | 1081 |
Here flatten behaves like the traditional functional programming flatten |
1082 |
function, except that it also removes $\ZERO$s. What it does is |
|
1083 |
basically removing parentheses like changing $a+(b+c)$ into $a+b+c$. |
|
47 | 1084 |
|
77 | 1085 |
Suppose we apply simplification after each derivative step, and view |
1086 |
these two operations as an atomic one: $a \backslash_{simp}\,c \dn |
|
1087 |
\textit{simp}(a \backslash c)$. Then we can use the previous natural |
|
1088 |
extension from derivative w.r.t.~character to derivative |
|
1089 |
w.r.t.~string:\comment{simp in the [] case?} |
|
53 | 1090 |
|
1091 |
\begin{center} |
|
1092 |
\begin{tabular}{lcl} |
|
77 | 1093 |
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\ |
80 | 1094 |
$r \backslash_{simp} [\,] $ & $\dn$ & $r$ |
53 | 1095 |
\end{tabular} |
1096 |
\end{center} |
|
1097 |
||
77 | 1098 |
\noindent |
1099 |
we obtain an optimised version of the algorithm: |
|
1100 |
||
1101 |
\begin{center} |
|
53 | 1102 |
\begin{tabular}{lcl} |
1103 |
$\textit{blexer\_simp}\;r\,s$ & $\dn$ & |
|
77 | 1104 |
$\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\ |
53 | 1105 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
1106 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
1107 |
& & $\;\;\textit{else}\;\textit{None}$ |
|
1108 |
\end{tabular} |
|
1109 |
\end{center} |
|
48 | 1110 |
|
77 | 1111 |
\noindent |
1112 |
This algorithm keeps the regular expression size small, for example, |
|
1113 |
with this simplification our previous $(a + aa)^*$ example's 8000 nodes |
|
1114 |
will be reduced to just 6 and stays constant, no matter how long the |
|
1115 |
input string is. |
|
35 | 1116 |
|
30 | 1117 |
|
35 | 1118 |
|
70 | 1119 |
\section{Current Work} |
1120 |
||
77 | 1121 |
We are currently engaged in two tasks related to this algorithm. The |
1122 |
first task is proving that our simplification rules actually do not |
|
1123 |
affect the POSIX value that should be generated by the algorithm |
|
1124 |
according to the specification of a POSIX value and furthermore obtain a |
|
1125 |
much tighter bound on the sizes of derivatives. The result is that our |
|
49 | 1126 |
algorithm should be correct and faster on all inputs. The original |
1127 |
blow-up, as observed in JavaScript, Python and Java, would be excluded |
|
77 | 1128 |
from happening in our algorithm. For this proof we use the theorem prover |
1129 |
Isabelle. Once completed, this result will advance the state-of-the-art: |
|
1130 |
Sulzmann and Lu wrote in their paper~\cite{Sulzmann2014} about the |
|
1131 |
bitcoded ``incremental parsing method'' (that is the lexing algorithm |
|
1132 |
outlined in this section): |
|
30 | 1133 |
|
1134 |
\begin{quote}\it |
|
1135 |
``Correctness Claim: We further claim that the incremental parsing |
|
1136 |
method in Figure~5 in combination with the simplification steps in |
|
1137 |
Figure 6 yields POSIX parse trees. We have tested this claim |
|
1138 |
extensively by using the method in Figure~3 as a reference but yet |
|
1139 |
have to work out all proof details.'' |
|
1140 |
\end{quote} |
|
1141 |
||
79 | 1142 |
\noindent We would settle this correctness claim. It is relatively |
1143 |
straightforward to establish that after one simplification step, the part of a |
|
1144 |
nullable derivative that corresponds to a POSIX value remains intact and can |
|
1145 |
still be collected, in other words, we can show that\comment{Double-check....I |
|
80 | 1146 |
think this is not the case}\comment{If i remember correctly, you have proved this lemma. |
1147 |
I feel this is indeed not true because you might place arbitrary |
|
1148 |
bits on the regex r, however if this is the case, did i remember wrongly that |
|
1149 |
you proved something like simplification does not affect $\textit{bmkeps}$ results? |
|
1150 |
Anyway, i have amended this a little bit so it does not allow arbitrary bits attached |
|
1151 |
to a regex. Maybe it works now.} |
|
74
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1152 |
|
71 | 1153 |
\begin{center} |
80 | 1154 |
$\textit{bmkeps} \; a = \textit{bmkeps} \; \textit{bsimp} \; a\;( a\; \textit{bnullable} and \textit{decode}(r, \textit{bmkeps}(a)) is a \textit{POSIX} value)$ |
71 | 1155 |
\end{center} |
74
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1156 |
|
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1157 |
\noindent |
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1158 |
as this basically comes down to proving actions like removing the |
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1159 |
additional $r$ in $r+r$ does not delete important POSIX information in |
77 | 1160 |
a regular expression. The hard part of this proof is to establish that |
74
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1161 |
|
71 | 1162 |
\begin{center} |
79 | 1163 |
$\textit{bmkeps} \; \textit{blexer}\_{simp}(s, \; r) = \textit{bmkeps} \; \textit{blexer} \; \textit{simp}(s, \; r)$ |
71 | 1164 |
\end{center} |
74
9e791ef6022f
just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents:
72
diff
changeset
|
1165 |
|
79 | 1166 |
\noindent That is, if we do derivative on regular expression $r$ and then |
1167 |
simplify it, and repeat this process until we exhaust the string, we get a |
|
80 | 1168 |
regular expression $r''$($\textit{LHS}$) that provides the POSIX matching |
1169 |
information, which is exactly the same as the result $r'$($\textit{RHS}$ of the |
|
1170 |
normal derivative algorithm that only does derivative repeatedly and has no |
|
1171 |
simplification at all. This might seem at first glance very unintuitive, as |
|
1172 |
the $r'$ is exponentially larger than $r''$, but can be explained in the |
|
1173 |
following way: we are pruning away the possible matches that are not POSIX. |
|
1174 |
Since there are exponentially non-POSIX matchings and only 1 POSIX matching, it |
|
1175 |
is understandable that our $r''$ can be a lot smaller. we can still provide |
|
1176 |
the same POSIX value if there is one. This is not as straightforward as the |
|
1177 |
previous proposition, as the two regular expressions $r'$ and $r''$ might have |
|
1178 |
become very different regular expressions. The crucial point is to find the |
|
1179 |
$\textit{POSIX}$ information of a regular expression and how it is modified, |
|
1180 |
augmented and propagated |
|
1181 |
during simplification in parallel with the regularr expression that |
|
1182 |
has not been simplified in the subsequent derivative operations. To aid this, |
|
1183 |
we use the helping function retrieve described by Sulzmann and Lu: \\definition |
|
1184 |
of retrieve TODO\\ |
|
1185 |
This function assembles the bitcode that corresponds to a parse tree for how |
|
1186 |
the current derivative matches the suffix of the string(the characters that |
|
1187 |
have not yet appeared, but will appear as the successive derivatives go on, |
|
1188 |
how do we get this "future" information? By the value $v$, which is |
|
1189 |
computed by a pass of the algorithm that uses |
|
1190 |
$inj$ as described in the previous section). Sulzmann and Lu used this |
|
1191 |
to connect the bitcoded algorithm to the older algorithm by the following |
|
1192 |
equation: |
|
77 | 1193 |
|
79 | 1194 |
\begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\; |
80 | 1195 |
((\textit{internalise}\; r)\backslash_{simp} c) v)$ |
1196 |
\end{center} |
|
1197 |
A little fact that needs to be stated to help comprehension: |
|
1198 |
\begin{center} |
|
1199 |
$r^\uparrow = a$($a$ stands for $\textit{annotated}).$ |
|
1200 |
\end{center} |
|
1201 |
Ausaf and Urban also used this fact to prove the |
|
1202 |
correctness of bitcoded algorithm without simplification. Our purpose |
|
1203 |
of using this, however, is to establish |
|
1204 |
\begin{center} |
|
1205 |
$ \textit{retrieve} \; |
|
1206 |
a \; v \;=\; \textit{retrieve} \; \textit{simp}(a) \; v'.$ |
|
1207 |
\end{center} |
|
1208 |
The idea |
|
1209 |
is that using $v'$, a simplified version of $v$ that possibly had gone |
|
1210 |
through the same simplification step as $\textit{simp}(a)$ we are |
|
1211 |
able to extract the bit-sequence that gives the same parsing |
|
1212 |
information as the unsimplified one. After establishing this, we |
|
1213 |
might be able to finally bridge the gap of proving |
|
1214 |
\begin{center} |
|
1215 |
$\textit{retrieve} \; r \backslash s \; v = \;\textit{retrieve} \; |
|
1216 |
\textit{simp}(r) \backslash s \; v'$ |
|
1217 |
\end{center} |
|
1218 |
and subsequently |
|
1219 |
\begin{center} |
|
1220 |
$\textit{retrieve} \; r \backslash s \; v\; = \; \textit{retrieve} \; |
|
1221 |
r \backslash_{simp} s \; v'$. |
|
1222 |
\end{center} |
|
1223 |
This proves that our simplified |
|
1224 |
version of regular expression still contains all the bitcodes needed. |
|
49 | 1225 |
|
72 | 1226 |
|
70 | 1227 |
The second task is to speed up the more aggressive simplification. |
1228 |
Currently it is slower than a naive simplification(the naive version as |
|
1229 |
implemented in ADU of course can explode in some cases). So it needs to |
|
1230 |
be explored how to make it faster. Our possibility would be to explore |
|
1231 |
again the connection to DFAs. This is very much work in progress. |
|
30 | 1232 |
|
1233 |
\section{Conclusion} |
|
1234 |
||
1235 |
In this PhD-project we are interested in fast algorithms for regular |
|
1236 |
expression matching. While this seems to be a ``settled'' area, in |
|
1237 |
fact interesting research questions are popping up as soon as one steps |
|
1238 |
outside the classic automata theory (for example in terms of what kind |
|
1239 |
of regular expressions are supported). The reason why it is |
|
1240 |
interesting for us to look at the derivative approach introduced by |
|
1241 |
Brzozowski for regular expression matching, and then much further |
|
1242 |
developed by Sulzmann and Lu, is that derivatives can elegantly deal |
|
1243 |
with some of the regular expressions that are of interest in ``real |
|
1244 |
life''. This includes the not-regular expression, written $\neg\,r$ |
|
1245 |
(that is all strings that are not recognised by $r$), but also bounded |
|
1246 |
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is |
|
1247 |
also hope that the derivatives can provide another angle for how to |
|
1248 |
deal more efficiently with back-references, which are one of the |
|
1249 |
reasons why regular expression engines in JavaScript, Python and Java |
|
1250 |
choose to not implement the classic automata approach of transforming |
|
1251 |
regular expressions into NFAs and then DFAs---because we simply do not |
|
1252 |
know how such back-references can be represented by DFAs. |
|
1253 |
||
1254 |
||
1255 |
\bibliographystyle{plain} |
|
1256 |
\bibliography{root} |
|
1257 |
||
1258 |
||
1259 |
\end{document} |