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