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