etnms/etnms.tex
author Chengsong
Sat, 11 Jan 2020 22:17:27 +0000
changeset 101 4a327e70d538
parent 100 397b31867ea6
child 102 9c3c118896bb
permissions -rw-r--r--
b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
Chengsong
parents:
diff changeset
     1
\documentclass[a4paper,UKenglish]{lipics}
Chengsong
parents:
diff changeset
     2
\usepackage{graphic}
Chengsong
parents:
diff changeset
     3
\usepackage{data}
Chengsong
parents:
diff changeset
     4
\usepackage{tikz-cd}
Chengsong
parents:
diff changeset
     5
%\usepackage{algorithm}
Chengsong
parents:
diff changeset
     6
\usepackage{amsmath}
Chengsong
parents:
diff changeset
     7
\usepackage[noend]{algpseudocode}
Chengsong
parents:
diff changeset
     8
\usepackage{enumitem}
Chengsong
parents:
diff changeset
     9
\usepackage{nccmath}
Chengsong
parents:
diff changeset
    10
Chengsong
parents:
diff changeset
    11
\definecolor{darkblue}{rgb}{0,0,0.6}
Chengsong
parents:
diff changeset
    12
\hypersetup{colorlinks=true,allcolors=darkblue}
Chengsong
parents:
diff changeset
    13
\newcommand{\comment}[1]%
Chengsong
parents:
diff changeset
    14
{{\color{red}$\Rightarrow$}\marginpar{\raggedright\small{\bf\color{red}#1}}}
Chengsong
parents:
diff changeset
    15
Chengsong
parents:
diff changeset
    16
% \documentclass{article}
Chengsong
parents:
diff changeset
    17
%\usepackage[utf8]{inputenc}
Chengsong
parents:
diff changeset
    18
%\usepackage[english]{babel}
Chengsong
parents:
diff changeset
    19
%\usepackage{listings}
Chengsong
parents:
diff changeset
    20
% \usepackage{amsthm}
Chengsong
parents:
diff changeset
    21
%\usepackage{hyperref}
Chengsong
parents:
diff changeset
    22
% \usepackage[margin=0.5in]{geometry}
Chengsong
parents:
diff changeset
    23
%\usepackage{pmboxdraw}
Chengsong
parents:
diff changeset
    24
 
Chengsong
parents:
diff changeset
    25
\title{POSIX Regular Expression Matching and Lexing}
Chengsong
parents:
diff changeset
    26
\author{Chengsong Tan}
Chengsong
parents:
diff changeset
    27
\affil{King's College London\\
Chengsong
parents:
diff changeset
    28
London, UK\\
Chengsong
parents:
diff changeset
    29
\texttt{chengsong.tan@kcl.ac.uk}}
Chengsong
parents:
diff changeset
    30
\authorrunning{Chengsong Tan}
Chengsong
parents:
diff changeset
    31
\Copyright{Chengsong Tan}
Chengsong
parents:
diff changeset
    32
Chengsong
parents:
diff changeset
    33
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
Chengsong
parents:
diff changeset
    34
\newcommand{\ZERO}{\mbox{\bf 0}}
Chengsong
parents:
diff changeset
    35
\newcommand{\ONE}{\mbox{\bf 1}}
101
Chengsong
parents: 100
diff changeset
    36
\def\erase{\textit{erase}}
94
Chengsong
parents:
diff changeset
    37
\def\bders{\textit{bders}}
Chengsong
parents:
diff changeset
    38
\def\lexer{\mathit{lexer}}
Chengsong
parents:
diff changeset
    39
\def\blexer{\textit{blexer}}
Chengsong
parents:
diff changeset
    40
\def\blexers{\mathit{blexer\_simp}}
95
Chengsong
parents: 94
diff changeset
    41
\def\simp{\mathit{simp}}
94
Chengsong
parents:
diff changeset
    42
\def\mkeps{\mathit{mkeps}}
Chengsong
parents:
diff changeset
    43
\def\bmkeps{\textit{bmkeps}}
Chengsong
parents:
diff changeset
    44
\def\inj{\mathit{inj}}
Chengsong
parents:
diff changeset
    45
\def\Empty{\mathit{Empty}}
Chengsong
parents:
diff changeset
    46
\def\Left{\mathit{Left}}
Chengsong
parents:
diff changeset
    47
\def\Right{\mathit{Right}}
Chengsong
parents:
diff changeset
    48
\def\Stars{\mathit{Stars}}
Chengsong
parents:
diff changeset
    49
\def\Char{\mathit{Char}}
Chengsong
parents:
diff changeset
    50
\def\Seq{\mathit{Seq}}
Chengsong
parents:
diff changeset
    51
\def\Der{\mathit{Der}}
Chengsong
parents:
diff changeset
    52
\def\nullable{\mathit{nullable}}
Chengsong
parents:
diff changeset
    53
\def\Z{\mathit{Z}}
Chengsong
parents:
diff changeset
    54
\def\S{\mathit{S}}
Chengsong
parents:
diff changeset
    55
\def\flex{\textit{flex}}
Chengsong
parents:
diff changeset
    56
\def\rup{r^\uparrow}
Chengsong
parents:
diff changeset
    57
\def\retrieve{\textit{retrieve}}
Chengsong
parents:
diff changeset
    58
\def\AALTS{\textit{AALTS}}
Chengsong
parents:
diff changeset
    59
\def\AONE{\textit{AONE}}
Chengsong
parents:
diff changeset
    60
%\theoremstyle{theorem}
Chengsong
parents:
diff changeset
    61
%\newtheorem{theorem}{Theorem}
Chengsong
parents:
diff changeset
    62
%\theoremstyle{lemma}
Chengsong
parents:
diff changeset
    63
%\newtheorem{lemma}{Lemma}
Chengsong
parents:
diff changeset
    64
%\newcommand{\lemmaautorefname}{Lemma}
Chengsong
parents:
diff changeset
    65
%\theoremstyle{definition}
Chengsong
parents:
diff changeset
    66
%\newtheorem{definition}{Definition}
Chengsong
parents:
diff changeset
    67
\algnewcommand\algorithmicswitch{\textbf{switch}}
Chengsong
parents:
diff changeset
    68
\algnewcommand\algorithmiccase{\textbf{case}}
Chengsong
parents:
diff changeset
    69
\algnewcommand\algorithmicassert{\texttt{assert}}
Chengsong
parents:
diff changeset
    70
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}%
Chengsong
parents:
diff changeset
    71
% New "environments"
Chengsong
parents:
diff changeset
    72
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
Chengsong
parents:
diff changeset
    73
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
Chengsong
parents:
diff changeset
    74
\algtext*{EndSwitch}%
Chengsong
parents:
diff changeset
    75
\algtext*{EndCase}%
Chengsong
parents:
diff changeset
    76
Chengsong
parents:
diff changeset
    77
Chengsong
parents:
diff changeset
    78
\begin{document}
Chengsong
parents:
diff changeset
    79
Chengsong
parents:
diff changeset
    80
\maketitle
Chengsong
parents:
diff changeset
    81
Chengsong
parents:
diff changeset
    82
\begin{abstract}
Chengsong
parents:
diff changeset
    83
  Brzozowski introduced in 1964 a beautifully simple algorithm for
Chengsong
parents:
diff changeset
    84
  regular expression matching based on the notion of derivatives of
Chengsong
parents:
diff changeset
    85
  regular expressions. In 2014, Sulzmann and Lu extended this
Chengsong
parents:
diff changeset
    86
  algorithm to not just give a YES/NO answer for whether or not a
Chengsong
parents:
diff changeset
    87
  regular expression matches a string, but in case it does also
Chengsong
parents:
diff changeset
    88
  answers with \emph{how} it matches the string.  This is important for
Chengsong
parents:
diff changeset
    89
  applications such as lexing (tokenising a string). The problem is to
Chengsong
parents:
diff changeset
    90
  make the algorithm by Sulzmann and Lu fast on all inputs without
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    91
  breaking its correctness. Being fast depends on a complete set of 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    92
  simplification rules, some of which 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    93
  have been put forward by Sulzmann and Lu. We have extended their
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    94
  rules in order to obtain a tight bound on size of regular expressions.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    95
  We have tested the correctness of these extended rules, but have not 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    96
  formally established their correctness. We also have not yet looked 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
    97
  at extended regular expressions, such as bounded repetitions,
94
Chengsong
parents:
diff changeset
    98
  negation and back-references.
Chengsong
parents:
diff changeset
    99
\end{abstract}
Chengsong
parents:
diff changeset
   100
Chengsong
parents:
diff changeset
   101
\section{Introduction}
Chengsong
parents:
diff changeset
   102
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   103
While we believe derivatives of regular expressions is a beautiful 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   104
concept (interms of ease to implementing them in functional programming
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   105
language and ease to reason about them formally), they have one major 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   106
drawback: every derivative step can make regular expressions grow 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   107
drastically in size. This in turn has negative effects on the runtime of 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   108
the corresponding lexing algorithms. Consider for example the regular 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   109
expression $(a+aa)^*$ and the short string $aaaaaaaaaaaa$. The size of
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   110
the corresponding derivative is already 8668 node assuming the derivatives
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   111
is seen as a tree. The reason for the poor runtime of the lexing algorithms is
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   112
that they need to traverse such trees over and over again. The solution is to
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   113
find a complete set of simplification rules that keep the sizes of derivatives
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   114
uniformly small.
94
Chengsong
parents:
diff changeset
   115
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   116
For reasons beyond this report, it turns out that a complete set of 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   117
simplification rules depend on values being encoded as bitsequences.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   118
(Vlue are the results of the lexing algorithms generate; they encode how
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   119
a regular expression matched a string.) We already know that the lexing 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   120
algorithm \emph{without} simplification is correct. Therefore in the
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   121
past 6 months we were trying to prove  that the algorithm using bitsequences plus
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   122
our simplification rules is correct. Formally this amounts to show that
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   123
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   124
\begin{equation}\label{mainthm}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   125
\blexers \; r \; s = \blexer \;r\;s
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   126
\end{equation}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   127
94
Chengsong
parents:
diff changeset
   128
\noindent
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   129
whereby $\blexers$ simplifies (makes derivatives smaller) in each step,
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   130
whereas with $\blexer$ the size can grow exponentially. This would be an
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   131
important milestone, because we already have a very good idea how to 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   132
establish that our set our simplification rules keeps the size below a
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   133
relatively tight bound.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   134
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   135
In order to prove the main theorem \eqref{mainthm}, we need to prove the
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   136
two functions produce the same output. The definition of these functions 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   137
is shown below.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   138
94
Chengsong
parents:
diff changeset
   139
\begin{center}
Chengsong
parents:
diff changeset
   140
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   141
  $\textit{blexer}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
   142
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
   143
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
   144
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
   145
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
   146
\end{tabular}
Chengsong
parents:
diff changeset
   147
\end{center}
Chengsong
parents:
diff changeset
   148
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   149
\begin{center}
94
Chengsong
parents:
diff changeset
   150
\begin{tabular}{lcl}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   151
  $\blexers \; r \, s$ &$\dn$ &
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   152
    $\textit{let} \; a = (r^\uparrow)\backslash_{simp}\, s\; \textit{in}$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   153
  & & $\; \; \textit{if} \; \textit{bnullable}(a)$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   154
  & & $\; \; \textit{then} \; \textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   155
  & & $\;\;   \textit{else}\;\textit{None}$
94
Chengsong
parents:
diff changeset
   156
\end{tabular}
Chengsong
parents:
diff changeset
   157
\end{center}
Chengsong
parents:
diff changeset
   158
\noindent
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   159
In these definitions $(r^\uparrow)$ is a kind of coding function that is the
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   160
same in each case, similarly the decode and the \textit{bmkeps}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   161
functions. Our main theorem \eqref{mainthm} therefore boils down to
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   162
proving the following two  propositions (depending on which branch the 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   163
if-else clause takes). It establishes how the derivatives  \emph{with}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   164
simplification do not change the computed result:
94
Chengsong
parents:
diff changeset
   165
Chengsong
parents:
diff changeset
   166
\begin{itemize}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   167
\item{} If a string $s$ is in the language of $L(r)$, then \\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   168
$\textit{bmkeps} (r^\uparrow)\backslash_{simp}\,s = \textit{bmkeps} (r^\uparrow)\backslash s$,\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   169
\item{} If a string $s$ is in the language $L(r)$, then 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   170
$\rup \backslash_{simp} \,s$ is not nullable.
94
Chengsong
parents:
diff changeset
   171
\end{itemize}
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   172
94
Chengsong
parents:
diff changeset
   173
\noindent
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   174
We have already proved in Isabelle the second part. This is actually not 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   175
too difficult because we can show that simplification does not change
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   176
the language of regular expressions. If we can prove the first case,
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   177
that is the bitsequence algorithm with simplification produces the same 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   178
result as the one without simplification, then we are done.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   179
Unfortunately that part requires more effort, because simplification does not 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   180
only.need to \emph{not} change the language, but also not change
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   181
the value (computed result).
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   182
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   183
\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   184
Do you want to keep this? You essentially want to say that the old
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   185
method used retrieve, which unfortunately cannot be adopted to 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   186
the simplification rules. You could just say that and give an example.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   187
However you have to think about how you give the example....nobody knows
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   188
about AZERO etc yet. Maybe it might be better to use normal regexes
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   189
like $a + aa$, but annotate bitsequences as subscript like $_1(_0a + _1aa)$.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   190
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   191
\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   192
REPLY:\\
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   193
Yes, I am essentially saying that the old method
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   194
cannot be adopted without adjustments.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   195
But this does not mean we should skip
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   196
the proof of the bit-coded algorithm
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   197
as it is still the main direction we are looking into
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   198
to prove things. We are trying to modify
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   199
the old proof to suit our needs, but not give 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   200
up it totally, that is why i believe the old 
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   201
proof is fundamental in understanding
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   202
what we are doing in the past 6 months.
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   203
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   204
\bigskip\noindent\rule[1.5ex]{\linewidth}{5pt}
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   205
94
Chengsong
parents:
diff changeset
   206
The correctness proof of
Chengsong
parents:
diff changeset
   207
\begin{center}
Chengsong
parents:
diff changeset
   208
$\blexer \; r^\uparrow  s = \lexer \;r \;s$
Chengsong
parents:
diff changeset
   209
\end{center}
Chengsong
parents:
diff changeset
   210
\noindent
Chengsong
parents:
diff changeset
   211
might provide us insight into proving 
Chengsong
parents:
diff changeset
   212
\begin{center}
Chengsong
parents:
diff changeset
   213
$\blexer \; r^\uparrow \;s = \blexers \; r^\uparrow \;s$
Chengsong
parents:
diff changeset
   214
\end{center}
Chengsong
parents:
diff changeset
   215
\noindent
Chengsong
parents:
diff changeset
   216
(that is also
Chengsong
parents:
diff changeset
   217
why we say the new proof builds on the older one).
Chengsong
parents:
diff changeset
   218
The proof defined the function $\flex$ as another way of
Chengsong
parents:
diff changeset
   219
expressing the $\lexer$ function:
Chengsong
parents:
diff changeset
   220
\begin{center}
Chengsong
parents:
diff changeset
   221
$\lexer \;r\; s = \flex \;\textit{id} \; r\;s \;(\mkeps \; r\backslash s)$
Chengsong
parents:
diff changeset
   222
\end{center}.
Chengsong
parents:
diff changeset
   223
\noindent
Chengsong
parents:
diff changeset
   224
(proof for the above equality will be explained later)
Chengsong
parents:
diff changeset
   225
The definition of $flex$ is as follows:
Chengsong
parents:
diff changeset
   226
\begin{center}
Chengsong
parents:
diff changeset
   227
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   228
$\textit{flex} \;r\; f\; (c\!::\!s) $ & $\dn$ & $\textit{flex} \;  (r\backslash c) \;(\lambda v. f (inj \; r \; c \; v)) \;s$ \\
Chengsong
parents:
diff changeset
   229
$\textit{flex} \;r\; f\;  [\,] $ & $\dn$ & $f$
Chengsong
parents:
diff changeset
   230
\end{tabular}
Chengsong
parents:
diff changeset
   231
\end{center}
Chengsong
parents:
diff changeset
   232
\noindent
Chengsong
parents:
diff changeset
   233
here $\flex$ essentially does lexing by
Chengsong
parents:
diff changeset
   234
stacking up injection functions while doing derivatives,
Chengsong
parents:
diff changeset
   235
explicitly showing the order of characters being
Chengsong
parents:
diff changeset
   236
injected back in each step.
Chengsong
parents:
diff changeset
   237
With $\flex$ we can write $\lexer$ this way: 
Chengsong
parents:
diff changeset
   238
\begin{center}
Chengsong
parents:
diff changeset
   239
$\lexer \;r\; s = \flex \;id \; r\;s \;(\mkeps r\backslash s)$
Chengsong
parents:
diff changeset
   240
\end{center}
Chengsong
parents:
diff changeset
   241
\noindent
Chengsong
parents:
diff changeset
   242
$\flex$ focuses on
Chengsong
parents:
diff changeset
   243
 the injections instead 
Chengsong
parents:
diff changeset
   244
of the derivatives ,
Chengsong
parents:
diff changeset
   245
compared 
Chengsong
parents:
diff changeset
   246
to the original definition of $\lexer$,
Chengsong
parents:
diff changeset
   247
which puts equal amount of emphasis on 
Chengsong
parents:
diff changeset
   248
injection and derivative with respect to each character:
Chengsong
parents:
diff changeset
   249
\begin{center}
Chengsong
parents:
diff changeset
   250
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   251
$\textit{lexer} \; r\; (c\!::\!s) $ & $\dn$ & $\textit{case} \; \lexer \; (r\backslash c) \;s \; \textit{of}$ \\
Chengsong
parents:
diff changeset
   252
 & & $\textit{None} \; \Longrightarrow \; \textit{None}$\\
Chengsong
parents:
diff changeset
   253
  & & $\textbar \; v \; \Longrightarrow \; \inj \; r\;c\;v$\\
Chengsong
parents:
diff changeset
   254
$\textit{lexer} \; r\;  [\,] $ & $\dn$ & $\textit{if} \; \nullable (r) \; \textit{then} \; \mkeps (r) \; \textit{else} \;None$
Chengsong
parents:
diff changeset
   255
\end{tabular}
Chengsong
parents:
diff changeset
   256
\end{center}
Chengsong
parents:
diff changeset
   257
\noindent
Chengsong
parents:
diff changeset
   258
Using this feature of $\flex$  we can rewrite the lexing
Chengsong
parents:
diff changeset
   259
$w.r.t \; s @ [c]$ in term of lexing 
Chengsong
parents:
diff changeset
   260
$w.r.t \; s$:
Chengsong
parents:
diff changeset
   261
\begin{center}
Chengsong
parents:
diff changeset
   262
$\flex \; r \; id \; (s@[c]) \; v = \flex \;  r \; id \; s \; (inj \; (r\backslash s) \; c\; v)$.
Chengsong
parents:
diff changeset
   263
\end{center}
Chengsong
parents:
diff changeset
   264
\noindent
Chengsong
parents:
diff changeset
   265
this allows us to use 
Chengsong
parents:
diff changeset
   266
the inductive hypothesis to get
Chengsong
parents:
diff changeset
   267
\begin{center} 
Chengsong
parents:
diff changeset
   268
$ \flex \; r\; id\; (s@[c])\; v = \textit{decode} \;( \textit{retrieve}\; (\rup \backslash s) \; (\inj \; (r\backslash s) \;c\;v)\;) r$
Chengsong
parents:
diff changeset
   269
\end{center}
Chengsong
parents:
diff changeset
   270
\noindent
Chengsong
parents:
diff changeset
   271
By using a property of retrieve we have the $\textit{RHS}$ of the above equality is
Chengsong
parents:
diff changeset
   272
$decode (retrieve (r^\uparrow \backslash(s @ [c])) v) r$, and this gives the 
Chengsong
parents:
diff changeset
   273
main lemma result:
Chengsong
parents:
diff changeset
   274
\begin{center}
Chengsong
parents:
diff changeset
   275
$ \flex \;r\;  id \; (s@[c]) \; v =\textit{decode}(\textit{retrieve} (\rup \backslash (s@[c])) \;v) r$
Chengsong
parents:
diff changeset
   276
\end{center}
Chengsong
parents:
diff changeset
   277
\noindent
Chengsong
parents:
diff changeset
   278
To use this lemma result for our 
Chengsong
parents:
diff changeset
   279
correctness proof, simply replace the $v$ in the
Chengsong
parents:
diff changeset
   280
$\textit{RHS}$ of the above equality with
Chengsong
parents:
diff changeset
   281
$\mkeps\;(r\backslash (s@[c]))$, and apply the lemma that
Chengsong
parents:
diff changeset
   282
 
Chengsong
parents:
diff changeset
   283
\begin{center}
Chengsong
parents:
diff changeset
   284
$\textit{decode} \; \bmkeps \; \rup \; r = \textit{decode} \; (\textit{retrieve} \; \rup \; \mkeps(r)) \;r$
Chengsong
parents:
diff changeset
   285
\end{center}
Chengsong
parents:
diff changeset
   286
\noindent
Chengsong
parents:
diff changeset
   287
We get the correctness of our bit-coded algorithm:
Chengsong
parents:
diff changeset
   288
\begin{center}
Chengsong
parents:
diff changeset
   289
$\flex \;r\;  id \; s \; (\mkeps \; r\backslash s) = \textit{decode} \; \bmkeps \; \rup\backslash s \; r$
Chengsong
parents:
diff changeset
   290
\end{center}
Chengsong
parents:
diff changeset
   291
\noindent
Chengsong
parents:
diff changeset
   292
The bridge between the above chain of equalities
Chengsong
parents:
diff changeset
   293
is the use of $\retrieve$,
Chengsong
parents:
diff changeset
   294
if we want to use a similar technique for the 
Chengsong
parents:
diff changeset
   295
simplified version of algorithm,
Chengsong
parents:
diff changeset
   296
we face the problem that in the above 
Chengsong
parents:
diff changeset
   297
equalities,
Chengsong
parents:
diff changeset
   298
$\retrieve \; a \; v$ is not always defined.
Chengsong
parents:
diff changeset
   299
for example,
100
397b31867ea6 copied christian changes
Chengsong
parents: 95
diff changeset
   300
$\retrieve \; _0(_1a+_0a) \; \Left(\Empty)$
101
Chengsong
parents: 100
diff changeset
   301
is defined, but not $\retrieve \; (_{01}a) \;\Left(\Empty)$,
94
Chengsong
parents:
diff changeset
   302
though we can extract the same POSIX
Chengsong
parents:
diff changeset
   303
bits from the two annotated regular expressions.
95
Chengsong
parents: 94
diff changeset
   304
The latter might occur when we try to retrieve from 
Chengsong
parents: 94
diff changeset
   305
a simplified regular expression using the same value
Chengsong
parents: 94
diff changeset
   306
as the unsimplified one.
Chengsong
parents: 94
diff changeset
   307
This is because $\Left(\Empty)$ corresponds to
101
Chengsong
parents: 100
diff changeset
   308
the regular expression structure $\ONE+r_2$ instead of
Chengsong
parents: 100
diff changeset
   309
$\ONE$.
94
Chengsong
parents:
diff changeset
   310
That means, if we 
Chengsong
parents:
diff changeset
   311
want to prove that 
Chengsong
parents:
diff changeset
   312
\begin{center}
Chengsong
parents:
diff changeset
   313
$\textit{decode} \; \bmkeps \; \rup\backslash s \; r = \textit{decode} \; \bmkeps \; \rup\backslash_{simp} s \; r$
Chengsong
parents:
diff changeset
   314
\end{center}
Chengsong
parents:
diff changeset
   315
\noindent
Chengsong
parents:
diff changeset
   316
holds by using $\retrieve$,
Chengsong
parents:
diff changeset
   317
we probably need to prove an equality like below:
Chengsong
parents:
diff changeset
   318
\begin{center}
Chengsong
parents:
diff changeset
   319
%$\retrieve \; \rup\backslash_{simp} s \; \mkeps(r\backslash_{simp} s)=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
101
Chengsong
parents: 100
diff changeset
   320
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(f(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
94
Chengsong
parents:
diff changeset
   321
\end{center}
Chengsong
parents:
diff changeset
   322
\noindent
101
Chengsong
parents: 100
diff changeset
   323
$f$ rectifies $r\backslash s$ so the value $\mkeps(f(r\backslash s))$ becomes
Chengsong
parents: 100
diff changeset
   324
 something simpler
94
Chengsong
parents:
diff changeset
   325
to make the retrieve function defined.\\
95
Chengsong
parents: 94
diff changeset
   326
One way to do this is to prove the following:
Chengsong
parents: 94
diff changeset
   327
\begin{center}
Chengsong
parents: 94
diff changeset
   328
$\retrieve \; \rup\backslash_{simp} s \; \mkeps(\simp(r\backslash s))=\textit{retrieve} \; \rup\backslash s \; \mkeps(r\backslash s)$
Chengsong
parents: 94
diff changeset
   329
\end{center}
Chengsong
parents: 94
diff changeset
   330
\noindent
101
Chengsong
parents: 100
diff changeset
   331
The reason why we choose $\simp$ as $f$ is because
Chengsong
parents: 100
diff changeset
   332
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash \, s)$
Chengsong
parents: 100
diff changeset
   333
have the same shape:
Chengsong
parents: 100
diff changeset
   334
\begin{center}
Chengsong
parents: 100
diff changeset
   335
$\erase (\rup\backslash_{simp} \, s) = \erase(\simp(\rup\backslash s))$
Chengsong
parents: 100
diff changeset
   336
\end{center}
Chengsong
parents: 100
diff changeset
   337
Chengsong
parents: 100
diff changeset
   338
\noindent
Chengsong
parents: 100
diff changeset
   339
$\erase$ in the above equality means to remove the bit-codes
Chengsong
parents: 100
diff changeset
   340
in an annotated regular expression and only keep the original
Chengsong
parents: 100
diff changeset
   341
regular expression(just like "erasing" the bits). Its definition is omitted.
Chengsong
parents: 100
diff changeset
   342
$\rup\backslash_{simp} \, s$ and $\simp(\rup\backslash s)$
Chengsong
parents: 100
diff changeset
   343
are very closely related, but not identical.
Chengsong
parents: 100
diff changeset
   344
For example, let $r$ be the regular expression
Chengsong
parents: 100
diff changeset
   345
$(a+b)(a+a*)$ and $s$  be the string $aa$, then
Chengsong
parents: 100
diff changeset
   346
\begin{center}
Chengsong
parents: 100
diff changeset
   347
$\rup\backslash_{simp} \, s$ is equal to $_0(_0\ONE +_{11}a^*)$
Chengsong
parents: 100
diff changeset
   348
\end{center}
Chengsong
parents: 100
diff changeset
   349
\noindent
Chengsong
parents: 100
diff changeset
   350
whereas
Chengsong
parents: 100
diff changeset
   351
\begin{center}
Chengsong
parents: 100
diff changeset
   352
$\rup\backslash \, s$ is equal to $(_{00}\ONE +_{011}a^*)$
Chengsong
parents: 100
diff changeset
   353
\end{center}
Chengsong
parents: 100
diff changeset
   354
\noindent
Chengsong
parents: 100
diff changeset
   355
Two "rules" might be inferred from the above example.
Chengsong
parents: 100
diff changeset
   356
First, after erasing the bits the two regular expressions
Chengsong
parents: 100
diff changeset
   357
are exactly the same: both become $1+a^*$. Here the 
Chengsong
parents: 100
diff changeset
   358
function $\simp$ exhibits the "once equals many times"
Chengsong
parents: 100
diff changeset
   359
property: one simplification in the end causes the 
Chengsong
parents: 100
diff changeset
   360
same regular expression structure as
Chengsong
parents: 100
diff changeset
   361
 successive simplifications done alongside derivatives.
Chengsong
parents: 100
diff changeset
   362
Second, the bit-codes are different, but they are essentially
Chengsong
parents: 100
diff changeset
   363
the same: if we push the outmost bits ${\bf_0}(_0\ONE +_{11}a^*)$ of $\rup\backslash_{simp} \, s$
Chengsong
parents: 100
diff changeset
   364
inside then we get $(_{00}\ONE +_{011}a^*)$, exactly the 
Chengsong
parents: 100
diff changeset
   365
same as that of $\rup\backslash \, s$. And this difference 
Chengsong
parents: 100
diff changeset
   366
does not matter when we try to apply $\bmkeps$ or $\retrieve$
Chengsong
parents: 100
diff changeset
   367
to it.\\
Chengsong
parents: 100
diff changeset
   368
If we look into the difference above, we could see why:
Chengsong
parents: 100
diff changeset
   369
Chengsong
parents: 100
diff changeset
   370
that is to say, despite the bits are being moved around on the regular expression
Chengsong
parents: 100
diff changeset
   371
(difference in bits), the structure of the (unannotated)regular expression
Chengsong
parents: 100
diff changeset
   372
after one simplification is exactly the same after the 
Chengsong
parents: 100
diff changeset
   373
same sequence of derivative operations 
Chengsong
parents: 100
diff changeset
   374
regardless of whether we did simplification
Chengsong
parents: 100
diff changeset
   375
along the way.
Chengsong
parents: 100
diff changeset
   376
However, without erase the above equality does not hold:
Chengsong
parents: 100
diff changeset
   377
for the regular expression  
Chengsong
parents: 100
diff changeset
   378
$(a+b)(a+a*)$,
Chengsong
parents: 100
diff changeset
   379
if we do derivative with respect to string $aa$,
Chengsong
parents: 100
diff changeset
   380
we get
Chengsong
parents: 100
diff changeset
   381
%TODO
Chengsong
parents: 100
diff changeset
   382
sdddddr does not equal sdsdsdsr sometimes.\\
Chengsong
parents: 100
diff changeset
   383
For example,
Chengsong
parents: 100
diff changeset
   384
Chengsong
parents: 100
diff changeset
   385
This equicalence class method might still have the potential of proving this,
Chengsong
parents: 100
diff changeset
   386
but not yet
Chengsong
parents: 100
diff changeset
   387
i parallelly tried another method of using retrieve\\
Chengsong
parents: 100
diff changeset
   388
Chengsong
parents: 100
diff changeset
   389
Chengsong
parents: 100
diff changeset
   390
94
Chengsong
parents:
diff changeset
   391
%HERE CONSTRUCTION SITE
Chengsong
parents:
diff changeset
   392
The vsimp function, defined as follows
Chengsong
parents:
diff changeset
   393
tries to simplify the value in lockstep with 
Chengsong
parents:
diff changeset
   394
regular expression:\\
Chengsong
parents:
diff changeset
   395
Chengsong
parents:
diff changeset
   396
Chengsong
parents:
diff changeset
   397
The problem here is that 
Chengsong
parents:
diff changeset
   398
Chengsong
parents:
diff changeset
   399
we used retrieve for the key induction:
Chengsong
parents:
diff changeset
   400
$decode (retrieve (r\backslash (s @ [c])) v) r $
Chengsong
parents:
diff changeset
   401
$decode (retrieve (r\backslash s) (inj (r\backslash s) c v)) r$
Chengsong
parents:
diff changeset
   402
Here, decode recovers a value that corresponds to a match(possibly partial)
Chengsong
parents:
diff changeset
   403
from bits, and the bits are extracted by retrieve,
Chengsong
parents:
diff changeset
   404
and the key value $v$ that guides retrieve is
Chengsong
parents:
diff changeset
   405
$mkeps r\backslash s$, $inj r c (mkeps r\backslash s)$, $inj (inj (v))$, ......
Chengsong
parents:
diff changeset
   406
if we can 
Chengsong
parents:
diff changeset
   407
the problem is that 
Chengsong
parents:
diff changeset
   408
need vsiimp to make a value that is suitable for decoding
Chengsong
parents:
diff changeset
   409
$Some(flex rid(s@[c])v) = Some(flex rids(inj (r\backslash s)cv))$
Chengsong
parents:
diff changeset
   410
another way that christian came up with that might circumvent the 
Chengsong
parents:
diff changeset
   411
prblem of finding suitable value is by not stating the visimp
Chengsong
parents:
diff changeset
   412
function but include all possible value in a set that a regex is able to produce,
Chengsong
parents:
diff changeset
   413
and proving that both r and sr are able to produce the bits that correspond the POSIX value
Chengsong
parents:
diff changeset
   414
Chengsong
parents:
diff changeset
   415
produced by feeding the same initial regular expression $r$ and string $s$ to the
Chengsong
parents:
diff changeset
   416
 two functions $ders$ and $ders\_simp$.
Chengsong
parents:
diff changeset
   417
The reason why
Chengsong
parents:
diff changeset
   418
Namely, if $bmkeps( r_1) = bmkeps(r_2)$, then we 
Chengsong
parents:
diff changeset
   419
Chengsong
parents:
diff changeset
   420
Chengsong
parents:
diff changeset
   421
If we define the equivalence relation $\sim_{m\epsilon}$ between two regular expressions
Chengsong
parents:
diff changeset
   422
$r_1$ and $r_2$as follows:
Chengsong
parents:
diff changeset
   423
$r_1 \sim_{m\epsilon} r_2  \iff bmkeps(r_1)= bmkeps(r_2)$
Chengsong
parents:
diff changeset
   424
(in other words, they $r1$ and $r2$ produce the same output under the function $bmkeps$.)
Chengsong
parents:
diff changeset
   425
Then the first goal 
Chengsong
parents:
diff changeset
   426
might be restated as 
Chengsong
parents:
diff changeset
   427
$(r^\uparrow)\backslash_{simp}\, s  \sim_{m\epsilon} (r^\uparrow)\backslash s$.
Chengsong
parents:
diff changeset
   428
I tried to establish an equivalence relation between the regular experssions 
Chengsong
parents:
diff changeset
   429
like dddr dddsr,.....
Chengsong
parents:
diff changeset
   430
but right now i am only able to establish dsr and dr, using structural induction on r.
Chengsong
parents:
diff changeset
   431
Those involve multiple derivative operations are harder to prove.
Chengsong
parents:
diff changeset
   432
Two attempts have been made:
Chengsong
parents:
diff changeset
   433
(1)induction on the number of der operations(or in other words, the length of the string s),
Chengsong
parents:
diff changeset
   434
the inductive hypothesis was initially specified as 
Chengsong
parents:
diff changeset
   435
"For an arbitrary regular expression r, 
Chengsong
parents:
diff changeset
   436
For all string s in the language of r whose length do not exceed 
Chengsong
parents:
diff changeset
   437
the number n, ders s r me derssimp s r"
Chengsong
parents:
diff changeset
   438
and the proof goal may be stated as
Chengsong
parents:
diff changeset
   439
"For an arbitrary regular expression r, 
Chengsong
parents:
diff changeset
   440
For all string s in the language of r whose length do not exceed 
Chengsong
parents:
diff changeset
   441
the number n+1, ders s r me derssimp s r"
Chengsong
parents:
diff changeset
   442
the problem here is that although we can easily break down
Chengsong
parents:
diff changeset
   443
a string s of length n+1 into s1@list(c), it is not that easy
Chengsong
parents:
diff changeset
   444
to use the i.h. as a stepping stone to prove anything because s1 may well be not
Chengsong
parents:
diff changeset
   445
in the language L(r). This inhibits us from obtaining the fact that
Chengsong
parents:
diff changeset
   446
ders s1 r me derssimps s1 r.
Chengsong
parents:
diff changeset
   447
Further exploration is needed to amend this hypothesis so it includes the
Chengsong
parents:
diff changeset
   448
situation when s1 is not nullable.
Chengsong
parents:
diff changeset
   449
For example, what information(bits? 
Chengsong
parents:
diff changeset
   450
values?) can be extracted
Chengsong
parents:
diff changeset
   451
from the regular expression ders(s1,r) so that we can compute or predict the possible 
Chengsong
parents:
diff changeset
   452
result of bmkeps after another derivative operation. What function f can used to 
Chengsong
parents:
diff changeset
   453
carry out the task? The possible way of exploration can be 
Chengsong
parents:
diff changeset
   454
more directly perceived throught the graph below:
Chengsong
parents:
diff changeset
   455
find a function
Chengsong
parents:
diff changeset
   456
f
Chengsong
parents:
diff changeset
   457
such that
Chengsong
parents:
diff changeset
   458
f(bders s1 r)
Chengsong
parents:
diff changeset
   459
= re1
Chengsong
parents:
diff changeset
   460
f(bderss s1 r)
Chengsong
parents:
diff changeset
   461
= re2
Chengsong
parents:
diff changeset
   462
bmkeps(bders s r) = g(re1,c)
Chengsong
parents:
diff changeset
   463
bmkeps(bderssimp s r) = g(re2,c)
Chengsong
parents:
diff changeset
   464
and g(re1,c) = g(re2,c)
Chengsong
parents:
diff changeset
   465
The inductive hypothesis would be
Chengsong
parents:
diff changeset
   466
"For all strings s1 of length <= n, 
Chengsong
parents:
diff changeset
   467
f(bders s1 r)
Chengsong
parents:
diff changeset
   468
= re1
Chengsong
parents:
diff changeset
   469
f(bderss s1 r)
Chengsong
parents:
diff changeset
   470
= re2"
Chengsong
parents:
diff changeset
   471
proving this would be a lemma for the main proof:
Chengsong
parents:
diff changeset
   472
the main proof would be 
Chengsong
parents:
diff changeset
   473
"
Chengsong
parents:
diff changeset
   474
bmkeps(bders s r) = g(re1,c)
Chengsong
parents:
diff changeset
   475
bmkeps(bderssimp s r) = g(re2,c)
Chengsong
parents:
diff changeset
   476
for s = s1@c
Chengsong
parents:
diff changeset
   477
"
Chengsong
parents:
diff changeset
   478
and f need to be a recursive property for the lemma to be proved:
Chengsong
parents:
diff changeset
   479
it needs to store not only the "after one char nullable info",
Chengsong
parents:
diff changeset
   480
but also the "after two char nullable info",
Chengsong
parents:
diff changeset
   481
and so on so that it is able to  predict what f will compute after a derivative operation,
Chengsong
parents:
diff changeset
   482
in other words, it needs to be "infinitely recursive"\\
Chengsong
parents:
diff changeset
   483
To prove the lemma, in other words, to get
Chengsong
parents:
diff changeset
   484
"For all strings s1 of length <= n+1, 
Chengsong
parents:
diff changeset
   485
f(bders s1 r)
Chengsong
parents:
diff changeset
   486
= re3
Chengsong
parents:
diff changeset
   487
f(bderss s1 r)
Chengsong
parents:
diff changeset
   488
= re4"\\
Chengsong
parents:
diff changeset
   489
from\\
Chengsong
parents:
diff changeset
   490
"For all strings s1 of length <= n, 
Chengsong
parents:
diff changeset
   491
f(bders s1 r)
Chengsong
parents:
diff changeset
   492
= re1
Chengsong
parents:
diff changeset
   493
f(bderss s1 r)
Chengsong
parents:
diff changeset
   494
= re2"\\
Chengsong
parents:
diff changeset
   495
it might be best to construct an auxiliary function h such that\\
Chengsong
parents:
diff changeset
   496
h(re1, c) = re3\\
Chengsong
parents:
diff changeset
   497
h(re2, c) = re4\\
Chengsong
parents:
diff changeset
   498
and re3 = f(bder c (bders s1 r))\\
Chengsong
parents:
diff changeset
   499
re4 = f(simp(bder c (bderss s1 r)))
Chengsong
parents:
diff changeset
   500
The key point here is that we are not satisfied with what bders s r will produce under
Chengsong
parents:
diff changeset
   501
bmkeps, but also how it will perform after a derivative operation and then bmkeps, and two 
Chengsong
parents:
diff changeset
   502
derivative operations and so on. In essence, we are preserving the regular expression 
Chengsong
parents:
diff changeset
   503
itself under the function f, in a less compact way than the regluar expression: we are
Chengsong
parents:
diff changeset
   504
not just recording but also interpreting what the regular expression matches.
Chengsong
parents:
diff changeset
   505
In other words, we need to prove the properties of bderss s r beyond the bmkeps result,
Chengsong
parents:
diff changeset
   506
i.e., not just the nullable ones, but also those containing remaining characters.\\
Chengsong
parents:
diff changeset
   507
(2)we observed the fact that 
Chengsong
parents:
diff changeset
   508
erase sdddddr= erase sdsdsdsr
Chengsong
parents:
diff changeset
   509
that is to say, despite the bits are being moved around on the regular expression
Chengsong
parents:
diff changeset
   510
(difference in bits), the structure of the (unannotated)regular expression
Chengsong
parents:
diff changeset
   511
after one simplification is exactly the same after the 
Chengsong
parents:
diff changeset
   512
same sequence of derivative operations 
Chengsong
parents:
diff changeset
   513
regardless of whether we did simplification
Chengsong
parents:
diff changeset
   514
along the way.
Chengsong
parents:
diff changeset
   515
However, without erase the above equality does not hold:
Chengsong
parents:
diff changeset
   516
for the regular expression  
Chengsong
parents:
diff changeset
   517
$(a+b)(a+a*)$,
Chengsong
parents:
diff changeset
   518
if we do derivative with respect to string $aa$,
Chengsong
parents:
diff changeset
   519
we get
Chengsong
parents:
diff changeset
   520
%TODO
Chengsong
parents:
diff changeset
   521
sdddddr does not equal sdsdsdsr sometimes.\\
Chengsong
parents:
diff changeset
   522
For example,
Chengsong
parents:
diff changeset
   523
Chengsong
parents:
diff changeset
   524
This equicalence class method might still have the potential of proving this,
Chengsong
parents:
diff changeset
   525
but not yet
Chengsong
parents:
diff changeset
   526
i parallelly tried another method of using retrieve\\
Chengsong
parents:
diff changeset
   527
Chengsong
parents:
diff changeset
   528
Chengsong
parents:
diff changeset
   529
Chengsong
parents:
diff changeset
   530
\noindent\rule[0.5ex]{\linewidth}{1pt}
Chengsong
parents:
diff changeset
   531
Chengsong
parents:
diff changeset
   532
This PhD-project is about regular expression matching and
Chengsong
parents:
diff changeset
   533
lexing. Given the maturity of this topic, the reader might wonder:
Chengsong
parents:
diff changeset
   534
Surely, regular expressions must have already been studied to death?
Chengsong
parents:
diff changeset
   535
What could possibly be \emph{not} known in this area? And surely all
Chengsong
parents:
diff changeset
   536
implemented algorithms for regular expression matching are blindingly
Chengsong
parents:
diff changeset
   537
fast?
Chengsong
parents:
diff changeset
   538
Chengsong
parents:
diff changeset
   539
Unfortunately these preconceptions are not supported by evidence: Take
Chengsong
parents:
diff changeset
   540
for example the regular expression $(a^*)^*\,b$ and ask whether
Chengsong
parents:
diff changeset
   541
strings of the form $aa..a$ match this regular
Chengsong
parents:
diff changeset
   542
expression. Obviously this is not the case---the expected $b$ in the last
Chengsong
parents:
diff changeset
   543
position is missing. One would expect that modern regular expression
Chengsong
parents:
diff changeset
   544
matching engines can find this out very quickly. Alas, if one tries
Chengsong
parents:
diff changeset
   545
this example in JavaScript, Python or Java 8 with strings like 28
Chengsong
parents:
diff changeset
   546
$a$'s, one discovers that this decision takes around 30 seconds and
Chengsong
parents:
diff changeset
   547
takes considerably longer when adding a few more $a$'s, as the graphs
Chengsong
parents:
diff changeset
   548
below show:
Chengsong
parents:
diff changeset
   549
Chengsong
parents:
diff changeset
   550
\begin{center}
Chengsong
parents:
diff changeset
   551
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
Chengsong
parents:
diff changeset
   552
\begin{tikzpicture}
Chengsong
parents:
diff changeset
   553
\begin{axis}[
Chengsong
parents:
diff changeset
   554
    xlabel={$n$},
Chengsong
parents:
diff changeset
   555
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
   556
    ylabel={time in secs},
Chengsong
parents:
diff changeset
   557
    enlargelimits=false,
Chengsong
parents:
diff changeset
   558
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
   559
    xmax=33,
Chengsong
parents:
diff changeset
   560
    ymax=35,
Chengsong
parents:
diff changeset
   561
    ytick={0,5,...,30},
Chengsong
parents:
diff changeset
   562
    scaled ticks=false,
Chengsong
parents:
diff changeset
   563
    axis lines=left,
Chengsong
parents:
diff changeset
   564
    width=5cm,
Chengsong
parents:
diff changeset
   565
    height=4cm, 
Chengsong
parents:
diff changeset
   566
    legend entries={JavaScript},  
Chengsong
parents:
diff changeset
   567
    legend pos=north west,
Chengsong
parents:
diff changeset
   568
    legend cell align=left]
Chengsong
parents:
diff changeset
   569
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
Chengsong
parents:
diff changeset
   570
\end{axis}
Chengsong
parents:
diff changeset
   571
\end{tikzpicture}
Chengsong
parents:
diff changeset
   572
  &
Chengsong
parents:
diff changeset
   573
\begin{tikzpicture}
Chengsong
parents:
diff changeset
   574
\begin{axis}[
Chengsong
parents:
diff changeset
   575
    xlabel={$n$},
Chengsong
parents:
diff changeset
   576
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
   577
    %ylabel={time in secs},
Chengsong
parents:
diff changeset
   578
    enlargelimits=false,
Chengsong
parents:
diff changeset
   579
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
   580
    xmax=33,
Chengsong
parents:
diff changeset
   581
    ymax=35,
Chengsong
parents:
diff changeset
   582
    ytick={0,5,...,30},
Chengsong
parents:
diff changeset
   583
    scaled ticks=false,
Chengsong
parents:
diff changeset
   584
    axis lines=left,
Chengsong
parents:
diff changeset
   585
    width=5cm,
Chengsong
parents:
diff changeset
   586
    height=4cm, 
Chengsong
parents:
diff changeset
   587
    legend entries={Python},  
Chengsong
parents:
diff changeset
   588
    legend pos=north west,
Chengsong
parents:
diff changeset
   589
    legend cell align=left]
Chengsong
parents:
diff changeset
   590
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
Chengsong
parents:
diff changeset
   591
\end{axis}
Chengsong
parents:
diff changeset
   592
\end{tikzpicture}
Chengsong
parents:
diff changeset
   593
  &
Chengsong
parents:
diff changeset
   594
\begin{tikzpicture}
Chengsong
parents:
diff changeset
   595
\begin{axis}[
Chengsong
parents:
diff changeset
   596
    xlabel={$n$},
Chengsong
parents:
diff changeset
   597
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
   598
    %ylabel={time in secs},
Chengsong
parents:
diff changeset
   599
    enlargelimits=false,
Chengsong
parents:
diff changeset
   600
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
   601
    xmax=33,
Chengsong
parents:
diff changeset
   602
    ymax=35,
Chengsong
parents:
diff changeset
   603
    ytick={0,5,...,30},
Chengsong
parents:
diff changeset
   604
    scaled ticks=false,
Chengsong
parents:
diff changeset
   605
    axis lines=left,
Chengsong
parents:
diff changeset
   606
    width=5cm,
Chengsong
parents:
diff changeset
   607
    height=4cm, 
Chengsong
parents:
diff changeset
   608
    legend entries={Java 8},  
Chengsong
parents:
diff changeset
   609
    legend pos=north west,
Chengsong
parents:
diff changeset
   610
    legend cell align=left]
Chengsong
parents:
diff changeset
   611
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
Chengsong
parents:
diff changeset
   612
\end{axis}
Chengsong
parents:
diff changeset
   613
\end{tikzpicture}\\
Chengsong
parents:
diff changeset
   614
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
Chengsong
parents:
diff changeset
   615
           of the form $\underbrace{aa..a}_{n}$.}
Chengsong
parents:
diff changeset
   616
\end{tabular}    
Chengsong
parents:
diff changeset
   617
\end{center}  
Chengsong
parents:
diff changeset
   618
Chengsong
parents:
diff changeset
   619
\noindent These are clearly abysmal and possibly surprising results. One
Chengsong
parents:
diff changeset
   620
would expect these systems to do  much better than that---after all,
Chengsong
parents:
diff changeset
   621
given a DFA and a string, deciding whether a string is matched by this
Chengsong
parents:
diff changeset
   622
DFA should be linear in terms of the size of the regular expression and
Chengsong
parents:
diff changeset
   623
the string?
Chengsong
parents:
diff changeset
   624
Chengsong
parents:
diff changeset
   625
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to
Chengsong
parents:
diff changeset
   626
exhibit this super-linear behaviour.  But unfortunately, such regular
Chengsong
parents:
diff changeset
   627
expressions are not just a few outliers. They are actually 
Chengsong
parents:
diff changeset
   628
frequent enough to have a separate name created for
Chengsong
parents:
diff changeset
   629
them---\emph{evil regular expressions}. In empiric work, Davis et al
Chengsong
parents:
diff changeset
   630
report that they have found thousands of such evil regular expressions
Chengsong
parents:
diff changeset
   631
in the JavaScript and Python ecosystems \cite{Davis18}. Static analysis
Chengsong
parents:
diff changeset
   632
approach that is both sound and complete exists\cite{17Bir}, but the running 
Chengsong
parents:
diff changeset
   633
time on certain examples in the RegExLib and Snort regular expressions
Chengsong
parents:
diff changeset
   634
libraries is unacceptable. Therefore the problem of efficiency still remains.
Chengsong
parents:
diff changeset
   635
Chengsong
parents:
diff changeset
   636
This superlinear blowup in matching algorithms sometimes causes
Chengsong
parents:
diff changeset
   637
considerable grief in real life: for example on 20 July 2016 one evil
Chengsong
parents:
diff changeset
   638
regular expression brought the webpage
Chengsong
parents:
diff changeset
   639
\href{http://stackexchange.com}{Stack Exchange} to its
Chengsong
parents:
diff changeset
   640
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}}
Chengsong
parents:
diff changeset
   641
In this instance, a regular expression intended to just trim white
Chengsong
parents:
diff changeset
   642
spaces from the beginning and the end of a line actually consumed
Chengsong
parents:
diff changeset
   643
massive amounts of CPU-resources---causing web servers to grind to a
Chengsong
parents:
diff changeset
   644
halt. This happened when a post with 20,000 white spaces was submitted,
Chengsong
parents:
diff changeset
   645
but importantly the white spaces were neither at the beginning nor at
Chengsong
parents:
diff changeset
   646
the end. As a result, the regular expression matching engine needed to
Chengsong
parents:
diff changeset
   647
backtrack over many choices. In this example, the time needed to process
Chengsong
parents:
diff changeset
   648
the string was $O(n^2)$ with respect to the string length. This
Chengsong
parents:
diff changeset
   649
quadratic overhead was enough for the homepage of Stack Exchange to
Chengsong
parents:
diff changeset
   650
respond so slowly that the load balancer assumed there must be some
Chengsong
parents:
diff changeset
   651
attack and therefore stopped the servers from responding to any
Chengsong
parents:
diff changeset
   652
requests. This made the whole site become unavailable. Another very
Chengsong
parents:
diff changeset
   653
recent example is a global outage of all Cloudflare servers on 2 July
Chengsong
parents:
diff changeset
   654
2019. A poorly written regular expression exhibited exponential
Chengsong
parents:
diff changeset
   655
behaviour and exhausted CPUs that serve HTTP traffic. Although the
Chengsong
parents:
diff changeset
   656
outage had several causes, at the heart was a regular expression that
Chengsong
parents:
diff changeset
   657
was used to monitor network
Chengsong
parents:
diff changeset
   658
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}}
Chengsong
parents:
diff changeset
   659
Chengsong
parents:
diff changeset
   660
The underlying problem is that many ``real life'' regular expression
Chengsong
parents:
diff changeset
   661
matching engines do not use DFAs for matching. This is because they
Chengsong
parents:
diff changeset
   662
support regular expressions that are not covered by the classical
Chengsong
parents:
diff changeset
   663
automata theory, and in this more general setting there are quite a few
Chengsong
parents:
diff changeset
   664
research questions still unanswered and fast algorithms still need to be
Chengsong
parents:
diff changeset
   665
developed (for example how to treat efficiently bounded repetitions, negation and
Chengsong
parents:
diff changeset
   666
back-references).
Chengsong
parents:
diff changeset
   667
%question: dfa can have exponential states. isn't this the actual reason why they do not use dfas?
Chengsong
parents:
diff changeset
   668
%how do they avoid dfas exponential states if they use them for fast matching?
Chengsong
parents:
diff changeset
   669
Chengsong
parents:
diff changeset
   670
There is also another under-researched problem to do with regular
Chengsong
parents:
diff changeset
   671
expressions and lexing, i.e.~the process of breaking up strings into
Chengsong
parents:
diff changeset
   672
sequences of tokens according to some regular expressions. In this
Chengsong
parents:
diff changeset
   673
setting one is not just interested in whether or not a regular
Chengsong
parents:
diff changeset
   674
expression matches a string, but also in \emph{how}.  Consider for
Chengsong
parents:
diff changeset
   675
example a regular expression $r_{key}$ for recognising keywords such as
Chengsong
parents:
diff changeset
   676
\textit{if}, \textit{then} and so on; and a regular expression $r_{id}$
Chengsong
parents:
diff changeset
   677
for recognising identifiers (say, a single character followed by
Chengsong
parents:
diff changeset
   678
characters or numbers). One can then form the compound regular
Chengsong
parents:
diff changeset
   679
expression $(r_{key} + r_{id})^*$ and use it to tokenise strings.  But
Chengsong
parents:
diff changeset
   680
then how should the string \textit{iffoo} be tokenised?  It could be
Chengsong
parents:
diff changeset
   681
tokenised as a keyword followed by an identifier, or the entire string
Chengsong
parents:
diff changeset
   682
as a single identifier.  Similarly, how should the string \textit{if} be
Chengsong
parents:
diff changeset
   683
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would
Chengsong
parents:
diff changeset
   684
``fire''---so is it an identifier or a keyword?  While in applications
Chengsong
parents:
diff changeset
   685
there is a well-known strategy to decide these questions, called POSIX
Chengsong
parents:
diff changeset
   686
matching, only relatively recently precise definitions of what POSIX
Chengsong
parents:
diff changeset
   687
matching actually means have been formalised
Chengsong
parents:
diff changeset
   688
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. Such a
Chengsong
parents:
diff changeset
   689
definition has also been given by Sulzmann and  Lu \cite{Sulzmann2014},
Chengsong
parents:
diff changeset
   690
but the corresponding correctness proof turned out to be  faulty
Chengsong
parents:
diff changeset
   691
\cite{AusafDyckhoffUrban2016}. Roughly, POSIX matching means matching
Chengsong
parents:
diff changeset
   692
the longest initial substring. In the case of a tie, the initial
Chengsong
parents:
diff changeset
   693
sub-match is chosen according to some priorities attached to the regular
Chengsong
parents:
diff changeset
   694
expressions (e.g.~keywords have a higher priority than identifiers).
Chengsong
parents:
diff changeset
   695
This sounds rather simple, but according to Grathwohl et al \cite[Page
Chengsong
parents:
diff changeset
   696
36]{CrashCourse2014} this is not the case. They wrote:
Chengsong
parents:
diff changeset
   697
Chengsong
parents:
diff changeset
   698
\begin{quote}
Chengsong
parents:
diff changeset
   699
\it{}``The POSIX strategy is more complicated than the greedy because of 
Chengsong
parents:
diff changeset
   700
the dependence on information about the length of matched strings in the 
Chengsong
parents:
diff changeset
   701
various subexpressions.''
Chengsong
parents:
diff changeset
   702
\end{quote}
Chengsong
parents:
diff changeset
   703
Chengsong
parents:
diff changeset
   704
\noindent
Chengsong
parents:
diff changeset
   705
This is also supported by evidence collected by Kuklewicz
Chengsong
parents:
diff changeset
   706
\cite{Kuklewicz} who noticed that a number of POSIX regular expression
Chengsong
parents:
diff changeset
   707
matchers calculate incorrect results.
Chengsong
parents:
diff changeset
   708
Chengsong
parents:
diff changeset
   709
Our focus in this project is on an algorithm introduced by Sulzmann and
Chengsong
parents:
diff changeset
   710
Lu in 2014 for regular expression matching according to the POSIX
Chengsong
parents:
diff changeset
   711
strategy \cite{Sulzmann2014}. Their algorithm is based on an older
Chengsong
parents:
diff changeset
   712
algorithm by Brzozowski from 1964 where he introduced the notion of
Chengsong
parents:
diff changeset
   713
derivatives of regular expressions~\cite{Brzozowski1964}. We shall
Chengsong
parents:
diff changeset
   714
briefly explain this algorithm next.
Chengsong
parents:
diff changeset
   715
Chengsong
parents:
diff changeset
   716
\section{The Algorithm by Brzozowski based on Derivatives of Regular
Chengsong
parents:
diff changeset
   717
Expressions}
Chengsong
parents:
diff changeset
   718
Chengsong
parents:
diff changeset
   719
Suppose (basic) regular expressions are given by the following grammar:
Chengsong
parents:
diff changeset
   720
\[			r ::=   \ZERO \mid  \ONE
Chengsong
parents:
diff changeset
   721
			 \mid  c  
Chengsong
parents:
diff changeset
   722
			 \mid  r_1 \cdot r_2
Chengsong
parents:
diff changeset
   723
			 \mid  r_1 + r_2   
Chengsong
parents:
diff changeset
   724
			 \mid r^*         
Chengsong
parents:
diff changeset
   725
\]
Chengsong
parents:
diff changeset
   726
Chengsong
parents:
diff changeset
   727
\noindent
Chengsong
parents:
diff changeset
   728
The intended meaning of the constructors is as follows: $\ZERO$
Chengsong
parents:
diff changeset
   729
cannot match any string, $\ONE$ can match the empty string, the
Chengsong
parents:
diff changeset
   730
character regular expression $c$ can match the character $c$, and so
Chengsong
parents:
diff changeset
   731
on.
Chengsong
parents:
diff changeset
   732
Chengsong
parents:
diff changeset
   733
The ingenious contribution by Brzozowski is the notion of
Chengsong
parents:
diff changeset
   734
\emph{derivatives} of regular expressions.  The idea behind this
Chengsong
parents:
diff changeset
   735
notion is as follows: suppose a regular expression $r$ can match a
Chengsong
parents:
diff changeset
   736
string of the form $c\!::\! s$ (that is a list of characters starting
Chengsong
parents:
diff changeset
   737
with $c$), what does the regular expression look like that can match
Chengsong
parents:
diff changeset
   738
just $s$? Brzozowski gave a neat answer to this question. He started
Chengsong
parents:
diff changeset
   739
with the definition of $nullable$:
Chengsong
parents:
diff changeset
   740
\begin{center}
Chengsong
parents:
diff changeset
   741
		\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   742
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
Chengsong
parents:
diff changeset
   743
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
Chengsong
parents:
diff changeset
   744
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
Chengsong
parents:
diff changeset
   745
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
Chengsong
parents:
diff changeset
   746
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
Chengsong
parents:
diff changeset
   747
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
Chengsong
parents:
diff changeset
   748
		\end{tabular}
Chengsong
parents:
diff changeset
   749
	\end{center}
Chengsong
parents:
diff changeset
   750
This function simply tests whether the empty string is in $L(r)$.
Chengsong
parents:
diff changeset
   751
He then defined
Chengsong
parents:
diff changeset
   752
the following operation on regular expressions, written
Chengsong
parents:
diff changeset
   753
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$):
Chengsong
parents:
diff changeset
   754
Chengsong
parents:
diff changeset
   755
\begin{center}
Chengsong
parents:
diff changeset
   756
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   757
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents:
diff changeset
   758
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
Chengsong
parents:
diff changeset
   759
		$d \backslash c$     & $\dn$ & 
Chengsong
parents:
diff changeset
   760
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
Chengsong
parents:
diff changeset
   761
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
Chengsong
parents:
diff changeset
   762
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
Chengsong
parents:
diff changeset
   763
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
Chengsong
parents:
diff changeset
   764
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
Chengsong
parents:
diff changeset
   765
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
Chengsong
parents:
diff changeset
   766
\end{tabular}
Chengsong
parents:
diff changeset
   767
\end{center}
Chengsong
parents:
diff changeset
   768
Chengsong
parents:
diff changeset
   769
%Assuming the classic notion of a
Chengsong
parents:
diff changeset
   770
%\emph{language} of a regular expression, written $L(\_)$, t
Chengsong
parents:
diff changeset
   771
Chengsong
parents:
diff changeset
   772
\noindent
Chengsong
parents:
diff changeset
   773
The main property of the derivative operation is that
Chengsong
parents:
diff changeset
   774
Chengsong
parents:
diff changeset
   775
\begin{center}
Chengsong
parents:
diff changeset
   776
$c\!::\!s \in L(r)$ holds
Chengsong
parents:
diff changeset
   777
if and only if $s \in L(r\backslash c)$.
Chengsong
parents:
diff changeset
   778
\end{center}
Chengsong
parents:
diff changeset
   779
Chengsong
parents:
diff changeset
   780
\noindent
Chengsong
parents:
diff changeset
   781
For us the main advantage is that derivatives can be
Chengsong
parents:
diff changeset
   782
straightforwardly implemented in any functional programming language,
Chengsong
parents:
diff changeset
   783
and are easily definable and reasoned about in theorem provers---the
Chengsong
parents:
diff changeset
   784
definitions just consist of inductive datatypes and simple recursive
Chengsong
parents:
diff changeset
   785
functions. Moreover, the notion of derivatives can be easily
Chengsong
parents:
diff changeset
   786
generalised to cover extended regular expression constructors such as
Chengsong
parents:
diff changeset
   787
the not-regular expression, written $\neg\,r$, or bounded repetitions
Chengsong
parents:
diff changeset
   788
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so
Chengsong
parents:
diff changeset
   789
straightforwardly realised within the classic automata approach.
Chengsong
parents:
diff changeset
   790
For the moment however, we focus only on the usual basic regular expressions.
Chengsong
parents:
diff changeset
   791
Chengsong
parents:
diff changeset
   792
Chengsong
parents:
diff changeset
   793
Now if we want to find out whether a string $s$ matches with a regular
Chengsong
parents:
diff changeset
   794
expression $r$, we can build the derivatives of $r$ w.r.t.\ (in succession)
Chengsong
parents:
diff changeset
   795
all the characters of the string $s$. Finally, test whether the
Chengsong
parents:
diff changeset
   796
resulting regular expression can match the empty string.  If yes, then
Chengsong
parents:
diff changeset
   797
$r$ matches $s$, and no in the negative case. To implement this idea
Chengsong
parents:
diff changeset
   798
we can generalise the derivative operation to strings like this:
Chengsong
parents:
diff changeset
   799
Chengsong
parents:
diff changeset
   800
\begin{center}
Chengsong
parents:
diff changeset
   801
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   802
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
Chengsong
parents:
diff changeset
   803
$r \backslash [\,] $ & $\dn$ & $r$
Chengsong
parents:
diff changeset
   804
\end{tabular}
Chengsong
parents:
diff changeset
   805
\end{center}
Chengsong
parents:
diff changeset
   806
Chengsong
parents:
diff changeset
   807
\noindent
Chengsong
parents:
diff changeset
   808
and then define as  regular-expression matching algorithm: 
Chengsong
parents:
diff changeset
   809
\[
Chengsong
parents:
diff changeset
   810
match\;s\;r \;\dn\; nullable(r\backslash s)
Chengsong
parents:
diff changeset
   811
\]
Chengsong
parents:
diff changeset
   812
Chengsong
parents:
diff changeset
   813
\noindent
Chengsong
parents:
diff changeset
   814
This algorithm looks graphically as follows:
Chengsong
parents:
diff changeset
   815
\begin{equation}\label{graph:*}
Chengsong
parents:
diff changeset
   816
\begin{tikzcd}
Chengsong
parents:
diff changeset
   817
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed]  & r_n  \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO}
Chengsong
parents:
diff changeset
   818
\end{tikzcd}
Chengsong
parents:
diff changeset
   819
\end{equation}
Chengsong
parents:
diff changeset
   820
Chengsong
parents:
diff changeset
   821
\noindent
Chengsong
parents:
diff changeset
   822
where we start with  a regular expression  $r_0$, build successive
Chengsong
parents:
diff changeset
   823
derivatives until we exhaust the string and then use \textit{nullable}
Chengsong
parents:
diff changeset
   824
to test whether the result can match the empty string. It can  be
Chengsong
parents:
diff changeset
   825
relatively  easily shown that this matcher is correct  (that is given
Chengsong
parents:
diff changeset
   826
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
Chengsong
parents:
diff changeset
   827
Chengsong
parents:
diff changeset
   828
 
Chengsong
parents:
diff changeset
   829
\section{Values and the Algorithm by Sulzmann and Lu}
Chengsong
parents:
diff changeset
   830
Chengsong
parents:
diff changeset
   831
One limitation of Brzozowski's algorithm is that it only produces a
Chengsong
parents:
diff changeset
   832
YES/NO answer for whether a string is being matched by a regular
Chengsong
parents:
diff changeset
   833
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm
Chengsong
parents:
diff changeset
   834
to allow generation of an actual matching, called a \emph{value} or
Chengsong
parents:
diff changeset
   835
sometimes also \emph{lexical value}.  These values and regular
Chengsong
parents:
diff changeset
   836
expressions correspond to each other as illustrated in the following
Chengsong
parents:
diff changeset
   837
table:
Chengsong
parents:
diff changeset
   838
Chengsong
parents:
diff changeset
   839
Chengsong
parents:
diff changeset
   840
\begin{center}
Chengsong
parents:
diff changeset
   841
	\begin{tabular}{c@{\hspace{20mm}}c}
Chengsong
parents:
diff changeset
   842
		\begin{tabular}{@{}rrl@{}}
Chengsong
parents:
diff changeset
   843
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
Chengsong
parents:
diff changeset
   844
			$r$ & $::=$  & $\ZERO$\\
Chengsong
parents:
diff changeset
   845
			& $\mid$ & $\ONE$   \\
Chengsong
parents:
diff changeset
   846
			& $\mid$ & $c$          \\
Chengsong
parents:
diff changeset
   847
			& $\mid$ & $r_1 \cdot r_2$\\
Chengsong
parents:
diff changeset
   848
			& $\mid$ & $r_1 + r_2$   \\
Chengsong
parents:
diff changeset
   849
			\\
Chengsong
parents:
diff changeset
   850
			& $\mid$ & $r^*$         \\
Chengsong
parents:
diff changeset
   851
		\end{tabular}
Chengsong
parents:
diff changeset
   852
		&
Chengsong
parents:
diff changeset
   853
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
Chengsong
parents:
diff changeset
   854
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
Chengsong
parents:
diff changeset
   855
			$v$ & $::=$  & \\
Chengsong
parents:
diff changeset
   856
			&        & $\Empty$   \\
Chengsong
parents:
diff changeset
   857
			& $\mid$ & $\Char(c)$          \\
Chengsong
parents:
diff changeset
   858
			& $\mid$ & $\Seq\,v_1\, v_2$\\
Chengsong
parents:
diff changeset
   859
			& $\mid$ & $\Left(v)$   \\
Chengsong
parents:
diff changeset
   860
			& $\mid$ & $\Right(v)$  \\
Chengsong
parents:
diff changeset
   861
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
Chengsong
parents:
diff changeset
   862
		\end{tabular}
Chengsong
parents:
diff changeset
   863
	\end{tabular}
Chengsong
parents:
diff changeset
   864
\end{center}
Chengsong
parents:
diff changeset
   865
Chengsong
parents:
diff changeset
   866
\noindent
Chengsong
parents:
diff changeset
   867
No value  corresponds to $\ZERO$; $\Empty$ corresponds to $\ONE$;
Chengsong
parents:
diff changeset
   868
$\Char$ to the character regular expression; $\Seq$ to the sequence
Chengsong
parents:
diff changeset
   869
regular expression and so on. The idea of values is to encode a kind of
Chengsong
parents:
diff changeset
   870
lexical value for how the sub-parts of a regular expression match the
Chengsong
parents:
diff changeset
   871
sub-parts of a string. To see this, suppose a \emph{flatten} operation,
Chengsong
parents:
diff changeset
   872
written $|v|$ for values. We can use this function to extract the
Chengsong
parents:
diff changeset
   873
underlying string of a value $v$. For example, $|\mathit{Seq} \,
Chengsong
parents:
diff changeset
   874
(\textit{Char x}) \, (\textit{Char y})|$ is the string $xy$.  Using
Chengsong
parents:
diff changeset
   875
flatten, we can describe how values encode lexical values: $\Seq\,v_1\,
Chengsong
parents:
diff changeset
   876
v_2$ encodes a tree with two children nodes that tells how the string
Chengsong
parents:
diff changeset
   877
$|v_1| @ |v_2|$ matches the regex $r_1 \cdot r_2$ whereby $r_1$ matches
Chengsong
parents:
diff changeset
   878
the substring $|v_1|$ and, respectively, $r_2$ matches the substring
Chengsong
parents:
diff changeset
   879
$|v_2|$. Exactly how these two are matched is contained in the children
Chengsong
parents:
diff changeset
   880
nodes $v_1$ and $v_2$ of parent $\textit{Seq}$. 
Chengsong
parents:
diff changeset
   881
Chengsong
parents:
diff changeset
   882
To give a concrete example of how values work, consider the string $xy$
Chengsong
parents:
diff changeset
   883
and the regular expression $(x + (y + xy))^*$. We can view this regular
Chengsong
parents:
diff changeset
   884
expression as a tree and if the string $xy$ is matched by two Star
Chengsong
parents:
diff changeset
   885
``iterations'', then the $x$ is matched by the left-most alternative in
Chengsong
parents:
diff changeset
   886
this tree and the $y$ by the right-left alternative. This suggests to
Chengsong
parents:
diff changeset
   887
record this matching as
Chengsong
parents:
diff changeset
   888
Chengsong
parents:
diff changeset
   889
\begin{center}
Chengsong
parents:
diff changeset
   890
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$
Chengsong
parents:
diff changeset
   891
\end{center}
Chengsong
parents:
diff changeset
   892
Chengsong
parents:
diff changeset
   893
\noindent
Chengsong
parents:
diff changeset
   894
where $\Stars \; [\ldots]$ records all the
Chengsong
parents:
diff changeset
   895
iterations; and $\Left$, respectively $\Right$, which
Chengsong
parents:
diff changeset
   896
alternative is used. The value for
Chengsong
parents:
diff changeset
   897
matching $xy$ in a single ``iteration'', i.e.~the POSIX value,
Chengsong
parents:
diff changeset
   898
would look as follows
Chengsong
parents:
diff changeset
   899
Chengsong
parents:
diff changeset
   900
\begin{center}
Chengsong
parents:
diff changeset
   901
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$
Chengsong
parents:
diff changeset
   902
\end{center}
Chengsong
parents:
diff changeset
   903
Chengsong
parents:
diff changeset
   904
\noindent
Chengsong
parents:
diff changeset
   905
where $\Stars$ has only a single-element list for the single iteration
Chengsong
parents:
diff changeset
   906
and $\Seq$ indicates that $xy$ is matched by a sequence regular
Chengsong
parents:
diff changeset
   907
expression.
Chengsong
parents:
diff changeset
   908
Chengsong
parents:
diff changeset
   909
The contribution of Sulzmann and Lu is an extension of Brzozowski's
Chengsong
parents:
diff changeset
   910
algorithm by a second phase (the first phase being building successive
Chengsong
parents:
diff changeset
   911
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value 
Chengsong
parents:
diff changeset
   912
is generated in case the regular expression matches  the string. 
Chengsong
parents:
diff changeset
   913
Pictorially, the Sulzmann and Lu algorithm is as follows:
Chengsong
parents:
diff changeset
   914
Chengsong
parents:
diff changeset
   915
\begin{ceqn}
Chengsong
parents:
diff changeset
   916
\begin{equation}\label{graph:2}
Chengsong
parents:
diff changeset
   917
\begin{tikzcd}
Chengsong
parents:
diff changeset
   918
r_0 \arrow[r, "\backslash c_0"]  \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
Chengsong
parents:
diff changeset
   919
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]         
Chengsong
parents:
diff changeset
   920
\end{tikzcd}
Chengsong
parents:
diff changeset
   921
\end{equation}
Chengsong
parents:
diff changeset
   922
\end{ceqn}
Chengsong
parents:
diff changeset
   923
Chengsong
parents:
diff changeset
   924
\noindent
Chengsong
parents:
diff changeset
   925
For convenience, we shall employ the following notations: the regular
Chengsong
parents:
diff changeset
   926
expression we start with is $r_0$, and the given string $s$ is composed
Chengsong
parents:
diff changeset
   927
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
Chengsong
parents:
diff changeset
   928
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
Chengsong
parents:
diff changeset
   929
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
Chengsong
parents:
diff changeset
   930
the derivative $r_n$. We test whether this derivative is
Chengsong
parents:
diff changeset
   931
$\textit{nullable}$ or not. If not, we know the string does not match
Chengsong
parents:
diff changeset
   932
$r$ and no value needs to be generated. If yes, we start building the
Chengsong
parents:
diff changeset
   933
values incrementally by \emph{injecting} back the characters into the
Chengsong
parents:
diff changeset
   934
earlier values $v_n, \ldots, v_0$. This is the second phase of the
Chengsong
parents:
diff changeset
   935
algorithm from the right to left. For the first value $v_n$, we call the
Chengsong
parents:
diff changeset
   936
function $\textit{mkeps}$, which builds the lexical value
Chengsong
parents:
diff changeset
   937
for how the empty string has been matched by the (nullable) regular
Chengsong
parents:
diff changeset
   938
expression $r_n$. This function is defined as
Chengsong
parents:
diff changeset
   939
Chengsong
parents:
diff changeset
   940
	\begin{center}
Chengsong
parents:
diff changeset
   941
		\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
   942
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
Chengsong
parents:
diff changeset
   943
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
Chengsong
parents:
diff changeset
   944
			& \textit{if} $\nullable(r_{1})$\\ 
Chengsong
parents:
diff changeset
   945
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
Chengsong
parents:
diff changeset
   946
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
Chengsong
parents:
diff changeset
   947
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
Chengsong
parents:
diff changeset
   948
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
Chengsong
parents:
diff changeset
   949
		\end{tabular}
Chengsong
parents:
diff changeset
   950
	\end{center}
Chengsong
parents:
diff changeset
   951
Chengsong
parents:
diff changeset
   952
Chengsong
parents:
diff changeset
   953
\noindent There are no cases for $\ZERO$ and $c$, since
Chengsong
parents:
diff changeset
   954
these regular expression cannot match the empty string. Note
Chengsong
parents:
diff changeset
   955
also that in case of alternatives we give preference to the
Chengsong
parents:
diff changeset
   956
regular expression on the left-hand side. This will become
Chengsong
parents:
diff changeset
   957
important later on about what value is calculated.
Chengsong
parents:
diff changeset
   958
Chengsong
parents:
diff changeset
   959
After the $\mkeps$-call, we inject back the characters one by one in order to build
Chengsong
parents:
diff changeset
   960
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
Chengsong
parents:
diff changeset
   961
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
Chengsong
parents:
diff changeset
   962
After injecting back $n$ characters, we get the lexical value for how $r_0$
Chengsong
parents:
diff changeset
   963
matches $s$. For this Sulzmann and Lu defined a function that reverses
Chengsong
parents:
diff changeset
   964
the ``chopping off'' of characters during the derivative phase. The
Chengsong
parents:
diff changeset
   965
corresponding function is called \emph{injection}, written
Chengsong
parents:
diff changeset
   966
$\textit{inj}$; it takes three arguments: the first one is a regular
Chengsong
parents:
diff changeset
   967
expression ${r_{i-1}}$, before the character is chopped off, the second
Chengsong
parents:
diff changeset
   968
is a character ${c_{i-1}}$, the character we want to inject and the
Chengsong
parents:
diff changeset
   969
third argument is the value ${v_i}$, into which one wants to inject the
Chengsong
parents:
diff changeset
   970
character (it corresponds to the regular expression after the character
Chengsong
parents:
diff changeset
   971
has been chopped off). The result of this function is a new value. The
Chengsong
parents:
diff changeset
   972
definition of $\textit{inj}$ is as follows: 
Chengsong
parents:
diff changeset
   973
Chengsong
parents:
diff changeset
   974
\begin{center}
Chengsong
parents:
diff changeset
   975
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
Chengsong
parents:
diff changeset
   976
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
Chengsong
parents:
diff changeset
   977
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
Chengsong
parents:
diff changeset
   978
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
Chengsong
parents:
diff changeset
   979
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
Chengsong
parents:
diff changeset
   980
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
Chengsong
parents:
diff changeset
   981
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
Chengsong
parents:
diff changeset
   982
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
Chengsong
parents:
diff changeset
   983
\end{tabular}
Chengsong
parents:
diff changeset
   984
\end{center}
Chengsong
parents:
diff changeset
   985
Chengsong
parents:
diff changeset
   986
\noindent This definition is by recursion on the ``shape'' of regular
Chengsong
parents:
diff changeset
   987
expressions and values. To understands this definition better consider
Chengsong
parents:
diff changeset
   988
the situation when we build the derivative on regular expression $r_{i-1}$.
Chengsong
parents:
diff changeset
   989
For this we chop off a character from $r_{i-1}$ to form $r_i$. This leaves a
Chengsong
parents:
diff changeset
   990
``hole'' in $r_i$ and its corresponding value $v_i$. 
Chengsong
parents:
diff changeset
   991
To calculate $v_{i-1}$, we need to
Chengsong
parents:
diff changeset
   992
locate where that hole is and fill it. 
Chengsong
parents:
diff changeset
   993
We can find this location by
Chengsong
parents:
diff changeset
   994
comparing $r_{i-1}$ and $v_i$. For instance, if $r_{i-1}$ is of shape
Chengsong
parents:
diff changeset
   995
$r_a \cdot r_b$, and $v_i$ is of shape $\Left(Seq(v_1,v_2))$, we know immediately that 
Chengsong
parents:
diff changeset
   996
%
Chengsong
parents:
diff changeset
   997
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b \,+\, r_b\backslash c,\]
Chengsong
parents:
diff changeset
   998
Chengsong
parents:
diff changeset
   999
\noindent
Chengsong
parents:
diff changeset
  1000
otherwise if $r_a$ is not nullable,
Chengsong
parents:
diff changeset
  1001
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b,\]
Chengsong
parents:
diff changeset
  1002
Chengsong
parents:
diff changeset
  1003
\noindent
Chengsong
parents:
diff changeset
  1004
the value $v_i$ should be  $\Seq(\ldots)$, contradicting the fact that
Chengsong
parents:
diff changeset
  1005
$v_i$ is actually of shape $\Left(\ldots)$. Furthermore, since $v_i$ is of shape
Chengsong
parents:
diff changeset
  1006
$\Left(\ldots)$ instead of $\Right(\ldots)$, we know that the left
Chengsong
parents:
diff changeset
  1007
branch of \[ (r_a \cdot r_b)\backslash c =
Chengsong
parents:
diff changeset
  1008
\bold{\underline{ (r_a\backslash c) \cdot r_b} }\,+\, r_b\backslash c,\](underlined)
Chengsong
parents:
diff changeset
  1009
 is taken instead of the right one. This means $c$ is chopped off 
Chengsong
parents:
diff changeset
  1010
from $r_a$ rather than $r_b$.
Chengsong
parents:
diff changeset
  1011
We have therefore found out 
Chengsong
parents:
diff changeset
  1012
that the hole will be on $r_a$. So we recursively call $\inj\, 
Chengsong
parents:
diff changeset
  1013
r_a\,c\,v_a$ to fill that hole in $v_a$. After injection, the value 
Chengsong
parents:
diff changeset
  1014
$v_i$ for $r_i = r_a \cdot r_b$ should be $\Seq\,(\inj\,r_a\,c\,v_a)\,v_b$.
Chengsong
parents:
diff changeset
  1015
Other clauses can be understood in a similar way.
Chengsong
parents:
diff changeset
  1016
Chengsong
parents:
diff changeset
  1017
%\comment{Other word: insight?}
Chengsong
parents:
diff changeset
  1018
The following example gives an insight of $\textit{inj}$'s effect and
Chengsong
parents:
diff changeset
  1019
how Sulzmann and Lu's algorithm works as a whole. Suppose we have a
Chengsong
parents:
diff changeset
  1020
regular expression $((((a+b)+ab)+c)+abc)^*$, and want to match it
Chengsong
parents:
diff changeset
  1021
against the string $abc$ (when $abc$ is written as a regular expression,
Chengsong
parents:
diff changeset
  1022
the standard way of expressing it is $a \cdot (b \cdot c)$. But we
Chengsong
parents:
diff changeset
  1023
usually omit the parentheses and dots here for better readability. This
Chengsong
parents:
diff changeset
  1024
algorithm returns a POSIX value, which means it will produce the longest
Chengsong
parents:
diff changeset
  1025
matching. Consequently, it matches the string $abc$ in one star
Chengsong
parents:
diff changeset
  1026
iteration, using the longest alternative $abc$ in the sub-expression (we shall use $r$ to denote this
Chengsong
parents:
diff changeset
  1027
sub-expression for conciseness):
Chengsong
parents:
diff changeset
  1028
Chengsong
parents:
diff changeset
  1029
\[((((a+b)+ab)+c)+\underbrace{abc}_r)\] 
Chengsong
parents:
diff changeset
  1030
Chengsong
parents:
diff changeset
  1031
\noindent
Chengsong
parents:
diff changeset
  1032
Before $\textit{inj}$ is called, our lexer first builds derivative using
Chengsong
parents:
diff changeset
  1033
string $abc$ (we simplified some regular expressions like $\ZERO \cdot
Chengsong
parents:
diff changeset
  1034
b$ to $\ZERO$ for conciseness; we also omit parentheses if they are
Chengsong
parents:
diff changeset
  1035
clear from the context):
Chengsong
parents:
diff changeset
  1036
Chengsong
parents:
diff changeset
  1037
%Similarly, we allow
Chengsong
parents:
diff changeset
  1038
%$\textit{ALT}$ to take a list of regular expressions as an argument
Chengsong
parents:
diff changeset
  1039
%instead of just 2 operands to reduce the nested depth of
Chengsong
parents:
diff changeset
  1040
%$\textit{ALT}$
Chengsong
parents:
diff changeset
  1041
Chengsong
parents:
diff changeset
  1042
\begin{center}
Chengsong
parents:
diff changeset
  1043
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1044
$r^*$ & $\xrightarrow{\backslash a}$ & $r_1 = (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r^*$\\
Chengsong
parents:
diff changeset
  1045
      & $\xrightarrow{\backslash b}$ & $r_2 = (\ZERO+\ZERO+\ONE \cdot \ONE + \ZERO + \ONE \cdot \ONE \cdot c) \cdot r^* +(\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^*$\\
Chengsong
parents:
diff changeset
  1046
      & $\xrightarrow{\backslash c}$ & $r_3 = ((\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot \ONE) \cdot r^* + (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^*) + $\\ 
Chengsong
parents:
diff changeset
  1047
      &                              & $\phantom{r_3 = (} ((\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^* + (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^* )$
Chengsong
parents:
diff changeset
  1048
\end{tabular}
Chengsong
parents:
diff changeset
  1049
\end{center}
Chengsong
parents:
diff changeset
  1050
Chengsong
parents:
diff changeset
  1051
\noindent
Chengsong
parents:
diff changeset
  1052
In  case $r_3$ is nullable, we can call $\textit{mkeps}$ 
Chengsong
parents:
diff changeset
  1053
to construct a lexical value for how $r_3$ matched the string $abc$. 
Chengsong
parents:
diff changeset
  1054
This function gives the following value $v_3$: 
Chengsong
parents:
diff changeset
  1055
Chengsong
parents:
diff changeset
  1056
Chengsong
parents:
diff changeset
  1057
\begin{center}
Chengsong
parents:
diff changeset
  1058
$\Left(\Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty,\Empty))), \Stars [])))$
Chengsong
parents:
diff changeset
  1059
\end{center}
Chengsong
parents:
diff changeset
  1060
The outer $\Left(\Left(\ldots))$ tells us the leftmost nullable part of $r_3$(underlined):
Chengsong
parents:
diff changeset
  1061
Chengsong
parents:
diff changeset
  1062
\begin{center}
Chengsong
parents:
diff changeset
  1063
	\begin{tabular}{l@{\hspace{2mm}}l}
Chengsong
parents:
diff changeset
  1064
    & $\big(\underline{(\ZERO+\ZERO+\ZERO+ \ZERO+ \ONE \cdot \ONE \cdot \ONE) \cdot r^*} 
Chengsong
parents:
diff changeset
  1065
    \;+\; (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^*\big)$ \smallskip\\
Chengsong
parents:
diff changeset
  1066
    $+$ & $\big((\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^*
Chengsong
parents:
diff changeset
  1067
    \;+\; (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^* \big)$
Chengsong
parents:
diff changeset
  1068
  	\end{tabular}
Chengsong
parents:
diff changeset
  1069
 \end{center}
Chengsong
parents:
diff changeset
  1070
Chengsong
parents:
diff changeset
  1071
\noindent
Chengsong
parents:
diff changeset
  1072
 Note that the leftmost location of term $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot
Chengsong
parents:
diff changeset
  1073
 \ONE) \cdot r^*$ (which corresponds to the initial sub-match $abc$) allows
Chengsong
parents:
diff changeset
  1074
 $\textit{mkeps}$ to pick it up because $\textit{mkeps}$ is defined to always choose the
Chengsong
parents:
diff changeset
  1075
 left one when it is nullable. In the case of this example, $abc$ is
Chengsong
parents:
diff changeset
  1076
 preferred over $a$ or $ab$. This $\Left(\Left(\ldots))$ location is
Chengsong
parents:
diff changeset
  1077
 generated by two applications of the splitting clause
Chengsong
parents:
diff changeset
  1078
Chengsong
parents:
diff changeset
  1079
\begin{center}
Chengsong
parents:
diff changeset
  1080
     $(r_1 \cdot r_2)\backslash c  \;\;(when \; r_1 \; nullable) \, = (r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c.$
Chengsong
parents:
diff changeset
  1081
\end{center}
Chengsong
parents:
diff changeset
  1082
       
Chengsong
parents:
diff changeset
  1083
\noindent
Chengsong
parents:
diff changeset
  1084
By this clause, we put $r_1 \backslash c \cdot r_2 $ at the
Chengsong
parents:
diff changeset
  1085
$\textit{front}$ and $r_2 \backslash c$ at the $\textit{back}$. This
Chengsong
parents:
diff changeset
  1086
allows $\textit{mkeps}$ to always pick up among two matches the one with a longer
Chengsong
parents:
diff changeset
  1087
initial sub-match. Removing the outside $\Left(\Left(...))$, the inside
Chengsong
parents:
diff changeset
  1088
sub-value 
Chengsong
parents:
diff changeset
  1089
 
Chengsong
parents:
diff changeset
  1090
\begin{center}
Chengsong
parents:
diff changeset
  1091
 $\Seq(\Right(\Seq(\Empty, \Seq(\Empty, \Empty))), \Stars [])$
Chengsong
parents:
diff changeset
  1092
\end{center}
Chengsong
parents:
diff changeset
  1093
Chengsong
parents:
diff changeset
  1094
\noindent
Chengsong
parents:
diff changeset
  1095
tells us how the empty string $[]$ is matched with $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot
Chengsong
parents:
diff changeset
  1096
\ONE \cdot \ONE) \cdot r^*$. We match $[]$ by a sequence of two nullable regular
Chengsong
parents:
diff changeset
  1097
expressions. The first one is an alternative, we take the rightmost
Chengsong
parents:
diff changeset
  1098
alternative---whose language contains the empty string. The second
Chengsong
parents:
diff changeset
  1099
nullable regular expression is a Kleene star. $\Stars$ tells us how it
Chengsong
parents:
diff changeset
  1100
generates the nullable regular expression: by 0 iterations to form
Chengsong
parents:
diff changeset
  1101
$\ONE$. Now $\textit{inj}$ injects characters back and incrementally
Chengsong
parents:
diff changeset
  1102
builds a lexical value based on $v_3$. Using the value $v_3$, the character
Chengsong
parents:
diff changeset
  1103
c, and the regular expression $r_2$, we can recover how $r_2$ matched
Chengsong
parents:
diff changeset
  1104
the string $[c]$ : $\textit{inj} \; r_2 \; c \; v_3$ gives us
Chengsong
parents:
diff changeset
  1105
 \begin{center}
Chengsong
parents:
diff changeset
  1106
 $v_2 = \Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty, c))), \Stars [])),$
Chengsong
parents:
diff changeset
  1107
 \end{center}
Chengsong
parents:
diff changeset
  1108
which tells us how $r_2$ matched $[c]$. After this we inject back the character $b$, and get
Chengsong
parents:
diff changeset
  1109
\begin{center}
Chengsong
parents:
diff changeset
  1110
$v_1 = \Seq(\Right(\Seq(\Empty, \Seq(b, c))), \Stars [])$
Chengsong
parents:
diff changeset
  1111
\end{center}
Chengsong
parents:
diff changeset
  1112
 for how 
Chengsong
parents:
diff changeset
  1113
 \begin{center}
Chengsong
parents:
diff changeset
  1114
 $r_1= (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r*$
Chengsong
parents:
diff changeset
  1115
 \end{center}
Chengsong
parents:
diff changeset
  1116
  matched  the string $bc$ before it split into two substrings. 
Chengsong
parents:
diff changeset
  1117
  Finally, after injecting character $a$ back to $v_1$, 
Chengsong
parents:
diff changeset
  1118
  we get  the lexical value tree 
Chengsong
parents:
diff changeset
  1119
  \begin{center}
Chengsong
parents:
diff changeset
  1120
  $v_0= \Stars [\Right(\Seq(a, \Seq(b, c)))]$
Chengsong
parents:
diff changeset
  1121
  \end{center}
Chengsong
parents:
diff changeset
  1122
   for how $r$ matched $abc$. This completes the algorithm.
Chengsong
parents:
diff changeset
  1123
   
Chengsong
parents:
diff changeset
  1124
%We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}. 
Chengsong
parents:
diff changeset
  1125
Readers might have noticed that the lexical value information is actually
Chengsong
parents:
diff changeset
  1126
already available when doing derivatives. For example, immediately after
Chengsong
parents:
diff changeset
  1127
the operation $\backslash a$ we know that if we want to match a string
Chengsong
parents:
diff changeset
  1128
that starts with $a$, we can either take the initial match to be 
Chengsong
parents:
diff changeset
  1129
Chengsong
parents:
diff changeset
  1130
 \begin{center}
Chengsong
parents:
diff changeset
  1131
\begin{enumerate}
Chengsong
parents:
diff changeset
  1132
    \item[1)] just $a$ or
Chengsong
parents:
diff changeset
  1133
    \item[2)] string $ab$ or 
Chengsong
parents:
diff changeset
  1134
    \item[3)] string $abc$.
Chengsong
parents:
diff changeset
  1135
\end{enumerate}
Chengsong
parents:
diff changeset
  1136
\end{center}
Chengsong
parents:
diff changeset
  1137
Chengsong
parents:
diff changeset
  1138
\noindent
Chengsong
parents:
diff changeset
  1139
In order to differentiate between these choices, we just need to
Chengsong
parents:
diff changeset
  1140
remember their positions---$a$ is on the left, $ab$ is in the middle ,
Chengsong
parents:
diff changeset
  1141
and $abc$ is on the right. Which of these alternatives is chosen
Chengsong
parents:
diff changeset
  1142
later does not affect their relative position because the algorithm does
Chengsong
parents:
diff changeset
  1143
not change this order. If this parsing information can be determined and
Chengsong
parents:
diff changeset
  1144
does not change because of later derivatives, there is no point in
Chengsong
parents:
diff changeset
  1145
traversing this information twice. This leads to an optimisation---if we
Chengsong
parents:
diff changeset
  1146
store the information for lexical values inside the regular expression,
Chengsong
parents:
diff changeset
  1147
update it when we do derivative on them, and collect the information
Chengsong
parents:
diff changeset
  1148
when finished with derivatives and call $\textit{mkeps}$ for deciding which
Chengsong
parents:
diff changeset
  1149
branch is POSIX, we can generate the lexical value in one pass, instead of
Chengsong
parents:
diff changeset
  1150
doing the rest $n$ injections. This leads to Sulzmann and Lu's novel
Chengsong
parents:
diff changeset
  1151
idea of using bitcodes in derivatives.
Chengsong
parents:
diff changeset
  1152
Chengsong
parents:
diff changeset
  1153
In the next section, we shall focus on the bitcoded algorithm and the
Chengsong
parents:
diff changeset
  1154
process of simplification of regular expressions. This is needed in
Chengsong
parents:
diff changeset
  1155
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann
Chengsong
parents:
diff changeset
  1156
and Lu's algorithms.  This is where the PhD-project aims to advance the
Chengsong
parents:
diff changeset
  1157
state-of-the-art.
Chengsong
parents:
diff changeset
  1158
Chengsong
parents:
diff changeset
  1159
Chengsong
parents:
diff changeset
  1160
\section{Simplification of Regular Expressions}
Chengsong
parents:
diff changeset
  1161
Chengsong
parents:
diff changeset
  1162
Using bitcodes to guide  parsing is not a novel idea. It was applied to
Chengsong
parents:
diff changeset
  1163
context free grammars and then adapted by Henglein and Nielson for
Chengsong
parents:
diff changeset
  1164
efficient regular expression  lexing using DFAs~\cite{nielson11bcre}.
Chengsong
parents:
diff changeset
  1165
Sulzmann and Lu took this idea of bitcodes a step further by integrating
Chengsong
parents:
diff changeset
  1166
bitcodes into derivatives. The reason why we want to use bitcodes in
Chengsong
parents:
diff changeset
  1167
this project is that we want to introduce more aggressive simplification
Chengsong
parents:
diff changeset
  1168
rules in order to keep the size of derivatives small throughout. This is
Chengsong
parents:
diff changeset
  1169
because the main drawback of building successive derivatives according
Chengsong
parents:
diff changeset
  1170
to Brzozowski's definition is that they can grow very quickly in size.
Chengsong
parents:
diff changeset
  1171
This is mainly due to the fact that the derivative operation generates
Chengsong
parents:
diff changeset
  1172
often ``useless'' $\ZERO$s and $\ONE$s in derivatives.  As a result, if
Chengsong
parents:
diff changeset
  1173
implemented naively both algorithms by Brzozowski and by Sulzmann and Lu
Chengsong
parents:
diff changeset
  1174
are excruciatingly slow. For example when starting with the regular
Chengsong
parents:
diff changeset
  1175
expression $(a + aa)^*$ and building 12 successive derivatives
Chengsong
parents:
diff changeset
  1176
w.r.t.~the character $a$, one obtains a derivative regular expression
Chengsong
parents:
diff changeset
  1177
with more than 8000 nodes (when viewed as a tree). Operations like
Chengsong
parents:
diff changeset
  1178
$\textit{der}$ and $\nullable$ need to traverse such trees and
Chengsong
parents:
diff changeset
  1179
consequently the bigger the size of the derivative the slower the
Chengsong
parents:
diff changeset
  1180
algorithm. 
Chengsong
parents:
diff changeset
  1181
Chengsong
parents:
diff changeset
  1182
Fortunately, one can simplify regular expressions after each derivative
Chengsong
parents:
diff changeset
  1183
step. Various simplifications of regular expressions are possible, such
Chengsong
parents:
diff changeset
  1184
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r
Chengsong
parents:
diff changeset
  1185
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not
Chengsong
parents:
diff changeset
  1186
affect the answer for whether a regular expression matches a string or
Chengsong
parents:
diff changeset
  1187
not, but fortunately also do not affect the POSIX strategy of how
Chengsong
parents:
diff changeset
  1188
regular expressions match strings---although the latter is much harder
Chengsong
parents:
diff changeset
  1189
to establish. Some initial results in this regard have been
Chengsong
parents:
diff changeset
  1190
obtained in \cite{AusafDyckhoffUrban2016}. 
Chengsong
parents:
diff changeset
  1191
Chengsong
parents:
diff changeset
  1192
Unfortunately, the simplification rules outlined above  are not
Chengsong
parents:
diff changeset
  1193
sufficient to prevent a size explosion in all cases. We
Chengsong
parents:
diff changeset
  1194
believe a tighter bound can be achieved that prevents an explosion in
Chengsong
parents:
diff changeset
  1195
\emph{all} cases. Such a tighter bound is suggested by work of Antimirov who
Chengsong
parents:
diff changeset
  1196
proved that (partial) derivatives can be bound by the number of
Chengsong
parents:
diff changeset
  1197
characters contained in the initial regular expression
Chengsong
parents:
diff changeset
  1198
\cite{Antimirov95}. He defined the \emph{partial derivatives} of regular
Chengsong
parents:
diff changeset
  1199
expressions as follows:
Chengsong
parents:
diff changeset
  1200
Chengsong
parents:
diff changeset
  1201
\begin{center}
Chengsong
parents:
diff changeset
  1202
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1203
 $\textit{pder} \; c \; \ZERO$ & $\dn$ & $\emptyset$\\
Chengsong
parents:
diff changeset
  1204
 $\textit{pder} \; c \; \ONE$ & $\dn$ & $\emptyset$ \\
Chengsong
parents:
diff changeset
  1205
 $\textit{pder} \; c \; d$ & $\dn$ & $\textit{if} \; c \,=\, d \; \{  \ONE   \}  \; \textit{else} \; \emptyset$ \\ 
Chengsong
parents:
diff changeset
  1206
  $\textit{pder} \; c \; r_1+r_2$ & $\dn$ & $pder \; c \; r_1 \cup pder \; c \;  r_2$ \\
Chengsong
parents:
diff changeset
  1207
   $\textit{pder} \; c \; r_1 \cdot r_2$ & $\dn$ & $\textit{if} \; nullable \; r_1 $\\
Chengsong
parents:
diff changeset
  1208
     & & $\textit{then} \; \{  r \cdot r_2 \mid r \in pder \; c \; r_1   \}  \cup pder \; c \; r_2 \;$\\
Chengsong
parents:
diff changeset
  1209
     & & $\textit{else} \; \{  r \cdot r_2 \mid r \in pder \; c \; r_1   \} $ \\ 
Chengsong
parents:
diff changeset
  1210
     $\textit{pder} \; c \; r^*$ & $\dn$ & $ \{  r' \cdot r^* \mid r' \in pder \; c \; r   \}  $ \\  
Chengsong
parents:
diff changeset
  1211
 \end{tabular}
Chengsong
parents:
diff changeset
  1212
 \end{center}
Chengsong
parents:
diff changeset
  1213
Chengsong
parents:
diff changeset
  1214
\noindent
Chengsong
parents:
diff changeset
  1215
A partial derivative of a regular expression $r$ is essentially a set of
Chengsong
parents:
diff changeset
  1216
regular expressions that are either $r$'s children expressions or a
Chengsong
parents:
diff changeset
  1217
concatenation of them. Antimirov has proved a tight bound of the sum of
Chengsong
parents:
diff changeset
  1218
the size of \emph{all} partial derivatives no matter what the string
Chengsong
parents:
diff changeset
  1219
looks like. Roughly speaking the size sum will be at most cubic in the
Chengsong
parents:
diff changeset
  1220
size of the regular expression.
Chengsong
parents:
diff changeset
  1221
Chengsong
parents:
diff changeset
  1222
If we want the size of derivatives in Sulzmann and Lu's algorithm to
Chengsong
parents:
diff changeset
  1223
stay below this bound, we would need more aggressive simplifications.
Chengsong
parents:
diff changeset
  1224
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
Chengsong
parents:
diff changeset
  1225
deleting duplicates whenever possible. For example, the parentheses in
Chengsong
parents:
diff changeset
  1226
$(a+b) \cdot c + bc$ can be opened up to get $a\cdot c +  b \cdot c + b
Chengsong
parents:
diff changeset
  1227
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
Chengsong
parents:
diff changeset
  1228
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
Chengsong
parents:
diff changeset
  1229
$a^*+a+\ONE$. Adding these more aggressive simplification rules helps us
Chengsong
parents:
diff changeset
  1230
to achieve the same size bound as that of the partial derivatives. 
Chengsong
parents:
diff changeset
  1231
Chengsong
parents:
diff changeset
  1232
In order to implement the idea of ``spilling out alternatives'' and to
Chengsong
parents:
diff changeset
  1233
make them compatible with the $\text{inj}$-mechanism, we use
Chengsong
parents:
diff changeset
  1234
\emph{bitcodes}. Bits and bitcodes (lists of bits) are just:
Chengsong
parents:
diff changeset
  1235
Chengsong
parents:
diff changeset
  1236
%This allows us to prove a tight
Chengsong
parents:
diff changeset
  1237
%bound on the size of regular expression during the running time of the
Chengsong
parents:
diff changeset
  1238
%algorithm if we can establish the connection between our simplification
Chengsong
parents:
diff changeset
  1239
%rules and partial derivatives.
Chengsong
parents:
diff changeset
  1240
Chengsong
parents:
diff changeset
  1241
 %We believe, and have generated test
Chengsong
parents:
diff changeset
  1242
%data, that a similar bound can be obtained for the derivatives in
Chengsong
parents:
diff changeset
  1243
%Sulzmann and Lu's algorithm. Let us give some details about this next.
Chengsong
parents:
diff changeset
  1244
Chengsong
parents:
diff changeset
  1245
Chengsong
parents:
diff changeset
  1246
\begin{center}
Chengsong
parents:
diff changeset
  1247
		$b ::=   S \mid  Z \qquad
Chengsong
parents:
diff changeset
  1248
bs ::= [] \mid b:bs    
Chengsong
parents:
diff changeset
  1249
$
Chengsong
parents:
diff changeset
  1250
\end{center}
Chengsong
parents:
diff changeset
  1251
Chengsong
parents:
diff changeset
  1252
\noindent
Chengsong
parents:
diff changeset
  1253
The $S$ and $Z$ are arbitrary names for the bits in order to avoid 
Chengsong
parents:
diff changeset
  1254
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
Chengsong
parents:
diff changeset
  1255
bit-lists) can be used to encode values (or incomplete values) in a
Chengsong
parents:
diff changeset
  1256
compact form. This can be straightforwardly seen in the following
Chengsong
parents:
diff changeset
  1257
coding function from values to bitcodes: 
Chengsong
parents:
diff changeset
  1258
Chengsong
parents:
diff changeset
  1259
\begin{center}
Chengsong
parents:
diff changeset
  1260
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1261
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
Chengsong
parents:
diff changeset
  1262
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
Chengsong
parents:
diff changeset
  1263
  $\textit{code}(\Left\,v)$ & $\dn$ & $\Z :: code(v)$\\
Chengsong
parents:
diff changeset
  1264
  $\textit{code}(\Right\,v)$ & $\dn$ & $\S :: code(v)$\\
Chengsong
parents:
diff changeset
  1265
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
Chengsong
parents:
diff changeset
  1266
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[\Z]$\\
Chengsong
parents:
diff changeset
  1267
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $\S :: code(v) \;@\;
Chengsong
parents:
diff changeset
  1268
                                                 code(\Stars\,vs)$
Chengsong
parents:
diff changeset
  1269
\end{tabular}    
Chengsong
parents:
diff changeset
  1270
\end{center} 
Chengsong
parents:
diff changeset
  1271
Chengsong
parents:
diff changeset
  1272
\noindent
Chengsong
parents:
diff changeset
  1273
Here $\textit{code}$ encodes a value into a bitcodes by converting
Chengsong
parents:
diff changeset
  1274
$\Left$ into $\Z$, $\Right$ into $\S$, the start point of a non-empty
Chengsong
parents:
diff changeset
  1275
star iteration into $\S$, and the border where a local star terminates
Chengsong
parents:
diff changeset
  1276
into $\Z$. This coding is lossy, as it throws away the information about
Chengsong
parents:
diff changeset
  1277
characters, and also does not encode the ``boundary'' between two
Chengsong
parents:
diff changeset
  1278
sequence values. Moreover, with only the bitcode we cannot even tell
Chengsong
parents:
diff changeset
  1279
whether the $\S$s and $\Z$s are for $\Left/\Right$ or $\Stars$. The
Chengsong
parents:
diff changeset
  1280
reason for choosing this compact way of storing information is that the
Chengsong
parents:
diff changeset
  1281
relatively small size of bits can be easily manipulated and ``moved
Chengsong
parents:
diff changeset
  1282
around'' in a regular expression. In order to recover values, we will 
Chengsong
parents:
diff changeset
  1283
need the corresponding regular expression as an extra information. This
Chengsong
parents:
diff changeset
  1284
means the decoding function is defined as:
Chengsong
parents:
diff changeset
  1285
Chengsong
parents:
diff changeset
  1286
Chengsong
parents:
diff changeset
  1287
%\begin{definition}[Bitdecoding of Values]\mbox{}
Chengsong
parents:
diff changeset
  1288
\begin{center}
Chengsong
parents:
diff changeset
  1289
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
Chengsong
parents:
diff changeset
  1290
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
Chengsong
parents:
diff changeset
  1291
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
Chengsong
parents:
diff changeset
  1292
  $\textit{decode}'\,(\Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1293
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
Chengsong
parents:
diff changeset
  1294
       (\Left\,v, bs_1)$\\
Chengsong
parents:
diff changeset
  1295
  $\textit{decode}'\,(\S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1296
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
Chengsong
parents:
diff changeset
  1297
       (\Right\,v, bs_1)$\\                           
Chengsong
parents:
diff changeset
  1298
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1299
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
Chengsong
parents:
diff changeset
  1300
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
Chengsong
parents:
diff changeset
  1301
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
Chengsong
parents:
diff changeset
  1302
  $\textit{decode}'\,(\Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
Chengsong
parents:
diff changeset
  1303
  $\textit{decode}'\,(\S\!::\!bs)\,(r^*)$ & $\dn$ & 
Chengsong
parents:
diff changeset
  1304
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents:
diff changeset
  1305
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
Chengsong
parents:
diff changeset
  1306
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
Chengsong
parents:
diff changeset
  1307
  
Chengsong
parents:
diff changeset
  1308
  $\textit{decode}\,bs\,r$ & $\dn$ &
Chengsong
parents:
diff changeset
  1309
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents:
diff changeset
  1310
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
Chengsong
parents:
diff changeset
  1311
       \textit{else}\;\textit{None}$                       
Chengsong
parents:
diff changeset
  1312
\end{tabular}    
Chengsong
parents:
diff changeset
  1313
\end{center}    
Chengsong
parents:
diff changeset
  1314
%\end{definition}
Chengsong
parents:
diff changeset
  1315
Chengsong
parents:
diff changeset
  1316
Sulzmann and Lu's integrated the bitcodes into regular expressions to
Chengsong
parents:
diff changeset
  1317
create annotated regular expressions \cite{Sulzmann2014}.
Chengsong
parents:
diff changeset
  1318
\emph{Annotated regular expressions} are defined by the following
Chengsong
parents:
diff changeset
  1319
grammar:%\comment{ALTS should have  an $as$ in  the definitions, not  just $a_1$ and $a_2$}
Chengsong
parents:
diff changeset
  1320
Chengsong
parents:
diff changeset
  1321
\begin{center}
Chengsong
parents:
diff changeset
  1322
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1323
  $\textit{a}$ & $::=$  & $\textit{ZERO}$\\
Chengsong
parents:
diff changeset
  1324
                  & $\mid$ & $\textit{ONE}\;\;bs$\\
Chengsong
parents:
diff changeset
  1325
                  & $\mid$ & $\textit{CHAR}\;\;bs\,c$\\
Chengsong
parents:
diff changeset
  1326
                  & $\mid$ & $\textit{ALTS}\;\;bs\,as$\\
Chengsong
parents:
diff changeset
  1327
                  & $\mid$ & $\textit{SEQ}\;\;bs\,a_1\,a_2$\\
Chengsong
parents:
diff changeset
  1328
                  & $\mid$ & $\textit{STAR}\;\;bs\,a$
Chengsong
parents:
diff changeset
  1329
\end{tabular}    
Chengsong
parents:
diff changeset
  1330
\end{center}  
Chengsong
parents:
diff changeset
  1331
%(in \textit{ALTS})
Chengsong
parents:
diff changeset
  1332
Chengsong
parents:
diff changeset
  1333
\noindent
Chengsong
parents:
diff changeset
  1334
where $bs$ stands for bitcodes, $a$  for $\bold{a}$nnotated regular
Chengsong
parents:
diff changeset
  1335
expressions and $as$ for a list of annotated regular expressions.
Chengsong
parents:
diff changeset
  1336
The alternative constructor($\textit{ALTS}$) has been generalized to 
Chengsong
parents:
diff changeset
  1337
accept a list of annotated regular expressions rather than just 2.
Chengsong
parents:
diff changeset
  1338
We will show that these bitcodes encode information about
Chengsong
parents:
diff changeset
  1339
the (POSIX) value that should be generated by the Sulzmann and Lu
Chengsong
parents:
diff changeset
  1340
algorithm.
Chengsong
parents:
diff changeset
  1341
Chengsong
parents:
diff changeset
  1342
Chengsong
parents:
diff changeset
  1343
To do lexing using annotated regular expressions, we shall first
Chengsong
parents:
diff changeset
  1344
transform the usual (un-annotated) regular expressions into annotated
Chengsong
parents:
diff changeset
  1345
regular expressions. This operation is called \emph{internalisation} and
Chengsong
parents:
diff changeset
  1346
defined as follows:
Chengsong
parents:
diff changeset
  1347
Chengsong
parents:
diff changeset
  1348
%\begin{definition}
Chengsong
parents:
diff changeset
  1349
\begin{center}
Chengsong
parents:
diff changeset
  1350
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1351
  $(\ZERO)^\uparrow$ & $\dn$ & $\textit{ZERO}$\\
Chengsong
parents:
diff changeset
  1352
  $(\ONE)^\uparrow$ & $\dn$ & $\textit{ONE}\,[]$\\
Chengsong
parents:
diff changeset
  1353
  $(c)^\uparrow$ & $\dn$ & $\textit{CHAR}\,[]\,c$\\
Chengsong
parents:
diff changeset
  1354
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
Chengsong
parents:
diff changeset
  1355
  $\textit{ALTS}\;[]\,List((\textit{fuse}\,[\Z]\,r_1^\uparrow),\,
Chengsong
parents:
diff changeset
  1356
  (\textit{fuse}\,[\S]\,r_2^\uparrow))$\\
Chengsong
parents:
diff changeset
  1357
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
Chengsong
parents:
diff changeset
  1358
         $\textit{SEQ}\;[]\,r_1^\uparrow\,r_2^\uparrow$\\
Chengsong
parents:
diff changeset
  1359
  $(r^*)^\uparrow$ & $\dn$ &
Chengsong
parents:
diff changeset
  1360
         $\textit{STAR}\;[]\,r^\uparrow$\\
Chengsong
parents:
diff changeset
  1361
\end{tabular}    
Chengsong
parents:
diff changeset
  1362
\end{center}    
Chengsong
parents:
diff changeset
  1363
%\end{definition}
Chengsong
parents:
diff changeset
  1364
Chengsong
parents:
diff changeset
  1365
\noindent
Chengsong
parents:
diff changeset
  1366
We use up arrows here to indicate that the basic un-annotated regular
Chengsong
parents:
diff changeset
  1367
expressions are ``lifted up'' into something slightly more complex. In the
Chengsong
parents:
diff changeset
  1368
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
Chengsong
parents:
diff changeset
  1369
attach bits to the front of an annotated regular expression. Its
Chengsong
parents:
diff changeset
  1370
definition is as follows:
Chengsong
parents:
diff changeset
  1371
Chengsong
parents:
diff changeset
  1372
\begin{center}
Chengsong
parents:
diff changeset
  1373
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1374
  $\textit{fuse}\;bs\,(\textit{ZERO})$ & $\dn$ & $\textit{ZERO}$\\
Chengsong
parents:
diff changeset
  1375
  $\textit{fuse}\;bs\,(\textit{ONE}\,bs')$ & $\dn$ &
Chengsong
parents:
diff changeset
  1376
     $\textit{ONE}\,(bs\,@\,bs')$\\
Chengsong
parents:
diff changeset
  1377
  $\textit{fuse}\;bs\,(\textit{CHAR}\,bs'\,c)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1378
     $\textit{CHAR}\,(bs\,@\,bs')\,c$\\
Chengsong
parents:
diff changeset
  1379
  $\textit{fuse}\;bs\,(\textit{ALTS}\,bs'\,as)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1380
     $\textit{ALTS}\,(bs\,@\,bs')\,as$\\
Chengsong
parents:
diff changeset
  1381
  $\textit{fuse}\;bs\,(\textit{SEQ}\,bs'\,a_1\,a_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1382
     $\textit{SEQ}\,(bs\,@\,bs')\,a_1\,a_2$\\
Chengsong
parents:
diff changeset
  1383
  $\textit{fuse}\;bs\,(\textit{STAR}\,bs'\,a)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1384
     $\textit{STAR}\,(bs\,@\,bs')\,a$
Chengsong
parents:
diff changeset
  1385
\end{tabular}    
Chengsong
parents:
diff changeset
  1386
\end{center}  
Chengsong
parents:
diff changeset
  1387
Chengsong
parents:
diff changeset
  1388
\noindent
Chengsong
parents:
diff changeset
  1389
After internalising the regular expression, we perform successive
Chengsong
parents:
diff changeset
  1390
derivative operations on the annotated regular expressions. This
Chengsong
parents:
diff changeset
  1391
derivative operation is the same as what we had previously for the
Chengsong
parents:
diff changeset
  1392
basic regular expressions, except that we beed to take care of
Chengsong
parents:
diff changeset
  1393
the bitcodes:
Chengsong
parents:
diff changeset
  1394
Chengsong
parents:
diff changeset
  1395
 %\begin{definition}{bder}
Chengsong
parents:
diff changeset
  1396
\begin{center}
Chengsong
parents:
diff changeset
  1397
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents:
diff changeset
  1398
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents:
diff changeset
  1399
  $(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents:
diff changeset
  1400
  $(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  1401
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents:
diff changeset
  1402
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
Chengsong
parents:
diff changeset
  1403
  $(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  1404
  $\textit{ALTS}\;bs\,(as.map(\backslash c))$\\
Chengsong
parents:
diff changeset
  1405
  $(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  1406
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents:
diff changeset
  1407
					       & &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
Chengsong
parents:
diff changeset
  1408
					       & &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
Chengsong
parents:
diff changeset
  1409
  & &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
Chengsong
parents:
diff changeset
  1410
  $(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
Chengsong
parents:
diff changeset
  1411
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
Chengsong
parents:
diff changeset
  1412
       (\textit{STAR}\,[]\,r)$
Chengsong
parents:
diff changeset
  1413
\end{tabular}    
Chengsong
parents:
diff changeset
  1414
\end{center}    
Chengsong
parents:
diff changeset
  1415
%\end{definition}
Chengsong
parents:
diff changeset
  1416
Chengsong
parents:
diff changeset
  1417
\noindent
Chengsong
parents:
diff changeset
  1418
For instance, when we unfold $\textit{STAR} \; bs \; a$ into a sequence,
Chengsong
parents:
diff changeset
  1419
we need to attach an additional bit $Z$ to the front of $r \backslash c$
Chengsong
parents:
diff changeset
  1420
to indicate that there is one more star iteration. Also the $SEQ$ clause
Chengsong
parents:
diff changeset
  1421
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
Chengsong
parents:
diff changeset
  1422
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
Chengsong
parents:
diff changeset
  1423
that it is for annotated regular expressions, therefore we omit the
Chengsong
parents:
diff changeset
  1424
definition). Assume that $bmkeps$ correctly extracts the bitcode for how
Chengsong
parents:
diff changeset
  1425
$a_1$ matches the string prior to character $c$ (more on this later),
Chengsong
parents:
diff changeset
  1426
then the right branch of $ALTS$, which is $fuse \; bmkeps \;  a_1 (a_2
Chengsong
parents:
diff changeset
  1427
\backslash c)$ will collapse the regular expression $a_1$(as it has
Chengsong
parents:
diff changeset
  1428
already been fully matched) and store the parsing information at the
Chengsong
parents:
diff changeset
  1429
head of the regular expression $a_2 \backslash c$ by fusing to it. The
Chengsong
parents:
diff changeset
  1430
bitsequence $bs$, which was initially attached to the head of $SEQ$, has
Chengsong
parents:
diff changeset
  1431
now been elevated to the top-level of $ALTS$, as this information will be
Chengsong
parents:
diff changeset
  1432
needed whichever way the $SEQ$ is matched---no matter whether $c$ belongs
Chengsong
parents:
diff changeset
  1433
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
Chengsong
parents:
diff changeset
  1434
the lexing information, we complete the lexing by collecting the
Chengsong
parents:
diff changeset
  1435
bitcodes using a generalised version of the $\textit{mkeps}$ function
Chengsong
parents:
diff changeset
  1436
for annotated regular expressions, called $\textit{bmkeps}$:
Chengsong
parents:
diff changeset
  1437
Chengsong
parents:
diff changeset
  1438
Chengsong
parents:
diff changeset
  1439
%\begin{definition}[\textit{bmkeps}]\mbox{}
Chengsong
parents:
diff changeset
  1440
\begin{center}
Chengsong
parents:
diff changeset
  1441
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1442
  $\textit{bmkeps}\,(\textit{ONE}\;bs)$ & $\dn$ & $bs$\\
Chengsong
parents:
diff changeset
  1443
  $\textit{bmkeps}\,(\textit{ALTS}\;bs\,a::as)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1444
     $\textit{if}\;\textit{bnullable}\,a$\\
Chengsong
parents:
diff changeset
  1445
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
Chengsong
parents:
diff changeset
  1446
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(\textit{ALTS}\;bs\,as)$\\
Chengsong
parents:
diff changeset
  1447
  $\textit{bmkeps}\,(\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1448
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
Chengsong
parents:
diff changeset
  1449
  $\textit{bmkeps}\,(\textit{STAR}\;bs\,a)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1450
     $bs \,@\, [\S]$
Chengsong
parents:
diff changeset
  1451
\end{tabular}    
Chengsong
parents:
diff changeset
  1452
\end{center}    
Chengsong
parents:
diff changeset
  1453
%\end{definition}
Chengsong
parents:
diff changeset
  1454
Chengsong
parents:
diff changeset
  1455
\noindent
Chengsong
parents:
diff changeset
  1456
This function completes the value information by travelling along the
Chengsong
parents:
diff changeset
  1457
path of the regular expression that corresponds to a POSIX value and
Chengsong
parents:
diff changeset
  1458
collecting all the bitcodes, and using $S$ to indicate the end of star
Chengsong
parents:
diff changeset
  1459
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
Chengsong
parents:
diff changeset
  1460
decode them, we get the value we expect. The corresponding lexing
Chengsong
parents:
diff changeset
  1461
algorithm looks as follows:
Chengsong
parents:
diff changeset
  1462
Chengsong
parents:
diff changeset
  1463
\begin{center}
Chengsong
parents:
diff changeset
  1464
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1465
  $\textit{blexer}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
  1466
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
  1467
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
  1468
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
  1469
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
  1470
\end{tabular}
Chengsong
parents:
diff changeset
  1471
\end{center}
Chengsong
parents:
diff changeset
  1472
Chengsong
parents:
diff changeset
  1473
\noindent
Chengsong
parents:
diff changeset
  1474
In this definition $\_\backslash s$ is the  generalisation  of the derivative
Chengsong
parents:
diff changeset
  1475
operation from characters to strings (just like the derivatives for un-annotated
Chengsong
parents:
diff changeset
  1476
regular expressions).
Chengsong
parents:
diff changeset
  1477
Chengsong
parents:
diff changeset
  1478
The main point of the bitcodes and annotated regular expressions is that
Chengsong
parents:
diff changeset
  1479
we can apply rather aggressive (in terms of size) simplification rules
Chengsong
parents:
diff changeset
  1480
in order to keep derivatives small. We have developed such
Chengsong
parents:
diff changeset
  1481
``aggressive'' simplification rules and generated test data that show
Chengsong
parents:
diff changeset
  1482
that the expected bound can be achieved. Obviously we could only
Chengsong
parents:
diff changeset
  1483
partially cover  the search space as there are infinitely many regular
Chengsong
parents:
diff changeset
  1484
expressions and strings. 
Chengsong
parents:
diff changeset
  1485
Chengsong
parents:
diff changeset
  1486
One modification we introduced is to allow a list of annotated regular
Chengsong
parents:
diff changeset
  1487
expressions in the \textit{ALTS} constructor. This allows us to not just
Chengsong
parents:
diff changeset
  1488
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
Chengsong
parents:
diff changeset
  1489
also unnecessary ``copies'' of regular expressions (very similar to
Chengsong
parents:
diff changeset
  1490
simplifying $r + r$ to just $r$, but in a more general setting). Another
Chengsong
parents:
diff changeset
  1491
modification is that we use simplification rules inspired by Antimirov's
Chengsong
parents:
diff changeset
  1492
work on partial derivatives. They maintain the idea that only the first
Chengsong
parents:
diff changeset
  1493
``copy'' of a regular expression in an alternative contributes to the
Chengsong
parents:
diff changeset
  1494
calculation of a POSIX value. All subsequent copies can be pruned away from
Chengsong
parents:
diff changeset
  1495
the regular expression. A recursive definition of our  simplification function 
Chengsong
parents:
diff changeset
  1496
that looks somewhat similar to our Scala code is given below:
Chengsong
parents:
diff changeset
  1497
%\comment{Use $\ZERO$, $\ONE$ and so on. 
Chengsong
parents:
diff changeset
  1498
%Is it $ALTS$ or $ALTS$?}\\
Chengsong
parents:
diff changeset
  1499
Chengsong
parents:
diff changeset
  1500
\begin{center}
Chengsong
parents:
diff changeset
  1501
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents:
diff changeset
  1502
   
Chengsong
parents:
diff changeset
  1503
  $\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
Chengsong
parents:
diff changeset
  1504
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
Chengsong
parents:
diff changeset
  1505
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
Chengsong
parents:
diff changeset
  1506
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
Chengsong
parents:
diff changeset
  1507
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
Chengsong
parents:
diff changeset
  1508
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow  \textit{SEQ} \; bs \; a_1' \;  a_2'$ \\
Chengsong
parents:
diff changeset
  1509
Chengsong
parents:
diff changeset
  1510
  $\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
Chengsong
parents:
diff changeset
  1511
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
Chengsong
parents:
diff changeset
  1512
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
Chengsong
parents:
diff changeset
  1513
   &&$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$\\ 
Chengsong
parents:
diff changeset
  1514
Chengsong
parents:
diff changeset
  1515
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
Chengsong
parents:
diff changeset
  1516
\end{tabular}    
Chengsong
parents:
diff changeset
  1517
\end{center}    
Chengsong
parents:
diff changeset
  1518
Chengsong
parents:
diff changeset
  1519
\noindent
Chengsong
parents:
diff changeset
  1520
The simplification does a pattern matching on the regular expression.
Chengsong
parents:
diff changeset
  1521
When it detected that the regular expression is an alternative or
Chengsong
parents:
diff changeset
  1522
sequence, it will try to simplify its children regular expressions
Chengsong
parents:
diff changeset
  1523
recursively and then see if one of the children turn into $\ZERO$ or
Chengsong
parents:
diff changeset
  1524
$\ONE$, which might trigger further simplification at the current level.
Chengsong
parents:
diff changeset
  1525
The most involved part is the $\textit{ALTS}$ clause, where we use two
Chengsong
parents:
diff changeset
  1526
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
Chengsong
parents:
diff changeset
  1527
$\textit{ALTS}$ and reduce as many duplicates as possible. Function
Chengsong
parents:
diff changeset
  1528
$\textit{distinct}$  keeps the first occurring copy only and remove all later ones
Chengsong
parents:
diff changeset
  1529
when detected duplicates. Function $\textit{flatten}$ opens up nested \textit{ALTS}.
Chengsong
parents:
diff changeset
  1530
Its recursive definition is given below:
Chengsong
parents:
diff changeset
  1531
Chengsong
parents:
diff changeset
  1532
 \begin{center}
Chengsong
parents:
diff changeset
  1533
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents:
diff changeset
  1534
  $\textit{flatten} \; (\textit{ALTS}\;bs\,as) :: as'$ & $\dn$ & $(\textit{map} \;
Chengsong
parents:
diff changeset
  1535
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
Chengsong
parents:
diff changeset
  1536
  $\textit{flatten} \; \textit{ZERO} :: as'$ & $\dn$ & $ \textit{flatten} \;  as' $ \\
Chengsong
parents:
diff changeset
  1537
    $\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; as'$ \quad(otherwise) 
Chengsong
parents:
diff changeset
  1538
\end{tabular}    
Chengsong
parents:
diff changeset
  1539
\end{center}  
Chengsong
parents:
diff changeset
  1540
Chengsong
parents:
diff changeset
  1541
\noindent
Chengsong
parents:
diff changeset
  1542
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
Chengsong
parents:
diff changeset
  1543
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
Chengsong
parents:
diff changeset
  1544
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
Chengsong
parents:
diff changeset
  1545
Chengsong
parents:
diff changeset
  1546
Suppose we apply simplification after each derivative step, and view
Chengsong
parents:
diff changeset
  1547
these two operations as an atomic one: $a \backslash_{simp}\,c \dn
Chengsong
parents:
diff changeset
  1548
\textit{simp}(a \backslash c)$. Then we can use the previous natural
Chengsong
parents:
diff changeset
  1549
extension from derivative w.r.t.~character to derivative
Chengsong
parents:
diff changeset
  1550
w.r.t.~string:%\comment{simp in  the [] case?}
Chengsong
parents:
diff changeset
  1551
Chengsong
parents:
diff changeset
  1552
\begin{center}
Chengsong
parents:
diff changeset
  1553
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1554
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
Chengsong
parents:
diff changeset
  1555
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
Chengsong
parents:
diff changeset
  1556
\end{tabular}
Chengsong
parents:
diff changeset
  1557
\end{center}
Chengsong
parents:
diff changeset
  1558
Chengsong
parents:
diff changeset
  1559
\noindent
Chengsong
parents:
diff changeset
  1560
we obtain an optimised version of the algorithm:
Chengsong
parents:
diff changeset
  1561
Chengsong
parents:
diff changeset
  1562
 \begin{center}
Chengsong
parents:
diff changeset
  1563
\begin{tabular}{lcl}
Chengsong
parents:
diff changeset
  1564
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
Chengsong
parents:
diff changeset
  1565
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
Chengsong
parents:
diff changeset
  1566
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents:
diff changeset
  1567
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents:
diff changeset
  1568
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents:
diff changeset
  1569
\end{tabular}
Chengsong
parents:
diff changeset
  1570
\end{center}
Chengsong
parents:
diff changeset
  1571
Chengsong
parents:
diff changeset
  1572
\noindent
Chengsong
parents:
diff changeset
  1573
This algorithm keeps the regular expression size small, for example,
Chengsong
parents:
diff changeset
  1574
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
Chengsong
parents:
diff changeset
  1575
will be reduced to just 6 and stays constant, no matter how long the
Chengsong
parents:
diff changeset
  1576
input string is.
Chengsong
parents:
diff changeset
  1577
Chengsong
parents:
diff changeset
  1578
Chengsong
parents:
diff changeset
  1579
Chengsong
parents:
diff changeset
  1580
\section{Current Work}
Chengsong
parents:
diff changeset
  1581
Chengsong
parents:
diff changeset
  1582
We are currently engaged in two tasks related to this algorithm. The
Chengsong
parents:
diff changeset
  1583
first task is proving that our simplification rules actually do not
Chengsong
parents:
diff changeset
  1584
affect the POSIX value that should be generated by the algorithm
Chengsong
parents:
diff changeset
  1585
according to the specification of a POSIX value and furthermore obtain a
Chengsong
parents:
diff changeset
  1586
much tighter bound on the sizes of derivatives. The result is that our
Chengsong
parents:
diff changeset
  1587
algorithm should be correct and faster on all inputs.  The original
Chengsong
parents:
diff changeset
  1588
blow-up, as observed in JavaScript, Python and Java, would be excluded
Chengsong
parents:
diff changeset
  1589
from happening in our algorithm. For this proof we use the theorem prover
Chengsong
parents:
diff changeset
  1590
Isabelle. Once completed, this result will advance the state-of-the-art:
Chengsong
parents:
diff changeset
  1591
Sulzmann and Lu wrote in their paper~\cite{Sulzmann2014} about the
Chengsong
parents:
diff changeset
  1592
bitcoded ``incremental parsing method'' (that is the lexing algorithm
Chengsong
parents:
diff changeset
  1593
outlined in this section):
Chengsong
parents:
diff changeset
  1594
Chengsong
parents:
diff changeset
  1595
\begin{quote}\it
Chengsong
parents:
diff changeset
  1596
  ``Correctness Claim: We further claim that the incremental parsing
Chengsong
parents:
diff changeset
  1597
  method in Figure~5 in combination with the simplification steps in
Chengsong
parents:
diff changeset
  1598
  Figure 6 yields POSIX parse tree [our lexical values]. We have tested this claim
Chengsong
parents:
diff changeset
  1599
  extensively by using the method in Figure~3 as a reference but yet
Chengsong
parents:
diff changeset
  1600
  have to work out all proof details.''
Chengsong
parents:
diff changeset
  1601
\end{quote}  
Chengsong
parents:
diff changeset
  1602
Chengsong
parents:
diff changeset
  1603
\noindent 
Chengsong
parents:
diff changeset
  1604
We like to settle this correctness claim. It is relatively
Chengsong
parents:
diff changeset
  1605
straightforward to establish that after one simplification step, the part of a
Chengsong
parents:
diff changeset
  1606
nullable derivative that corresponds to a POSIX value remains intact and can
Chengsong
parents:
diff changeset
  1607
still be collected, in other words, we can show that
Chengsong
parents:
diff changeset
  1608
%\comment{Double-check....I
Chengsong
parents:
diff changeset
  1609
%think this  is not the case}
Chengsong
parents:
diff changeset
  1610
%\comment{If i remember correctly, you have proved this lemma.
Chengsong
parents:
diff changeset
  1611
%I feel this is indeed not true because you might place arbitrary 
Chengsong
parents:
diff changeset
  1612
%bits on the regex r, however if this is the case, did i remember wrongly that
Chengsong
parents:
diff changeset
  1613
%you proved something like simplification does not affect $\textit{bmkeps}$ results?
Chengsong
parents:
diff changeset
  1614
%Anyway, i have amended this a little bit so it does not allow arbitrary bits attached
Chengsong
parents:
diff changeset
  1615
%to a regex. Maybe it works now.}
Chengsong
parents:
diff changeset
  1616
Chengsong
parents:
diff changeset
  1617
\begin{center}
Chengsong
parents:
diff changeset
  1618
	$\textit{bmkeps} \; a = \textit{bmkeps} \; \textit{bsimp} \; a\;($\textit{provided}$ \; a\; is \; \textit{bnullable} )$
Chengsong
parents:
diff changeset
  1619
\end{center} 
Chengsong
parents:
diff changeset
  1620
Chengsong
parents:
diff changeset
  1621
\noindent
Chengsong
parents:
diff changeset
  1622
as this basically comes down to proving actions like removing the
Chengsong
parents:
diff changeset
  1623
additional $r$ in $r+r$  does not delete important POSIX information in
Chengsong
parents:
diff changeset
  1624
a regular expression. The hard part of this proof is to establish that
Chengsong
parents:
diff changeset
  1625
Chengsong
parents:
diff changeset
  1626
\begin{center}
Chengsong
parents:
diff changeset
  1627
	$ \textit{blexer}\_{simp}(r, \; s) =  \textit{blexer}(r, \; s)$
Chengsong
parents:
diff changeset
  1628
\end{center}
Chengsong
parents:
diff changeset
  1629
%comment{This is not true either...look at the definion blexer/blexer-simp}
Chengsong
parents:
diff changeset
  1630
Chengsong
parents:
diff changeset
  1631
\noindent That is, if we do derivative on regular expression $r$ and then
Chengsong
parents:
diff changeset
  1632
simplify it, and repeat this process until we exhaust the string, we get a
Chengsong
parents:
diff changeset
  1633
regular expression $r''$($\textit{LHS}$)  that provides the POSIX matching
Chengsong
parents:
diff changeset
  1634
information, which is exactly the same as the result $r'$($\textit{RHS}$ of the
Chengsong
parents:
diff changeset
  1635
normal derivative algorithm that only does derivative repeatedly and has no
Chengsong
parents:
diff changeset
  1636
simplification at all.  This might seem at first glance very unintuitive, as
Chengsong
parents:
diff changeset
  1637
the $r'$ could be exponentially larger than $r''$, but can be explained in the
Chengsong
parents:
diff changeset
  1638
following way: we are pruning away the possible matches that are not POSIX.
Chengsong
parents:
diff changeset
  1639
Since there could be exponentially many 
Chengsong
parents:
diff changeset
  1640
non-POSIX matchings and only 1 POSIX matching, it
Chengsong
parents:
diff changeset
  1641
is understandable that our $r''$ can be a lot smaller.  we can still provide
Chengsong
parents:
diff changeset
  1642
the same POSIX value if there is one.  This is not as straightforward as the
Chengsong
parents:
diff changeset
  1643
previous proposition, as the two regular expressions $r'$ and $r''$ might have
Chengsong
parents:
diff changeset
  1644
become very different.  The crucial point is to find the
Chengsong
parents:
diff changeset
  1645
$\textit{POSIX}$  information of a regular expression and how it is modified,
Chengsong
parents:
diff changeset
  1646
augmented and propagated 
Chengsong
parents:
diff changeset
  1647
during simplification in parallel with the regular expression that
Chengsong
parents:
diff changeset
  1648
has not been simplified in the subsequent derivative operations.  To aid this,
Chengsong
parents:
diff changeset
  1649
we use the helper function retrieve described by Sulzmann and Lu:
Chengsong
parents:
diff changeset
  1650
\begin{center}
Chengsong
parents:
diff changeset
  1651
\begin{tabular}{@{}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
Chengsong
parents:
diff changeset
  1652
  $\textit{retrieve}\,(\textit{ONE}\,bs)\,\Empty$ & $\dn$ & $bs$\\
Chengsong
parents:
diff changeset
  1653
  $\textit{retrieve}\,(\textit{CHAR}\,bs\,c)\,(\Char\,d)$ & $\dn$ & $bs$\\
Chengsong
parents:
diff changeset
  1654
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Left\,v)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1655
     $bs \,@\, \textit{retrieve}\,a\,v$\\
Chengsong
parents:
diff changeset
  1656
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Right\,v)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1657
  $bs \,@\, \textit{retrieve}\,(\textit{ALTS}\,bs\,as)\,v$\\
Chengsong
parents:
diff changeset
  1658
  $\textit{retrieve}\,(\textit{SEQ}\,bs\,a_1\,a_2)\,(\Seq\,v_1\,v_2)$ & $\dn$ &
Chengsong
parents:
diff changeset
  1659
     $bs \,@\,\textit{retrieve}\,a_1\,v_1\,@\, \textit{retrieve}\,a_2\,v_2$\\
Chengsong
parents:
diff changeset
  1660
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,[])$ & $\dn$ &
Chengsong
parents:
diff changeset
  1661
     $bs \,@\, [\S]$\\
Chengsong
parents:
diff changeset
  1662
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,(v\!::\!vs))$ & $\dn$ &\\
Chengsong
parents:
diff changeset
  1663
  \multicolumn{3}{l}{
Chengsong
parents:
diff changeset
  1664
     \hspace{3cm}$bs \,@\, [\Z] \,@\, \textit{retrieve}\,a\,v\,@\,
Chengsong
parents:
diff changeset
  1665
                    \textit{retrieve}\,(\textit{STAR}\,[]\,a)\,(\Stars\,vs)$}\\
Chengsong
parents:
diff changeset
  1666
\end{tabular}
Chengsong
parents:
diff changeset
  1667
\end{center}
Chengsong
parents:
diff changeset
  1668
%\comment{Did not read further}\\
Chengsong
parents:
diff changeset
  1669
This function assembles the bitcode 
Chengsong
parents:
diff changeset
  1670
%that corresponds to a lexical value for how
Chengsong
parents:
diff changeset
  1671
%the current derivative matches the suffix of the string(the characters that
Chengsong
parents:
diff changeset
  1672
%have not yet appeared, but will appear as the successive derivatives go on.
Chengsong
parents:
diff changeset
  1673
%How do we get this "future" information? By the value $v$, which is
Chengsong
parents:
diff changeset
  1674
%computed by a pass of the algorithm that uses
Chengsong
parents:
diff changeset
  1675
%$inj$ as described in the previous section).  
Chengsong
parents:
diff changeset
  1676
using information from both the derivative regular expression and the
Chengsong
parents:
diff changeset
  1677
value. Sulzmann and Lu poroposed this function, but did not prove
Chengsong
parents:
diff changeset
  1678
anything about it. Ausaf and Urban used it to connect the bitcoded
Chengsong
parents:
diff changeset
  1679
algorithm to the older algorithm by the following equation:
Chengsong
parents:
diff changeset
  1680
 
Chengsong
parents:
diff changeset
  1681
 \begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\;
Chengsong
parents:
diff changeset
  1682
	 (r^\uparrow)\backslash_{simp} \,c)\,v)$ 
Chengsong
parents:
diff changeset
  1683
 \end{center} 
Chengsong
parents:
diff changeset
  1684
Chengsong
parents:
diff changeset
  1685
\noindent
Chengsong
parents:
diff changeset
  1686
whereby $r^\uparrow$ stands for the internalised version of $r$. Ausaf
Chengsong
parents:
diff changeset
  1687
and Urban also used this fact to prove  the correctness of bitcoded
Chengsong
parents:
diff changeset
  1688
algorithm without simplification.  Our purpose of using this, however,
Chengsong
parents:
diff changeset
  1689
is to establish 
Chengsong
parents:
diff changeset
  1690
Chengsong
parents:
diff changeset
  1691
\begin{center}
Chengsong
parents:
diff changeset
  1692
$ \textit{retrieve} \;
Chengsong
parents:
diff changeset
  1693
a \; v \;=\; \textit{retrieve}  \; (\textit{simp}\,a) \; v'.$
Chengsong
parents:
diff changeset
  1694
\end{center}
Chengsong
parents:
diff changeset
  1695
The idea is that using $v'$, a simplified version of $v$ that had gone
Chengsong
parents:
diff changeset
  1696
through the same simplification step as $\textit{simp}(a)$, we are able
Chengsong
parents:
diff changeset
  1697
to extract the bitcode that gives the same parsing information as the
Chengsong
parents:
diff changeset
  1698
unsimplified one. However, we noticed that constructing such a  $v'$
Chengsong
parents:
diff changeset
  1699
from $v$ is not so straightforward. The point of this is that  we might
Chengsong
parents:
diff changeset
  1700
be able to finally bridge the gap by proving
Chengsong
parents:
diff changeset
  1701
Chengsong
parents:
diff changeset
  1702
\begin{center}
Chengsong
parents:
diff changeset
  1703
$\textit{retrieve} \; (r^\uparrow   \backslash  s) \; v = \;\textit{retrieve} \;
Chengsong
parents:
diff changeset
  1704
(\textit{simp}(r^\uparrow)  \backslash  s) \; v'$
Chengsong
parents:
diff changeset
  1705
\end{center}
Chengsong
parents:
diff changeset
  1706
Chengsong
parents:
diff changeset
  1707
\noindent
Chengsong
parents:
diff changeset
  1708
and subsequently
Chengsong
parents:
diff changeset
  1709
Chengsong
parents:
diff changeset
  1710
\begin{center}
Chengsong
parents:
diff changeset
  1711
$\textit{retrieve} \; (r^\uparrow \backslash  s) \; v\; = \; \textit{retrieve} \;
Chengsong
parents:
diff changeset
  1712
(r^\uparrow  \backslash_{simp}  \, s) \; v'$.
Chengsong
parents:
diff changeset
  1713
\end{center}
Chengsong
parents:
diff changeset
  1714
Chengsong
parents:
diff changeset
  1715
\noindent
Chengsong
parents:
diff changeset
  1716
The $\textit{LHS}$ of the above equation is the bitcode we want. This
Chengsong
parents:
diff changeset
  1717
would prove that our simplified version of regular expression still
Chengsong
parents:
diff changeset
  1718
contains all the bitcodes needed. The task here is to find a way to
Chengsong
parents:
diff changeset
  1719
compute the correct $v'$.
Chengsong
parents:
diff changeset
  1720
Chengsong
parents:
diff changeset
  1721
The second task is to speed up the more aggressive simplification.  Currently
Chengsong
parents:
diff changeset
  1722
it is slower than the original naive simplification by Ausaf and Urban (the
Chengsong
parents:
diff changeset
  1723
naive version as implemented by Ausaf   and Urban of course can ``explode'' in
Chengsong
parents:
diff changeset
  1724
some cases).  It is therefore not surprising that the speed is also much slower
Chengsong
parents:
diff changeset
  1725
than regular expression engines in popular programming languages such as Java
Chengsong
parents:
diff changeset
  1726
and Python on most inputs that are linear. For example, just by rewriting the
Chengsong
parents:
diff changeset
  1727
example regular expression in the beginning of this report  $(a^*)^*\,b$ into
Chengsong
parents:
diff changeset
  1728
$a^*\,b$ would eliminate the ambiguity in the matching and make the time
Chengsong
parents:
diff changeset
  1729
for matching linear with respect to the input string size. This allows the 
Chengsong
parents:
diff changeset
  1730
DFA approach to become blindingly fast, and dwarf the speed of our current
Chengsong
parents:
diff changeset
  1731
implementation. For example, here is a comparison of Java regex engine 
Chengsong
parents:
diff changeset
  1732
and our implementation on this example.
Chengsong
parents:
diff changeset
  1733
Chengsong
parents:
diff changeset
  1734
\begin{center}
Chengsong
parents:
diff changeset
  1735
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
Chengsong
parents:
diff changeset
  1736
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  1737
\begin{axis}[
Chengsong
parents:
diff changeset
  1738
    xlabel={$n*1000$},
Chengsong
parents:
diff changeset
  1739
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  1740
    ylabel={time in secs},
Chengsong
parents:
diff changeset
  1741
    enlargelimits=false,
Chengsong
parents:
diff changeset
  1742
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  1743
    xmax=33,
Chengsong
parents:
diff changeset
  1744
    ymax=9,
Chengsong
parents:
diff changeset
  1745
    scaled ticks=true,
Chengsong
parents:
diff changeset
  1746
    axis lines=left,
Chengsong
parents:
diff changeset
  1747
    width=5cm,
Chengsong
parents:
diff changeset
  1748
    height=4cm, 
Chengsong
parents:
diff changeset
  1749
    legend entries={Bitcoded Algorithm},  
Chengsong
parents:
diff changeset
  1750
    legend pos=north west,
Chengsong
parents:
diff changeset
  1751
    legend cell align=left]
Chengsong
parents:
diff changeset
  1752
\addplot[red,mark=*, mark options={fill=white}] table {bad-scala.data};
Chengsong
parents:
diff changeset
  1753
\end{axis}
Chengsong
parents:
diff changeset
  1754
\end{tikzpicture}
Chengsong
parents:
diff changeset
  1755
  &
Chengsong
parents:
diff changeset
  1756
\begin{tikzpicture}
Chengsong
parents:
diff changeset
  1757
\begin{axis}[
Chengsong
parents:
diff changeset
  1758
    xlabel={$n*1000$},
Chengsong
parents:
diff changeset
  1759
    x label style={at={(1.05,-0.05)}},
Chengsong
parents:
diff changeset
  1760
    %ylabel={time in secs},
Chengsong
parents:
diff changeset
  1761
    enlargelimits=false,
Chengsong
parents:
diff changeset
  1762
    xtick={0,5,...,30},
Chengsong
parents:
diff changeset
  1763
    xmax=33,
Chengsong
parents:
diff changeset
  1764
    ymax=9,
Chengsong
parents:
diff changeset
  1765
    scaled ticks=false,
Chengsong
parents:
diff changeset
  1766
    axis lines=left,
Chengsong
parents:
diff changeset
  1767
    width=5cm,
Chengsong
parents:
diff changeset
  1768
    height=4cm, 
Chengsong
parents:
diff changeset
  1769
    legend entries={Java},  
Chengsong
parents:
diff changeset
  1770
    legend pos=north west,
Chengsong
parents:
diff changeset
  1771
    legend cell align=left]
Chengsong
parents:
diff changeset
  1772
\addplot[cyan,mark=*, mark options={fill=white}] table {good-java.data};
Chengsong
parents:
diff changeset
  1773
\end{axis}
Chengsong
parents:
diff changeset
  1774
\end{tikzpicture}\\
Chengsong
parents:
diff changeset
  1775
\multicolumn{3}{c}{Graphs: Runtime for matching $a^*\,b$ with strings 
Chengsong
parents:
diff changeset
  1776
           of the form $\underbrace{aa..a}_{n}$.}
Chengsong
parents:
diff changeset
  1777
\end{tabular}    
Chengsong
parents:
diff changeset
  1778
\end{center}  
Chengsong
parents:
diff changeset
  1779
Chengsong
parents:
diff changeset
  1780
Chengsong
parents:
diff changeset
  1781
Java regex engine can match string of thousands of characters in a few milliseconds,
Chengsong
parents:
diff changeset
  1782
whereas our current algorithm gets excruciatingly slow on input of this size.
Chengsong
parents:
diff changeset
  1783
The running time in theory is linear, however it does not appear to be the 
Chengsong
parents:
diff changeset
  1784
case in an actual implementation. So it needs to be explored how to
Chengsong
parents:
diff changeset
  1785
make our algorithm faster on all inputs.  It could be the recursive calls that are
Chengsong
parents:
diff changeset
  1786
needed to manipulate bits that are causing the slow down. A possible solution
Chengsong
parents:
diff changeset
  1787
is to write recursive functions into tail-recusive form.
Chengsong
parents:
diff changeset
  1788
Another possibility would be to explore
Chengsong
parents:
diff changeset
  1789
again the connection to DFAs to speed up the algorithm on 
Chengsong
parents:
diff changeset
  1790
subcalls that are small enough. This is very much work in progress.
Chengsong
parents:
diff changeset
  1791
Chengsong
parents:
diff changeset
  1792
\section{Conclusion}
Chengsong
parents:
diff changeset
  1793
Chengsong
parents:
diff changeset
  1794
In this PhD-project we are interested in fast algorithms for regular
Chengsong
parents:
diff changeset
  1795
expression matching. While this seems to be a ``settled'' area, in
Chengsong
parents:
diff changeset
  1796
fact interesting research questions are popping up as soon as one steps
Chengsong
parents:
diff changeset
  1797
outside the classic automata theory (for example in terms of what kind
Chengsong
parents:
diff changeset
  1798
of regular expressions are supported). The reason why it is
Chengsong
parents:
diff changeset
  1799
interesting for us to look at the derivative approach introduced by
Chengsong
parents:
diff changeset
  1800
Brzozowski for regular expression matching, and then much further
Chengsong
parents:
diff changeset
  1801
developed by Sulzmann and Lu, is that derivatives can elegantly deal
Chengsong
parents:
diff changeset
  1802
with some of the regular expressions that are of interest in ``real
Chengsong
parents:
diff changeset
  1803
life''. This includes the not-regular expression, written $\neg\,r$
Chengsong
parents:
diff changeset
  1804
(that is all strings that are not recognised by $r$), but also bounded
Chengsong
parents:
diff changeset
  1805
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is
Chengsong
parents:
diff changeset
  1806
also hope that the derivatives can provide another angle for how to
Chengsong
parents:
diff changeset
  1807
deal more efficiently with back-references, which are one of the
Chengsong
parents:
diff changeset
  1808
reasons why regular expression engines in JavaScript, Python and Java
Chengsong
parents:
diff changeset
  1809
choose to not implement the classic automata approach of transforming
Chengsong
parents:
diff changeset
  1810
regular expressions into NFAs and then DFAs---because we simply do not
Chengsong
parents:
diff changeset
  1811
know how such back-references can be represented by DFAs.
Chengsong
parents:
diff changeset
  1812
We also plan to implement the bitcoded algorithm
Chengsong
parents:
diff changeset
  1813
in some imperative language like C to see if the inefficiency of the 
Chengsong
parents:
diff changeset
  1814
Scala implementation
Chengsong
parents:
diff changeset
  1815
is language specific. To make this research more comprehensive we also plan
Chengsong
parents:
diff changeset
  1816
to contrast our (faster) version of bitcoded algorithm with the
Chengsong
parents:
diff changeset
  1817
Symbolic Regex Matcher, the RE2, the Rust Regex Engine, and the static
Chengsong
parents:
diff changeset
  1818
analysis approach by implementing them in the same language and then compare
Chengsong
parents:
diff changeset
  1819
their performance.
Chengsong
parents:
diff changeset
  1820
Chengsong
parents:
diff changeset
  1821
\bibliographystyle{plain}
Chengsong
parents:
diff changeset
  1822
\bibliography{root}
Chengsong
parents:
diff changeset
  1823
Chengsong
parents:
diff changeset
  1824
Chengsong
parents:
diff changeset
  1825
\end{document}