ChengsongTanPhdThesis/Chapters/Inj.tex
author Chengsong
Mon, 06 Jun 2022 16:45:42 +0100
changeset 536 aff7bf93b9c7
parent 532 cc54ce075db5
child 538 8016a2480704
permissions -rwxr-xr-x
comments addressed all
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     1
% Chapter Template
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     3
\chapter{Regular Expressions and POSIX Lexing} % Main chapter title
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     4
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     5
\label{Inj} % In chapter 2 \ref{Chapter2} we will introduce the concepts
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     6
%and notations we 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     7
%use for describing the lexing algorithm by Sulzmann and Lu,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     8
%and then give the algorithm and its variant, and discuss
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     9
%why more aggressive simplifications are needed. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    10
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    11
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    12
\section{Basic Concepts and Notations for Strings, Languages, and Regular Expressions}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    13
We have a primitive datatype char, denoting characters.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    14
\[			char ::=  a
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    15
			 \mid b
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    16
			 \mid c
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    17
			 \mid  \ldots
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    18
			 \mid z       
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    19
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    20
(which one is better?)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    21
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    22
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    23
$\textit{char}$ & $\dn$ & $a | b | c | \ldots$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    24
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    25
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    26
They can form strings by lists:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    27
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    28
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    29
$\textit{string}$ & $\dn$ & $[] | c  :: cs$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    30
& & $(c\; \text{has char type})$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    31
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    32
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    33
And strings can be concatenated to form longer strings:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    34
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    35
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    36
$[] @ s_2$ & $\dn$ & $s_2$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    37
$(c :: s_1) @ s_2$ & $\dn$ & $c :: (s_1 @ s_2)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    38
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    39
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    40
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    41
A set of strings can operate with another set of strings:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    42
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    43
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    44
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A; s_B \in B \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    45
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    46
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    47
We also call the above "language concatenation".
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    48
The power of a language is defined recursively, using the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    49
concatenation operator $@$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    50
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    51
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    52
$A^0 $ & $\dn$ & $\{ [] \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    53
$A^{n+1}$ & $\dn$ & $A^n @ A$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    54
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    55
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    56
The union of all the natural number powers of a language   
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    57
is denoted by the Kleene star operator:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    58
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    59
\begin{tabular}{lcl}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    60
 $A*$ & $\dn$ & $\bigcup_{i \geq 0} A^i$ \\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    61
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    62
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    63
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    64
To get an induction principle in Isabelle/HOL, 
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    65
we instead define the Kleene star
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    66
as an inductive set: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    67
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    68
\begin{tabular}{lcl}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    69
$[] \in A*$  & &\\
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    70
$s_1 \in A \land \; s_2 \in A* $ & $\implies$ & $s_1 @ s_2 \in A*$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    71
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    72
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    73
\subsection{Language Derivatives}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    74
We also define an operation of chopping off a character from
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    75
a language:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    76
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    77
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    78
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    79
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    80
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    81
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    82
This can be generalised to chopping off a string from all strings within set $A$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    83
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    84
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    85
$\textit{Ders} \;w \;A$ & $\dn$ & $\{ s \mid w@s \in A \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    86
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    87
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    88
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    89
which is essentially the left quotient $A \backslash L'$ of $A$ against 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    90
the singleton language $L' = \{w\}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    91
in formal language theory.
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    92
For this dissertation the $\textit{Ders}$ definition with 
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    93
a single string suffices.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    94
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    95
With the  sequencing, Kleene star, and $\textit{Der}$ operator on languages,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    96
we have a  few properties of how the language derivative can be defined using 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    97
sub-languages.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    98
\begin{lemma}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    99
\[
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   100
	\Der \; c \; (A @ B) =
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   101
	\begin{cases}
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   102
	((\Der \; c \; A) @ B ) \cup \Der \; c\; B, &  \text{if} \;  [] \in A  \\
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   103
	 (\Der \; c \; A) @ B, & \text{otherwise}
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   104
	 \end{cases}	
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   105
\]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   106
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   107
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   108
This lemma states that if $A$ contains the empty string, $\Der$ can "pierce" through it
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   109
and get to $B$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   110
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   111
The language $A*$'s derivative can be described using the language derivative
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   112
of $A$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   113
\begin{lemma}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   114
$\textit{Der} \;c \;A* = (\textit{Der}\; c A) @ (A*)$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   115
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   116
\begin{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   117
\begin{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   118
\item{$\subseteq$}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   119
The set 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   120
\[ \{s \mid c :: s \in A*\} \]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   121
is enclosed in the set
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   122
\[ \{s_1 @ s_2 \mid s_1 \, s_2. s_1 \in \{s \mid c :: s \in A\} \land s_2 \in A* \} \]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   123
because whenever you have a string starting with a character 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   124
in the language of a Kleene star $A*$, then that character together with some sub-string
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   125
immediately after it will form the first iteration, and the rest of the string will 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   126
be still in $A*$.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   127
\item{$\supseteq$}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   128
Note that
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   129
\[ \Der \; c \; A* = \Der \; c \;  (\{ [] \} \cup (A @ A*) ) \]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   130
and 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   131
\[ \Der \; c \;  (\{ [] \} \cup (A @ A*) ) = \Der\; c \; (A @ A*) \]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   132
where the $\textit{RHS}$ of the above equatioin can be rewritten
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   133
as \[ (\Der \; c\; A) @ A* \cup A' \], $A'$ being a possibly empty set.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   134
\end{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   135
\end{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   136
Before we define the $\textit{Der}$ and $\textit{Ders}$ counterpart
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   137
for regular languages, we need to first give definitions for regular expressions.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   138
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   139
\subsection{Regular Expressions and Their Meaning}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   140
Suppose we have an alphabet $\Sigma$, the strings  whose characters
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   141
are from $\Sigma$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   142
can be expressed as $\Sigma^*$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   143
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   144
We use patterns to define a set of strings concisely. Regular expressions
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   145
are one of such patterns systems:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   146
The basic regular expressions  are defined inductively
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   147
 by the following grammar:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   148
\[			r ::=   \ZERO \mid  \ONE
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   149
			 \mid  c  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   150
			 \mid  r_1 \cdot r_2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   151
			 \mid  r_1 + r_2   
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   152
			 \mid r^*         
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   153
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   154
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   155
The language or set of strings denoted 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   156
by regular expressions are defined as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   157
%TODO: FILL in the other defs
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   158
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   159
\begin{tabular}{lcl}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   160
$L \; (\ZERO)$ & $\dn$ & $\phi$\\
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   161
$L \; (\ONE)$ & $\dn$ & $\{[]\}$\\
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   162
$L \; (c)$ & $\dn$ & $\{[c]\}$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   163
$L \; (r_1 + r_2)$ & $\dn$ & $ L \; (r_1) \cup L \; ( r_2)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   164
$L \; (r_1 \cdot r_2)$ & $\dn$ & $ L \; (r_1) \cap L \; (r_2)$\\
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   165
$L \; (r^*)$ & $\dn$ & $ (L(r))^*$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   166
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   167
\end{center}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   168
\noindent
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   169
Which is also called the "language interpretation" of
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   170
a regular expression.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   171
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   172
Now with semantic derivatives of a language and regular expressions and
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   173
their language interpretations in place, we are ready to define derivatives on regexes.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   174
\subsection{Brzozowski Derivatives and a Regular Expression Matcher}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   175
The language derivative acts on a string set and chops off a character from
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   176
all strings in that set, we want to define a derivative operation on regular expressions
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   177
so that after derivative $L(r\backslash c)$ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   178
will look as if it was obtained by doing a language derivative on $L(r)$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   179
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   180
L(r \backslash c) = \Der \; c \; L(r)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   181
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   182
So we mimic the equalities we have for $\Der$ on language concatenation
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   183
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   184
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   185
\Der \; c \; (A @ B) = \textit{if} \;  [] \in A \; \textit{then} ((\Der \; c \; A) @ B ) \cup \Der \; c\; B \quad \textit{else}\; (\Der \; c \; A) @ B\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   186
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   187
to get the derivative for sequence regular expressions:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   188
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   189
(r_1 \cdot r_2 ) \backslash c = \textit{if}\,([] \in L(r_1)) r_1 \backslash c \cdot r_2 + r_2 \backslash c \textit{else} (r_1 \backslash c) \cdot r_2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   190
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   191
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   192
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   193
and language Kleene star:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   194
\[
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   195
\textit{Der} \;c \;A* = (\textit{Der}\; c A) @ (A*)
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   196
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   197
to get derivative of the Kleene star regular expression:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   198
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   199
r^* \backslash c = (r \backslash c)\cdot r^*
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   200
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   201
Note that although we can formalise the boolean predicate
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   202
$[] \in L(r_1)$ without problems, if we want a function that works
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   203
computationally, then we would have to define a function that tests
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   204
whether an empty string is in the language of a regular expression.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   205
We call such a function $\nullable$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   206
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   207
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   208
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   209
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   210
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   211
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   212
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   213
		$d \backslash c$     & $\dn$ & 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   214
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   215
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   216
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   217
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   218
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   219
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   220
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   221
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   222
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   223
The function derivative, written $r\backslash c$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   224
defines how a regular expression evolves into
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   225
a new regular expression after all the string it contains
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   226
is chopped off a certain head character $c$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   227
The most involved cases are the sequence 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   228
and star case.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   229
The sequence case says that if the first regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   230
contains an empty string then the second component of the sequence
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   231
might be chosen as the target regular expression to be chopped
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   232
off its head character.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   233
The star regular expression's derivative unwraps the iteration of
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   234
regular expression and attaches the star regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   235
to the sequence's second element to make sure a copy is retained
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   236
for possible more iterations in later phases of lexing.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   237
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   238
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   239
The $\nullable$ function tests whether the empty string $""$ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   240
is in the language of $r$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   241
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   242
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   243
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   244
		\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   245
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   246
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   247
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   248
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   249
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   250
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   251
		\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   252
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   253
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   254
The empty set does not contain any string and
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   255
therefore not the empty string, the empty string 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   256
regular expression contains the empty string
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   257
by definition, the character regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   258
is the singleton that contains character only,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   259
and therefore does not contain the empty string,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   260
the alternative regular expression (or "or" expression)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   261
might have one of its children regular expressions
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   262
being nullable and any one of its children being nullable
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   263
would suffice. The sequence regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   264
would require both children to have the empty string
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   265
to compose an empty string and the Kleene star
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   266
operation naturally introduced the empty string. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   267
  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   268
We have the following property where the derivative on regular 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   269
expressions coincides with the derivative on a set of strings:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   270
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   271
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   272
$\textit{Der} \; c \; L(r) = L (r\backslash c)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   273
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   274
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   275
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   276
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   277
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   278
The main property of the derivative operation
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   279
that enables us to reason about the correctness of
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   280
an algorithm using derivatives is 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   281
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   282
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   283
$c\!::\!s \in L(r)$ holds
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   284
if and only if $s \in L(r\backslash c)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   285
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   286
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   287
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   288
We can generalise the derivative operation shown above for single characters
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   289
to strings as follows:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   290
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   291
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   292
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   293
$r \backslash_s (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash_s s$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   294
$r \backslash [\,] $ & $\dn$ & $r$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   295
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   296
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   297
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   298
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   299
When there is no ambiguity we will use  $\backslash$ to denote
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   300
string derivatives for brevity.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   301
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   302
and then define Brzozowski's  regular-expression matching algorithm as:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   303
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   304
\begin{definition}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   305
$match\;s\;r \;\dn\; nullable(r\backslash s)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   306
\end{definition}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   307
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   308
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   309
Assuming the string is given as a sequence of characters, say $c_0c_1..c_n$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   310
this algorithm presented graphically is as follows:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   311
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   312
\begin{equation}\label{graph:successive_ders}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   313
\begin{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   314
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed]  & r_n  \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   315
\end{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   316
\end{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   317
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   318
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   319
where we start with  a regular expression  $r_0$, build successive
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   320
derivatives until we exhaust the string and then use \textit{nullable}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   321
to test whether the result can match the empty string. It can  be
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   322
relatively  easily shown that this matcher is correct  (that is given
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   323
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   324
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   325
Beautiful and simple definition.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   326
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   327
If we implement the above algorithm naively, however,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   328
the algorithm can be excruciatingly slow. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   329
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   330
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   331
\begin{figure}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   332
\centering
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   333
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   334
\begin{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   335
\begin{axis}[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   336
    xlabel={$n$},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   337
    x label style={at={(1.05,-0.05)}},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   338
    ylabel={time in secs},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   339
    enlargelimits=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   340
    xtick={0,5,...,30},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   341
    xmax=33,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   342
    ymax=10000,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   343
    ytick={0,1000,...,10000},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   344
    scaled ticks=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   345
    axis lines=left,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   346
    width=5cm,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   347
    height=4cm, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   348
    legend entries={JavaScript},  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   349
    legend pos=north west,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   350
    legend cell align=left]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   351
\addplot[red,mark=*, mark options={fill=white}] table {EightThousandNodes.data};
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   352
\end{axis}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   353
\end{tikzpicture}\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   354
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   355
           of the form $\underbrace{aa..a}_{n}$.}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   356
\end{tabular}    
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   357
\caption{EightThousandNodes} \label{fig:EightThousandNodes}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   358
\end{figure}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   359
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   360
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   361
(8000 node data to be added here)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   362
For example, when starting with the regular
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   363
expression $(a + aa)^*$ and building a few successive derivatives (around 10)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   364
w.r.t.~the character $a$, one obtains a derivative regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   365
with more than 8000 nodes (when viewed as a tree)\ref{EightThousandNodes}.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   366
The reason why $(a + aa) ^*$ explodes so drastically is that without
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   367
pruning, the algorithm will keep records of all possible ways of matching:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   368
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   369
$(a + aa) ^* \backslash [aa] = (\ZERO + \ONE \ONE)\cdot(a + aa)^* + (\ONE + \ONE a) \cdot (a + aa)^*$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   370
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   371
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   372
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   373
Each of the above alternative branches correspond to the match 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   374
$aa $, $a \quad a$ and $a \quad a \cdot (a)$(incomplete).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   375
These different ways of matching will grow exponentially with the string length,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   376
and without simplifications that throw away some of these very similar matchings,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   377
it is no surprise that these expressions grow so quickly.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   378
Operations like
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   379
$\backslash$ and $\nullable$ need to traverse such trees and
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   380
consequently the bigger the size of the derivative the slower the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   381
algorithm. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   382
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   383
Brzozowski was quick in finding that during this process a lot useless
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   384
$\ONE$s and $\ZERO$s are generated and therefore not optimal.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   385
He also introduced some "similarity rules", such
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   386
as $P+(Q+R) = (P+Q)+R$ to merge syntactically 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   387
different but language-equivalent sub-regexes to further decrease the size
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   388
of the intermediate regexes. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   389
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   390
More simplifications are possible, such as deleting duplicates
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   391
and opening up nested alternatives to trigger even more simplifications.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   392
And suppose we apply simplification after each derivative step, and compose
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   393
these two operations together as an atomic one: $a \backslash_{simp}\,c \dn
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   394
\textit{simp}(a \backslash c)$. Then we can build
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   395
a matcher with simpler regular expressions.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   396
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   397
If we want the size of derivatives in the algorithm to
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   398
stay even lower, we would need more aggressive simplifications.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   399
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   400
delete duplicates whenever possible. For example, the parentheses in
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   401
$(a+b) \cdot c + b\cdot c$ can be opened up to get $a\cdot c + b \cdot c + b
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   402
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   403
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   404
$a^*+a+\ONE$.  These more aggressive simplification rules are for
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   405
 a very tight size bound, possibly as low
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   406
  as that of the \emph{partial derivatives}\parencite{Antimirov1995}. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   407
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   408
Building derivatives and then simplifying them.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   409
So far, so good. But what if we want to 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   410
do lexing instead of just getting a YES/NO answer?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   411
This requires us to go back again to the world 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   412
without simplification first for a moment.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   413
Sulzmann and Lu~\cite{Sulzmann2014} first came up with a nice and 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   414
elegant(arguably as beautiful as the original
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   415
derivatives definition) solution for this.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   416
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   417
\subsection*{Values and the Lexing Algorithm by Sulzmann and Lu}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   418
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   419
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   420
They first defined the datatypes for storing the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   421
lexing information called a \emph{value} or
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   422
sometimes also \emph{lexical value}.  These values and regular
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   423
expressions correspond to each other as illustrated in the following
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   424
table:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   425
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   426
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   427
	\begin{tabular}{c@{\hspace{20mm}}c}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   428
		\begin{tabular}{@{}rrl@{}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   429
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   430
			$r$ & $::=$  & $\ZERO$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   431
			& $\mid$ & $\ONE$   \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   432
			& $\mid$ & $c$          \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   433
			& $\mid$ & $r_1 \cdot r_2$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   434
			& $\mid$ & $r_1 + r_2$   \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   435
			\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   436
			& $\mid$ & $r^*$         \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   437
		\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   438
		&
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   439
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   440
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   441
			$v$ & $::=$  & \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   442
			&        & $\Empty$   \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   443
			& $\mid$ & $\Char(c)$          \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   444
			& $\mid$ & $\Seq\,v_1\, v_2$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   445
			& $\mid$ & $\Left(v)$   \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   446
			& $\mid$ & $\Right(v)$  \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   447
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   448
		\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   449
	\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   450
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   451
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   452
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   453
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   454
Building on top of Sulzmann and Lu's attempt to formalise the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   455
notion of POSIX lexing rules \parencite{Sulzmann2014}, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   456
Ausaf and Urban\parencite{AusafDyckhoffUrban2016} modelled
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   457
POSIX matching as a ternary relation recursively defined in a
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   458
natural deduction style.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   459
With the formally-specified rules for what a POSIX matching is,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   460
they proved in Isabelle/HOL that the algorithm gives correct results.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   461
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   462
But having a correct result is still not enough, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   463
we want at least some degree of $\mathbf{efficiency}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   464
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   465
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   466
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   467
One regular expression can have multiple lexical values. For example
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   468
for the regular expression $(a+b)^*$, it has a infinite list of
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   469
values corresponding to it: $\Stars\,[]$, $\Stars\,[\Left(Char(a))]$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   470
$\Stars\,[\Right(Char(b))]$, $\Stars\,[\Left(Char(a),\,\Right(Char(b))]$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   471
$\ldots$, and vice versa.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   472
Even for the regular expression matching a particular string, there could 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   473
be more than one value corresponding to it.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   474
Take the example where $r= (a^*\cdot a^*)^*$ and the string 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   475
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   476
If we do not allow any empty iterations in its lexical values,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   477
there will be $n - 1$ "splitting points" on $s$ we can choose to 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   478
split or not so that each sub-string
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   479
segmented by those chosen splitting points will form different iterations:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   480
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   481
\begin{tabular}{lcr}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   482
$a \mid aaa $ & $\rightarrow$ & $\Stars\, [v_{iteration \,a},\,  v_{iteration \,aaa}]$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   483
$aa \mid aa $ & $\rightarrow$ & $\Stars\, [v_{iteration \, aa},\,  v_{iteration \, aa}]$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   484
$a \mid aa\mid a $ & $\rightarrow$ & $\Stars\, [v_{iteration \, a},\,  v_{iteration \, aa}, \, v_{iteration \, a}]$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   485
 & $\textit{etc}.$ &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   486
 \end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   487
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   488
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   489
And for each iteration, there are still multiple ways to split
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   490
between the two $a^*$s.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   491
It is not surprising there are exponentially many lexical values
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   492
that are distinct for the regex and string pair $r= (a^*\cdot a^*)^*$  and 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   493
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   494
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   495
A lexer to keep all the possible values will naturally 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   496
have an exponential runtime on ambiguous regular expressions.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   497
Somehow one has to decide which lexical value to keep and
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   498
output in a lexing algorithm.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   499
In practice, we are usually 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   500
interested in POSIX values, which by intuition always
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   501
\begin{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   502
\item
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   503
match the leftmost regular expression when multiple options of matching
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   504
are available  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   505
\item 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   506
always match a subpart as much as possible before proceeding
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   507
to the next token.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   508
\end{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   509
The formal definition of a $\POSIX$ value $v$ for a regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   510
$r$ and string $s$, denoted as $(s, r) \rightarrow v$, can be specified 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   511
in the following set of rules:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   512
(TODO: write the entire set of inference rules for POSIX )
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   513
\newcommand*{\inference}[3][t]{%
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   514
   \begingroup
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   515
   \def\and{\\}%
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   516
   \begin{tabular}[#1]{@{\enspace}c@{\enspace}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   517
   #2 \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   518
   \hline
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   519
   #3
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   520
   \end{tabular}%
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   521
   \endgroup
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   522
}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   523
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   524
\inference{$s_1 @ s_2 = s$ \and $(\nexists s_3 s_4 s_5. s_1 @ s_5 = s_3 \land s_5 \neq [] \land s_3 @ s_4 = s \land (s_3, r_1) \rightarrow v_3 \land (s_4, r_2) \rightarrow v_4)$ \and $(s_1, r_1) \rightarrow v_1$ \and $(s_2, r_2) \rightarrow v_2$  }{$(s, r_1 \cdot r_2) \rightarrow \Seq(v_1, v_2)$ }
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   525
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   526
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   527
The reason why we are interested in $\POSIX$ values is that they can
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   528
be practically used in the lexing phase of a compiler front end.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   529
For instance, when lexing a code snippet 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   530
$\textit{iffoo} = 3$ with the regular expression $\textit{keyword} + \textit{identifier}$, we want $\textit{iffoo}$ to be recognized
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   531
as an identifier rather than a keyword.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   533
 For example, the above $r= (a^*\cdot a^*)^*$  and 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   534
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$ example has the POSIX value
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   535
$ \Stars\,[\Seq(Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}], Stars\,[])]$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   536
The output of an algorithm we want would be a POSIX matching
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   537
encoded as a value.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   538
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   539
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   540
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   541
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   542
The contribution of Sulzmann and Lu is an extension of Brzozowski's
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   543
algorithm by a second phase (the first phase being building successive
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   544
derivatives---see \eqref{graph:successive_ders}). In this second phase, a POSIX value 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   545
is generated if the regular expression matches the string.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   546
How can we construct a value out of regular expressions and character
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   547
sequences only?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   548
Two functions are involved: $\inj$ and $\mkeps$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   549
The function $\mkeps$ constructs a value from the last
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   550
one of all the successive derivatives:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   551
\begin{ceqn}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   552
\begin{equation}\label{graph:mkeps}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   553
\begin{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   554
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed] & r_n \arrow[d, "mkeps" description] \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   555
	        & 	              & 	            & v_n       
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   556
\end{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   557
\end{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   558
\end{ceqn}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   559
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   560
It tells us how can an empty string be matched by a 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   561
regular expression, in a $\POSIX$ way:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   562
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   563
	\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   564
		\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   565
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   566
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   567
			& \textit{if} $\nullable(r_{1})$\\ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   568
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   569
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   570
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   571
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   572
		\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   573
	\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   574
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   575
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   576
\noindent 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   577
We favour the left to match an empty string if there is a choice.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   578
When there is a star for us to match the empty string,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   579
we give the $\Stars$ constructor an empty list, meaning
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   580
no iterations are taken.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   581
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   582
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   583
After the $\mkeps$-call, we inject back the characters one by one in order to build
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   584
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   585
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   586
After injecting back $n$ characters, we get the lexical value for how $r_0$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   587
matches $s$. The POSIX value is maintained throughout the process.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   588
For this Sulzmann and Lu defined a function that reverses
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   589
the ``chopping off'' of characters during the derivative phase. The
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   590
corresponding function is called \emph{injection}, written
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   591
$\textit{inj}$; it takes three arguments: the first one is a regular
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   592
expression ${r_{i-1}}$, before the character is chopped off, the second
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   593
is a character ${c_{i-1}}$, the character we want to inject and the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   594
third argument is the value ${v_i}$, into which one wants to inject the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   595
character (it corresponds to the regular expression after the character
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   596
has been chopped off). The result of this function is a new value. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   597
\begin{ceqn}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   598
\begin{equation}\label{graph:inj}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   599
\begin{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   600
r_1 \arrow[r, dashed] \arrow[d]& r_i \arrow[r, "\backslash c_i"]  \arrow[d]  & r_{i+1}  \arrow[r, dashed] \arrow[d]        & r_n \arrow[d, "mkeps" description] \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   601
v_1           \arrow[u]                 & v_i  \arrow[l, dashed]                              & v_{i+1} \arrow[l,"inj_{r_i} c_i"]                 & v_n \arrow[l, dashed]         
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   602
\end{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   603
\end{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   604
\end{ceqn}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   605
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   606
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   607
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   608
The
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   609
definition of $\textit{inj}$ is as follows: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   610
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   611
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   612
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   613
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   614
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   615
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   616
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   617
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   618
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   619
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   620
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   621
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   622
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   623
\noindent This definition is by recursion on the ``shape'' of regular
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   624
expressions and values. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   625
The clauses do one thing--identifying the ``hole'' on a
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   626
value to inject the character back into.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   627
For instance, in the last clause for injecting back to a value
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   628
that would turn into a new star value that corresponds to a star,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   629
we know it must be a sequence value. And we know that the first 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   630
value of that sequence corresponds to the child regex of the star
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   631
with the first character being chopped off--an iteration of the star
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   632
that had just been unfolded. This value is followed by the already
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   633
matched star iterations we collected before. So we inject the character 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   634
back to the first value and form a new value with this latest iteration
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   635
being added to the previous list of iterations, all under the $\Stars$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   636
top level.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   637
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   638
Putting all the functions $\inj$, $\mkeps$, $\backslash$ together,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   639
we have a lexer with the following recursive definition:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   640
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   641
\begin{tabular}{lcr}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   642
$\lexer \; r \; [] $ & $=$ & $\mkeps \; r$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   643
$\lexer \; r \;c::s$ & $=$ & $\inj \; r \; c (\lexer (r\backslash c) s)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   644
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   645
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   646
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   647
Pictorially, the algorithm is as follows:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   648
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   649
\begin{ceqn}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   650
\begin{equation}\label{graph:2}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   651
\begin{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   652
r_0 \arrow[r, "\backslash c_0"]  \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   653
v_0           & v_1 \arrow[l,"inj_{r_0} c_0"]                & v_2 \arrow[l, "inj_{r_1} c_1"]              & v_n \arrow[l, dashed]         
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   654
\end{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   655
\end{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   656
\end{ceqn}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   657
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   658
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   659
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   660
For convenience, we shall employ the following notations: the regular
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   661
expression we start with is $r_0$, and the given string $s$ is composed
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   662
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   663
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   664
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   665
the derivative $r_n$. We test whether this derivative is
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   666
$\textit{nullable}$ or not. If not, we know the string does not match
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   667
$r$, and no value needs to be generated. If yes, we start building the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   668
values incrementally by \emph{injecting} back the characters into the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   669
earlier values $v_n, \ldots, v_0$. This is the second phase of the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   670
algorithm from right to left. For the first value $v_n$, we call the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   671
function $\textit{mkeps}$, which builds a POSIX lexical value
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   672
for how the empty string has been matched by the (nullable) regular
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   673
expression $r_n$. This function is defined as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   674
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   675
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   676
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   677
We have mentioned before that derivatives without simplification 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   678
can get clumsy, and this is true for values as well--they reflect
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   679
the size of the regular expression by definition.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   680
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   681
One can introduce simplification on the regex and values but have to
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   682
be careful not to break the correctness, as the injection 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   683
function heavily relies on the structure of the regexes and values
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   684
being correct and matching each other.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   685
It can be achieved by recording some extra rectification functions
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   686
during the derivatives step, and applying these rectifications in 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   687
each run during the injection phase.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   688
And we can prove that the POSIX value of how
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   689
regular expressions match strings will not be affected---although it is much harder
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   690
to establish. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   691
Some initial results in this regard have been
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   692
obtained in \cite{AusafDyckhoffUrban2016}. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   693
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   694
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   695
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   696
%Brzozowski, after giving the derivatives and simplification,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   697
%did not explore lexing with simplification, or he may well be 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   698
%stuck on an efficient simplification with proof.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   699
%He went on to examine the use of derivatives together with 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   700
%automaton, and did not try lexing using products.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   701
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   702
We want to get rid of the complex and fragile rectification of values.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   703
Can we not create those intermediate values $v_1,\ldots v_n$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   704
and get the lexing information that should be already there while
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   705
doing derivatives in one pass, without a second injection phase?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   706
In the meantime, can we make sure that simplifications
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   707
are easily handled without breaking the correctness of the algorithm?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   708
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   709
Sulzmann and Lu solved this problem by
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   710
introducing additional information to the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   711
regular expressions called \emph{bitcodes}.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   712
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   713
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   714
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   715
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   716
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   717
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   718