ChengsongTanPhdThesis/Chapters/RelatedWork.tex
author Chengsong
Mon, 21 Nov 2022 23:56:15 +0000
changeset 626 1c8525061545
parent 625 b797c9a709d9
child 627 94db2636a296
permissions -rw-r--r--
finished!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     1
% Chapter Template
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     2
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     3
\chapter{Related Work} % Main chapter title
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     4
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     5
\label{RelatedWork} 
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     6
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
     7
In this chapter, we introduce
626
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
     8
work relevant to this thesis.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
     9
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    10
\section{Regular Expressions, Derivatives and POSIX Lexing}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    11
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    12
%\subsection{Formalisations of Automata, Regular Expressions, and Matching Algorithms}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    13
Regular expressions were introduced by Kleene in the 1950s \cite{Kleene1956}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    14
Since then they have become a fundamental concept in 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    15
formal languages and automata theory \cite{Sakarovitch2009}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    16
Brzozowski defined derivatives on regular expressions in his PhD thesis
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    17
in 1964 \cite{Brzozowski1964}, in which he proved the finiteness of the numbers
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    18
of regular expression derivatives modulo the ACI-axioms.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    19
It is worth pointing out that this result does not directly
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    20
translate to our
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    21
finiteness proof, 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    22
and our proof does not depend on it to be valid.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    23
The key observation is that our version of the Sulzmann and Lu's algorithm
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    24
\cite{Sulzmann2014} represents 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    25
derivative terms in a way that allows efficient de-duplication,
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    26
and we do not make use of an equivalence checker that exploits the ACI-equivalent
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    27
terms.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    28
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    29
Central to this thesis is the work by Sulzmann and Lu \cite{Sulzmann2014}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    30
They first introduced the elegant and simple idea of injection-based lexing
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    31
and bit-coded lexing.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    32
In a follow-up work \cite{Sulzmann2014b}, Sulzmann and Steenhoven
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    33
incorporated these ideas
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    34
into a tool called \emph{dreml}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    35
The pencil-and-paper proofs in \cite{Sulzmann2014} based on the ideas
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    36
by Frisch and Cardelli \cite{Frisch2004} were later
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    37
found to have unfillable gaps by Ausaf \cite{Ausaf},
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    38
who came up with an alternative proof inspired by 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    39
Vansummeren \cite{Vansummeren2006}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    40
Sulzmann and Thiemann extended the Brzozowski derivatives to 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    41
shuffling regular expressions \cite{Sulzmann2015},
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    42
which are a very succinct way of describing basic regular expressions.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    43
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    44
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    45
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    46
Regular expressions and lexers have been a popular topic among
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    47
the theorem proving and functional programming community.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    48
In the next section we give a list of lexers
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    49
and matchers that come with a machine-checked correctness proof.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    50
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    51
\subsection{Matchers and Lexers with Mechanised Proofs}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    52
We are aware
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    53
of a mechanised correctness proof of Brzozowski's derivative-based matcher in HOL4 by
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    54
Owens and Slind~\parencite{Owens2008}. Another one in Isabelle/HOL is part
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    55
of the work by Krauss and Nipkow \parencite{Krauss2011}.  Another one
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    56
in Coq is given by Coquand and Siles \parencite{Coquand2012}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    57
Also Ribeiro and Du Bois gave one in Agda \parencite{RibeiroAgda2017}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    58
The most recent works to our best knowledge are the Verbatim \cite{Verbatim}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    59
and Verbatim++ \cite{Verbatim} lexers.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    60
The Verbatim++ lexer adds many correctness-preserving 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    61
optimisations to the Verbatim lexer,
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    62
and is therefore quite fast on many inputs.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    63
The problem is that they chose to use DFA to speed up things,
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    64
for which dealing with bounded repetitions is a bottleneck.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    65
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    66
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    67
This thesis builds on the formal specifications of POSIX
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    68
rules and formal proofs by Ausaf et al. \cite{AusafDyckhoffUrban2016}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    69
The bounded repetitions is a continuation of the work by Ausaf \cite{Ausaf}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    70
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    71
Automata formalisations are in general harder and more cumbersome to deal
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    72
with for theorem provers \cite{Nipkow1998}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    73
To represent them, one way is to use graphs, but graphs are not inductive datatypes.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    74
Having to set the inductive principle on the number of nodes
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    75
in a graph makes formal reasoning non-intuitive and convoluted. 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    76
When combining two graphs, one also needs to make sure that the nodes in
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    77
both graphs are distinct. 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    78
If they are not distinct, then renaming of the nodes is needed.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    79
Using Coq which provides dependent types can potentially make things slightly easier
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    80
\cite{Doczkal2013}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    81
Another representation for automata are matrices. 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    82
But the induction for them is not as straightforward either.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    83
Both approaches have been used in the past and resulted in huge formalisations.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    84
There are some more clever representations, for example one by Paulson 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    85
using hereditarily finite sets. 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    86
There the problem with combining graphs can be solved better, 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    87
but we believe that such clever tricks are not very obvious for 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    88
the John-average-Isabelle-user.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    89
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    90
\subsection{Different Definitions of POSIX Rules}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    91
There are different ways to formalise values and POSIX matching.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    92
Cardelli and Frisch \cite{Frisch2004} have developed a notion of
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    93
\emph{non-problematic values} which is a slight variation
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    94
of the values defined in the inhabitation relation in \ref{fig:inhab}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    95
They then defined an ordering between values, and showed that
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    96
the maximal element of those values correspond to the output
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    97
of their GREEDY lexing algorithm.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    98
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
    99
Okui and Suzuki \cite{Okui10} allow iterations of values to 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   100
flatten to the empty
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   101
string for the star inhabitation rule in
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   102
\ref{fig:inhab}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   103
They refer to the more restrictive version as used 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   104
in this thesis (which was defined by Ausaf et al.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   105
\cite{AusafDyckhoffUrban2016}) as \emph{canonical values}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   106
The very interesting link between the work by Ausaf et al.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   107
and Okui and Suzuki is that they have distinct formalisations
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   108
of POSIX values, and yet they are provably equivalent! See
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   109
\cite{AusafDyckhoffUrban2016} for details of the
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   110
alternative definitions given by Okui and Suzuki and the formalisation.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   111
%TODO: EXPLICITLY STATE the OKUI SUZUKI POSIX DEF.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   112
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   113
Sulzmann and Lu themselves have come up with POSIX definitions \cite{Sulzmann2014}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   114
In their paper they defined an ordering between values with respect
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   115
to regular expressions, and tried to establish that their
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   116
algorithm outputs the minimal element by a pencil-and-paper proof.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   117
But having the ordering relation taking regular expression as parameters
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   118
causes the transitivity of their ordering to not go through.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   119
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   120
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   121
\section{Optimisations}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   122
Perhaps the biggest problem that prevents derivative-based lexing 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   123
from being more widely adopted
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   124
is that they tend to be not blindingly fast in practice, unable to 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   125
reach throughputs like gigabytes per second, which is the
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   126
application we had in mind when we initially started looking at the topic.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   127
Commercial
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   128
regular expression matchers such as Snort \cite{Snort1999} and Bro \cite{Bro}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   129
are capable of inspecting payloads
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   130
at line rates (which can be up to a few gigabits per second) against 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   131
thousands of rules \cite{communityRules}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   132
For our algorithm to be more attractive for practical use, we
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   133
need more correctness-preserving optimisations.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   134
%So far our focus has been mainly on the bit-coded algorithm,
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   135
%but the injection-based lexing algorithm 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   136
%could also be sped up in various ways:
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   137
%
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   138
One such optimisation is defining string derivatives
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   139
directly, without recursively decomposing them into 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   140
character-by-character derivatives. For example, instead of
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   141
replacing $(r_1 + r_2)\backslash (c::cs)$ by
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   142
$((r_1 + r_2)\backslash c)\backslash cs$ (as in definition \ref{table:der}), we rather
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   143
calculate $(r_1\backslash (c::cs) + r_2\backslash (c::cs))$.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   144
This has the potential to speed up matching because input is 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   145
processed in larger granularity.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   146
One interesting thing is to explore whether this can be done
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   147
to $\inj$ as well, so that we can generate a lexical value 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   148
rather than simply get a matcher.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   149
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   150
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   151
\subsection{Derivatives and Zippers}
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   152
Zipper is a data structure designed to focus on 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   153
and navigate between local parts of a tree.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   154
The idea is that often operations on a large tree only deal with
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   155
local regions each time.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   156
Therefore it would be a waste to 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   157
traverse the entire tree if
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   158
the operation only
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   159
involves a small fraction of it.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   160
It was first formally described by Huet \cite{HuetZipper}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   161
Typical applications of zippers involve text editor buffers
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   162
and proof system databases.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   163
In our setting, the idea is to compactify the representation
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   164
of derivatives with zippers, thereby making our algorithm faster.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   165
We introduce several works on parsing, derivatives
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   166
and zippers.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   167
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   168
Edelmann et al. developed a formalised parser for LL(1) grammars using derivatives
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   169
\cite{Zippy2020}.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   170
They adopted zippers to improve the speed, and argued that the runtime
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   171
complexity of their algorithm was linear with respect to the input string.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   172
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   173
The idea of using Brzozowski derivatives on general context-free 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   174
parsing was first implemented
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   175
by Might et al. \ref{Might2011}. 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   176
They used memoization and fixpoint construction to eliminate infinite recursion,
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   177
which is a major problem for using derivatives on context-free grammars.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   178
The initial version was quite slow----exponential on the size of the input.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   179
Adams et al. \ref{Adams2016} improved the speed and argued that their version
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   180
was cubic with respect to the input.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   181
Darragh and Adams \cite{Darragh2020} further improved the performance
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   182
by using zippers in an innovative way--their zippers had multiple focuses
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   183
instead of just one in a traditional zipper to handle ambiguity.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   184
Their algorithm was not formalised, though.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   185
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   186
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   187
Some initial results have been obtained, but more care needs to be taken to make sure 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   188
that the lexer output conforms to the POSIX standard. Formalising correctness of 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   189
such a lexer using zippers will probably require using an imperative
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   190
framework with separation logic as it involves
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   191
mutable data structures, which is also interesting to look into.  
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   192
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   193
To further optimise the algorithm, 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   194
I plan to add a ``deduplicate'' function that tells 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   195
whether two regular expressions denote the same 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   196
language using
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   197
an efficient and verified equivalence checker.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   198
In this way, the internal data structures can be pruned more aggressively, 
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   199
leading to better simplifications and ultimately faster algorithms.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   200
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   201
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   202
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   203
The idea is to put the focus on that subtree, turning other parts
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   204
of the tree into a context
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   205
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   206
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   207
One observation about our derivative-based lexing algorithm is that
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   208
the derivative operation sometimes traverses the entire regular expression
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   209
unnecessarily:
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   210
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   211
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   212
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   213
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   214
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   215
\section{Work on Back-References}
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   216
We introduced regular expressions with back-references
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   217
in chapter \ref{Introduction}.
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   218
We adopt the common practice of calling them rewbrs (Regular Expressions
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   219
With Back References) in this section for brevity.
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   220
It has been shown by Aho \cite{Aho1990}
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   221
that the k-vertex cover problem can be reduced
626
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   222
to the problem of rewbrs matching, and is therefore NP-complete.
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   223
Given the depth of the problem, the progress made at the full generality
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   224
of arbitrary rewbrs matching has been slow, with
1c8525061545 finished!
Chengsong
parents: 625
diff changeset
   225
theoretical work on them being 
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   226
fairly recent. 
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   227
624
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   228
Campaneu et al. studied regexes with back-references
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   229
in the context of formal languages theory in 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   230
their 2003 work \cite{campeanu2003formal}.
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   231
They devised a pumping lemma for Perl-like regexes, 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   232
proving that the langugages denoted by them
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   233
is  properly  contained in context-sensitive
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   234
languages.
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   235
More interesting questions such as 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   236
whether the language denoted by Perl-like regexes
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   237
can express palindromes ($\{w \mid w = w^R\}$)
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   238
are discussed in \cite{campeanu2009patterns} 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   239
and \cite{CAMPEANU2009Intersect}.
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   240
Freydenberger \cite{Frey2013} investigated the 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   241
expressive power of back-references. He showed several 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   242
undecidability and decriptional complexity results 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   243
of back-references, concluding that they add
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   244
great power to certain programming tasks but are difficult to harness.
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   245
An interesting question would then be
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   246
whether we can add restrictions to them, 
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   247
so that they become expressive enough for practical use such
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   248
as html files, but not too powerful.
624
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   249
Freydenberger and Schmid \cite{FREYDENBERGER20191}
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   250
introduced the notion of deterministic
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   251
regular expressions with back-references to achieve
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   252
a better balance between expressiveness and tractability.
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   253
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   254
624
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   255
Fernau and Schmid \cite{FERNAU2015287} and Schmid \cite{Schmid2012}
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   256
investigated the time complexity of different variants
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   257
of back-references.
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   258
We are not aware of any work that uses derivatives on back-references.
624
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   259
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   260
See \cite{BERGLUND2022} for a survey
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   261
of these works and comparison between different
624
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   262
flavours  of back-references syntax (e.g. whether references can be circular, 
8ffa28fce271 all comments incorporated!!+related work
Chengsong
parents: 622
diff changeset
   263
can labels be repeated etc.).
608
37b6fd310a16 added related work chap
Chengsong
parents:
diff changeset
   264
609
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   265
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   266
 
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   267
\subsection{Static Analysis of Evil Regex Patterns} 
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   268
When a regular expression does not behave as intended,
609
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   269
people usually try to rewrite the regex to some equivalent form
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   270
or they try to avoid the possibly problematic patterns completely,
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   271
for which many false positives exist\parencite{Davis18}.
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   272
Animated tools to "debug" regular expressions such as
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   273
 \parencite{regexploit2021} \parencite{regex101} are also popular.
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   274
We are also aware of static analysis work on regular expressions that
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   275
aims to detect potentially expoential regex patterns. Rathnayake and Thielecke 
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   276
\parencite{Rathnayake2014StaticAF} proposed an algorithm
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   277
that detects regular expressions triggering exponential
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   278
behavious on backtracking matchers.
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   279
Weideman \parencite{Weideman2017Static} came up with 
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   280
non-linear polynomial worst-time estimates
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   281
for regexes, attack string that exploit the worst-time 
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   282
scenario, and "attack automata" that generates
61139fdddae0 chap1 totally done
Chengsong
parents: 608
diff changeset
   283
attack strings.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   284
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   285
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   286
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   287
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   288
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   289
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   290
Thanks to our theorem-prover-friendly approach,
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   291
we believe that 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   292
this finiteness bound can be improved to a bound
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   293
linear to input and
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   294
cubic to the regular expression size using a technique by
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   295
Antimirov\cite{Antimirov95}.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   296
Once formalised, this would be a guarantee for the absence of all super-linear behavious.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   297
We are working out the
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   298
details.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   299
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   300
 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   301
To our best knowledge, no lexing libraries using Brzozowski derivatives
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   302
have similar complexity-related bounds, 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   303
and claims about running time are usually speculative and backed by empirical
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   304
evidence on a few test cases.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   305
If a matching or lexing algorithm
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   306
does not come with certain basic complexity related 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   307
guarantees (for examaple the internal data structure size
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   308
does not grow indefinitely), 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   309
then they cannot claim with confidence having solved the problem
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 609
diff changeset
   310
of catastrophic backtracking.
625
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   311
b797c9a709d9 section reorganising, related work
Chengsong
parents: 624
diff changeset
   312