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