ninems/ninems.tex
author Chengsong
Sun, 30 Jun 2019 22:23:52 +0100
changeset 34 96bba9b493e9
parent 30 bd9eb959dbce
child 35 f70e9ab4e680
permissions -rw-r--r--
hope works
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
96bba9b493e9 hope works
Chengsong
parents: 30
diff changeset
     1
 \documentclass[a4paper,UKenglish]{lipics}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     2
\usepackage{graphic}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     3
\usepackage{data}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     4
\usepackage{tikz-cd}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     5
% \documentclass{article}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     6
%\usepackage[utf8]{inputenc}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     7
%\usepackage[english]{babel}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     8
%\usepackage{listings}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     9
% \usepackage{amsthm}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    10
% \usepackage{hyperref}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    11
% \usepackage[margin=0.5in]{geometry}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    12
%\usepackage{pmboxdraw}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    13
 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    14
\title{POSIX Regular Expression Matching and Lexing}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    15
\author{Chengsong Tan}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    16
\affil{King's College London\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    17
London, UK\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    18
\texttt{chengsong.tan@kcl.ac.uk}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    19
\authorrunning{Chengsong Tan}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    20
\Copyright{Chengsong Tan}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    21
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    22
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    23
\newcommand{\ZERO}{\mbox{\bf 0}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    24
\newcommand{\ONE}{\mbox{\bf 1}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    25
\def\lexer{\mathit{lexer}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    26
\def\mkeps{\mathit{mkeps}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    27
\def\inj{\mathit{inj}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    28
\def\Empty{\mathit{Empty}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    29
\def\Left{\mathit{Left}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    30
\def\Right{\mathit{Right}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    31
\def\Stars{\mathit{Stars}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    32
\def\Char{\mathit{Char}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    33
\def\Seq{\mathit{Seq}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    34
\def\Der{\mathit{Der}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    35
\def\nullable{\mathit{nullable}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    36
\def\Z{\mathit{Z}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    37
\def\S{\mathit{S}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    38
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    39
%\theoremstyle{theorem}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    40
%\newtheorem{theorem}{Theorem}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    41
%\theoremstyle{lemma}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    42
%\newtheorem{lemma}{Lemma}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    43
%\newcommand{\lemmaautorefname}{Lemma}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    44
%\theoremstyle{definition}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    45
%\newtheorem{definition}{Definition}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    46
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    47
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    48
\begin{document}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    49
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    50
\maketitle
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    51
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    52
\begin{abstract}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    53
  Brzozowski introduced in 1964 a beautifully simple algorithm for
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    54
  regular expression matching based on the notion of derivatives of
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    55
  regular expressions. In 2014, Sulzmann and Lu extended this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    56
  algorithm to not just give a YES/NO answer for whether or not a regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    57
  expression matches a string, but in case it matches also \emph{how}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    58
  it matches the string.  This is important for applications such as
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    59
  lexing (tokenising a string). The problem is to make the algorithm
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    60
  by Sulzmann and Lu fast on all inputs without breaking its
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    61
  correctness.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    62
\end{abstract}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    63
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    64
\section{Introduction}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    65
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    66
This PhD-project is about regular expression matching and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    67
lexing. Given the maturity of this topic, the reader might wonder:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    68
Surely, regular expressions must have already been studied to death?
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    69
What could possibly be \emph{not} known in this area? And surely all
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    70
implemented algorithms for regular expression matching are blindingly
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    71
fast?
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    72
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    73
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    74
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    75
Unfortunately these preconceptions are not supported by evidence: Take
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    76
for example the regular expression $(a^*)^*\,b$ and ask whether
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    77
strings of the form $aa..a$ match this regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    78
expression. Obviously they do not match---the expected $b$ in the last
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    79
position is missing. One would expect that modern regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    80
matching engines can find this out very quickly. Alas, if one tries
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    81
this example in JavaScript, Python or Java 8 with strings like 28
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    82
$a$'s, one discovers that this decision takes around 30 seconds and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    83
takes considerably longer when adding a few more $a$'s, as the graphs
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    84
below show:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    85
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    86
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    87
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    88
\begin{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    89
\begin{axis}[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    90
    xlabel={$n$},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    91
    x label style={at={(1.05,-0.05)}},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    92
    ylabel={time in secs},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    93
    enlargelimits=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    94
    xtick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    95
    xmax=33,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    96
    ymax=35,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    97
    ytick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    98
    scaled ticks=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    99
    axis lines=left,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   100
    width=5cm,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   101
    height=4cm, 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   102
    legend entries={JavaScript},  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   103
    legend pos=north west,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   104
    legend cell align=left]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   105
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   106
\end{axis}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   107
\end{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   108
  &
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   109
\begin{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   110
\begin{axis}[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   111
    xlabel={$n$},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   112
    x label style={at={(1.05,-0.05)}},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   113
    %ylabel={time in secs},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   114
    enlargelimits=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   115
    xtick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   116
    xmax=33,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   117
    ymax=35,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   118
    ytick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   119
    scaled ticks=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   120
    axis lines=left,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   121
    width=5cm,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   122
    height=4cm, 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   123
    legend entries={Python},  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   124
    legend pos=north west,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   125
    legend cell align=left]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   126
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   127
\end{axis}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   128
\end{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   129
  &
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   130
\begin{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   131
\begin{axis}[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   132
    xlabel={$n$},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   133
    x label style={at={(1.05,-0.05)}},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   134
    %ylabel={time in secs},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   135
    enlargelimits=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   136
    xtick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   137
    xmax=33,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   138
    ymax=35,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   139
    ytick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   140
    scaled ticks=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   141
    axis lines=left,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   142
    width=5cm,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   143
    height=4cm, 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   144
    legend entries={Java 8},  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   145
    legend pos=north west,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   146
    legend cell align=left]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   147
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   148
\end{axis}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   149
\end{tikzpicture}\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   150
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   151
           of the form $\underbrace{aa..a}_{n}$.}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   152
\end{tabular}    
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   153
\end{center}  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   154
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   155
\noindent These are clearly abysmal and possibly surprising results.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   156
One would expect these systems doing much better than that---after
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   157
all, given a DFA and a string, whether a string is matched by this DFA
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   158
should be linear.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   159
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   160
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   161
exhibit this ``exponential behaviour''.  Unfortunately, such regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   162
expressions are not just a few ``outliers'', but actually they are
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   163
frequent enough that a separate name has been created for
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   164
them---\emph{evil regular expressions}. In empiric work, Davis et al
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   165
report that they have found thousands of such evil regular expressions
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   166
in the JavaScript and Python ecosystems \cite{Davis18}.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   167
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   168
This exponential blowup sometimes causes real pain in real life:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   169
for example on 20 July 2016 one evil regular expression brought the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   170
webpage \href{http://stackexchange.com}{Stack Exchange} to its knees \footnote{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   171
In this instance, a regular expression intended to just trim white
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   172
spaces from the beginning and the end of a line actually consumed
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   173
massive amounts of CPU-resources and because of this the web servers
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   174
ground to a halt. This happened when a post with 20,000 white spaces
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   175
was submitted, but importantly the white spaces were neither at the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   176
beginning nor at the end. As a result, the regular expression matching
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   177
engine needed to backtrack over many choices.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   178
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   179
The underlying problem is that many ``real life'' regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   180
matching engines do not use DFAs for matching. This is because they
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   181
support regular expressions that are not covered by the classical
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   182
automata theory, and in this more general setting there are quite a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   183
few research questions still unanswered and fast algorithms still need
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   184
to be developed.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   185
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   186
There is also another under-researched problem to do with regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   187
expressions and lexing, i.e.~the process of breaking up strings into
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   188
sequences of tokens according to some regular expressions. In this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   189
setting one is not just interested in whether or not a regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   190
expression matches a string, but if it matches also in \emph{how} it
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   191
matches the string.  Consider for example a regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   192
$r_{key}$ for recognising keywords such as \textit{if}, \textit{then}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   193
and so on; and a regular expression $r_{id}$ for recognising
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   194
identifiers (say, a single character followed by characters or
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   195
numbers). One can then form the compound regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   196
$(r_{key} + r_{id})^*$ and use it to tokenise strings.  But then how
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   197
should the string \textit{iffoo} be tokenised?  It could be tokenised
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   198
as a keyword followed by an identifier, or the entire string as a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   199
single identifier.  Similarly, how should the string \textit{if} be
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   200
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   201
``fire''---so is it an identifier or a keyword?  While in applications
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   202
there is a well-known strategy to decide these questions, called POSIX
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   203
matching, only relatively recently precise definitions of what POSIX
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   204
matching actually means have been formalised
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   205
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. Roughly,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   206
POSIX matching means matching the longest initial substring and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   207
in the case of a tie, the initial submatch is chosen according to some priorities attached to the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   208
regular expressions (e.g.~keywords have a higher priority than
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   209
identifiers). This sounds rather simple, but according to Grathwohl et
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   210
al \cite[Page 36]{CrashCourse2014} this is not the case. They wrote:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   211
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   212
\begin{quote}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   213
\it{}``The POSIX strategy is more complicated than the greedy because of 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   214
the dependence on information about the length of matched strings in the 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   215
various subexpressions.''
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   216
\end{quote}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   217
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   218
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   219
This is also supported by evidence collected by Kuklewicz
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   220
\cite{Kuklewicz} who noticed that a number of POSIX regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   221
matchers calculate incorrect results.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   222
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   223
Our focus is on an algorithm introduced by Sulzmann and Lu in 2014 for
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   224
regular expression matching according to the POSIX strategy
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   225
\cite{Sulzmann2014}. Their algorithm is based on an older algorithm by
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   226
Brzozowski from 1964 where he introduced the notion of derivatives of
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   227
regular expressions \cite{Brzozowski1964}. We shall briefly explain
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   228
the algorithms next.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   229
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   230
\section{The Algorithms by  Brzozowski, and Sulzmann and Lu}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   231
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   232
Suppose regular expressions are given by the following grammar:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   233
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   234
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   235
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   236
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   237
		\begin{tabular}{@{}rrl@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   238
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   239
			$r$ & $::=$  & $\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   240
			& $\mid$ & $\ONE$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   241
			& $\mid$ & $c$          \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   242
			& $\mid$ & $r_1 \cdot r_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   243
			& $\mid$ & $r_1 + r_2$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   244
			\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   245
			& $\mid$ & $r^*$         \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   246
		\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   247
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   248
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   249
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   250
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   251
The intended meaning of the regular expressions is as usual: $\ZERO$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   252
cannot match any string, $\ONE$ can match the empty string, the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   253
character regular expression $c$ can match the character $c$, and so
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   254
on. The brilliant contribution by Brzozowski is the notion of
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   255
\emph{derivatives} of regular expressions.  The idea behind this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   256
notion is as follows: suppose a regular expression $r$ can match a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   257
string of the form $c\!::\! s$ (that is a list of characters starting
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   258
with $c$), what does the regular expression look like that can match
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   259
just $s$? Brzozowski gave a neat answer to this question. He defined
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   260
the following operation on regular expressions, written
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   261
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$):
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   262
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   263
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   264
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   265
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   266
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   267
		$d \backslash c$     & $\dn$ & 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   268
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   269
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   270
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, \epsilon \in L(r_1)$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   271
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   272
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   273
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   274
\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   275
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   276
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   277
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   278
The $\mathit{if}$ condition in the definition of $(r_1 \cdot r_2) \backslash c$ involves a membership testing: $\epsilon \overset{?}{\in} L(r_1)$.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   279
Such testing is easily implemented by the following simple recursive function $\nullable(\_)$:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   280
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   281
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   282
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   283
		\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   284
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   285
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   286
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   287
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   288
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   289
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   290
		\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   291
	\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   292
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   293
 %Assuming the classic notion of a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   294
%\emph{language} of a regular expression, written $L(\_)$, t
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   295
The main
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   296
property of the derivative operation is that
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   297
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   298
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   299
$c\!::\!s \in L(r)$ holds
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   300
if and only if $s \in L(r\backslash c)$.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   301
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   302
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   303
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   304
So if we want to find out whether a string $s$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   305
matches with a regular expression $r$, build the derivatives of $r$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   306
w.r.t.\ (in succession) all the characters of the string $s$. Finally,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   307
test whether the resulting regular expression can match the empty
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   308
string.  If yes, then $r$ matches $s$, and no in the negative
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   309
case.\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   310
If we define the successive derivative operation to be like this:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   311
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   312
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   313
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   314
$r \backslash \epsilon $ & $\dn$ & $r$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   315
\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   316
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   317
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   318
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   319
We obtain a simple and elegant regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   320
expression matching algorithm: 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   321
\begin{definition}{matcher}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   322
\[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   323
match\;s\;r \;\dn\; nullable(r\backslash s)
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   324
\]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   325
\end{definition}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   326
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   327
 For us the main advantage is that derivatives can be
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   328
straightforwardly implemented in any functional programming language,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   329
and are easily definable and reasoned about in theorem provers---the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   330
definitions just consist of inductive datatypes and simple recursive
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   331
functions. Moreover, the notion of derivatives can be easily
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   332
generalised to cover extended regular expression constructors such as
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   333
the not-regular expression, written $\neg\,r$, or bounded repetitions
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   334
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   335
straightforwardly realised within the classic automata approach.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   336
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   337
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   338
One limitation, however, of Brzozowski's algorithm is that it only
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   339
produces a YES/NO answer for whether a string is being matched by a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   340
regular expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   341
algorithm to allow generation of an actual matching, called a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   342
\emph{value}.  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   343
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   344
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   345
	\begin{tabular}{c@{\hspace{20mm}}c}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   346
		\begin{tabular}{@{}rrl@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   347
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   348
			$r$ & $::=$  & $\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   349
			& $\mid$ & $\ONE$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   350
			& $\mid$ & $c$          \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   351
			& $\mid$ & $r_1 \cdot r_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   352
			& $\mid$ & $r_1 + r_2$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   353
			\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   354
			& $\mid$ & $r^*$         \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   355
		\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   356
		&
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   357
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   358
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   359
			$v$ & $::=$  & \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   360
			&        & $\Empty$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   361
			& $\mid$ & $\Char(c)$          \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   362
			& $\mid$ & $\Seq\,v_1\, v_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   363
			& $\mid$ & $\Left(v)$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   364
			& $\mid$ & $\Right(v)$  \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   365
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   366
		\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   367
	\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   368
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   369
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   370
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   371
 Here we put the regular expression and values of the same shape on the same level to illustrate the corresponding relation between them. \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   372
 Values are a way of expressing parse trees(the tree structure that tells how a sub-regex matches a substring). For example, $\Seq\,v_1\, v_2$ tells us how the string $|v_1| \cdot |v_2|$ matches the regex $r_1 \cdot r_2$: $r_1$ matches $|v_1|$ and $r_2$ matches $|v_2|$. Exactly how these two are matched are contained in the sub-structure of $v_1$ and $v_2$. The flatten notation $| v |$ means extracting the characters in the value $v$ to form a string. For example, $|\mathit{Seq} \, \mathit{Char(c)} \, \mathit{Char(d)}|$ = $cd$.\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   373
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   374
 To give a concrete example of how value works, consider the string $xy$ and the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   375
regular expression $(x + (y + xy))^*$. We can view this regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   376
expression as a tree and if the string $xy$ is matched by two Star
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   377
``iterations'', then the $x$ is matched by the left-most alternative
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   378
in this tree and the $y$ by the right-left alternative. This suggests
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   379
to record this matching as
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   380
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   381
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   382
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   383
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   384
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   385
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   386
where $\Stars$ records how many
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   387
iterations were used; and $\Left$, respectively $\Right$, which
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   388
alternative is used. The value for
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   389
matching $xy$ in a single ``iteration'', i.e.~the POSIX value,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   390
would look as follows
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   391
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   392
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   393
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   394
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   395
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   396
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   397
where $\Stars$ has only a single-element list for the single iteration
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   398
and $\Seq$ indicates that $xy$ is matched by a sequence regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   399
expression.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   400
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   401
The contribution of Sulzmann and Lu is an extension of Brzozowski's
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   402
algorithm by a second phase (the first phase being building successive
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   403
derivatives). In this second phase, for every successful match the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   404
corresponding POSIX value is computed. The whole process looks like this diagram:\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   405
\begin{tikzcd}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   406
r_0 \arrow[r, "c_0"]  \arrow[d] & r_1 \arrow[r, "c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   407
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]         
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   408
\end{tikzcd}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   409
We shall briefly explain this interesting process.\\ For the convenience of explanation, we have the following notations: the regular expression $r$ used for matching is also called $r_0$ and the string $s$ is composed of $n$ characters $c_0 c_1 ... c_{n-1}$.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   410
First, we do the derivative operation on $r_0$, $r_1$, ..., using characters $c_0$, $c_1$, ...  until we get the final derivative $r_n$.We test whether it is $nullable$ or not. If no we know immediately the string does not match the regex. If yes, we start building the parse tree incrementally. We first call $mkeps$(which stands for make epsilon--make the parse tree for the empty word epsilon) to construct the parse tree $v_n$ for how the final derivative $r_n$ matches the empty string:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   411
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   412
 After this, we inject back the characters one by one, in reverse order as they were chopped off, to build the parse tree $v_i$ for how the regex $r_i$ matches the string $s_i$($s_i$ means the string s with the first $i$ characters being chopped off) from the previous parse tree. After $n$ transformations, we get the parse tree for how $r_0$ matches $s$, exactly as we wanted.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   413
An inductive proof can be routinely established.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   414
We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}. Rather, we shall focus next on the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   415
process of simplification of regular expressions, which is needed in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   416
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   417
and Lu's algorithms.  This is where the PhD-project hopes to advance
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   418
the state-of-the-art.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   419
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   420
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   421
\section{Simplification of Regular Expressions}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   422
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   423
The main drawback of building successive derivatives according to
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   424
Brzozowski's definition is that they can grow very quickly in size.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   425
This is mainly due to the fact that the derivative operation generates
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   426
often ``useless'' $\ZERO$s and $\ONE$s in derivatives.  As a result,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   427
if implemented naively both algorithms by Brzozowski and by Sulzmann
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   428
and Lu are excruciatingly slow. For example when starting with the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   429
regular expression $(a + aa)^*$ and building 12 successive derivatives
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   430
w.r.t.~the character $a$, one obtains a derivative regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   431
with more than 8000 nodes (when viewed as a tree). Operations like
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   432
derivative and $\nullable$ need to traverse such trees and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   433
consequently the bigger the size of the derivative the slower the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   434
algorithm. Fortunately, one can simplify regular expressions after
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   435
each derivative step. Various simplifications of regular expressions
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   436
are possible, such as the simplifications of $\ZERO + r$,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   437
$r + \ZERO$, $\ONE\cdot r$, $r \cdot \ONE$, and $r + r$ to just
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   438
$r$. These simplifications do not affect the answer for whether a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   439
regular expression matches a string or not, but fortunately also do
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   440
not affect the POSIX strategy of how regular expressions match
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   441
strings---although the latter is much harder to establish. Some
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   442
initial results in this regard have been obtained in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   443
\cite{AusafDyckhoffUrban2016}. However, what has not been achieved yet
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   444
is a very tight bound for the size. Such a tight bound is suggested by
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   445
work of Antimirov who proved that (partial) derivatives can be bound
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   446
by the number of characters contained in the initial regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   447
expression \cite{Antimirov95}. We believe, and have generated test
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   448
data, that a similar bound can be obtained for the derivatives in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   449
Sulzmann and Lu's algorithm. Let us give some details about this next.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   450
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   451
We first followed Sulzmann and Lu's idea of introducing
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   452
\emph{annotated regular expressions}~\cite{Sulzmann2014}. They are
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   453
defined by the following grammar:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   454
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   455
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   456
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   457
  $\textit{a}$ & $::=$  & $\textit{ZERO}$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   458
                  & $\mid$ & $\textit{ONE}\;\;bs$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   459
                  & $\mid$ & $\textit{CHAR}\;\;bs\,c$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   460
                  & $\mid$ & $\textit{ALTS}\;\;bs\,as$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   461
                  & $\mid$ & $\textit{SEQ}\;\;bs\,a_1\,a_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   462
                  & $\mid$ & $\textit{STAR}\;\;bs\,a$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   463
\end{tabular}    
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   464
\end{center}  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   465
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   466
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   467
where $bs$ stands for bitsequences, and $as$ (in \textit{ALTS}) for a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   468
list of annotated regular expressions. These bitsequences encode
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   469
information about the (POSIX) value that should be generated by the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   470
Sulzmann and Lu algorithm. Bitcodes are essentially incomplete values.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   471
This can be straightforwardly seen in the following transformation: 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   472
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   473
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   474
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   475
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   476
  $\textit{code}(\Left\,v)$ & $\dn$ & $\Z :: code(v)$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   477
  $\textit{code}(\Right\,v)$ & $\dn$ & $\S :: code(v)$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   478
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   479
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[\S]$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   480
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $\Z :: code(v) \;@\;
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   481
                                                 code(\Stars\,vs)$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   482
\end{tabular}    
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   483
\end{center} 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   484
where $\Z$ and $\S$ are arbitrary names for the bits in the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   485
bitsequences. 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   486
Here code encodes a value into a bitsequence by converting Left into $\Z$, Right into $\S$, the start point of a non-empty star iteration into $\S$, and the border where a local star terminates into $\Z$. This conversion is apparently lossy, as it throws away the character information, and does not decode the boundary between the two operands of the sequence constructor. Moreover, with only the bitcode we cannot even tell whether the $\S$s and $\Z$s are for $Left/Right$ or $Stars$. The reason for choosing this compact way of storing information is that the relatively small size of bits can be easily moved around during the lexing process. In order to recover the bitcode back into values, we will need the regular expression as the extra information and decode them back into value:\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   487
TODO: definition of decode
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   488
\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   489
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   490
To do lexing using annotated regular expressions, we shall first transform the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   491
usual (un-annotated) regular expressions into annotated regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   492
expressions:\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   493
TODO: definition of internalise
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   494
\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   495
Then we do successive derivative operations on the annotated regular expression. This derivative operation is the same as what we previously have for the simple regular expressions, except that we take special care of the bits to store the parse tree information:\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   496
TODO: bder
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   497
\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   498
This way, we do not have to use an injection function and a second phase, but instead only need to collect the bits while running $mkeps$:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   499
TODO: mkepsBC
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   500
\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   501
and then decode the bits using the regular expression. The whole process looks like this:\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   502
r
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   503
\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   504
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   505
The main point of the bitsequences and annotated regular expressions
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   506
is that we can apply rather aggressive (in terms of size)
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   507
simplification rules in order to keep derivatives small.  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   508
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   509
We have
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   510
developed such ``aggressive'' simplification rules and generated test
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   511
data that show that the expected bound can be achieved. Obviously we
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   512
could only partially cover  the search space as there are infinitely
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   513
many regular expressions and strings. One modification we introduced
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   514
is to allow a list of annotated regular expressions in the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   515
\textit{ALTS} constructor. This allows us to not just delete
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   516
unnecessary $\ZERO$s and $\ONE$s from regular expressions, but also
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   517
unnecessary ``copies'' of regular expressions (very similar to
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   518
simplifying $r + r$ to just $r$, but in a more general
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   519
setting). Another modification is that we use simplification rules
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   520
inspired by Antimirov's work on partial derivatives. They maintain the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   521
idea that only the first ``copy'' of a regular expression in an
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   522
alternative contributes to the calculation of a POSIX value. All
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   523
subsequent copies can be pruned from the regular expression.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   524
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   525
We are currently engaged with proving that our simplification rules
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   526
actually do not affect the POSIX value that should be generated by the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   527
algorithm according to the specification of a POSIX value and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   528
furthermore that our derivatives stay small for all derivatives. For
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   529
this proof we use the theorem prover Isabelle. Once completed, this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   530
result will advance the state-of-the-art: Sulzmann and Lu wrote in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   531
their paper \cite{Sulzmann2014} about the bitcoded ``incremental
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   532
parsing method'' (that is the matching algorithm outlined in this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   533
section):
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   534
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   535
\begin{quote}\it
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   536
  ``Correctness Claim: We further claim that the incremental parsing
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   537
  method in Figure~5 in combination with the simplification steps in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   538
  Figure 6 yields POSIX parse trees. We have tested this claim
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   539
  extensively by using the method in Figure~3 as a reference but yet
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   540
  have to work out all proof details.''
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   541
\end{quote}  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   542
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   543
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   544
We would settle the correctness claim and furthermore obtain a much
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   545
tighter bound on the sizes of derivatives. The result is that our
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   546
algorithm should be correct and faster on all inputs.  The original
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   547
blow-up, as observed in JavaScript, Python and Java, would be excluded
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   548
from happening in our algorithm.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   549
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   550
\section{Conclusion}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   551
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   552
In this PhD-project we are interested in fast algorithms for regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   553
expression matching. While this seems to be a ``settled'' area, in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   554
fact interesting research questions are popping up as soon as one steps
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   555
outside the classic automata theory (for example in terms of what kind
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   556
of regular expressions are supported). The reason why it is
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   557
interesting for us to look at the derivative approach introduced by
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   558
Brzozowski for regular expression matching, and then much further
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   559
developed by Sulzmann and Lu, is that derivatives can elegantly deal
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   560
with some of the regular expressions that are of interest in ``real
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   561
life''. This includes the not-regular expression, written $\neg\,r$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   562
(that is all strings that are not recognised by $r$), but also bounded
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   563
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   564
also hope that the derivatives can provide another angle for how to
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   565
deal more efficiently with back-references, which are one of the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   566
reasons why regular expression engines in JavaScript, Python and Java
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   567
choose to not implement the classic automata approach of transforming
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   568
regular expressions into NFAs and then DFAs---because we simply do not
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   569
know how such back-references can be represented by DFAs.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   570
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   571
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   572
\bibliographystyle{plain}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   573
\bibliography{root}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   574
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   575
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   576
\end{document}