ChengsongTanPhdThesis/Chapters/Chapter1.tex
author Chengsong
Mon, 30 May 2022 14:41:09 +0100
changeset 528 28751de4b4ba
parent 519 856d025dbc15
child 529 96e93df60954
permissions -rwxr-xr-x
revised according to comments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     1
% Chapter 1
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     2
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     3
\chapter{Introduction} % Main chapter title
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     4
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     5
\label{Chapter1} % For referencing the chapter elsewhere, use \ref{Chapter1} 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     6
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     7
%----------------------------------------------------------------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     8
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     9
% Define some commands to keep the formatting separated from the content 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    10
\newcommand{\keyword}[1]{\textbf{#1}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    11
\newcommand{\tabhead}[1]{\textbf{#1}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    12
\newcommand{\code}[1]{\texttt{#1}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    13
\newcommand{\file}[1]{\texttt{\bfseries#1}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    14
\newcommand{\option}[1]{\texttt{\itshape#1}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    15
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    16
%\newcommand{\sflataux}[1]{\textit{sflat}\_\textit{aux} \, #1}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    17
\newcommand\sflat[1]{\llparenthesis #1 \rrparenthesis }
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    18
\newcommand{\ASEQ}[3]{\textit{ASEQ}_{#1} \, #2 \, #3}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    19
\newcommand{\bderssimp}[2]{#1 \backslash_{bsimp} #2}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    20
\newcommand{\rderssimp}[2]{#1 \backslash_{rsimp} #2}
506
69ad05398894 thesis chapter 2 section 2.4 2.5
Chengsong
parents: 505
diff changeset
    21
\newcommand{\bders}[2]{#1 \backslash #2}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    22
\newcommand{\bsimp}[1]{\textit{bsimp}(#1)}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    23
\newcommand{\rsimp}[1]{\textit{rsimp}(#1)}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    24
\newcommand{\sflataux}[1]{\llparenthesis #1 \rrparenthesis'}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    25
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    26
\newcommand{\ZERO}{\mbox{\bf 0}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    27
\newcommand{\ONE}{\mbox{\bf 1}}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    28
\newcommand{\AALTS}[2]{\oplus {\scriptstyle #1}\, #2}
505
5ce3bd8e5696 thesis chapter2 section 2.4
Chengsong
parents: 503
diff changeset
    29
\newcommand{\rdistinct}[2]{\textit{distinct} \; \textit{#1} \; #2}
5ce3bd8e5696 thesis chapter2 section 2.4
Chengsong
parents: 503
diff changeset
    30
\newcommand\hflat[1]{\llparenthesis  #1 \rrparenthesis_*}
5ce3bd8e5696 thesis chapter2 section 2.4
Chengsong
parents: 503
diff changeset
    31
\newcommand\hflataux[1]{\llparenthesis #1 \rrparenthesis_*'}
506
69ad05398894 thesis chapter 2 section 2.4 2.5
Chengsong
parents: 505
diff changeset
    32
\newcommand\createdByStar[1]{\textit{createdByStar}(#1)}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    33
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    34
\newcommand\myequiv{\mathrel{\stackrel{\makebox[0pt]{\mbox{\normalfont\tiny equiv}}}{=}}}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    35
519
Chengsong
parents: 518
diff changeset
    36
\def\decode{\textit{decode}}
Chengsong
parents: 518
diff changeset
    37
\def\internalise{\textit{internalise}}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    38
\def\lexer{\mathit{lexer}}
519
Chengsong
parents: 518
diff changeset
    39
\def\mkeps{\textit{mkeps}}
505
5ce3bd8e5696 thesis chapter2 section 2.4
Chengsong
parents: 503
diff changeset
    40
\newcommand{\rder}[2]{#2 \backslash #1}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    41
500
Chengsong
parents: 472
diff changeset
    42
\def\AZERO{\textit{AZERO}}
Chengsong
parents: 472
diff changeset
    43
\def\AONE{\textit{AONE}}
Chengsong
parents: 472
diff changeset
    44
\def\ACHAR{\textit{ACHAR}}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    45
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
    46
\def\POSIX{\textit{POSIX}}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    47
\def\ALTS{\textit{ALTS}}
500
Chengsong
parents: 472
diff changeset
    48
\def\ASTAR{\textit{ASTAR}}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    49
\def\DFA{\textit{DFA}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    50
\def\bmkeps{\textit{bmkeps}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    51
\def\retrieve{\textit{retrieve}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    52
\def\blexer{\textit{blexer}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    53
\def\flex{\textit{flex}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    54
\def\inj{\mathit{inj}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    55
\def\Empty{\mathit{Empty}}
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
    56
\def\Left{\mathit{Left}}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    57
\def\Right{\mathit{Right}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    58
\def\Stars{\mathit{Stars}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    59
\def\Char{\mathit{Char}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    60
\def\Seq{\mathit{Seq}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    61
\def\Der{\mathit{Der}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    62
\def\nullable{\mathit{nullable}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    63
\def\Z{\mathit{Z}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    64
\def\S{\mathit{S}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    65
\def\rup{r^\uparrow}
500
Chengsong
parents: 472
diff changeset
    66
%\def\bderssimp{\mathit{bders}\_\mathit{simp}}
Chengsong
parents: 472
diff changeset
    67
\def\distinctWith{\textit{distinctWith}}
528
28751de4b4ba revised according to comments
Chengsong
parents: 519
diff changeset
    68
\def\lf{\textit{lf}}
28751de4b4ba revised according to comments
Chengsong
parents: 519
diff changeset
    69
\def\PD{\textit{PD}}
28751de4b4ba revised according to comments
Chengsong
parents: 519
diff changeset
    70
500
Chengsong
parents: 472
diff changeset
    71
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
    72
\def\size{\mathit{size}}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    73
\def\rexp{\mathbf{rexp}}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    74
\def\simp{\mathit{simp}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    75
\def\simpALTs{\mathit{simp}\_\mathit{ALTs}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    76
\def\map{\mathit{map}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    77
\def\distinct{\mathit{distinct}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
    78
\def\blexersimp{\mathit{blexer}\_\mathit{simp}}
500
Chengsong
parents: 472
diff changeset
    79
\def\map{\textit{map}}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    80
%\def\vsuf{\textit{vsuf}}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    81
%\def\sflataux{\textit{sflat}\_\textit{aux}}
500
Chengsong
parents: 472
diff changeset
    82
\def\rrexp{\textit{rrexp}}
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    83
\newcommand\rnullable[1]{\textit{rnullable}(#1)}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    84
\newcommand\rsize[1]{\llbracket #1 \rrbracket_r}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    85
\newcommand\asize[1]{\llbracket #1 \rrbracket}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    86
\newcommand\rerase[1]{ (#1)\downarrow_r}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    87
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
    88
500
Chengsong
parents: 472
diff changeset
    89
\def\erase{\textit{erase}}
Chengsong
parents: 472
diff changeset
    90
\def\STAR{\textit{STAR}}
Chengsong
parents: 472
diff changeset
    91
\def\flts{\textit{flts}}
Chengsong
parents: 472
diff changeset
    92
503
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    93
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    94
\def\RZERO{\mathbf{0}_r }
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    95
\def\RONE{\mathbf{1}_r}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    96
\newcommand\RCHAR[1]{\mathbf{#1}_r}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    97
\newcommand\RSEQ[2]{#1 \cdot #2}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    98
\newcommand\RALTS[1]{\oplus #1}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
    99
\newcommand\RSTAR[1]{#1^*}
35b80e37dfe7 new writing
Chengsong
parents: 500
diff changeset
   100
\newcommand\vsuf[2]{\textit{vsuf} \;#1\;#2}
505
5ce3bd8e5696 thesis chapter2 section 2.4
Chengsong
parents: 503
diff changeset
   101
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   102
%----------------------------------------------------------------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   103
%This part is about regular expressions, Brzozowski derivatives,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   104
%and a bit-coded lexing algorithm with proven correctness and time bounds.
472
Chengsong
parents: 471
diff changeset
   105
Chengsong
parents: 471
diff changeset
   106
%TODO: look up snort rules to use here--give readers idea of what regexes look like
Chengsong
parents: 471
diff changeset
   107
Chengsong
parents: 471
diff changeset
   108
471
Chengsong
parents: 469
diff changeset
   109
Regular expressions are widely used in computer science: 
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   110
be it in text-editors\parencite{atomEditor} with syntax highlighting and auto-completion, 
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   111
command-line tools like $\mathit{grep}$ that facilitate easy 
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   112
text processing, network intrusion
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   113
detection systems that reject suspicious traffic, or compiler
500
Chengsong
parents: 472
diff changeset
   114
front ends--the majority of the solutions to these tasks 
Chengsong
parents: 472
diff changeset
   115
involve lexing with regular 
Chengsong
parents: 472
diff changeset
   116
expressions.
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   117
Given its usefulness and ubiquity, one would imagine that
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   118
modern regular expression matching implementations
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   119
are mature and fully studied.
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   120
Indeed, in a popular programming language' regex engine, 
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   121
supplying it with regular expressions and strings, one can
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   122
get rich matching information in a very short time.
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   123
Some network intrusion detection systems
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   124
use regex engines that are able to process 
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   125
megabytes or even gigabytes of data per second\parencite{Turo_ov__2020}.
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   126
Unfortunately, this is not the case for $\mathbf{all}$ inputs.
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   127
%TODO: get source for SNORT/BRO's regex matching engine/speed
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   128
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   129
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   130
Take $(a^*)^*\,b$ and ask whether
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   131
strings of the form $aa..a$ match this regular
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   132
expression. Obviously this is not the case---the expected $b$ in the last
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   133
position is missing. One would expect that modern regular expression
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   134
matching engines can find this out very quickly. Alas, if one tries
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   135
this example in JavaScript, Python or Java 8, even with strings of a small
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   136
length, say around 30 $a$'s, one discovers that 
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   137
this decision takes crazy time to finish given the simplicity of the problem.
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   138
This is clearly exponential behaviour, and 
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   139
is triggered by some relatively simple regex patterns, as the graphs
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   140
below show:
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   141
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   142
\begin{figure}
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   143
\centering
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   144
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   145
\begin{tikzpicture}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   146
\begin{axis}[
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   147
    xlabel={$n$},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   148
    x label style={at={(1.05,-0.05)}},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   149
    ylabel={time in secs},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   150
    enlargelimits=false,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   151
    xtick={0,5,...,30},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   152
    xmax=33,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   153
    ymax=35,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   154
    ytick={0,5,...,30},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   155
    scaled ticks=false,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   156
    axis lines=left,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   157
    width=5cm,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   158
    height=4cm, 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   159
    legend entries={JavaScript},  
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   160
    legend pos=north west,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   161
    legend cell align=left]
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   162
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   163
\end{axis}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   164
\end{tikzpicture}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   165
  &
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   166
\begin{tikzpicture}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   167
\begin{axis}[
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   168
    xlabel={$n$},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   169
    x label style={at={(1.05,-0.05)}},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   170
    %ylabel={time in secs},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   171
    enlargelimits=false,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   172
    xtick={0,5,...,30},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   173
    xmax=33,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   174
    ymax=35,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   175
    ytick={0,5,...,30},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   176
    scaled ticks=false,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   177
    axis lines=left,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   178
    width=5cm,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   179
    height=4cm, 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   180
    legend entries={Python},  
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   181
    legend pos=north west,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   182
    legend cell align=left]
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   183
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   184
\end{axis}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   185
\end{tikzpicture}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   186
  &
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   187
\begin{tikzpicture}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   188
\begin{axis}[
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   189
    xlabel={$n$},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   190
    x label style={at={(1.05,-0.05)}},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   191
    %ylabel={time in secs},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   192
    enlargelimits=false,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   193
    xtick={0,5,...,30},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   194
    xmax=33,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   195
    ymax=35,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   196
    ytick={0,5,...,30},
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   197
    scaled ticks=false,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   198
    axis lines=left,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   199
    width=5cm,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   200
    height=4cm, 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   201
    legend entries={Java 8},  
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   202
    legend pos=north west,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   203
    legend cell align=left]
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   204
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   205
\end{axis}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   206
\end{tikzpicture}\\
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   207
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   208
           of the form $\underbrace{aa..a}_{n}$.}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   209
\end{tabular}    
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   210
\caption{aStarStarb} \label{fig:aStarStarb}
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   211
\end{figure}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   212
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   213
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   214
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   215
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   216
This superlinear blowup in matching algorithms sometimes cause
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   217
considerable grief in real life: for example on 20 July 2016 one evil
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   218
regular expression brought the webpage
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   219
\href{http://stackexchange.com}{Stack Exchange} to its
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   220
knees.\footnote{\url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   221
In this instance, a regular expression intended to just trim white
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   222
spaces from the beginning and the end of a line actually consumed
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   223
massive amounts of CPU-resources---causing web servers to grind to a
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   224
halt. This happened when a post with 20,000 white spaces was submitted,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   225
but importantly the white spaces were neither at the beginning nor at
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   226
the end. As a result, the regular expression matching engine needed to
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   227
backtrack over many choices. In this example, the time needed to process
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   228
the string was $O(n^2)$ with respect to the string length. This
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   229
quadratic overhead was enough for the homepage of Stack Exchange to
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   230
respond so slowly that the load balancer assumed a $\mathit{DoS}$ 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   231
attack and therefore stopped the servers from responding to any
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   232
requests. This made the whole site become unavailable. 
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   233
A more recent example is a global outage of all Cloudflare servers on 2 July
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   234
2019. A poorly written regular expression exhibited exponential
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   235
behaviour and exhausted CPUs that serve HTTP traffic. Although the outage
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   236
had several causes, at the heart was a regular expression that
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   237
was used to monitor network
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   238
traffic.\footnote{\url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   239
%TODO: data points for some new versions of languages
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   240
These problems with regular expressions 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   241
are not isolated events that happen
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   242
very occasionally, but actually widespread.
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   243
They occur so often that they get a 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   244
name--Regular-Expression-Denial-Of-Service (ReDoS)
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   245
attack.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   246
Davis et al. \parencite{Davis18} detected more
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   247
than 1000 super-linear (SL) regular expressions
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   248
in Node.js, Python core libraries, and npm and pypi. 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   249
They therefore concluded that evil regular expressions
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   250
are problems more than "a parlour trick", but one that
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   251
requires
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   252
more research attention.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   253
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   254
 \section{The Problem Behind Slow Cases}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   255
471
Chengsong
parents: 469
diff changeset
   256
%find literature/find out for yourself that REGEX->DFA on basic regexes
Chengsong
parents: 469
diff changeset
   257
%does not blow up the size
Chengsong
parents: 469
diff changeset
   258
Shouldn't regular expression matching be linear?
Chengsong
parents: 469
diff changeset
   259
How can one explain the super-linear behaviour of the 
Chengsong
parents: 469
diff changeset
   260
regex matching engines we have?
Chengsong
parents: 469
diff changeset
   261
The time cost of regex matching algorithms in general
Chengsong
parents: 469
diff changeset
   262
involve two phases: 
Chengsong
parents: 469
diff changeset
   263
the construction phase, in which the algorithm builds some  
Chengsong
parents: 469
diff changeset
   264
suitable data structure from the input regex $r$, we denote
Chengsong
parents: 469
diff changeset
   265
the time cost by $P_1(r)$.
Chengsong
parents: 469
diff changeset
   266
The lexing
Chengsong
parents: 469
diff changeset
   267
phase, when the input string $s$ is read and the data structure
Chengsong
parents: 469
diff changeset
   268
representing that regex $r$ is being operated on. We represent the time
Chengsong
parents: 469
diff changeset
   269
it takes by $P_2(r, s)$.\\
Chengsong
parents: 469
diff changeset
   270
In the case of a $\mathit{DFA}$,
Chengsong
parents: 469
diff changeset
   271
we have $P_2(r, s) = O( |s| )$,
Chengsong
parents: 469
diff changeset
   272
because we take at most $|s|$ steps, 
Chengsong
parents: 469
diff changeset
   273
and each step takes
Chengsong
parents: 469
diff changeset
   274
at most one transition--
Chengsong
parents: 469
diff changeset
   275
a deterministic-finite-automata
Chengsong
parents: 469
diff changeset
   276
by definition has at most one state active and at most one
Chengsong
parents: 469
diff changeset
   277
transition upon receiving an input symbol.
Chengsong
parents: 469
diff changeset
   278
But unfortunately in the  worst case
Chengsong
parents: 469
diff changeset
   279
$P_1(r) = O(exp^{|r|})$. An example will be given later. \\
Chengsong
parents: 469
diff changeset
   280
For $\mathit{NFA}$s, we have $P_1(r) = O(|r|)$ if we do not unfold 
Chengsong
parents: 469
diff changeset
   281
expressions like $r^n$ into $\underbrace{r \cdots r}_{\text{n copies of r}}$.
Chengsong
parents: 469
diff changeset
   282
The $P_2(r, s)$ is bounded by $|r|\cdot|s|$, if we do not backtrack.
Chengsong
parents: 469
diff changeset
   283
On the other hand, if backtracking is used, the worst-case time bound bloats
Chengsong
parents: 469
diff changeset
   284
to $|r| * 2^|s|$ .
Chengsong
parents: 469
diff changeset
   285
%on the input
Chengsong
parents: 469
diff changeset
   286
%And when calculating the time complexity of the matching algorithm,
Chengsong
parents: 469
diff changeset
   287
%we are assuming that each input reading step requires constant time.
Chengsong
parents: 469
diff changeset
   288
%which translates to that the number of 
Chengsong
parents: 469
diff changeset
   289
%states active and transitions taken each time is bounded by a
Chengsong
parents: 469
diff changeset
   290
%constant $C$.
Chengsong
parents: 469
diff changeset
   291
%But modern  regex libraries in popular language engines
Chengsong
parents: 469
diff changeset
   292
% often want to support much richer constructs than just
Chengsong
parents: 469
diff changeset
   293
% sequences and Kleene stars,
Chengsong
parents: 469
diff changeset
   294
%such as negation, intersection, 
Chengsong
parents: 469
diff changeset
   295
%bounded repetitions and back-references.
Chengsong
parents: 469
diff changeset
   296
%And de-sugaring these "extended" regular expressions 
Chengsong
parents: 469
diff changeset
   297
%into basic ones might bloat the size exponentially.
Chengsong
parents: 469
diff changeset
   298
%TODO: more reference for exponential size blowup on desugaring. 
Chengsong
parents: 469
diff changeset
   299
\subsection{Tools that uses $\mathit{DFA}$s}
Chengsong
parents: 469
diff changeset
   300
%TODO:more tools that use DFAs?
Chengsong
parents: 469
diff changeset
   301
$\mathit{LEX}$ and $\mathit{JFLEX}$ are tools
Chengsong
parents: 469
diff changeset
   302
in $C$ and $\mathit{JAVA}$ that generates $\mathit{DFA}$-based
Chengsong
parents: 469
diff changeset
   303
lexers. The user provides a set of regular expressions
Chengsong
parents: 469
diff changeset
   304
and configurations to such lexer generators, and then 
Chengsong
parents: 469
diff changeset
   305
gets an output program encoding a minimized $\mathit{DFA}$
Chengsong
parents: 469
diff changeset
   306
that can be compiled and run. 
Chengsong
parents: 469
diff changeset
   307
The good things about $\mathit{DFA}$s is that once
Chengsong
parents: 469
diff changeset
   308
generated, they are fast and stable, unlike
Chengsong
parents: 469
diff changeset
   309
backtracking algorithms. 
Chengsong
parents: 469
diff changeset
   310
However, they do not scale well with bounded repetitions.\\
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   311
516
6fecb7fe8cd0 blexer2: modified for plotting
Chengsong
parents: 506
diff changeset
   312
6fecb7fe8cd0 blexer2: modified for plotting
Chengsong
parents: 506
diff changeset
   313
6fecb7fe8cd0 blexer2: modified for plotting
Chengsong
parents: 506
diff changeset
   314
471
Chengsong
parents: 469
diff changeset
   315
Bounded repetitions, usually written in the form
Chengsong
parents: 469
diff changeset
   316
$r^{\{c\}}$ (where $c$ is a constant natural number),
Chengsong
parents: 469
diff changeset
   317
denotes a regular expression accepting strings
Chengsong
parents: 469
diff changeset
   318
that can be divided into $c$ substrings, where each 
Chengsong
parents: 469
diff changeset
   319
substring is in $r$. 
Chengsong
parents: 469
diff changeset
   320
For the regular expression $(a|b)^*a(a|b)^{\{2\}}$,
Chengsong
parents: 469
diff changeset
   321
an $\mathit{NFA}$ describing it would look like:
Chengsong
parents: 469
diff changeset
   322
\begin{center}
Chengsong
parents: 469
diff changeset
   323
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto] 
Chengsong
parents: 469
diff changeset
   324
   \node[state,initial] (q_0)   {$q_0$}; 
Chengsong
parents: 469
diff changeset
   325
   \node[state, red] (q_1) [right=of q_0] {$q_1$}; 
Chengsong
parents: 469
diff changeset
   326
   \node[state, red] (q_2) [right=of q_1] {$q_2$}; 
Chengsong
parents: 469
diff changeset
   327
   \node[state, accepting, red](q_3) [right=of q_2] {$q_3$};
Chengsong
parents: 469
diff changeset
   328
    \path[->] 
Chengsong
parents: 469
diff changeset
   329
    (q_0) edge  node {a} (q_1)
Chengsong
parents: 469
diff changeset
   330
    	  edge [loop below] node {a,b} ()
Chengsong
parents: 469
diff changeset
   331
    (q_1) edge  node  {a,b} (q_2)
Chengsong
parents: 469
diff changeset
   332
    (q_2) edge  node  {a,b} (q_3);
Chengsong
parents: 469
diff changeset
   333
\end{tikzpicture}
Chengsong
parents: 469
diff changeset
   334
\end{center}
Chengsong
parents: 469
diff changeset
   335
The red states are "countdown states" which counts down 
Chengsong
parents: 469
diff changeset
   336
the number of characters needed in addition to the current
Chengsong
parents: 469
diff changeset
   337
string to make a successful match.
Chengsong
parents: 469
diff changeset
   338
For example, state $q_1$ indicates a match that has
Chengsong
parents: 469
diff changeset
   339
gone past the $(a|b)^*$ part of $(a|b)^*a(a|b)^{\{2\}}$,
Chengsong
parents: 469
diff changeset
   340
and just consumed the "delimiter" $a$ in the middle, and 
Chengsong
parents: 469
diff changeset
   341
need to match 2 more iterations of $(a|b)$ to complete.
Chengsong
parents: 469
diff changeset
   342
State $q_2$ on the other hand, can be viewed as a state
Chengsong
parents: 469
diff changeset
   343
after $q_1$ has consumed 1 character, and just waits
Chengsong
parents: 469
diff changeset
   344
for 1 more character to complete.
Chengsong
parents: 469
diff changeset
   345
$q_3$ is the last state, requiring 0 more character and is accepting.
Chengsong
parents: 469
diff changeset
   346
Depending on the suffix of the
Chengsong
parents: 469
diff changeset
   347
input string up to the current read location,
Chengsong
parents: 469
diff changeset
   348
the states $q_1$ and $q_2$, $q_3$
Chengsong
parents: 469
diff changeset
   349
may or may
Chengsong
parents: 469
diff changeset
   350
not be active, independent from each other.
Chengsong
parents: 469
diff changeset
   351
A $\mathit{DFA}$ for such an $\mathit{NFA}$ would
Chengsong
parents: 469
diff changeset
   352
contain at least $2^3$ non-equivalent states that cannot be merged, 
Chengsong
parents: 469
diff changeset
   353
because the subset construction during determinisation will generate
Chengsong
parents: 469
diff changeset
   354
all the elements in the power set $\mathit{Pow}\{q_1, q_2, q_3\}$.
Chengsong
parents: 469
diff changeset
   355
Generalizing this to regular expressions with larger
Chengsong
parents: 469
diff changeset
   356
bounded repetitions number, we have that
Chengsong
parents: 469
diff changeset
   357
regexes shaped like $r^*ar^{\{n\}}$ when converted to $\mathit{DFA}$s
Chengsong
parents: 469
diff changeset
   358
would require at least $2^{n+1}$ states, if $r$ contains
Chengsong
parents: 469
diff changeset
   359
more than 1 string.
Chengsong
parents: 469
diff changeset
   360
This is to represent all different 
Chengsong
parents: 469
diff changeset
   361
scenarios which "countdown" states are active.
Chengsong
parents: 469
diff changeset
   362
For those regexes, tools such as $\mathit{JFLEX}$ 
Chengsong
parents: 469
diff changeset
   363
would generate gigantic $\mathit{DFA}$'s or
Chengsong
parents: 469
diff changeset
   364
out of memory errors.
Chengsong
parents: 469
diff changeset
   365
For this reason, regex libraries that support 
Chengsong
parents: 469
diff changeset
   366
bounded repetitions often choose to use the $\mathit{NFA}$ 
Chengsong
parents: 469
diff changeset
   367
approach.
Chengsong
parents: 469
diff changeset
   368
\subsection{The $\mathit{NFA}$ approach to regex matching}
Chengsong
parents: 469
diff changeset
   369
One can simulate the $\mathit{NFA}$ running in two ways:
Chengsong
parents: 469
diff changeset
   370
one by keeping track of all active states after consuming 
Chengsong
parents: 469
diff changeset
   371
a character, and update that set of states iteratively.
Chengsong
parents: 469
diff changeset
   372
This can be viewed as a breadth-first-search of the $\mathit{NFA}$
Chengsong
parents: 469
diff changeset
   373
for a path terminating
Chengsong
parents: 469
diff changeset
   374
at an accepting state.
Chengsong
parents: 469
diff changeset
   375
Languages like $\mathit{Go}$ and $\mathit{Rust}$ use this
Chengsong
parents: 469
diff changeset
   376
type of $\mathit{NFA}$ simulation, and guarantees a linear runtime
Chengsong
parents: 469
diff changeset
   377
in terms of input string length.
Chengsong
parents: 469
diff changeset
   378
%TODO:try out these lexers
Chengsong
parents: 469
diff changeset
   379
The other way to use $\mathit{NFA}$ for matching is choosing  
Chengsong
parents: 469
diff changeset
   380
a single transition each time, keeping all the other options in 
Chengsong
parents: 469
diff changeset
   381
a queue or stack, and backtracking if that choice eventually 
Chengsong
parents: 469
diff changeset
   382
fails. This method, often called a  "depth-first-search", 
Chengsong
parents: 469
diff changeset
   383
is efficient in a lot of cases, but could end up
Chengsong
parents: 469
diff changeset
   384
with exponential run time.\\
Chengsong
parents: 469
diff changeset
   385
%TODO:COMPARE java python lexer speed with Rust and Go
Chengsong
parents: 469
diff changeset
   386
The reason behind backtracking algorithms in languages like
Chengsong
parents: 469
diff changeset
   387
Java and Python is that they support back-references.
Chengsong
parents: 469
diff changeset
   388
\subsection{Back References in Regex--Non-Regular part}
Chengsong
parents: 469
diff changeset
   389
If we have a regular expression like this (the sequence
Chengsong
parents: 469
diff changeset
   390
operator is omitted for brevity):
Chengsong
parents: 469
diff changeset
   391
\begin{center}
Chengsong
parents: 469
diff changeset
   392
	$r_1(r_2(r_3r_4))$
Chengsong
parents: 469
diff changeset
   393
\end{center}
Chengsong
parents: 469
diff changeset
   394
We could label sub-expressions of interest 
Chengsong
parents: 469
diff changeset
   395
by parenthesizing them and giving 
Chengsong
parents: 469
diff changeset
   396
them a number by the order in which their opening parentheses appear.
Chengsong
parents: 469
diff changeset
   397
One possible way of parenthesizing and labelling is given below:
Chengsong
parents: 469
diff changeset
   398
\begin{center}
Chengsong
parents: 469
diff changeset
   399
	$\underset{1}{(}r_1\underset{2}{(}r_2\underset{3}{(}r_3)\underset{4}{(}r_4)))$
Chengsong
parents: 469
diff changeset
   400
\end{center}
Chengsong
parents: 469
diff changeset
   401
$r_1r_2r_3r_4$, $r_1r_2r_3$, $r_3$, $r_4$ are labelled
Chengsong
parents: 469
diff changeset
   402
by 1 to 4. $1$ would refer to the entire expression 
Chengsong
parents: 469
diff changeset
   403
$(r_1(r_2(r_3)(r_4)))$, $2$ referring to $r_2(r_3)(r_4)$, etc.
Chengsong
parents: 469
diff changeset
   404
These sub-expressions are called "capturing groups".
Chengsong
parents: 469
diff changeset
   405
We can use the following syntax to denote that we want a string just matched by a 
Chengsong
parents: 469
diff changeset
   406
sub-expression (capturing group) to appear at a certain location again, 
Chengsong
parents: 469
diff changeset
   407
exactly as it was:
Chengsong
parents: 469
diff changeset
   408
\begin{center}
Chengsong
parents: 469
diff changeset
   409
$\ldots\underset{\text{i-th lparen}}{(}{r_i})\ldots 
Chengsong
parents: 469
diff changeset
   410
\underset{s_i \text{ which just matched} \;r_i}{\backslash i}$
Chengsong
parents: 469
diff changeset
   411
\end{center}
Chengsong
parents: 469
diff changeset
   412
The backslash and number $i$ are used to denote such 
Chengsong
parents: 469
diff changeset
   413
so-called "back-references".
Chengsong
parents: 469
diff changeset
   414
Let $e$ be an expression made of regular expressions 
Chengsong
parents: 469
diff changeset
   415
and back-references. $e$ contains the expression $e_i$
Chengsong
parents: 469
diff changeset
   416
as its $i$-th capturing group.
Chengsong
parents: 469
diff changeset
   417
The semantics of back-reference can be recursively
Chengsong
parents: 469
diff changeset
   418
written as:
Chengsong
parents: 469
diff changeset
   419
\begin{center}
Chengsong
parents: 469
diff changeset
   420
	\begin{tabular}{c}
Chengsong
parents: 469
diff changeset
   421
		$L ( e \cdot \backslash i) = \{s @ s_i \mid s \in L (e)\quad s_i \in L(r_i)$\\
Chengsong
parents: 469
diff changeset
   422
		$s_i\; \text{match of ($e$, $s$)'s $i$-th capturing group string}\}$
Chengsong
parents: 469
diff changeset
   423
	\end{tabular}
Chengsong
parents: 469
diff changeset
   424
\end{center}
Chengsong
parents: 469
diff changeset
   425
The concrete example
Chengsong
parents: 469
diff changeset
   426
$((a|b|c|\ldots|z)^*)\backslash 1$
Chengsong
parents: 469
diff changeset
   427
would match the string like $\mathit{bobo}$, $\mathit{weewee}$ and etc.\\
Chengsong
parents: 469
diff changeset
   428
Back-reference is a construct in the "regex" standard
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   429
that programmers found useful, but not exactly 
471
Chengsong
parents: 469
diff changeset
   430
regular any more.
Chengsong
parents: 469
diff changeset
   431
In fact, that allows the regex construct to express 
Chengsong
parents: 469
diff changeset
   432
languages that cannot be contained in context-free
Chengsong
parents: 469
diff changeset
   433
languages either.
Chengsong
parents: 469
diff changeset
   434
For example, the back-reference $((a^*)b\backslash1 b \backslash 1$
Chengsong
parents: 469
diff changeset
   435
expresses the language $\{a^n b a^n b a^n\mid n \in \mathbb{N}\}$,
Chengsong
parents: 469
diff changeset
   436
which cannot be expressed by context-free grammars\parencite{campeanu2003formal}.
Chengsong
parents: 469
diff changeset
   437
Such a language is contained in the context-sensitive hierarchy
Chengsong
parents: 469
diff changeset
   438
of formal languages. 
Chengsong
parents: 469
diff changeset
   439
Solving the back-reference expressions matching problem
Chengsong
parents: 469
diff changeset
   440
is NP-complete\parencite{alfred2014algorithms} and a non-bactracking,
Chengsong
parents: 469
diff changeset
   441
efficient solution is not known to exist.
Chengsong
parents: 469
diff changeset
   442
%TODO:read a bit more about back reference algorithms
Chengsong
parents: 469
diff changeset
   443
It seems that languages like Java and Python made the trade-off
Chengsong
parents: 469
diff changeset
   444
to support back-references at the expense of having to backtrack,
Chengsong
parents: 469
diff changeset
   445
even in the case of regexes not involving back-references.\\
Chengsong
parents: 469
diff changeset
   446
Summing these up, we can categorise existing 
Chengsong
parents: 469
diff changeset
   447
practical regex libraries into the ones  with  linear
Chengsong
parents: 469
diff changeset
   448
time guarantees like Go and Rust, which impose restrictions
Chengsong
parents: 469
diff changeset
   449
on the user input (not allowing back-references, 
Chengsong
parents: 469
diff changeset
   450
bounded repetitions canno exceed 1000 etc.), and ones  
Chengsong
parents: 469
diff changeset
   451
 that allows the programmer much freedom, but grinds to a halt
Chengsong
parents: 469
diff changeset
   452
 in some non-negligible portion of cases.
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   453
 %TODO: give examples such as RE2 GOLANG 1000 restriction, rust no repetitions 
471
Chengsong
parents: 469
diff changeset
   454
% For example, the Rust regex engine claims to be linear, 
Chengsong
parents: 469
diff changeset
   455
% but does not support lookarounds and back-references.
Chengsong
parents: 469
diff changeset
   456
% The GoLang regex library does not support over 1000 repetitions.  
Chengsong
parents: 469
diff changeset
   457
% Java and Python both support back-references, but shows
Chengsong
parents: 469
diff changeset
   458
%catastrophic backtracking behaviours on inputs without back-references(
Chengsong
parents: 469
diff changeset
   459
%when the language is still regular).
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   460
 %TODO: test performance of Rust on (((((a*a*)b*)b){20})*)c  baabaabababaabaaaaaaaaababaaaababababaaaabaaabaaaaaabaabaabababaababaaaaaaaaababaaaababababaaaaaaaaaaaaac
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   461
 %TODO: verify the fact Rust does not allow 1000+ reps
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   462
 %TODO: Java 17 updated graphs? Is it ok to still use Java 8 graphs?
471
Chengsong
parents: 469
diff changeset
   463
\section{Buggy Regex Engines} 
Chengsong
parents: 469
diff changeset
   464
Chengsong
parents: 469
diff changeset
   465
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   466
 Another thing about these libraries is that there
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   467
 is no correctness guarantee.
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   468
 In some cases, they either fail to generate a lexing result when there exists a match,
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   469
 or give the wrong way of matching.
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   470
 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   471
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   472
It turns out that regex libraries not only suffer from 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   473
exponential backtracking problems, 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   474
but also undesired (or even buggy) outputs.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   475
%TODO: comment from who
471
Chengsong
parents: 469
diff changeset
   476
Kuklewicz\parencite{KuklewiczHaskell} commented that most regex libraries are not
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   477
correctly implementing the POSIX (maximum-munch)
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   478
rule of regular expression matching.
471
Chengsong
parents: 469
diff changeset
   479
This experience is echoed by the writer's
Chengsong
parents: 469
diff changeset
   480
tryout of a few online regex testers:
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   481
A concrete example would be 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   482
the regex
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   483
\begin{verbatim}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   484
(((((a*a*)b*)b){20})*)c
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   485
\end{verbatim}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   486
and the string
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   487
\begin{verbatim}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   488
baabaabababaabaaaaaaaaababaa
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   489
aababababaaaabaaabaaaaaabaab
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   490
aabababaababaaaaaaaaababaaaa
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   491
babababaaaaaaaaaaaaac
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   492
\end{verbatim}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   493
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   494
This seemingly complex regex simply says "some $a$'s
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   495
followed by some $b$'s then followed by 1 single $b$,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   496
and this iterates 20 times, finally followed by a $c$.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   497
And a POSIX match would involve the entire string,"eating up"
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   498
all the $b$'s in it.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   499
%TODO: give a coloured example of how this matches POSIXly
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   500
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   501
This regex would trigger catastrophic backtracking in 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   502
languages like Python and Java,
471
Chengsong
parents: 469
diff changeset
   503
whereas it gives a non-POSIX  and uninformative 
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   504
match in languages like Go or .NET--The match with only 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   505
character $c$.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   506
471
Chengsong
parents: 469
diff changeset
   507
As Grathwohl\parencite{grathwohl2014crash} commented,
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   508
\begin{center}
471
Chengsong
parents: 469
diff changeset
   509
	``The POSIX strategy is more complicated than the greedy because of the dependence on information about the length of matched strings in the various subexpressions.''
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   510
\end{center}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   511
472
Chengsong
parents: 471
diff changeset
   512
%\section{How people solve problems with regexes}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   513
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   514
472
Chengsong
parents: 471
diff changeset
   515
When a regular expression does not behave as intended,
Chengsong
parents: 471
diff changeset
   516
people usually try to rewrite the regex to some equivalent form
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   517
or they try to avoid the possibly problematic patterns completely,
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   518
for which many false positives exist\parencite{Davis18}.
472
Chengsong
parents: 471
diff changeset
   519
Animated tools to "debug" regular expressions
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   520
are also popular, regexploit\parencite{regexploit2021}, regex101\parencite{regex101} 
472
Chengsong
parents: 471
diff changeset
   521
to name a few.
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   522
We are also aware of static analysis work on regular expressions that
472
Chengsong
parents: 471
diff changeset
   523
aims to detect potentially expoential regex patterns. Rathnayake and Thielecke 
471
Chengsong
parents: 469
diff changeset
   524
\parencite{Rathnayake2014StaticAF} proposed an algorithm
Chengsong
parents: 469
diff changeset
   525
that detects regular expressions triggering exponential
Chengsong
parents: 469
diff changeset
   526
behavious on backtracking matchers.
472
Chengsong
parents: 471
diff changeset
   527
Weideman \parencite{Weideman2017Static} came up with 
Chengsong
parents: 471
diff changeset
   528
non-linear polynomial worst-time estimates
471
Chengsong
parents: 469
diff changeset
   529
for regexes, attack string that exploit the worst-time 
Chengsong
parents: 469
diff changeset
   530
scenario, and "attack automata" that generates
472
Chengsong
parents: 471
diff changeset
   531
attack strings.
Chengsong
parents: 471
diff changeset
   532
%Arguably these methods limits the programmers' freedom
Chengsong
parents: 471
diff changeset
   533
%or productivity when all they want is to come up with a regex
Chengsong
parents: 471
diff changeset
   534
%that solves the text processing problem.
Chengsong
parents: 471
diff changeset
   535
471
Chengsong
parents: 469
diff changeset
   536
%TODO:also the regex101 debugger
Chengsong
parents: 469
diff changeset
   537
\section{Our Solution--Formal Specification of POSIX and Brzozowski Derivatives}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   538
 Is it possible to have a regex lexing algorithm with proven correctness and 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   539
 time complexity, which allows easy extensions to
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   540
  constructs like 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   541
 bounded repetitions, negation,  lookarounds, and even back-references? 
472
Chengsong
parents: 471
diff changeset
   542
  
Chengsong
parents: 471
diff changeset
   543
  We propose Brzozowski derivatives on regular expressions as
Chengsong
parents: 471
diff changeset
   544
  a solution to this.
Chengsong
parents: 471
diff changeset
   545
  
Chengsong
parents: 471
diff changeset
   546
  In the last fifteen or so years, Brzozowski's derivatives of regular
Chengsong
parents: 471
diff changeset
   547
expressions have sparked quite a bit of interest in the functional
Chengsong
parents: 471
diff changeset
   548
programming and theorem prover communities.  The beauty of
Chengsong
parents: 471
diff changeset
   549
Brzozowski's derivatives \parencite{Brzozowski1964} is that they are neatly
Chengsong
parents: 471
diff changeset
   550
expressible in any functional language, and easily definable and
Chengsong
parents: 471
diff changeset
   551
reasoned about in theorem provers---the definitions just consist of
Chengsong
parents: 471
diff changeset
   552
inductive datatypes and simple recursive functions. 
Chengsong
parents: 471
diff changeset
   553
And an algorithms based on it by 
Chengsong
parents: 471
diff changeset
   554
Suzmann and Lu  \parencite{Sulzmann2014} allows easy extension
Chengsong
parents: 471
diff changeset
   555
to include  extended regular expressions and 
Chengsong
parents: 471
diff changeset
   556
 simplification of internal data structures 
Chengsong
parents: 471
diff changeset
   557
 eliminating the exponential behaviours.
Chengsong
parents: 471
diff changeset
   558
 
Chengsong
parents: 471
diff changeset
   559
519
Chengsong
parents: 518
diff changeset
   560
  \section{Motivation}
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   561
  
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   562
Derivatives give a simple solution
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   563
to the problem of matching a string $s$ with a regular
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   564
expression $r$: if the derivative of $r$ w.r.t.\ (in
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   565
succession) all the characters of the string matches the empty string,
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   566
then $r$ matches $s$ (and {\em vice versa}).  
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   567
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   568
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   569
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
   570
However, two difficulties with derivative-based matchers exist:
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   571
First, Brzozowski's original matcher only generates a yes/no answer
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   572
for whether a regular expression matches a string or not.  This is too
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   573
little information in the context of lexing where separate tokens must
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   574
be identified and also classified (for example as keywords
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   575
or identifiers).  Sulzmann and Lu~\cite{Sulzmann2014} overcome this
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   576
difficulty by cleverly extending Brzozowski's matching
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   577
algorithm. Their extended version generates additional information on
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   578
\emph{how} a regular expression matches a string following the POSIX
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   579
rules for regular expression matching. They achieve this by adding a
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   580
second ``phase'' to Brzozowski's algorithm involving an injection
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   581
function.  In our own earlier work we provided the formal
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   582
specification of what POSIX matching means and proved in Isabelle/HOL
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   583
the correctness
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   584
of Sulzmann and Lu's extended algorithm accordingly
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   585
\cite{AusafDyckhoffUrban2016}.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   586
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   587
The second difficulty is that Brzozowski's derivatives can 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   588
grow to arbitrarily big sizes. For example if we start with the
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   589
regular expression $(a+aa)^*$ and take
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   590
successive derivatives according to the character $a$, we end up with
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   591
a sequence of ever-growing derivatives like 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   592
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   593
\def\ll{\stackrel{\_\backslash{} a}{\longrightarrow}}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   594
\begin{center}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   595
\begin{tabular}{rll}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   596
$(a + aa)^*$ & $\ll$ & $(\ONE + \ONE{}a) \cdot (a + aa)^*$\\
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   597
& $\ll$ & $(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* \;+\; (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   598
& $\ll$ & $(\ZERO + \ZERO{}a + \ZERO) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^* \;+\; $\\
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   599
& & $\qquad(\ZERO + \ZERO{}a + \ONE) \cdot (a + aa)^* + (\ONE + \ONE{}a) \cdot (a + aa)^*$\\
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   600
& $\ll$ & \ldots \hspace{15mm}(regular expressions of sizes 98, 169, 283, 468, 767, \ldots)
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   601
\end{tabular}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   602
\end{center}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   603
 
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   604
\noindent where after around 35 steps we run out of memory on a
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   605
typical computer (we shall define shortly the precise details of our
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   606
regular expressions and the derivative operation).  Clearly, the
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   607
notation involving $\ZERO$s and $\ONE$s already suggests
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   608
simplification rules that can be applied to regular regular
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   609
expressions, for example $\ZERO{}\,r \Rightarrow \ZERO$, $\ONE{}\,r
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   610
\Rightarrow r$, $\ZERO{} + r \Rightarrow r$ and $r + r \Rightarrow
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   611
r$. While such simple-minded simplifications have been proved in our
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   612
earlier work to preserve the correctness of Sulzmann and Lu's
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   613
algorithm \cite{AusafDyckhoffUrban2016}, they unfortunately do
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   614
\emph{not} help with limiting the growth of the derivatives shown
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   615
above: the growth is slowed, but the derivatives can still grow rather
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   616
quickly beyond any finite bound.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   617
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   618
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   619
Sulzmann and Lu overcome this ``growth problem'' in a second algorithm
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   620
\cite{Sulzmann2014} where they introduce bitcoded
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   621
regular expressions. In this version, POSIX values are
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   622
represented as bitsequences and such sequences are incrementally generated
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   623
when derivatives are calculated. The compact representation
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   624
of bitsequences and regular expressions allows them to define a more
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   625
``aggressive'' simplification method that keeps the size of the
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   626
derivatives finite no matter what the length of the string is.
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   627
They make some informal claims about the correctness and linear behaviour
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   628
of this version, but do not provide any supporting proof arguments, not
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   629
even ``pencil-and-paper'' arguments. They write about their bitcoded
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   630
\emph{incremental parsing method} (that is the algorithm to be formalised
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   631
in this paper):
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   632
519
Chengsong
parents: 518
diff changeset
   633
Chengsong
parents: 518
diff changeset
   634
  
Chengsong
parents: 518
diff changeset
   635
  \begin{quote}\it
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   636
  ``Correctness Claim: We further claim that the incremental parsing
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   637
  method [..] in combination with the simplification steps [..]
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   638
  yields POSIX parse trees. We have tested this claim
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   639
  extensively [..] but yet
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   640
  have to work out all proof details.'' \cite[Page 14]{Sulzmann2014}
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   641
\end{quote}  
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   642
519
Chengsong
parents: 518
diff changeset
   643
Ausaf and Urban were able to back this correctness claim with
Chengsong
parents: 518
diff changeset
   644
a formal proof.
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   645
519
Chengsong
parents: 518
diff changeset
   646
But as they stated,
Chengsong
parents: 518
diff changeset
   647
  \begin{quote}\it
Chengsong
parents: 518
diff changeset
   648
The next step would be to implement a more aggressive simplification procedure on annotated regular expressions and then prove the corresponding algorithm generates the same values as blexer. Alas due to time constraints we are unable to do so here.
Chengsong
parents: 518
diff changeset
   649
\end{quote}  
Chengsong
parents: 518
diff changeset
   650
Chengsong
parents: 518
diff changeset
   651
This thesis implements the aggressive simplifications envisioned
Chengsong
parents: 518
diff changeset
   652
by Ausaf and Urban,
Chengsong
parents: 518
diff changeset
   653
and gives a formal proof of the correctness with those simplifications.
Chengsong
parents: 518
diff changeset
   654
Chengsong
parents: 518
diff changeset
   655
Chengsong
parents: 518
diff changeset
   656
 
Chengsong
parents: 518
diff changeset
   657
Chengsong
parents: 518
diff changeset
   658
Chengsong
parents: 518
diff changeset
   659
Chengsong
parents: 518
diff changeset
   660
%----------------------------------------------------------------------------------------
Chengsong
parents: 518
diff changeset
   661
Chengsong
parents: 518
diff changeset
   662
\section{Contribution}
Chengsong
parents: 518
diff changeset
   663
Chengsong
parents: 518
diff changeset
   664
Chengsong
parents: 518
diff changeset
   665
Chengsong
parents: 518
diff changeset
   666
This work addresses the vulnerability of super-linear and
Chengsong
parents: 518
diff changeset
   667
buggy regex implementations by the combination
Chengsong
parents: 518
diff changeset
   668
of Brzozowski's derivatives and interactive theorem proving. 
Chengsong
parents: 518
diff changeset
   669
We give an 
Chengsong
parents: 518
diff changeset
   670
improved version of  Sulzmann and Lu's bit-coded algorithm using 
Chengsong
parents: 518
diff changeset
   671
derivatives, which come with a formal guarantee in terms of correctness and 
Chengsong
parents: 518
diff changeset
   672
running time as an Isabelle/HOL proof.
Chengsong
parents: 518
diff changeset
   673
Then we improve the algorithm with an even stronger version of 
Chengsong
parents: 518
diff changeset
   674
simplification, and prove a time bound linear to input and
Chengsong
parents: 518
diff changeset
   675
cubic to regular expression size using a technique by
Chengsong
parents: 518
diff changeset
   676
Antimirov.
Chengsong
parents: 518
diff changeset
   677
Chengsong
parents: 518
diff changeset
   678
 
Chengsong
parents: 518
diff changeset
   679
The main contribution of this thesis is a proven correct lexing algorithm
Chengsong
parents: 518
diff changeset
   680
with formalized time bounds.
Chengsong
parents: 518
diff changeset
   681
To our best knowledge, no lexing libraries using Brzozowski derivatives
Chengsong
parents: 518
diff changeset
   682
have a provable time guarantee, 
Chengsong
parents: 518
diff changeset
   683
and claims about running time are usually speculative and backed by thin empirical
Chengsong
parents: 518
diff changeset
   684
evidence.
Chengsong
parents: 518
diff changeset
   685
%TODO: give references
Chengsong
parents: 518
diff changeset
   686
For example, Sulzmann and Lu had proposed an algorithm  in which they
Chengsong
parents: 518
diff changeset
   687
claim a linear running time.
Chengsong
parents: 518
diff changeset
   688
But that was falsified by our experiments and the running time 
Chengsong
parents: 518
diff changeset
   689
is actually $\Omega(2^n)$ in the worst case.
Chengsong
parents: 518
diff changeset
   690
A similar claim about a theoretical runtime of $O(n^2)$ is made for the Verbatim
Chengsong
parents: 518
diff changeset
   691
%TODO: give references
Chengsong
parents: 518
diff changeset
   692
lexer, which calculates POSIX matches and is based on derivatives.
Chengsong
parents: 518
diff changeset
   693
They formalized the correctness of the lexer, but not the complexity.
Chengsong
parents: 518
diff changeset
   694
In the performance evaluation section, they simply analyzed the run time
Chengsong
parents: 518
diff changeset
   695
of matching $a$ with the string $\underbrace{a \ldots a}_{\text{n a's}}$
Chengsong
parents: 518
diff changeset
   696
and concluded that the algorithm is quadratic in terms of input length.
Chengsong
parents: 518
diff changeset
   697
When we tried out their extracted OCaml code with our example $(a+aa)^*$,
Chengsong
parents: 518
diff changeset
   698
the time it took to lex only 40 $a$'s was 5 minutes.
Chengsong
parents: 518
diff changeset
   699
Chengsong
parents: 518
diff changeset
   700
We  believe our results of a proof of performance on general
Chengsong
parents: 518
diff changeset
   701
inputs rather than specific examples a novel contribution.\\
Chengsong
parents: 518
diff changeset
   702
Chengsong
parents: 518
diff changeset
   703
Chengsong
parents: 518
diff changeset
   704
\subsection{Related Work}
Chengsong
parents: 518
diff changeset
   705
We are aware
Chengsong
parents: 518
diff changeset
   706
of a mechanised correctness proof of Brzozowski's derivative-based matcher in HOL4 by
Chengsong
parents: 518
diff changeset
   707
Owens and Slind~\parencite{Owens2008}. Another one in Isabelle/HOL is part
Chengsong
parents: 518
diff changeset
   708
of the work by Krauss and Nipkow \parencite{Krauss2011}.  And another one
Chengsong
parents: 518
diff changeset
   709
in Coq is given by Coquand and Siles \parencite{Coquand2012}.
Chengsong
parents: 518
diff changeset
   710
Also Ribeiro and Du Bois give one in Agda \parencite{RibeiroAgda2017}.
Chengsong
parents: 518
diff changeset
   711
 
Chengsong
parents: 518
diff changeset
   712
 %We propose Brzozowski's derivatives as a solution to this problem.
Chengsong
parents: 518
diff changeset
   713
% about Lexing Using Brzozowski derivatives
Chengsong
parents: 518
diff changeset
   714
Chengsong
parents: 518
diff changeset
   715
Chengsong
parents: 518
diff changeset
   716
\section{Structure of the thesis}
Chengsong
parents: 518
diff changeset
   717
In chapter 2 \ref{Chapter2} we will introduce the concepts
Chengsong
parents: 518
diff changeset
   718
and notations we 
Chengsong
parents: 518
diff changeset
   719
use for describing the lexing algorithm by Sulzmann and Lu,
Chengsong
parents: 518
diff changeset
   720
and then give the algorithm and its variant, and discuss
Chengsong
parents: 518
diff changeset
   721
why more aggressive simplifications are needed. 
Chengsong
parents: 518
diff changeset
   722
Then we illustrate in Chapter 3\ref{Chapter3}
Chengsong
parents: 518
diff changeset
   723
how the algorithm without bitcodes falls short for such aggressive 
Chengsong
parents: 518
diff changeset
   724
simplifications and therefore introduce our version of the
Chengsong
parents: 518
diff changeset
   725
 bitcoded algorithm and 
Chengsong
parents: 518
diff changeset
   726
its correctness proof .  
Chengsong
parents: 518
diff changeset
   727
In Chapter 4 \ref{Chapter4} we give the second guarantee
Chengsong
parents: 518
diff changeset
   728
of our bitcoded algorithm, that is a finite bound on the size of any 
Chengsong
parents: 518
diff changeset
   729
regex's derivatives.
Chengsong
parents: 518
diff changeset
   730
In Chapter 5\ref{Chapter5} we discuss stronger simplifications to improve the finite bound
Chengsong
parents: 518
diff changeset
   731
in Chapter 4 to a polynomial one, and demonstrate how one can extend the
Chengsong
parents: 518
diff changeset
   732
algorithm to include constructs such as bounded repetitions and negations.
Chengsong
parents: 518
diff changeset
   733
 
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   734
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   735
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   736
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   737
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   738
%----------------------------------------------------------------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   739
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   740
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   741
%----------------------------------------------------------------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   742
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   743
%----------------------------------------------------------------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   744
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   745
%----------------------------------------------------------------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   746
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   747