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