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