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