ninems/ninems.tex
author Chengsong
Sat, 08 Feb 2020 21:34:50 +0000
changeset 125 788f4aa28bc5
parent 88 bb1ff936e6cf
permissions -rw-r--r--
added a bit?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
60cb82639691 spell check
Christian Urban <urbanc@in.tum.de>
parents: 44
diff changeset
     1
\documentclass[a4paper,UKenglish]{lipics}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     2
\usepackage{graphic}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     3
\usepackage{data}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
     4
\usepackage{tikz-cd}
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
     5
%\usepackage{algorithm}
35
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
     6
\usepackage{amsmath}
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
     7
\usepackage[noend]{algpseudocode}
42
Chengsong
parents: 41
diff changeset
     8
\usepackage{enumitem}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
     9
\usepackage{nccmath}
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    10
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    11
\definecolor{darkblue}{rgb}{0,0,0.6}
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    12
\hypersetup{colorlinks=true,allcolors=darkblue}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    13
\newcommand{\comment}[1]%
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    14
{{\color{red}$\Rightarrow$}\marginpar{\raggedright\small{\bf\color{red}#1}}}
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    15
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    16
% \documentclass{article}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    17
%\usepackage[utf8]{inputenc}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    18
%\usepackage[english]{babel}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    19
%\usepackage{listings}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    20
% \usepackage{amsthm}
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    21
%\usepackage{hyperref}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    22
% \usepackage[margin=0.5in]{geometry}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    23
%\usepackage{pmboxdraw}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    24
 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    25
\title{POSIX Regular Expression Matching and Lexing}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    26
\author{Chengsong Tan}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    27
\affil{King's College London\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    28
London, UK\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    29
\texttt{chengsong.tan@kcl.ac.uk}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    30
\authorrunning{Chengsong Tan}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    31
\Copyright{Chengsong Tan}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    32
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    33
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    34
\newcommand{\ZERO}{\mbox{\bf 0}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    35
\newcommand{\ONE}{\mbox{\bf 1}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    36
\def\lexer{\mathit{lexer}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    37
\def\mkeps{\mathit{mkeps}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    38
\def\inj{\mathit{inj}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    39
\def\Empty{\mathit{Empty}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    40
\def\Left{\mathit{Left}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    41
\def\Right{\mathit{Right}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    42
\def\Stars{\mathit{Stars}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    43
\def\Char{\mathit{Char}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    44
\def\Seq{\mathit{Seq}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    45
\def\Der{\mathit{Der}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    46
\def\nullable{\mathit{nullable}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    47
\def\Z{\mathit{Z}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    48
\def\S{\mathit{S}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    49
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    50
%\theoremstyle{theorem}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    51
%\newtheorem{theorem}{Theorem}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    52
%\theoremstyle{lemma}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    53
%\newtheorem{lemma}{Lemma}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    54
%\newcommand{\lemmaautorefname}{Lemma}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    55
%\theoremstyle{definition}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    56
%\newtheorem{definition}{Definition}
35
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    57
\algnewcommand\algorithmicswitch{\textbf{switch}}
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    58
\algnewcommand\algorithmiccase{\textbf{case}}
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    59
\algnewcommand\algorithmicassert{\texttt{assert}}
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    60
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}%
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    61
% New "environments"
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    62
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    63
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    64
\algtext*{EndSwitch}%
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
    65
\algtext*{EndCase}%
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    66
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    67
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    68
\begin{document}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    69
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    70
\maketitle
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    71
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    72
\begin{abstract}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    73
  Brzozowski introduced in 1964 a beautifully simple algorithm for
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    74
  regular expression matching based on the notion of derivatives of
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    75
  regular expressions. In 2014, Sulzmann and Lu extended this
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    76
  algorithm to not just give a YES/NO answer for whether or not a
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
    77
  regular expression matches a string, but in case it does also
58
f0360e17080e proofread
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
    78
  answers with \emph{how} it matches the string.  This is important for
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    79
  applications such as lexing (tokenising a string). The problem is to
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    80
  make the algorithm by Sulzmann and Lu fast on all inputs without
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    81
  breaking its correctness. We have already developed some
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
    82
  simplification rules for this, but have not yet proved that they
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    83
  preserve the correctness of the algorithm. We also have not yet
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    84
  looked at extended regular expressions, such as bounded repetitions,
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    85
  negation and back-references.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    86
\end{abstract}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    87
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    88
\section{Introduction}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    89
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
    90
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    91
This PhD-project is about regular expression matching and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    92
lexing. Given the maturity of this topic, the reader might wonder:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    93
Surely, regular expressions must have already been studied to death?
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    94
What could possibly be \emph{not} known in this area? And surely all
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    95
implemented algorithms for regular expression matching are blindingly
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    96
fast?
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    97
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    98
Unfortunately these preconceptions are not supported by evidence: Take
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
    99
for example the regular expression $(a^*)^*\,b$ and ask whether
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   100
strings of the form $aa..a$ match this regular
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   101
expression. Obviously this is not the case---the expected $b$ in the last
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   102
position is missing. One would expect that modern regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   103
matching engines can find this out very quickly. Alas, if one tries
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   104
this example in JavaScript, Python or Java 8 with strings like 28
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   105
$a$'s, one discovers that this decision takes around 30 seconds and
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   106
takes considerably longer when adding a few more $a$'s, as the graphs
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   107
below show:
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   108
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   109
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   110
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   111
\begin{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   112
\begin{axis}[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   113
    xlabel={$n$},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   114
    x label style={at={(1.05,-0.05)}},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   115
    ylabel={time in secs},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   116
    enlargelimits=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   117
    xtick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   118
    xmax=33,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   119
    ymax=35,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   120
    ytick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   121
    scaled ticks=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   122
    axis lines=left,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   123
    width=5cm,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   124
    height=4cm, 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   125
    legend entries={JavaScript},  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   126
    legend pos=north west,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   127
    legend cell align=left]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   128
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   129
\end{axis}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   130
\end{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   131
  &
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   132
\begin{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   133
\begin{axis}[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   134
    xlabel={$n$},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   135
    x label style={at={(1.05,-0.05)}},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   136
    %ylabel={time in secs},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   137
    enlargelimits=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   138
    xtick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   139
    xmax=33,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   140
    ymax=35,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   141
    ytick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   142
    scaled ticks=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   143
    axis lines=left,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   144
    width=5cm,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   145
    height=4cm, 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   146
    legend entries={Python},  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   147
    legend pos=north west,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   148
    legend cell align=left]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   149
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   150
\end{axis}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   151
\end{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   152
  &
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   153
\begin{tikzpicture}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   154
\begin{axis}[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   155
    xlabel={$n$},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   156
    x label style={at={(1.05,-0.05)}},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   157
    %ylabel={time in secs},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   158
    enlargelimits=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   159
    xtick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   160
    xmax=33,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   161
    ymax=35,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   162
    ytick={0,5,...,30},
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   163
    scaled ticks=false,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   164
    axis lines=left,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   165
    width=5cm,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   166
    height=4cm, 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   167
    legend entries={Java 8},  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   168
    legend pos=north west,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   169
    legend cell align=left]
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   170
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   171
\end{axis}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   172
\end{tikzpicture}\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   173
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   174
           of the form $\underbrace{aa..a}_{n}$.}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   175
\end{tabular}    
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   176
\end{center}  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   177
58
f0360e17080e proofread
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   178
\noindent These are clearly abysmal and possibly surprising results. One
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   179
would expect these systems to do  much better than that---after all,
58
f0360e17080e proofread
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   180
given a DFA and a string, deciding whether a string is matched by this
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   181
DFA should be linear in terms of the size of the regular expression and
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   182
the string?
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   183
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   184
Admittedly, the regular expression $(a^*)^*\,b$ is carefully chosen to
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   185
exhibit this super-linear behaviour.  But unfortunately, such regular
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   186
expressions are not just a few outliers. They are actually 
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   187
frequent enough to have a separate name created for
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   188
them---\emph{evil regular expressions}. In empiric work, Davis et al
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   189
report that they have found thousands of such evil regular expressions
87
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
   190
in the JavaScript and Python ecosystems \cite{Davis18}. Static analysis
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
   191
approach that is both sound and complete exists\cite{17Bir}, but the running 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
   192
time on certain examples in the RegExLib and Snort regular expressions
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
   193
libraries is unacceptable. Therefore the problem of efficiency still remains.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   194
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   195
This superlinear blowup in matching algorithms sometimes causes
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   196
considerable grief in real life: for example on 20 July 2016 one evil
58
f0360e17080e proofread
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   197
regular expression brought the webpage
f0360e17080e proofread
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   198
\href{http://stackexchange.com}{Stack Exchange} to its
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   199
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   200
In this instance, a regular expression intended to just trim white
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   201
spaces from the beginning and the end of a line actually consumed
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   202
massive amounts of CPU-resources---causing web servers to grind to a
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   203
halt. This happened when a post with 20,000 white spaces was submitted,
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   204
but importantly the white spaces were neither at the beginning nor at
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   205
the end. As a result, the regular expression matching engine needed to
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   206
backtrack over many choices. In this example, the time needed to process
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   207
the string was $O(n^2)$ with respect to the string length. This
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   208
quadratic overhead was enough for the homepage of Stack Exchange to
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   209
respond so slowly that the load balancer assumed there must be some
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   210
attack and therefore stopped the servers from responding to any
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   211
requests. This made the whole site become unavailable. Another very
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   212
recent example is a global outage of all Cloudflare servers on 2 July
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   213
2019. A poorly written regular expression exhibited exponential
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   214
behaviour and exhausted CPUs that serve HTTP traffic. Although the
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   215
outage had several causes, at the heart was a regular expression that
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   216
was used to monitor network
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   217
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}}
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   218
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   219
The underlying problem is that many ``real life'' regular expression
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   220
matching engines do not use DFAs for matching. This is because they
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   221
support regular expressions that are not covered by the classical
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   222
automata theory, and in this more general setting there are quite a few
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   223
research questions still unanswered and fast algorithms still need to be
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   224
developed (for example how to treat efficiently bounded repetitions, negation and
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   225
back-references).
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   226
%question: dfa can have exponential states. isn't this the actual reason why they do not use dfas?
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   227
%how do they avoid dfas exponential states if they use them for fast matching?
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   228
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   229
There is also another under-researched problem to do with regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   230
expressions and lexing, i.e.~the process of breaking up strings into
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   231
sequences of tokens according to some regular expressions. In this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   232
setting one is not just interested in whether or not a regular
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   233
expression matches a string, but also in \emph{how}.  Consider for
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   234
example a regular expression $r_{key}$ for recognising keywords such as
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   235
\textit{if}, \textit{then} and so on; and a regular expression $r_{id}$
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   236
for recognising identifiers (say, a single character followed by
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   237
characters or numbers). One can then form the compound regular
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   238
expression $(r_{key} + r_{id})^*$ and use it to tokenise strings.  But
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   239
then how should the string \textit{iffoo} be tokenised?  It could be
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   240
tokenised as a keyword followed by an identifier, or the entire string
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   241
as a single identifier.  Similarly, how should the string \textit{if} be
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   242
tokenised? Both regular expressions, $r_{key}$ and $r_{id}$, would
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   243
``fire''---so is it an identifier or a keyword?  While in applications
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   244
there is a well-known strategy to decide these questions, called POSIX
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   245
matching, only relatively recently precise definitions of what POSIX
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   246
matching actually means have been formalised
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   247
\cite{AusafDyckhoffUrban2016,OkuiSuzuki2010,Vansummeren2006}. Such a
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   248
definition has also been given by Sulzmann and  Lu \cite{Sulzmann2014},
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   249
but the corresponding correctness proof turned out to be  faulty
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   250
\cite{AusafDyckhoffUrban2016}. Roughly, POSIX matching means matching
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   251
the longest initial substring. In the case of a tie, the initial
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   252
sub-match is chosen according to some priorities attached to the regular
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   253
expressions (e.g.~keywords have a higher priority than identifiers).
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   254
This sounds rather simple, but according to Grathwohl et al \cite[Page
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   255
36]{CrashCourse2014} this is not the case. They wrote:
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   256
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   257
\begin{quote}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   258
\it{}``The POSIX strategy is more complicated than the greedy because of 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   259
the dependence on information about the length of matched strings in the 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   260
various subexpressions.''
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   261
\end{quote}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   262
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   263
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   264
This is also supported by evidence collected by Kuklewicz
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   265
\cite{Kuklewicz} who noticed that a number of POSIX regular expression
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   266
matchers calculate incorrect results.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   267
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   268
Our focus in this project is on an algorithm introduced by Sulzmann and
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   269
Lu in 2014 for regular expression matching according to the POSIX
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   270
strategy \cite{Sulzmann2014}. Their algorithm is based on an older
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   271
algorithm by Brzozowski from 1964 where he introduced the notion of
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   272
derivatives of regular expressions~\cite{Brzozowski1964}. We shall
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   273
briefly explain this algorithm next.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   274
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   275
\section{The Algorithm by Brzozowski based on Derivatives of Regular
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   276
Expressions}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   277
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   278
Suppose (basic) regular expressions are given by the following grammar:
38
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   279
\[			r ::=   \ZERO \mid  \ONE
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   280
			 \mid  c  
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   281
			 \mid  r_1 \cdot r_2
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   282
			 \mid  r_1 + r_2   
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   283
			 \mid r^*         
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   284
\]
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   285
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   286
\noindent
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   287
The intended meaning of the constructors is as follows: $\ZERO$
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   288
cannot match any string, $\ONE$ can match the empty string, the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   289
character regular expression $c$ can match the character $c$, and so
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   290
on.
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   291
58
f0360e17080e proofread
Christian Urban <urbanc@in.tum.de>
parents: 57
diff changeset
   292
The ingenious contribution by Brzozowski is the notion of
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   293
\emph{derivatives} of regular expressions.  The idea behind this
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   294
notion is as follows: suppose a regular expression $r$ can match a
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   295
string of the form $c\!::\! s$ (that is a list of characters starting
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   296
with $c$), what does the regular expression look like that can match
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   297
just $s$? Brzozowski gave a neat answer to this question. He started
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   298
with the definition of $nullable$:
36
Chengsong
parents: 35
diff changeset
   299
\begin{center}
Chengsong
parents: 35
diff changeset
   300
		\begin{tabular}{lcl}
Chengsong
parents: 35
diff changeset
   301
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
Chengsong
parents: 35
diff changeset
   302
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
Chengsong
parents: 35
diff changeset
   303
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
Chengsong
parents: 35
diff changeset
   304
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
Chengsong
parents: 35
diff changeset
   305
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
Chengsong
parents: 35
diff changeset
   306
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
Chengsong
parents: 35
diff changeset
   307
		\end{tabular}
Chengsong
parents: 35
diff changeset
   308
	\end{center}
38
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   309
This function simply tests whether the empty string is in $L(r)$.
36
Chengsong
parents: 35
diff changeset
   310
He then defined
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   311
the following operation on regular expressions, written
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   312
$r\backslash c$ (the derivative of $r$ w.r.t.~the character $c$):
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   313
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   314
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   315
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   316
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   317
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   318
		$d \backslash c$     & $\dn$ & 
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   319
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   320
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
36
Chengsong
parents: 35
diff changeset
   321
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   322
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   323
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   324
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   325
\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   326
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   327
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   328
%Assuming the classic notion of a
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   329
%\emph{language} of a regular expression, written $L(\_)$, t
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   330
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   331
\noindent
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   332
The main property of the derivative operation is that
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   333
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   334
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   335
$c\!::\!s \in L(r)$ holds
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   336
if and only if $s \in L(r\backslash c)$.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   337
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   338
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   339
\noindent
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   340
For us the main advantage is that derivatives can be
38
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   341
straightforwardly implemented in any functional programming language,
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   342
and are easily definable and reasoned about in theorem provers---the
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   343
definitions just consist of inductive datatypes and simple recursive
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   344
functions. Moreover, the notion of derivatives can be easily
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   345
generalised to cover extended regular expression constructors such as
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   346
the not-regular expression, written $\neg\,r$, or bounded repetitions
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   347
(for example $r^{\{n\}}$ and $r^{\{n..m\}}$), which cannot be so
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   348
straightforwardly realised within the classic automata approach.
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   349
For the moment however, we focus only on the usual basic regular expressions.
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   350
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   351
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   352
Now if we want to find out whether a string $s$ matches with a regular
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   353
expression $r$, we can build the derivatives of $r$ w.r.t.\ (in succession)
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   354
all the characters of the string $s$. Finally, test whether the
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   355
resulting regular expression can match the empty string.  If yes, then
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   356
$r$ matches $s$, and no in the negative case. To implement this idea
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   357
we can generalise the derivative operation to strings like this:
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   358
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   359
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   360
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   361
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   362
$r \backslash [\,] $ & $\dn$ & $r$
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   363
\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   364
\end{center}
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   365
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   366
\noindent
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   367
and then define as  regular-expression matching algorithm: 
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   368
\[
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   369
match\;s\;r \;\dn\; nullable(r\backslash s)
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   370
\]
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   371
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   372
\noindent
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   373
This algorithm looks graphically as follows:
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   374
\begin{equation}\label{graph:*}
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   375
\begin{tikzcd}
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   376
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}
38
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   377
\end{tikzcd}
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   378
\end{equation}
40
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   379
8063792920ef proof reading
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
   380
\noindent
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   381
where we start with  a regular expression  $r_0$, build successive
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   382
derivatives until we exhaust the string and then use \textit{nullable}
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   383
to test whether the result can match the empty string. It can  be
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   384
relatively  easily shown that this matcher is correct  (that is given
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   385
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   386
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   387
 
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   388
\section{Values and the Algorithm by Sulzmann and Lu}
38
b5363c0dcd99 half easy changes
Chengsong
parents: 37
diff changeset
   389
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   390
One limitation of Brzozowski's algorithm is that it only produces a
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   391
YES/NO answer for whether a string is being matched by a regular
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   392
expression.  Sulzmann and Lu~\cite{Sulzmann2014} extended this algorithm
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   393
to allow generation of an actual matching, called a \emph{value} or
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   394
sometimes also \emph{lexical value}.  These values and regular
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   395
expressions correspond to each other as illustrated in the following
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   396
table:
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   397
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   398
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   399
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   400
	\begin{tabular}{c@{\hspace{20mm}}c}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   401
		\begin{tabular}{@{}rrl@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   402
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   403
			$r$ & $::=$  & $\ZERO$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   404
			& $\mid$ & $\ONE$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   405
			& $\mid$ & $c$          \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   406
			& $\mid$ & $r_1 \cdot r_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   407
			& $\mid$ & $r_1 + r_2$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   408
			\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   409
			& $\mid$ & $r^*$         \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   410
		\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   411
		&
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   412
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   413
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   414
			$v$ & $::=$  & \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   415
			&        & $\Empty$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   416
			& $\mid$ & $\Char(c)$          \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   417
			& $\mid$ & $\Seq\,v_1\, v_2$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   418
			& $\mid$ & $\Left(v)$   \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   419
			& $\mid$ & $\Right(v)$  \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   420
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   421
		\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   422
	\end{tabular}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   423
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   424
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   425
\noindent
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   426
No value  corresponds to $\ZERO$; $\Empty$ corresponds to $\ONE$;
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   427
$\Char$ to the character regular expression; $\Seq$ to the sequence
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   428
regular expression and so on. The idea of values is to encode a kind of
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   429
lexical value for how the sub-parts of a regular expression match the
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   430
sub-parts of a string. To see this, suppose a \emph{flatten} operation,
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   431
written $|v|$ for values. We can use this function to extract the
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   432
underlying string of a value $v$. For example, $|\mathit{Seq} \,
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   433
(\textit{Char x}) \, (\textit{Char y})|$ is the string $xy$.  Using
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   434
flatten, we can describe how values encode lexical values: $\Seq\,v_1\,
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   435
v_2$ encodes a tree with two children nodes that tells how the string
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   436
$|v_1| @ |v_2|$ matches the regex $r_1 \cdot r_2$ whereby $r_1$ matches
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   437
the substring $|v_1|$ and, respectively, $r_2$ matches the substring
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   438
$|v_2|$. Exactly how these two are matched is contained in the children
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   439
nodes $v_1$ and $v_2$ of parent $\textit{Seq}$. 
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   440
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   441
To give a concrete example of how values work, consider the string $xy$
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   442
and the regular expression $(x + (y + xy))^*$. We can view this regular
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   443
expression as a tree and if the string $xy$ is matched by two Star
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   444
``iterations'', then the $x$ is matched by the left-most alternative in
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   445
this tree and the $y$ by the right-left alternative. This suggests to
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   446
record this matching as
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   447
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   448
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   449
$\Stars\,[\Left\,(\Char\,x), \Right(\Left(\Char\,y))]$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   450
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   451
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   452
\noindent
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   453
where $\Stars \; [\ldots]$ records all the
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   454
iterations; and $\Left$, respectively $\Right$, which
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   455
alternative is used. The value for
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   456
matching $xy$ in a single ``iteration'', i.e.~the POSIX value,
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   457
would look as follows
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   458
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   459
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   460
$\Stars\,[\Seq\,(\Char\,x)\,(\Char\,y)]$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   461
\end{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   462
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   463
\noindent
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   464
where $\Stars$ has only a single-element list for the single iteration
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   465
and $\Seq$ indicates that $xy$ is matched by a sequence regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   466
expression.
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   467
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   468
The contribution of Sulzmann and Lu is an extension of Brzozowski's
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   469
algorithm by a second phase (the first phase being building successive
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   470
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value 
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   471
is generated in case the regular expression matches  the string. 
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   472
Pictorially, the Sulzmann and Lu algorithm is as follows:
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   473
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   474
\begin{ceqn}
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   475
\begin{equation}\label{graph:2}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   476
\begin{tikzcd}
36
Chengsong
parents: 35
diff changeset
   477
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] \\
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   478
v_0           & v_1 \arrow[l,"inj_{r_0} c_0"]                & v_2 \arrow[l, "inj_{r_1} c_1"]              & v_n \arrow[l, dashed]         
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   479
\end{tikzcd}
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   480
\end{equation}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   481
\end{ceqn}
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   482
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   483
\noindent
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   484
For convenience, we shall employ the following notations: the regular
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   485
expression we start with is $r_0$, and the given string $s$ is composed
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   486
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   487
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   488
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   489
the derivative $r_n$. We test whether this derivative is
46
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   490
$\textit{nullable}$ or not. If not, we know the string does not match
9b48724ec609 proofread
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
   491
$r$ and no value needs to be generated. If yes, we start building the
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   492
values incrementally by \emph{injecting} back the characters into the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   493
earlier values $v_n, \ldots, v_0$. This is the second phase of the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   494
algorithm from the right to left. For the first value $v_n$, we call the
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   495
function $\textit{mkeps}$, which builds the lexical value
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   496
for how the empty string has been matched by the (nullable) regular
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   497
expression $r_n$. This function is defined as
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   498
51
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   499
	\begin{center}
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   500
		\begin{tabular}{lcl}
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   501
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   502
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   503
			& \textit{if} $\nullable(r_{1})$\\ 
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   504
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   505
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   506
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   507
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   508
		\end{tabular}
5df7faf69238 added mkeps and pder, still have not proof read it
Chengsong
parents: 50
diff changeset
   509
	\end{center}
41
a1f90febbc7f example
Chengsong
parents: 40
diff changeset
   510
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   511
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   512
\noindent There are no cases for $\ZERO$ and $c$, since
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   513
these regular expression cannot match the empty string. Note
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   514
also that in case of alternatives we give preference to the
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   515
regular expression on the left-hand side. This will become
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   516
important later on about what value is calculated.
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   517
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   518
After the $\mkeps$-call, we inject back the characters one by one in order to build
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   519
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   520
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   521
After injecting back $n$ characters, we get the lexical value for how $r_0$
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   522
matches $s$. For this Sulzmann and Lu defined a function that reverses
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   523
the ``chopping off'' of characters during the derivative phase. The
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   524
corresponding function is called \emph{injection}, written
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   525
$\textit{inj}$; it takes three arguments: the first one is a regular
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   526
expression ${r_{i-1}}$, before the character is chopped off, the second
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   527
is a character ${c_{i-1}}$, the character we want to inject and the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   528
third argument is the value ${v_i}$, into which one wants to inject the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   529
character (it corresponds to the regular expression after the character
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   530
has been chopped off). The result of this function is a new value. The
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   531
definition of $\textit{inj}$ is as follows: 
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   532
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   533
\begin{center}
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   534
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   535
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   536
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   537
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   538
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   539
  $\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)$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   540
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   541
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   542
\end{tabular}
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   543
\end{center}
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   544
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   545
\noindent This definition is by recursion on the ``shape'' of regular
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   546
expressions and values. To understands this definition better consider
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   547
the situation when we build the derivative on regular expression $r_{i-1}$.
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   548
For this we chop off a character from $r_{i-1}$ to form $r_i$. This leaves a
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   549
``hole'' in $r_i$ and its corresponding value $v_i$. 
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   550
To calculate $v_{i-1}$, we need to
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   551
locate where that hole is and fill it. 
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   552
We can find this location by
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   553
comparing $r_{i-1}$ and $v_i$. For instance, if $r_{i-1}$ is of shape
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   554
$r_a \cdot r_b$, and $v_i$ is of shape $\Left(Seq(v_1,v_2))$, we know immediately that 
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   555
%
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   556
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b \,+\, r_b\backslash c,\]
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   557
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   558
\noindent
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   559
otherwise if $r_a$ is not nullable,
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   560
\[ (r_a \cdot r_b)\backslash c = (r_a\backslash c) \cdot r_b,\]
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   561
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   562
\noindent
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   563
the value $v_i$ should be  $\Seq(\ldots)$, contradicting the fact that
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   564
$v_i$ is actually of shape $\Left(\ldots)$. Furthermore, since $v_i$ is of shape
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   565
$\Left(\ldots)$ instead of $\Right(\ldots)$, we know that the left
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   566
branch of \[ (r_a \cdot r_b)\backslash c =
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   567
\bold{\underline{ (r_a\backslash c) \cdot r_b} }\,+\, r_b\backslash c,\](underlined)
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   568
 is taken instead of the right one. This means $c$ is chopped off 
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   569
from $r_a$ rather than $r_b$.
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   570
We have therefore found out 
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   571
that the hole will be on $r_a$. So we recursively call $\inj\, 
64
afd0d702a4fe some changes
Chengsong
parents: 63
diff changeset
   572
r_a\,c\,v_a$ to fill that hole in $v_a$. After injection, the value 
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   573
$v_i$ for $r_i = r_a \cdot r_b$ should be $\Seq\,(\inj\,r_a\,c\,v_a)\,v_b$.
60
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   574
Other clauses can be understood in a similar way.
59
8ff7b7508824 changes1
Chengsong
parents: 58
diff changeset
   575
71
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   576
%\comment{Other word: insight?}
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   577
The following example gives an insight of $\textit{inj}$'s effect and
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   578
how Sulzmann and Lu's algorithm works as a whole. Suppose we have a
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   579
regular expression $((((a+b)+ab)+c)+abc)^*$, and want to match it
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   580
against the string $abc$ (when $abc$ is written as a regular expression,
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   581
the standard way of expressing it is $a \cdot (b \cdot c)$. But we
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   582
usually omit the parentheses and dots here for better readability. This
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   583
algorithm returns a POSIX value, which means it will produce the longest
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   584
matching. Consequently, it matches the string $abc$ in one star
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   585
iteration, using the longest alternative $abc$ in the sub-expression (we shall use $r$ to denote this
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   586
sub-expression for conciseness):
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   587
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   588
\[((((a+b)+ab)+c)+\underbrace{abc}_r)\] 
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   589
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   590
\noindent
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   591
Before $\textit{inj}$ is called, our lexer first builds derivative using
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   592
string $abc$ (we simplified some regular expressions like $\ZERO \cdot
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   593
b$ to $\ZERO$ for conciseness; we also omit parentheses if they are
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   594
clear from the context):
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   595
60
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   596
%Similarly, we allow
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   597
%$\textit{ALT}$ to take a list of regular expressions as an argument
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   598
%instead of just 2 operands to reduce the nested depth of
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   599
%$\textit{ALT}$
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   600
60
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   601
\begin{center}
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   602
\begin{tabular}{lcl}
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   603
$r^*$ & $\xrightarrow{\backslash a}$ & $r_1 = (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r^*$\\
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   604
      & $\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^*$\\
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   605
      & $\xrightarrow{\backslash c}$ & $r_3 = ((\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot \ONE) \cdot r^* + (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^*) + $\\ 
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   606
      &                              & $\phantom{r_3 = (} ((\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^* + (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^* )$
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   607
\end{tabular}
60
c737a0259194 sorry not all done, need a few more mins for last few changes
Chengsong
parents: 59
diff changeset
   608
\end{center}
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   609
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   610
\noindent
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   611
In  case $r_3$ is nullable, we can call $\textit{mkeps}$ 
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   612
to construct a lexical value for how $r_3$ matched the string $abc$. 
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   613
This function gives the following value $v_3$: 
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   614
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   615
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   616
\begin{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   617
$\Left(\Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty,\Empty))), \Stars [])))$
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   618
\end{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   619
The outer $\Left(\Left(\ldots))$ tells us the leftmost nullable part of $r_3$(underlined):
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   620
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   621
\begin{center}
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   622
	\begin{tabular}{l@{\hspace{2mm}}l}
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   623
    & $\big(\underline{(\ZERO+\ZERO+\ZERO+ \ZERO+ \ONE \cdot \ONE \cdot \ONE) \cdot r^*} 
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   624
    \;+\; (\ZERO+\ZERO+\ZERO + \ONE + \ZERO) \cdot r^*\big)$ \smallskip\\
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   625
    $+$ & $\big((\ZERO+\ONE+\ZERO  + \ZERO + \ZERO) \cdot r^*
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   626
    \;+\; (\ZERO+\ZERO+\ZERO  + \ONE + \ZERO) \cdot r^* \big)$
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   627
  	\end{tabular}
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   628
 \end{center}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   629
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   630
\noindent
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   631
 Note that the leftmost location of term $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot \ONE \cdot
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   632
 \ONE) \cdot r^*$ (which corresponds to the initial sub-match $abc$) allows
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   633
 $\textit{mkeps}$ to pick it up because $\textit{mkeps}$ is defined to always choose the
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   634
 left one when it is nullable. In the case of this example, $abc$ is
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   635
 preferred over $a$ or $ab$. This $\Left(\Left(\ldots))$ location is
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   636
 generated by two applications of the splitting clause
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   637
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   638
\begin{center}
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   639
     $(r_1 \cdot r_2)\backslash c  \;\;(when \; r_1 \; nullable) \, = (r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c.$
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   640
\end{center}
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   641
       
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   642
\noindent
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   643
By this clause, we put $r_1 \backslash c \cdot r_2 $ at the
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   644
$\textit{front}$ and $r_2 \backslash c$ at the $\textit{back}$. This
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   645
allows $\textit{mkeps}$ to always pick up among two matches the one with a longer
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   646
initial sub-match. Removing the outside $\Left(\Left(...))$, the inside
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   647
sub-value 
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   648
 
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   649
\begin{center}
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   650
 $\Seq(\Right(\Seq(\Empty, \Seq(\Empty, \Empty))), \Stars [])$
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   651
\end{center}
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   652
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   653
\noindent
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   654
tells us how the empty string $[]$ is matched with $(\ZERO+\ZERO+\ZERO + \ZERO + \ONE \cdot
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   655
\ONE \cdot \ONE) \cdot r^*$. We match $[]$ by a sequence of two nullable regular
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   656
expressions. The first one is an alternative, we take the rightmost
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   657
alternative---whose language contains the empty string. The second
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   658
nullable regular expression is a Kleene star. $\Stars$ tells us how it
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   659
generates the nullable regular expression: by 0 iterations to form
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   660
$\ONE$. Now $\textit{inj}$ injects characters back and incrementally
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   661
builds a lexical value based on $v_3$. Using the value $v_3$, the character
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   662
c, and the regular expression $r_2$, we can recover how $r_2$ matched
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   663
the string $[c]$ : $\textit{inj} \; r_2 \; c \; v_3$ gives us
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   664
 \begin{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   665
 $v_2 = \Left(\Seq(\Right(\Seq(\Empty, \Seq(\Empty, c))), \Stars [])),$
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   666
 \end{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   667
which tells us how $r_2$ matched $[c]$. After this we inject back the character $b$, and get
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   668
\begin{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   669
$v_1 = \Seq(\Right(\Seq(\Empty, \Seq(b, c))), \Stars [])$
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   670
\end{center}
61
580c7b84f900 hi finished
Chengsong
parents: 60
diff changeset
   671
 for how 
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   672
 \begin{center}
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   673
 $r_1= (\ONE+\ZERO+\ONE \cdot b + \ZERO + \ONE \cdot b \cdot c) \cdot r*$
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   674
 \end{center}
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   675
  matched  the string $bc$ before it split into two substrings. 
61
580c7b84f900 hi finished
Chengsong
parents: 60
diff changeset
   676
  Finally, after injecting character $a$ back to $v_1$, 
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   677
  we get  the lexical value tree 
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   678
  \begin{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   679
  $v_0= \Stars [\Right(\Seq(a, \Seq(b, c)))]$
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   680
  \end{center}
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   681
   for how $r$ matched $abc$. This completes the algorithm.
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   682
   
61
580c7b84f900 hi finished
Chengsong
parents: 60
diff changeset
   683
%We omit the details of injection function, which is provided by Sulzmann and Lu's paper \cite{Sulzmann2014}. 
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   684
Readers might have noticed that the lexical value information is actually
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   685
already available when doing derivatives. For example, immediately after
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   686
the operation $\backslash a$ we know that if we want to match a string
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   687
that starts with $a$, we can either take the initial match to be 
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   688
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   689
 \begin{center}
42
Chengsong
parents: 41
diff changeset
   690
\begin{enumerate}
Chengsong
parents: 41
diff changeset
   691
    \item[1)] just $a$ or
Chengsong
parents: 41
diff changeset
   692
    \item[2)] string $ab$ or 
Chengsong
parents: 41
diff changeset
   693
    \item[3)] string $abc$.
Chengsong
parents: 41
diff changeset
   694
\end{enumerate}
65
df2e0faccb23 moew changes
Chengsong
parents: 64
diff changeset
   695
\end{center}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   696
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   697
\noindent
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   698
In order to differentiate between these choices, we just need to
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   699
remember their positions---$a$ is on the left, $ab$ is in the middle ,
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   700
and $abc$ is on the right. Which of these alternatives is chosen
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   701
later does not affect their relative position because the algorithm does
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   702
not change this order. If this parsing information can be determined and
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   703
does not change because of later derivatives, there is no point in
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   704
traversing this information twice. This leads to an optimisation---if we
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   705
store the information for lexical values inside the regular expression,
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   706
update it when we do derivative on them, and collect the information
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   707
when finished with derivatives and call $\textit{mkeps}$ for deciding which
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   708
branch is POSIX, we can generate the lexical value in one pass, instead of
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   709
doing the rest $n$ injections. This leads to Sulzmann and Lu's novel
71
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   710
idea of using bitcodes in derivatives.
42
Chengsong
parents: 41
diff changeset
   711
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   712
In the next section, we shall focus on the bitcoded algorithm and the
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   713
process of simplification of regular expressions. This is needed in
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   714
order to obtain \emph{fast} versions of the Brzozowski's, and Sulzmann
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   715
and Lu's algorithms.  This is where the PhD-project aims to advance the
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   716
state-of-the-art.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   717
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   718
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   719
\section{Simplification of Regular Expressions}
63
d3c22f809dde more proof-reading
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   720
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   721
Using bitcodes to guide  parsing is not a novel idea. It was applied to
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   722
context free grammars and then adapted by Henglein and Nielson for
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   723
efficient regular expression  lexing using DFAs~\cite{nielson11bcre}.
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   724
Sulzmann and Lu took this idea of bitcodes a step further by integrating
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   725
bitcodes into derivatives. The reason why we want to use bitcodes in
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   726
this project is that we want to introduce more aggressive simplification
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   727
rules in order to keep the size of derivatives small throughout. This is
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   728
because the main drawback of building successive derivatives according
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   729
to Brzozowski's definition is that they can grow very quickly in size.
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   730
This is mainly due to the fact that the derivative operation generates
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   731
often ``useless'' $\ZERO$s and $\ONE$s in derivatives.  As a result, if
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   732
implemented naively both algorithms by Brzozowski and by Sulzmann and Lu
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   733
are excruciatingly slow. For example when starting with the regular
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   734
expression $(a + aa)^*$ and building 12 successive derivatives
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   735
w.r.t.~the character $a$, one obtains a derivative regular expression
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   736
with more than 8000 nodes (when viewed as a tree). Operations like
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   737
$\textit{der}$ and $\nullable$ need to traverse such trees and
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   738
consequently the bigger the size of the derivative the slower the
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   739
algorithm. 
35
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
   740
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   741
Fortunately, one can simplify regular expressions after each derivative
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   742
step. Various simplifications of regular expressions are possible, such
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   743
as the simplification of $\ZERO + r$, $r + \ZERO$, $\ONE\cdot r$, $r
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   744
\cdot \ONE$, and $r + r$ to just $r$. These simplifications do not
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   745
affect the answer for whether a regular expression matches a string or
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   746
not, but fortunately also do not affect the POSIX strategy of how
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   747
regular expressions match strings---although the latter is much harder
71
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   748
to establish. Some initial results in this regard have been
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   749
obtained in \cite{AusafDyckhoffUrban2016}. 
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   750
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   751
Unfortunately, the simplification rules outlined above  are not
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   752
sufficient to prevent a size explosion in all cases. We
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   753
believe a tighter bound can be achieved that prevents an explosion in
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   754
\emph{all} cases. Such a tighter bound is suggested by work of Antimirov who
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   755
proved that (partial) derivatives can be bound by the number of
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   756
characters contained in the initial regular expression
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   757
\cite{Antimirov95}. He defined the \emph{partial derivatives} of regular
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   758
expressions as follows:
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   759
52
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
   760
\begin{center}
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
   761
\begin{tabular}{lcl}
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
   762
 $\textit{pder} \; c \; \ZERO$ & $\dn$ & $\emptyset$\\
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
   763
 $\textit{pder} \; c \; \ONE$ & $\dn$ & $\emptyset$ \\
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
   764
 $\textit{pder} \; c \; d$ & $\dn$ & $\textit{if} \; c \,=\, d \; \{  \ONE   \}  \; \textit{else} \; \emptyset$ \\ 
52
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
   765
  $\textit{pder} \; c \; r_1+r_2$ & $\dn$ & $pder \; c \; r_1 \cup pder \; c \;  r_2$ \\
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   766
   $\textit{pder} \; c \; r_1 \cdot r_2$ & $\dn$ & $\textit{if} \; nullable \; r_1 $\\
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   767
     & & $\textit{then} \; \{  r \cdot r_2 \mid r \in pder \; c \; r_1   \}  \cup pder \; c \; r_2 \;$\\
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   768
     & & $\textit{else} \; \{  r \cdot r_2 \mid r \in pder \; c \; r_1   \} $ \\ 
52
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
   769
     $\textit{pder} \; c \; r^*$ & $\dn$ & $ \{  r' \cdot r^* \mid r' \in pder \; c \; r   \}  $ \\  
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
   770
 \end{tabular}
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
   771
 \end{center}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   772
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   773
\noindent
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   774
A partial derivative of a regular expression $r$ is essentially a set of
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   775
regular expressions that are either $r$'s children expressions or a
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   776
concatenation of them. Antimirov has proved a tight bound of the sum of
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   777
the size of \emph{all} partial derivatives no matter what the string
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   778
looks like. Roughly speaking the size sum will be at most cubic in the
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   779
size of the regular expression.
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   780
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   781
If we want the size of derivatives in Sulzmann and Lu's algorithm to
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   782
stay below this bound, we would need more aggressive simplifications.
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   783
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   784
deleting duplicates whenever possible. For example, the parentheses in
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   785
$(a+b) \cdot c + bc$ can be opened up to get $a\cdot c +  b \cdot c + b
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   786
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   787
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   788
$a^*+a+\ONE$. Adding these more aggressive simplification rules helps us
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   789
to achieve the same size bound as that of the partial derivatives. 
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   790
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   791
In order to implement the idea of ``spilling out alternatives'' and to
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   792
make them compatible with the $\text{inj}$-mechanism, we use
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   793
\emph{bitcodes}. Bits and bitcodes (lists of bits) are just:
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   794
71
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   795
%This allows us to prove a tight
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   796
%bound on the size of regular expression during the running time of the
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   797
%algorithm if we can establish the connection between our simplification
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
   798
%rules and partial derivatives.
35
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
   799
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
   800
 %We believe, and have generated test
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
   801
%data, that a similar bound can be obtained for the derivatives in
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
   802
%Sulzmann and Lu's algorithm. Let us give some details about this next.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   803
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
   804
67
e974c5477a8c anotehr update
Chengsong
parents: 66
diff changeset
   805
\begin{center}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   806
		$b ::=   S \mid  Z \qquad
43
Chengsong
parents: 42
diff changeset
   807
bs ::= [] \mid b:bs    
67
e974c5477a8c anotehr update
Chengsong
parents: 66
diff changeset
   808
$
e974c5477a8c anotehr update
Chengsong
parents: 66
diff changeset
   809
\end{center}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   810
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   811
\noindent
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   812
The $S$ and $Z$ are arbitrary names for the bits in order to avoid 
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   813
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   814
bit-lists) can be used to encode values (or incomplete values) in a
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   815
compact form. This can be straightforwardly seen in the following
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   816
coding function from values to bitcodes: 
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   817
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   818
\begin{center}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   819
\begin{tabular}{lcl}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   820
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   821
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   822
  $\textit{code}(\Left\,v)$ & $\dn$ & $\Z :: code(v)$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   823
  $\textit{code}(\Right\,v)$ & $\dn$ & $\S :: code(v)$\\
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   824
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
68
3c73d95cbfef more upd
Chengsong
parents: 67
diff changeset
   825
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[\Z]$\\
3c73d95cbfef more upd
Chengsong
parents: 67
diff changeset
   826
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $\S :: code(v) \;@\;
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   827
                                                 code(\Stars\,vs)$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   828
\end{tabular}    
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   829
\end{center} 
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   830
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   831
\noindent
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   832
Here $\textit{code}$ encodes a value into a bitcodes by converting
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   833
$\Left$ into $\Z$, $\Right$ into $\S$, the start point of a non-empty
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   834
star iteration into $\S$, and the border where a local star terminates
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   835
into $\Z$. This coding is lossy, as it throws away the information about
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   836
characters, and also does not encode the ``boundary'' between two
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   837
sequence values. Moreover, with only the bitcode we cannot even tell
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   838
whether the $\S$s and $\Z$s are for $\Left/\Right$ or $\Stars$. The
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   839
reason for choosing this compact way of storing information is that the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   840
relatively small size of bits can be easily manipulated and ``moved
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   841
around'' in a regular expression. In order to recover values, we will 
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   842
need the corresponding regular expression as an extra information. This
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   843
means the decoding function is defined as:
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   844
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   845
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   846
%\begin{definition}[Bitdecoding of Values]\mbox{}
36
Chengsong
parents: 35
diff changeset
   847
\begin{center}
Chengsong
parents: 35
diff changeset
   848
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
Chengsong
parents: 35
diff changeset
   849
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
Chengsong
parents: 35
diff changeset
   850
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
Chengsong
parents: 35
diff changeset
   851
  $\textit{decode}'\,(\Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
Chengsong
parents: 35
diff changeset
   852
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
Chengsong
parents: 35
diff changeset
   853
       (\Left\,v, bs_1)$\\
Chengsong
parents: 35
diff changeset
   854
  $\textit{decode}'\,(\S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
Chengsong
parents: 35
diff changeset
   855
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
Chengsong
parents: 35
diff changeset
   856
       (\Right\,v, bs_1)$\\                           
Chengsong
parents: 35
diff changeset
   857
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
Chengsong
parents: 35
diff changeset
   858
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
Chengsong
parents: 35
diff changeset
   859
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
Chengsong
parents: 35
diff changeset
   860
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
Chengsong
parents: 35
diff changeset
   861
  $\textit{decode}'\,(\Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
Chengsong
parents: 35
diff changeset
   862
  $\textit{decode}'\,(\S\!::\!bs)\,(r^*)$ & $\dn$ & 
Chengsong
parents: 35
diff changeset
   863
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents: 35
diff changeset
   864
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
Chengsong
parents: 35
diff changeset
   865
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
Chengsong
parents: 35
diff changeset
   866
  
Chengsong
parents: 35
diff changeset
   867
  $\textit{decode}\,bs\,r$ & $\dn$ &
Chengsong
parents: 35
diff changeset
   868
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents: 35
diff changeset
   869
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
Chengsong
parents: 35
diff changeset
   870
       \textit{else}\;\textit{None}$                       
Chengsong
parents: 35
diff changeset
   871
\end{tabular}    
Chengsong
parents: 35
diff changeset
   872
\end{center}    
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   873
%\end{definition}
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
   874
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   875
Sulzmann and Lu's integrated the bitcodes into regular expressions to
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   876
create annotated regular expressions \cite{Sulzmann2014}.
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   877
\emph{Annotated regular expressions} are defined by the following
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   878
grammar:%\comment{ALTS should have  an $as$ in  the definitions, not  just $a_1$ and $a_2$}
43
Chengsong
parents: 42
diff changeset
   879
Chengsong
parents: 42
diff changeset
   880
\begin{center}
Chengsong
parents: 42
diff changeset
   881
\begin{tabular}{lcl}
Chengsong
parents: 42
diff changeset
   882
  $\textit{a}$ & $::=$  & $\textit{ZERO}$\\
Chengsong
parents: 42
diff changeset
   883
                  & $\mid$ & $\textit{ONE}\;\;bs$\\
Chengsong
parents: 42
diff changeset
   884
                  & $\mid$ & $\textit{CHAR}\;\;bs\,c$\\
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   885
                  & $\mid$ & $\textit{ALTS}\;\;bs\,as$\\
43
Chengsong
parents: 42
diff changeset
   886
                  & $\mid$ & $\textit{SEQ}\;\;bs\,a_1\,a_2$\\
Chengsong
parents: 42
diff changeset
   887
                  & $\mid$ & $\textit{STAR}\;\;bs\,a$
Chengsong
parents: 42
diff changeset
   888
\end{tabular}    
Chengsong
parents: 42
diff changeset
   889
\end{center}  
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   890
%(in \textit{ALTS})
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   891
43
Chengsong
parents: 42
diff changeset
   892
\noindent
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   893
where $bs$ stands for bitcodes, $a$  for $\bold{a}$nnotated regular
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   894
expressions and $as$ for a list of annotated regular expressions.
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   895
The alternative constructor($\textit{ALTS}$) has been generalized to 
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   896
accept a list of annotated regular expressions rather than just 2.
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   897
We will show that these bitcodes encode information about
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   898
the (POSIX) value that should be generated by the Sulzmann and Lu
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   899
algorithm.
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   900
43
Chengsong
parents: 42
diff changeset
   901
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   902
To do lexing using annotated regular expressions, we shall first
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   903
transform the usual (un-annotated) regular expressions into annotated
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   904
regular expressions. This operation is called \emph{internalisation} and
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   905
defined as follows:
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   906
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   907
%\begin{definition}
36
Chengsong
parents: 35
diff changeset
   908
\begin{center}
Chengsong
parents: 35
diff changeset
   909
\begin{tabular}{lcl}
Chengsong
parents: 35
diff changeset
   910
  $(\ZERO)^\uparrow$ & $\dn$ & $\textit{ZERO}$\\
Chengsong
parents: 35
diff changeset
   911
  $(\ONE)^\uparrow$ & $\dn$ & $\textit{ONE}\,[]$\\
Chengsong
parents: 35
diff changeset
   912
  $(c)^\uparrow$ & $\dn$ & $\textit{CHAR}\,[]\,c$\\
Chengsong
parents: 35
diff changeset
   913
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   914
  $\textit{ALTS}\;[]\,List((\textit{fuse}\,[\Z]\,r_1^\uparrow),\,
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   915
  (\textit{fuse}\,[\S]\,r_2^\uparrow))$\\
36
Chengsong
parents: 35
diff changeset
   916
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
Chengsong
parents: 35
diff changeset
   917
         $\textit{SEQ}\;[]\,r_1^\uparrow\,r_2^\uparrow$\\
Chengsong
parents: 35
diff changeset
   918
  $(r^*)^\uparrow$ & $\dn$ &
Chengsong
parents: 35
diff changeset
   919
         $\textit{STAR}\;[]\,r^\uparrow$\\
Chengsong
parents: 35
diff changeset
   920
\end{tabular}    
Chengsong
parents: 35
diff changeset
   921
\end{center}    
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   922
%\end{definition}
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   923
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   924
\noindent
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   925
We use up arrows here to indicate that the basic un-annotated regular
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   926
expressions are ``lifted up'' into something slightly more complex. In the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   927
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   928
attach bits to the front of an annotated regular expression. Its
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   929
definition is as follows:
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   930
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   931
\begin{center}
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   932
\begin{tabular}{lcl}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   933
  $\textit{fuse}\;bs\,(\textit{ZERO})$ & $\dn$ & $\textit{ZERO}$\\
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   934
  $\textit{fuse}\;bs\,(\textit{ONE}\,bs')$ & $\dn$ &
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   935
     $\textit{ONE}\,(bs\,@\,bs')$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   936
  $\textit{fuse}\;bs\,(\textit{CHAR}\,bs'\,c)$ & $\dn$ &
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   937
     $\textit{CHAR}\,(bs\,@\,bs')\,c$\\
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   938
  $\textit{fuse}\;bs\,(\textit{ALTS}\,bs'\,as)$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   939
     $\textit{ALTS}\,(bs\,@\,bs')\,as$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   940
  $\textit{fuse}\;bs\,(\textit{SEQ}\,bs'\,a_1\,a_2)$ & $\dn$ &
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   941
     $\textit{SEQ}\,(bs\,@\,bs')\,a_1\,a_2$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   942
  $\textit{fuse}\;bs\,(\textit{STAR}\,bs'\,a)$ & $\dn$ &
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   943
     $\textit{STAR}\,(bs\,@\,bs')\,a$
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   944
\end{tabular}    
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   945
\end{center}  
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   946
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   947
\noindent
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   948
After internalising the regular expression, we perform successive
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   949
derivative operations on the annotated regular expressions. This
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   950
derivative operation is the same as what we had previously for the
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   951
basic regular expressions, except that we beed to take care of
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
   952
the bitcodes:
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   953
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   954
 %\begin{definition}{bder}
36
Chengsong
parents: 35
diff changeset
   955
\begin{center}
Chengsong
parents: 35
diff changeset
   956
  \begin{tabular}{@{}lcl@{}}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   957
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   958
  $(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   959
  $(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
36
Chengsong
parents: 35
diff changeset
   960
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 35
diff changeset
   961
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   962
  $(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   963
  $\textit{ALTS}\;bs\,(as.map(\backslash c))$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   964
  $(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
36
Chengsong
parents: 35
diff changeset
   965
     $\textit{if}\;\textit{bnullable}\,a_1$\\
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   966
					       & &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
   967
					       & &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   968
  & &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   969
  $(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   970
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
36
Chengsong
parents: 35
diff changeset
   971
       (\textit{STAR}\,[]\,r)$
Chengsong
parents: 35
diff changeset
   972
\end{tabular}    
Chengsong
parents: 35
diff changeset
   973
\end{center}    
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   974
%\end{definition}
74
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
   975
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   976
\noindent
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   977
For instance, when we unfold $\textit{STAR} \; bs \; a$ into a sequence,
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   978
we need to attach an additional bit $Z$ to the front of $r \backslash c$
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   979
to indicate that there is one more star iteration. Also the $SEQ$ clause
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   980
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   981
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   982
that it is for annotated regular expressions, therefore we omit the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   983
definition). Assume that $bmkeps$ correctly extracts the bitcode for how
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   984
$a_1$ matches the string prior to character $c$ (more on this later),
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   985
then the right branch of $ALTS$, which is $fuse \; bmkeps \;  a_1 (a_2
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   986
\backslash c)$ will collapse the regular expression $a_1$(as it has
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   987
already been fully matched) and store the parsing information at the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   988
head of the regular expression $a_2 \backslash c$ by fusing to it. The
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   989
bitsequence $bs$, which was initially attached to the head of $SEQ$, has
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
   990
now been elevated to the top-level of $ALTS$, as this information will be
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   991
needed whichever way the $SEQ$ is matched---no matter whether $c$ belongs
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   992
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   993
the lexing information, we complete the lexing by collecting the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   994
bitcodes using a generalised version of the $\textit{mkeps}$ function
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   995
for annotated regular expressions, called $\textit{bmkeps}$:
44
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   996
4d674a971852 another changes. have written more. but havent typed them. tomorrow will continue.
Chengsong
parents: 43
diff changeset
   997
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
   998
%\begin{definition}[\textit{bmkeps}]\mbox{}
36
Chengsong
parents: 35
diff changeset
   999
\begin{center}
Chengsong
parents: 35
diff changeset
  1000
\begin{tabular}{lcl}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1001
  $\textit{bmkeps}\,(\textit{ONE}\;bs)$ & $\dn$ & $bs$\\
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1002
  $\textit{bmkeps}\,(\textit{ALTS}\;bs\,a::as)$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1003
     $\textit{if}\;\textit{bnullable}\,a$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1004
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1005
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(\textit{ALTS}\;bs\,as)$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1006
  $\textit{bmkeps}\,(\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ &
36
Chengsong
parents: 35
diff changeset
  1007
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1008
  $\textit{bmkeps}\,(\textit{STAR}\;bs\,a)$ & $\dn$ &
36
Chengsong
parents: 35
diff changeset
  1009
     $bs \,@\, [\S]$
Chengsong
parents: 35
diff changeset
  1010
\end{tabular}    
Chengsong
parents: 35
diff changeset
  1011
\end{center}    
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1012
%\end{definition}
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1013
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1014
\noindent
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1015
This function completes the value information by travelling along the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1016
path of the regular expression that corresponds to a POSIX value and
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1017
collecting all the bitcodes, and using $S$ to indicate the end of star
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1018
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1019
decode them, we get the value we expect. The corresponding lexing
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1020
algorithm looks as follows:
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1021
37
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1022
\begin{center}
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1023
\begin{tabular}{lcl}
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1024
  $\textit{blexer}\;r\,s$ & $\dn$ &
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1025
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1026
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1027
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1028
  & & $\;\;\textit{else}\;\textit{None}$
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1029
\end{tabular}
17d8e7599a01 new changes
Chengsong
parents: 36
diff changeset
  1030
\end{center}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1031
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1032
\noindent
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1033
In this definition $\_\backslash s$ is the  generalisation  of the derivative
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1034
operation from characters to strings (just like the derivatives for un-annotated
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1035
regular expressions).
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1036
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1037
The main point of the bitcodes and annotated regular expressions is that
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1038
we can apply rather aggressive (in terms of size) simplification rules
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1039
in order to keep derivatives small. We have developed such
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1040
``aggressive'' simplification rules and generated test data that show
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1041
that the expected bound can be achieved. Obviously we could only
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1042
partially cover  the search space as there are infinitely many regular
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1043
expressions and strings. 
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1044
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1045
One modification we introduced is to allow a list of annotated regular
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1046
expressions in the \textit{ALTS} constructor. This allows us to not just
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1047
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1048
also unnecessary ``copies'' of regular expressions (very similar to
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1049
simplifying $r + r$ to just $r$, but in a more general setting). Another
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1050
modification is that we use simplification rules inspired by Antimirov's
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1051
work on partial derivatives. They maintain the idea that only the first
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1052
``copy'' of a regular expression in an alternative contributes to the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1053
calculation of a POSIX value. All subsequent copies can be pruned away from
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1054
the regular expression. A recursive definition of our  simplification function 
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1055
that looks somewhat similar to our Scala code is given below:
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1056
%\comment{Use $\ZERO$, $\ONE$ and so on. 
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1057
%Is it $ALTS$ or $ALTS$?}\\
49
d256aabe88f3 still wont comiple hhh
Chengsong
parents: 48
diff changeset
  1058
52
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
  1059
\begin{center}
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
  1060
  \begin{tabular}{@{}lcl@{}}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1061
   
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1062
  $\textit{simp} \; (\textit{SEQ}\;bs\,a_1\,a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1063
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1064
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1065
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1066
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1067
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow  \textit{SEQ} \; bs \; a_1' \;  a_2'$ \\
52
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
  1068
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1069
  $\textit{simp} \; (\textit{ALTS}\;bs\,as)$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{map simp as})) \; \textit{match} $ \\
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1070
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1071
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1072
   &&$\quad\textit{case} \;  as' \Rightarrow  \textit{ALTS}\;bs\;as'$\\ 
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1073
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1074
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
52
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
  1075
\end{tabular}    
25bbbb8b0e90 just in case of some accidents from erasing my work
Chengsong
parents: 51
diff changeset
  1076
\end{center}    
47
d2a7e87ea6e1 will not compile, just text
Chengsong
parents: 46
diff changeset
  1077
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1078
\noindent
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1079
The simplification does a pattern matching on the regular expression.
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1080
When it detected that the regular expression is an alternative or
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1081
sequence, it will try to simplify its children regular expressions
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1082
recursively and then see if one of the children turn into $\ZERO$ or
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1083
$\ONE$, which might trigger further simplification at the current level.
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1084
The most involved part is the $\textit{ALTS}$ clause, where we use two
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1085
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1086
$\textit{ALTS}$ and reduce as many duplicates as possible. Function
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1087
$\textit{distinct}$  keeps the first occurring copy only and remove all later ones
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1088
when detected duplicates. Function $\textit{flatten}$ opens up nested \textit{ALTS}.
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1089
Its recursive definition is given below:
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1090
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1091
 \begin{center}
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1092
  \begin{tabular}{@{}lcl@{}}
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1093
  $\textit{flatten} \; (\textit{ALTS}\;bs\,as) :: as'$ & $\dn$ & $(\textit{map} \;
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1094
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1095
  $\textit{flatten} \; \textit{ZERO} :: as'$ & $\dn$ & $ \textit{flatten} \;  as' $ \\
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1096
    $\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; as'$ \quad(otherwise) 
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1097
\end{tabular}    
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1098
\end{center}  
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1099
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1100
\noindent
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1101
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1102
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1103
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
47
d2a7e87ea6e1 will not compile, just text
Chengsong
parents: 46
diff changeset
  1104
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1105
Suppose we apply simplification after each derivative step, and view
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1106
these two operations as an atomic one: $a \backslash_{simp}\,c \dn
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1107
\textit{simp}(a \backslash c)$. Then we can use the previous natural
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1108
extension from derivative w.r.t.~character to derivative
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1109
w.r.t.~string:%\comment{simp in  the [] case?}
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1110
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1111
\begin{center}
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1112
\begin{tabular}{lcl}
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1113
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1114
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1115
\end{tabular}
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1116
\end{center}
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1117
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1118
\noindent
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1119
we obtain an optimised version of the algorithm:
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1120
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1121
 \begin{center}
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1122
\begin{tabular}{lcl}
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1123
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1124
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
53
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1125
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1126
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1127
  & & $\;\;\textit{else}\;\textit{None}$
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1128
\end{tabular}
3ec403f650a8 readable version
Chengsong
parents: 52
diff changeset
  1129
\end{center}
48
bbefcf7351f2 still will not compile
Chengsong
parents: 47
diff changeset
  1130
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1131
\noindent
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1132
This algorithm keeps the regular expression size small, for example,
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1133
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1134
will be reduced to just 6 and stays constant, no matter how long the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1135
input string is.
35
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
  1136
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1137
35
f70e9ab4e680 psuedocode added
Chengsong
parents: 34
diff changeset
  1138
70
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1139
\section{Current Work}
cab5eab1f6f1 more proofreading
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
  1140
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1141
We are currently engaged in two tasks related to this algorithm. The
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1142
first task is proving that our simplification rules actually do not
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1143
affect the POSIX value that should be generated by the algorithm
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1144
according to the specification of a POSIX value and furthermore obtain a
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1145
much tighter bound on the sizes of derivatives. The result is that our
49
d256aabe88f3 still wont comiple hhh
Chengsong
parents: 48
diff changeset
  1146
algorithm should be correct and faster on all inputs.  The original
d256aabe88f3 still wont comiple hhh
Chengsong
parents: 48
diff changeset
  1147
blow-up, as observed in JavaScript, Python and Java, would be excluded
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1148
from happening in our algorithm. For this proof we use the theorem prover
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1149
Isabelle. Once completed, this result will advance the state-of-the-art:
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1150
Sulzmann and Lu wrote in their paper~\cite{Sulzmann2014} about the
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1151
bitcoded ``incremental parsing method'' (that is the lexing algorithm
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1152
outlined in this section):
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1153
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1154
\begin{quote}\it
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1155
  ``Correctness Claim: We further claim that the incremental parsing
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1156
  method in Figure~5 in combination with the simplification steps in
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
  1157
  Figure 6 yields POSIX parse tree [our lexical values]. We have tested this claim
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1158
  extensively by using the method in Figure~3 as a reference but yet
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1159
  have to work out all proof details.''
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1160
\end{quote}  
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1161
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
  1162
\noindent 
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1163
We like to settle this correctness claim. It is relatively
79
481c8000de6d works on the part that christian did not read
Chengsong
parents: 77
diff changeset
  1164
straightforward to establish that after one simplification step, the part of a
481c8000de6d works on the part that christian did not read
Chengsong
parents: 77
diff changeset
  1165
nullable derivative that corresponds to a POSIX value remains intact and can
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1166
still be collected, in other words, we can show that
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1167
%\comment{Double-check....I
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1168
%think this  is not the case}
81
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1169
%\comment{If i remember correctly, you have proved this lemma.
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1170
%I feel this is indeed not true because you might place arbitrary 
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1171
%bits on the regex r, however if this is the case, did i remember wrongly that
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1172
%you proved something like simplification does not affect $\textit{bmkeps}$ results?
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1173
%Anyway, i have amended this a little bit so it does not allow arbitrary bits attached
a0df84875788 updated and added comments
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
  1174
%to a regex. Maybe it works now.}
74
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1175
71
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
  1176
\begin{center}
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1177
	$\textit{bmkeps} \; a = \textit{bmkeps} \; \textit{bsimp} \; a\;($\textit{provided}$ \; a\; is \; \textit{bnullable} )$
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1178
\end{center} 
74
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1179
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1180
\noindent
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1181
as this basically comes down to proving actions like removing the
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1182
additional $r$ in $r+r$  does not delete important POSIX information in
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1183
a regular expression. The hard part of this proof is to establish that
74
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1184
71
0573615e41a3 new changes?
Chengsong
parents: 70
diff changeset
  1185
\begin{center}
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1186
	$ \textit{blexer}\_{simp}(r, \; s) =  \textit{blexer}(r, \; s)$
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1187
\end{center}
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1188
%comment{This is not true either...look at the definion blexer/blexer-simp}
74
9e791ef6022f just a merge - no changes
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
  1189
79
481c8000de6d works on the part that christian did not read
Chengsong
parents: 77
diff changeset
  1190
\noindent That is, if we do derivative on regular expression $r$ and then
481c8000de6d works on the part that christian did not read
Chengsong
parents: 77
diff changeset
  1191
simplify it, and repeat this process until we exhaust the string, we get a
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1192
regular expression $r''$($\textit{LHS}$)  that provides the POSIX matching
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1193
information, which is exactly the same as the result $r'$($\textit{RHS}$ of the
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1194
normal derivative algorithm that only does derivative repeatedly and has no
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1195
simplification at all.  This might seem at first glance very unintuitive, as
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1196
the $r'$ could be exponentially larger than $r''$, but can be explained in the
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1197
following way: we are pruning away the possible matches that are not POSIX.
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1198
Since there could be exponentially many 
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1199
non-POSIX matchings and only 1 POSIX matching, it
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1200
is understandable that our $r''$ can be a lot smaller.  we can still provide
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1201
the same POSIX value if there is one.  This is not as straightforward as the
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1202
previous proposition, as the two regular expressions $r'$ and $r''$ might have
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1203
become very different.  The crucial point is to find the
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1204
$\textit{POSIX}$  information of a regular expression and how it is modified,
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1205
augmented and propagated 
83
8c1195dd6136 updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
  1206
during simplification in parallel with the regular expression that
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1207
has not been simplified in the subsequent derivative operations.  To aid this,
86
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1208
we use the helper function retrieve described by Sulzmann and Lu:
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1209
\begin{center}
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1210
\begin{tabular}{@{}l@{\hspace{2mm}}c@{\hspace{2mm}}l@{}}
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1211
  $\textit{retrieve}\,(\textit{ONE}\,bs)\,\Empty$ & $\dn$ & $bs$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1212
  $\textit{retrieve}\,(\textit{CHAR}\,bs\,c)\,(\Char\,d)$ & $\dn$ & $bs$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1213
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Left\,v)$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1214
     $bs \,@\, \textit{retrieve}\,a\,v$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1215
  $\textit{retrieve}\,(\textit{ALTS}\,bs\,a::as)\,(\Right\,v)$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1216
  $bs \,@\, \textit{retrieve}\,(\textit{ALTS}\,bs\,as)\,v$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1217
  $\textit{retrieve}\,(\textit{SEQ}\,bs\,a_1\,a_2)\,(\Seq\,v_1\,v_2)$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1218
     $bs \,@\,\textit{retrieve}\,a_1\,v_1\,@\, \textit{retrieve}\,a_2\,v_2$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1219
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,[])$ & $\dn$ &
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1220
     $bs \,@\, [\S]$\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1221
  $\textit{retrieve}\,(\textit{STAR}\,bs\,a)\,(\Stars\,(v\!::\!vs))$ & $\dn$ &\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1222
  \multicolumn{3}{l}{
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1223
     \hspace{3cm}$bs \,@\, [\Z] \,@\, \textit{retrieve}\,a\,v\,@\,
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1224
                    \textit{retrieve}\,(\textit{STAR}\,[]\,a)\,(\Stars\,vs)$}\\
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1225
\end{tabular}
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1226
\end{center}
51ac1ab6e1fd final changes
Chengsong
parents: 85
diff changeset
  1227
%\comment{Did not read further}\\
82
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1228
This function assembles the bitcode 
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1229
%that corresponds to a lexical value for how
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1230
%the current derivative matches the suffix of the string(the characters that
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1231
%have not yet appeared, but will appear as the successive derivatives go on.
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1232
%How do we get this "future" information? By the value $v$, which is
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1233
%computed by a pass of the algorithm that uses
3153338ec6e4 addressed comments, did improvements from page 14 and on
Chengsong
parents: 81
diff changeset
  1234
%$inj$ as described in the previous section).  
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1235
using information from both the derivative regular expression and the
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1236
value. Sulzmann and Lu poroposed this function, but did not prove
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1237
anything about it. Ausaf and Urban used it to connect the bitcoded
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1238
algorithm to the older algorithm by the following equation:
77
058133a9ffe0 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
  1239
 
79
481c8000de6d works on the part that christian did not read
Chengsong
parents: 77
diff changeset
  1240
 \begin{center} $inj \;a\; c \; v = \textit{decode} \; (\textit{retrieve}\;
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1241
	 (r^\uparrow)\backslash_{simp} \,c)\,v)$ 
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1242
 \end{center} 
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1243
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1244
\noindent
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1245
whereby $r^\uparrow$ stands for the internalised version of $r$. Ausaf
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1246
and Urban also used this fact to prove  the correctness of bitcoded
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1247
algorithm without simplification.  Our purpose of using this, however,
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1248
is to establish 
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1249
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1250
\begin{center}
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1251
$ \textit{retrieve} \;
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1252
a \; v \;=\; \textit{retrieve}  \; (\textit{simp}\,a) \; v'.$
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1253
\end{center}
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1254
The idea is that using $v'$, a simplified version of $v$ that had gone
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1255
through the same simplification step as $\textit{simp}(a)$, we are able
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1256
to extract the bitcode that gives the same parsing information as the
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1257
unsimplified one. However, we noticed that constructing such a  $v'$
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1258
from $v$ is not so straightforward. The point of this is that  we might
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1259
be able to finally bridge the gap by proving
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1260
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1261
\begin{center}
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1262
$\textit{retrieve} \; (r^\uparrow   \backslash  s) \; v = \;\textit{retrieve} \;
85
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1263
(\textit{simp}(r^\uparrow)  \backslash  s) \; v'$
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1264
\end{center}
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1265
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1266
\noindent
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1267
and subsequently
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1268
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1269
\begin{center}
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1270
$\textit{retrieve} \; (r^\uparrow \backslash  s) \; v\; = \; \textit{retrieve} \;
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1271
(r^\uparrow  \backslash_{simp}  \, s) \; v'$.
80
d9d61a648292 addressed all comments.
Chengsong
parents: 79
diff changeset
  1272
\end{center}
84
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1273
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1274
\noindent
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1275
The $\textit{LHS}$ of the above equation is the bitcode we want. This
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1276
would prove that our simplified version of regular expression still
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1277
contains all the bitcodes needed. The task here is to find a way to
de50a65d1b15 proof-read
Christian Urban <urbanc@in.tum.de>
parents: 83
diff changeset
  1278
compute the correct $v'$.
72
83b021fc7d29 interesting?
Chengsong
parents: 71
diff changeset
  1279
87
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1280
The second task is to speed up the more aggressive simplification.  Currently
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1281
it is slower than the original naive simplification by Ausaf and Urban (the
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1282
naive version as implemented by Ausaf   and Urban of course can ``explode'' in
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1283
some cases).  It is therefore not surprising that the speed is also much slower
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1284
than regular expression engines in popular programming languages such as Java
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1285
and Python on most inputs that are linear. For example, just by rewriting the
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1286
example regular expression in the beginning of this report  $(a^*)^*\,b$ into
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1287
$a^*\,b$ would eliminate the ambiguity in the matching and make the time
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1288
for matching linear with respect to the input string size. This allows the 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1289
DFA approach to become blindingly fast, and dwarf the speed of our current
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1290
implementation. For example, here is a comparison of Java regex engine 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1291
and our implementation on this example.
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1292
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1293
\begin{center}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1294
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1295
\begin{tikzpicture}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1296
\begin{axis}[
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1297
    xlabel={$n*1000$},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1298
    x label style={at={(1.05,-0.05)}},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1299
    ylabel={time in secs},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1300
    enlargelimits=false,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1301
    xtick={0,5,...,30},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1302
    xmax=33,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1303
    ymax=9,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1304
    scaled ticks=true,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1305
    axis lines=left,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1306
    width=5cm,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1307
    height=4cm, 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1308
    legend entries={Bitcoded Algorithm},  
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1309
    legend pos=north west,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1310
    legend cell align=left]
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1311
\addplot[red,mark=*, mark options={fill=white}] table {bad-scala.data};
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1312
\end{axis}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1313
\end{tikzpicture}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1314
  &
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1315
\begin{tikzpicture}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1316
\begin{axis}[
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1317
    xlabel={$n*1000$},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1318
    x label style={at={(1.05,-0.05)}},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1319
    %ylabel={time in secs},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1320
    enlargelimits=false,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1321
    xtick={0,5,...,30},
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1322
    xmax=33,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1323
    ymax=9,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1324
    scaled ticks=false,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1325
    axis lines=left,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1326
    width=5cm,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1327
    height=4cm, 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1328
    legend entries={Java},  
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1329
    legend pos=north west,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1330
    legend cell align=left]
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1331
\addplot[cyan,mark=*, mark options={fill=white}] table {good-java.data};
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1332
\end{axis}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1333
\end{tikzpicture}\\
88
bb1ff936e6cf label fix
Chengsong
parents: 87
diff changeset
  1334
\multicolumn{3}{c}{Graphs: Runtime for matching $a^*\,b$ with strings 
87
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1335
           of the form $\underbrace{aa..a}_{n}$.}
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1336
\end{tabular}    
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1337
\end{center}  
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1338
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1339
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1340
Java regex engine can match string of thousands of characters in a few milliseconds,
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1341
whereas our current algorithm gets excruciatingly slow on input of this size.
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1342
The running time in theory is linear, however it does not appear to be the 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1343
case in an actual implementation. So it needs to be explored how to
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1344
make our algorithm faster on all inputs.  It could be the recursive calls that are
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1345
needed to manipulate bits that are causing the slow down. A possible solution
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1346
is to write recursive functions into tail-recusive form.
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1347
Another possibility would be to explore
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1348
again the connection to DFAs to speed up the algorithm on 
9c52c21b5db3 changes to report
Chengsong
parents: 86
diff changeset
  1349
subcalls that are small enough. This is very much work in progress.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1350
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1351
\section{Conclusion}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1352
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1353
In this PhD-project we are interested in fast algorithms for regular
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1354
expression matching. While this seems to be a ``settled'' area, in
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1355
fact interesting research questions are popping up as soon as one steps
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1356
outside the classic automata theory (for example in terms of what kind
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1357
of regular expressions are supported). The reason why it is
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1358
interesting for us to look at the derivative approach introduced by
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1359
Brzozowski for regular expression matching, and then much further
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1360
developed by Sulzmann and Lu, is that derivatives can elegantly deal
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1361
with some of the regular expressions that are of interest in ``real
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1362
life''. This includes the not-regular expression, written $\neg\,r$
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1363
(that is all strings that are not recognised by $r$), but also bounded
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1364
regular expressions such as $r^{\{n\}}$ and $r^{\{n..m\}}$). There is
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1365
also hope that the derivatives can provide another angle for how to
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1366
deal more efficiently with back-references, which are one of the
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1367
reasons why regular expression engines in JavaScript, Python and Java
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1368
choose to not implement the classic automata approach of transforming
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1369
regular expressions into NFAs and then DFAs---because we simply do not
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1370
know how such back-references can be represented by DFAs.
85
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1371
We also plan to implement the bitcoded algorithm
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1372
in some imperative language like C to see if the inefficiency of the 
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1373
Scala implementation
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1374
is language specific. To make this research more comprehensive we also plan
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1375
to contrast our (faster) version of bitcoded algorithm with the
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1376
Symbolic Regex Matcher, the RE2, the Rust Regex Engine, and the static
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1377
analysis approach by implementing them in the same language and then compare
ba40ab3658ca in case no wifi
Chengsong
parents: 84
diff changeset
  1378
their performance.
30
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1379
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1380
\bibliographystyle{plain}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1381
\bibliography{root}
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1382
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1383
bd9eb959dbce changed file name to ninems
Chengsong
parents:
diff changeset
  1384
\end{document}