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