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