ChengsongTanPhdThesis/Chapters/Inj.tex
author Chengsong
Sat, 26 Nov 2022 16:18:10 +0000
changeset 628 7af4e2420a8c
parent 626 1c8525061545
child 637 e3752aac8ec2
permissions -rwxr-xr-x
ready to submit~~
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 
564
Chengsong
parents: 543
diff changeset
     7
% used for describing the lexing algorithm by Sulzmann and Lu,
Chengsong
parents: 543
diff changeset
     8
%and then give the algorithm and its variant and discuss
532
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.
583
Chengsong
parents: 579
diff changeset
    13
This is essentially a description in ``English''
564
Chengsong
parents: 543
diff changeset
    14
of our formalisation in Isabelle/HOL.
Chengsong
parents: 543
diff changeset
    15
We also give the definition of what $\POSIX$ lexing means, 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    16
followed by a lexing algorithm by Sulzmanna and Lu \parencite{Sulzmann2014} 
564
Chengsong
parents: 543
diff changeset
    17
that produces the output conforming
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    18
to the $\POSIX$ standard\footnote{In what follows 
583
Chengsong
parents: 579
diff changeset
    19
we choose to use the Isabelle-style notation
564
Chengsong
parents: 543
diff changeset
    20
for function applications, where
583
Chengsong
parents: 579
diff changeset
    21
the parameters of a function are not enclosed
564
Chengsong
parents: 543
diff changeset
    22
inside a pair of parentheses (e.g. $f \;x \;y$
Chengsong
parents: 543
diff changeset
    23
instead of $f(x,\;y)$). This is mainly
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    24
to make the text visually more concise.}.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    25
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    26
\section{Basic Concepts}
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    27
Formal language theory usually starts with an alphabet 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    28
denoting a set of characters.
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    29
Here we just use the datatype of characters from Isabelle,
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    30
which roughly corresponds to the ASCII characters.
564
Chengsong
parents: 543
diff changeset
    31
In what follows, we shall leave the information about the alphabet
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    32
implicit.
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    33
Then using the usual bracket notation for lists,
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    34
we can define strings made up of characters as: 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    35
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    36
\begin{tabular}{lcl}
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    37
$\textit{s}$ & $\dn$ & $[] \; |\; c  :: s$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    38
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    39
\end{center}
583
Chengsong
parents: 579
diff changeset
    40
where $c$ is a variable ranging over characters.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    41
The $::$ stands for list cons and $[]$ for the empty
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    42
list.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
    43
A singleton list is sometimes written as $[c]$ for brevity.
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    44
Strings can be concatenated to form longer strings in the same
564
Chengsong
parents: 543
diff changeset
    45
way as we concatenate two lists, which we shall write as $s_1 @ s_2$.
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    46
We omit the precise 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    47
recursive definition here.
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    48
We overload this concatenation operator for two sets of strings:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    49
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    50
\begin{tabular}{lcl}
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    51
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A \land s_B \in B \}$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    52
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    53
\end{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    54
We also call the above \emph{language concatenation}.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    55
The power of a language is defined recursively, using the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    56
concatenation operator $@$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    57
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    58
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    59
$A^0 $ & $\dn$ & $\{ [] \}$\\
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    60
$A^{n+1}$ & $\dn$ & $A @ A^n$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    61
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    62
\end{center}
564
Chengsong
parents: 543
diff changeset
    63
The union of all powers of a language   
Chengsong
parents: 543
diff changeset
    64
can be used to define the Kleene star operator:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    65
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    66
\begin{tabular}{lcl}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    67
 $A*$ & $\dn$ & $\bigcup_{i \geq 0} A^i$ \\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    68
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    69
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    70
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    71
\noindent
564
Chengsong
parents: 543
diff changeset
    72
However, to obtain a more convenient induction principle 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    73
in Isabelle/HOL, 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
    74
we instead define the Kleene star
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    75
as an inductive set: 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    76
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    77
\begin{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    78
\begin{mathpar}
564
Chengsong
parents: 543
diff changeset
    79
	\inferrule{\mbox{}}{[] \in A*\\}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    80
564
Chengsong
parents: 543
diff changeset
    81
\inferrule{s_1 \in A \;\; s_2 \in A*}{s_1 @ s_2 \in A*}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    82
\end{mathpar}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    83
\end{center}
564
Chengsong
parents: 543
diff changeset
    84
\noindent
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    85
We also define an operation of "chopping off" a character from
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    86
a language, which we call $\Der$, meaning \emph{Derivative} (for a language):
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    87
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    88
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    89
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    90
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    91
\end{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
    92
\noindent
583
Chengsong
parents: 579
diff changeset
    93
This can be generalised to ``chopping off'' a string 
Chengsong
parents: 579
diff changeset
    94
from all strings within a set $A$, 
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    95
namely:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    96
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    97
\begin{tabular}{lcl}
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
    98
$\textit{Ders} \;s \;A$ & $\dn$ & $\{ s' \mid s@s' \in A \}$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    99
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   100
\end{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   101
\noindent
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
   102
which is essentially the left quotient $A \backslash L$ of $A$ against 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   103
the singleton language with $L = \{s\}$.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   104
However, for our purposes here, the $\textit{Ders}$ definition with 
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
   105
a single string is sufficient.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   106
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   107
The reason for defining derivatives
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   108
is that they provide another approach
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   109
to test membership of a string in 
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   110
a set of strings. 
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   111
For example, to test whether the string
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   112
$bar$ is contained in the set $\{foo, bar, brak\}$, one takes derivative of the set with
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   113
respect to the string $bar$:
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   114
\begin{center}
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   115
\begin{tabular}{lll}
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   116
	$S = \{foo, bar, brak\}$ & $ \stackrel{\backslash b}{\rightarrow }$ & 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   117
	$\{ar, rak\}$ \\
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   118
				 & $\stackrel{\backslash a}{\rightarrow}$ & $\{r \}$\\
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   119
				 & $\stackrel{\backslash r}{\rightarrow}$ & $\{[]\}$\\
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   120
				 %& $\stackrel{[] \in S \backslash bar}{\longrightarrow}$ & $bar \in S$\\
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   121
\end{tabular}	
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   122
\end{center}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   123
\noindent
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   124
and in the end test whether the set
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   125
has the empty string.\footnote{We use the infix notation $A\backslash c$
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   126
	instead of $\Der \; c \; A$ for brevity, as it is clear we are operating
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   127
on languages rather than regular expressions.}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   128
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   129
In general, if we have a language $S$,
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   130
then we can test whether $s$ is in $S$
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   131
by testing whether $[] \in S \backslash s$.
564
Chengsong
parents: 543
diff changeset
   132
With the sequencing, Kleene star, and $\textit{Der}$ operator on languages,
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   133
we have a  few properties of how the language derivative can be defined using 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   134
sub-languages.
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   135
For example, for the sequence operator, we have
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   136
something similar to a ``chain rule'':
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   137
\begin{lemma}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   138
\[
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   139
	\Der \; c \; (A @ B) =
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   140
	\begin{cases}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   141
	((\Der \; c \; A) \, @ \, B ) \cup (\Der \; c\; B) , &  \text{if} \;  [] \in A  \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   142
	 (\Der \; c \; A) \,  @ \, B, & \text{otherwise}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   143
	 \end{cases}	
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   144
\]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   145
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   146
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   147
This lemma states that if $A$ contains the empty string, $\Der$ can "pierce" through it
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   148
and get to $B$.
583
Chengsong
parents: 579
diff changeset
   149
The language derivative for $A*$ can be described using the language derivative
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   150
of $A$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   151
\begin{lemma}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   152
$\textit{Der} \;c \;(A*) = (\textit{Der}\; c A) @ (A*)$\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   153
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   154
\begin{proof}
583
Chengsong
parents: 579
diff changeset
   155
There are two inclusions to prove:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   156
\begin{itemize}
564
Chengsong
parents: 543
diff changeset
   157
\item{$\subseteq$}:\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   158
The set 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   159
\[ \{s \mid c :: s \in A*\} \]
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   160
is enclosed in the set
564
Chengsong
parents: 543
diff changeset
   161
\[ \{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
   162
because whenever you have a string starting with a character 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   163
in the language of a Kleene star $A*$, 
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   164
then that character together with some sub-string
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   165
immediately after it will form the first iteration, 
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   166
and the rest of the string will 
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   167
be still in $A*$.
564
Chengsong
parents: 543
diff changeset
   168
\item{$\supseteq$}:\\
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   169
Note that
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   170
\[ \Der \; c \; (A*) = \Der \; c \;  (\{ [] \} \cup (A @ A*) ) \]
583
Chengsong
parents: 579
diff changeset
   171
holds.
Chengsong
parents: 579
diff changeset
   172
Also the following holds:
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   173
\[ \Der \; c \;  (\{ [] \} \cup (A @ A*) ) = \Der\; c \; (A @ A*) \]
564
Chengsong
parents: 543
diff changeset
   174
where the $\textit{RHS}$ can be rewritten
Chengsong
parents: 543
diff changeset
   175
as \[ (\Der \; c\; A) @ A* \cup (\Der \; c \; (A*)) \]
Chengsong
parents: 543
diff changeset
   176
which of course contains $\Der \; c \; A @ A*$.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   177
\end{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   178
\end{proof}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   179
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   180
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   181
The clever idea of Brzozowski was to find counterparts of $\Der$ and $\Ders$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   182
for regular expressions.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   183
To introduce them, we need to first give definitions for regular expressions,
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   184
which we shall do next.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   185
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   186
\subsection{Regular Expressions and Their Meaning}
564
Chengsong
parents: 543
diff changeset
   187
The \emph{basic regular expressions} are defined inductively
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   188
 by the following grammar:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   189
\[			r ::=   \ZERO \mid  \ONE
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   190
			 \mid  c  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   191
			 \mid  r_1 \cdot r_2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   192
			 \mid  r_1 + r_2   
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   193
			 \mid r^*         
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   194
\]
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   195
\noindent
564
Chengsong
parents: 543
diff changeset
   196
We call them basic because we will introduce
Chengsong
parents: 543
diff changeset
   197
additional constructors in later chapters such as negation
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   198
and bounded repetitions.
564
Chengsong
parents: 543
diff changeset
   199
We use $\ZERO$ for the regular expression that
Chengsong
parents: 543
diff changeset
   200
matches no string, and $\ONE$ for the regular
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   201
expression that matches only the empty string.\footnote{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   202
Some authors
564
Chengsong
parents: 543
diff changeset
   203
also use $\phi$ and $\epsilon$ for $\ZERO$ and $\ONE$
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   204
but we prefer our notation.} 
564
Chengsong
parents: 543
diff changeset
   205
The sequence regular expression is written $r_1\cdot r_2$
Chengsong
parents: 543
diff changeset
   206
and sometimes we omit the dot if it is clear which
Chengsong
parents: 543
diff changeset
   207
regular expression is meant; the alternative
Chengsong
parents: 543
diff changeset
   208
is written $r_1 + r_2$.
Chengsong
parents: 543
diff changeset
   209
The \emph{language} or meaning of 
Chengsong
parents: 543
diff changeset
   210
a regular expression is defined recursively as
Chengsong
parents: 543
diff changeset
   211
a set of strings:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   212
%TODO: FILL in the other defs
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   213
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   214
\begin{tabular}{lcl}
564
Chengsong
parents: 543
diff changeset
   215
$L \; \ZERO$ & $\dn$ & $\phi$\\
Chengsong
parents: 543
diff changeset
   216
$L \; \ONE$ & $\dn$ & $\{[]\}$\\
Chengsong
parents: 543
diff changeset
   217
$L \; c$ & $\dn$ & $\{[c]\}$\\
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   218
$L \; (r_1 + r_2)$ & $\dn$ & $ L \; r_1 \cup L \; r_2$\\
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   219
$L \; (r_1 \cdot r_2)$ & $\dn$ & $ L \; r_1 @ L \; r_2$\\
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   220
$L \; (r^*)$ & $\dn$ & $ (L\;r)*$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   221
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   222
\end{center}
536
aff7bf93b9c7 comments addressed all
Chengsong
parents: 532
diff changeset
   223
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   224
%Now with language derivatives of a language and regular expressions and
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   225
%their language interpretations in place, we are ready to define derivatives on regular expressions.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   226
With $L$ we are ready to introduce Brzozowski derivatives on regular expressions.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   227
We do so by first introducing what properties it should satisfy.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   228
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   229
\subsection{Brzozowski Derivatives and a Regular Expression Matcher}
564
Chengsong
parents: 543
diff changeset
   230
%Recall, the language derivative acts on a set of strings
Chengsong
parents: 543
diff changeset
   231
%and essentially chops off a particular character from
Chengsong
parents: 543
diff changeset
   232
%all strings in that set, Brzozowski defined a derivative operation on regular expressions
Chengsong
parents: 543
diff changeset
   233
%so that after derivative $L(r\backslash c)$ 
Chengsong
parents: 543
diff changeset
   234
%will look as if it was obtained by doing a language derivative on $L(r)$:
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   235
%Recall that the language derivative acts on a 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   236
%language (set of strings).
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   237
%One can decide whether a string $s$ belongs
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   238
%to a language $S$ by taking derivative with respect to
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   239
%that string and then checking whether the empty 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   240
%string is in the derivative:
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   241
%\begin{center}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   242
%\parskip \baselineskip
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   243
%\def\myupbracefill#1{\rotatebox{90}{\stretchto{\{}{#1}}}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   244
%\def\rlwd{.5pt}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   245
%\newcommand\notate[3]{%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   246
%  \unskip\def\useanchorwidth{T}%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   247
%  \setbox0=\hbox{#1}%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   248
%  \def\stackalignment{c}\stackunder[-6pt]{%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   249
%    \def\stackalignment{c}\stackunder[-1.5pt]{%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   250
%      \stackunder[-2pt]{\strut #1}{\myupbracefill{\wd0}}}{%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   251
%    \rule{\rlwd}{#2\baselineskip}}}{%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   252
%  \strut\kern7pt$\hookrightarrow$\rlap{ \footnotesize#3}}\ignorespaces%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   253
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   254
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   255
%\notate{$\{ \ldots ,\;$ 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   256
%	\notate{s}{1}{$(c_1 :: s_1)$} 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   257
%	$, \; \ldots \}$ 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   258
%}{1}{$S_{start}$} 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   259
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   260
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   261
%	$\stackrel{\backslash c_1}{\longrightarrow}$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   262
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   263
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   264
%	$\{ \ldots,\;$  \notate{$s_1$}{1}{$(c_2::s_2)$} 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   265
%	$,\; \ldots \}$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   266
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   267
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   268
%	$\stackrel{\backslash c_2}{\longrightarrow}$ 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   269
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   270
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   271
%	$\{ \ldots,\;  s_2
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   272
%	,\; \ldots \}$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   273
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   274
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   275
%	$ \xdashrightarrow{\backslash c_3\ldots\ldots} $	
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   276
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   277
%\Longstack{
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   278
%	\notate{$\{\ldots, [], \ldots\}$}{1}{$S_{end} = 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   279
%	S_{start}\backslash s$}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   280
%}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   281
%\end{center}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   282
%\begin{center}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   283
%	$s \in S_{start} \iff [] \in S_{end}$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   284
%\end{center}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   285
%\noindent
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   286
Brzozowski noticed that $\Der$
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   287
can be ``mirrored'' on regular expressions which
564
Chengsong
parents: 543
diff changeset
   288
he calls the derivative of a regular expression $r$
Chengsong
parents: 543
diff changeset
   289
with respect to a character $c$, written
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   290
$r \backslash c$. This infix operator
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   291
takes an original regular expression $r$ as input
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   292
and a character as a right operand and
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   293
outputs a result, which is a new regular expression.
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   294
The derivative operation on regular expression
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   295
is defined such that the language of the derivative result 
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   296
coincides with the language of the original 
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   297
regular expression being taken 
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   298
derivative with respect to the same character:
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   299
\begin{property}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   300
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   301
\[
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   302
	L \; (r \backslash c) = \Der \; c \; (L \; r)
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   303
\]
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   304
\end{property}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   305
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   306
Pictorially, this looks as follows:\\
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   307
\vspace{3mm}
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   308
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   309
\parskip \baselineskip
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   310
\def\myupbracefill#1{\rotatebox{90}{\stretchto{\{}{#1}}}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   311
\def\rlwd{.5pt}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   312
\newcommand\notate[3]{%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   313
  \unskip\def\useanchorwidth{T}%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   314
  \setbox0=\hbox{#1}%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   315
  \def\stackalignment{c}\stackunder[-6pt]{%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   316
    \def\stackalignment{c}\stackunder[-1.5pt]{%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   317
      \stackunder[-2pt]{\strut #1}{\myupbracefill{\wd0}}}{%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   318
    \rule{\rlwd}{#2\baselineskip}}}{%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   319
  \strut\kern8pt$\hookrightarrow$\rlap{ \footnotesize#3}}\ignorespaces%
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   320
}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   321
\Longstack{
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   322
	\notate{$r$}{1}{$L \; r = \{\ldots, \;c::s_1, 
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   323
\;\ldots\}$}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   324
}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   325
\Longstack{
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   326
	$\stackrel{\backslash c}{\xrightarrow{\hspace*{8cm}}}$
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   327
}
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   328
\Longstack{
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   329
	\notate{$r\backslash c$}{1}{$L \; (r\backslash c)=
577
f47fc4840579 thesis chap2
Chengsong
parents: 573
diff changeset
   330
	\{\ldots,\;s_1,\;\ldots\}$}
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   331
}\\ 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   332
\vspace{ 3mm }
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   333
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   334
The derivatives on regular expression can again be 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   335
generalised to a string.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   336
One could compute $r_{start} \backslash s$  and test membership of $s$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   337
in $L \; r_{start}$ by checking
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   338
whether the empty string is in the language of 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   339
$r_{end}$ ($r_{start}\backslash s$).\\
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   340
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   341
\vspace{2mm}
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   342
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   343
	\notate{$r_{start}$}{4}{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   344
		\Longstack{$L \; r_{start} = \{\ldots, \;$ 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   345
			\notate{$s$}{1}{$c_1::s_1$} 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   346
		$, \ldots\} $
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   347
		}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   348
	} 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   349
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   350
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   351
	$\stackrel{\backslash c_1}{ \xrightarrow{\hspace*{1.8cm}} }$
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   352
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   353
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   354
	\notate{$r_1$}{3}{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   355
		$r_1 = r_{start}\backslash c_1$,
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   356
		$L \; r_1 = $
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   357
	\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   358
		$\{ \ldots,\;$  \notate{$s_1$}{1}{$c_2::s_2$} 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   359
		$,\; \ldots \}$
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   360
	}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   361
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   362
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   363
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   364
	$\stackrel{\backslash c_2}{\xrightarrow{\hspace*{1.8cm}}}$ 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   365
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   366
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   367
	$r_2$	
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   368
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   369
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   370
	$  \xdashrightarrow{\hspace*{0.3cm} \backslash c_3 \ldots \ldots \ldots \hspace*{0.3cm}} $	
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   371
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   372
\Longstack{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   373
	\notate{$r_{end}$}{1}{
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   374
	$L \; r_{end} = \{\ldots, \; [], \ldots\}$}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   375
}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   376
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   377
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   378
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   379
\begin{property}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   380
	$s \in L \; r_{start} \iff [] \in L \; r_{end}$
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   381
\end{property}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   382
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   383
Next we give the recursive definition of derivative on
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   384
regular expressions, so that it satisfies the properties above.
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   385
The derivative function, written $r\backslash c$, 
583
Chengsong
parents: 579
diff changeset
   386
takes a regular expression $r$ and character $c$, and
Chengsong
parents: 579
diff changeset
   387
returns a new regular expression representing
Chengsong
parents: 579
diff changeset
   388
the original regular expression's language $L \; r$
Chengsong
parents: 579
diff changeset
   389
being taken the language derivative with respect to $c$.
626
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   390
\begin{table}
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   391
	\begin{center}
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   392
\begin{tabular}{lcl}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   393
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   394
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   395
		$d \backslash c$     & $\dn$ & 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   396
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   397
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   398
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   399
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   400
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   401
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   402
\end{tabular}
626
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   403
	\end{center}
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   404
\caption{Derivative on Regular Expressions}
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   405
\label{table:der}
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   406
\end{table}
564
Chengsong
parents: 543
diff changeset
   407
\noindent
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   408
The most involved cases are the sequence case
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   409
and the star case.
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   410
The sequence case says that if the first regular expression
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   411
contains an empty string, then the second component of the sequence
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   412
needs to be considered, as its derivative will contribute to the
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   413
result of this derivative:
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   414
\begin{center}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   415
	\begin{tabular}{lcl}
583
Chengsong
parents: 579
diff changeset
   416
		$(r_1 \cdot r_2 ) \backslash c$ & $\dn$ & 
Chengsong
parents: 579
diff changeset
   417
		$\textit{if}\;\,([] \in L(r_1))\; 
Chengsong
parents: 579
diff changeset
   418
		\textit{then} \; r_1 \backslash c \cdot r_2 + r_2 \backslash c$ \\
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   419
		& & $\textit{else} \; (r_1 \backslash c) \cdot r_2$
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   420
	\end{tabular}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   421
\end{center}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   422
\noindent
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   423
Notice how this closely resembles
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   424
the language derivative operation $\Der$:
564
Chengsong
parents: 543
diff changeset
   425
\begin{center}
Chengsong
parents: 543
diff changeset
   426
	\begin{tabular}{lcl}
Chengsong
parents: 543
diff changeset
   427
		$\Der \; c \; (A @ B)$ & $\dn$ & 
Chengsong
parents: 543
diff changeset
   428
		$ \textit{if} \;\,  [] \in A \; 
Chengsong
parents: 543
diff changeset
   429
		\textit{then} \;\, ((\Der \; c \; A) @ B ) \cup 
Chengsong
parents: 543
diff changeset
   430
		\Der \; c\; B$\\
Chengsong
parents: 543
diff changeset
   431
		& & $\textit{else}\; (\Der \; c \; A) @ B$\\
Chengsong
parents: 543
diff changeset
   432
	\end{tabular}
Chengsong
parents: 543
diff changeset
   433
\end{center}
Chengsong
parents: 543
diff changeset
   434
\noindent
583
Chengsong
parents: 579
diff changeset
   435
The derivative of the star regular expression $r^*$ 
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   436
unwraps one iteration of $r$, turns it into $r\backslash c$,
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   437
and attaches the original $r^*$
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   438
after $r\backslash c$, so that 
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   439
we can further unfold it as many times as needed:
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   440
\[
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   441
	(r^*) \backslash c \dn (r \backslash c)\cdot r^*.
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   442
\]
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   443
Again,
583
Chengsong
parents: 579
diff changeset
   444
the structure is the same as the language derivative of Kleene star: 
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   445
\[
564
Chengsong
parents: 543
diff changeset
   446
	\textit{Der} \;c \;(A*) \dn (\textit{Der}\; c A) @ (A*)
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   447
\]
564
Chengsong
parents: 543
diff changeset
   448
In the above definition of $(r_1\cdot r_2) \backslash c$,
Chengsong
parents: 543
diff changeset
   449
the $\textit{if}$ clause's
Chengsong
parents: 543
diff changeset
   450
boolean condition 
Chengsong
parents: 543
diff changeset
   451
$[] \in L(r_1)$ needs to be 
Chengsong
parents: 543
diff changeset
   452
somehow recursively computed.
Chengsong
parents: 543
diff changeset
   453
We call such a function that checks
Chengsong
parents: 543
diff changeset
   454
whether the empty string $[]$ is 
Chengsong
parents: 543
diff changeset
   455
in the language of a regular expression $\nullable$:
Chengsong
parents: 543
diff changeset
   456
\begin{center}
Chengsong
parents: 543
diff changeset
   457
		\begin{tabular}{lcl}
Chengsong
parents: 543
diff changeset
   458
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
Chengsong
parents: 543
diff changeset
   459
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
Chengsong
parents: 543
diff changeset
   460
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
Chengsong
parents: 543
diff changeset
   461
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
Chengsong
parents: 543
diff changeset
   462
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
Chengsong
parents: 543
diff changeset
   463
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
Chengsong
parents: 543
diff changeset
   464
		\end{tabular}
Chengsong
parents: 543
diff changeset
   465
\end{center}
Chengsong
parents: 543
diff changeset
   466
\noindent
Chengsong
parents: 543
diff changeset
   467
The $\ZERO$ regular expression
Chengsong
parents: 543
diff changeset
   468
does not contain any string and
Chengsong
parents: 543
diff changeset
   469
therefore is not \emph{nullable}.
Chengsong
parents: 543
diff changeset
   470
$\ONE$ is \emph{nullable} 
Chengsong
parents: 543
diff changeset
   471
by definition. 
Chengsong
parents: 543
diff changeset
   472
The character regular expression $c$
Chengsong
parents: 543
diff changeset
   473
corresponds to the singleton set $\{c\}$, 
Chengsong
parents: 543
diff changeset
   474
and therefore does not contain the empty string.
Chengsong
parents: 543
diff changeset
   475
The alternative regular expression is nullable
Chengsong
parents: 543
diff changeset
   476
if at least one of its children is nullable.
Chengsong
parents: 543
diff changeset
   477
The sequence regular expression
Chengsong
parents: 543
diff changeset
   478
would require both children to have the empty string
Chengsong
parents: 543
diff changeset
   479
to compose an empty string, and the Kleene star
Chengsong
parents: 543
diff changeset
   480
is always nullable because it naturally
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   481
contains the empty string.  
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   482
\noindent
564
Chengsong
parents: 543
diff changeset
   483
We have the following correspondence between 
Chengsong
parents: 543
diff changeset
   484
derivatives on regular expressions and
Chengsong
parents: 543
diff changeset
   485
derivatives on a set of strings:
Chengsong
parents: 543
diff changeset
   486
\begin{lemma}\label{derDer}
608
37b6fd310a16 added related work chap
Chengsong
parents: 601
diff changeset
   487
	\mbox{}
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   488
	\begin{itemize}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   489
		\item
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   490
$\textit{Der} \; c \; L(r) = L (r\backslash c)$
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   491
\item
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   492
	$c\!::\!s \in L(r)$ \textit{iff} $s \in L(r\backslash c)$.
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   493
	\end{itemize}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   494
\end{lemma}
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   495
\begin{proof}
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   496
	By induction on $r$.
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   497
\end{proof}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   498
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   499
which is the main property of derivatives
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   500
that enables us to reason about the correctness of
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   501
derivative-based matching.
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   502
We can generalise the derivative operation shown above for single characters
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   503
to strings as follows:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   504
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   505
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   506
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   507
$r \backslash_s (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash_s s$ \\
583
Chengsong
parents: 579
diff changeset
   508
$r \backslash_s [\,] $ & $\dn$ & $r$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   509
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   510
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   511
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   512
\noindent
564
Chengsong
parents: 543
diff changeset
   513
When there is no ambiguity, we will 
Chengsong
parents: 543
diff changeset
   514
omit the subscript and use $\backslash$ instead
583
Chengsong
parents: 579
diff changeset
   515
of $\backslash_s$ to denote
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   516
string derivatives for brevity.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   517
Brzozowski's derivative-based
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   518
regular-expression matching algorithm can then be described as:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   519
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   520
\begin{definition}
564
Chengsong
parents: 543
diff changeset
   521
$\textit{match}\;s\;r \;\dn\; \nullable \; (r\backslash s)$
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   522
\end{definition}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   523
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   524
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   525
Assuming the string is given as a sequence of characters, say $c_0c_1..c_n$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   526
this algorithm presented graphically is as follows:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   527
601
Chengsong
parents: 591
diff changeset
   528
\begin{equation}\label{matcher}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   529
\begin{tikzcd}
583
Chengsong
parents: 579
diff changeset
   530
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & 
Chengsong
parents: 579
diff changeset
   531
r_2 \arrow[r, dashed]  & r_n  \arrow[r,"\textit{nullable}?"] & 
Chengsong
parents: 579
diff changeset
   532
\;\textrm{true}/\textrm{false}
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   533
\end{tikzcd}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   534
\end{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   535
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   536
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   537
 It is relatively
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   538
easy to show that this matcher is correct, namely
539
Chengsong
parents: 538
diff changeset
   539
\begin{lemma}
564
Chengsong
parents: 543
diff changeset
   540
	$\textit{match} \; s\; r  = \textit{true} \; \textit{iff} \; s \in L(r)$
539
Chengsong
parents: 538
diff changeset
   541
\end{lemma}
Chengsong
parents: 538
diff changeset
   542
\begin{proof}
583
Chengsong
parents: 579
diff changeset
   543
	By induction on $s$ using property of derivatives:
Chengsong
parents: 579
diff changeset
   544
	lemma \ref{derDer}.
539
Chengsong
parents: 538
diff changeset
   545
\end{proof}
601
Chengsong
parents: 591
diff changeset
   546
\begin{figure}
564
Chengsong
parents: 543
diff changeset
   547
\begin{center}
539
Chengsong
parents: 538
diff changeset
   548
\begin{tikzpicture}
Chengsong
parents: 538
diff changeset
   549
\begin{axis}[
Chengsong
parents: 538
diff changeset
   550
    xlabel={$n$},
Chengsong
parents: 538
diff changeset
   551
    ylabel={time in secs},
601
Chengsong
parents: 591
diff changeset
   552
    %ymode = log,
539
Chengsong
parents: 538
diff changeset
   553
    legend entries={Naive Matcher},  
Chengsong
parents: 538
diff changeset
   554
    legend pos=north west,
Chengsong
parents: 538
diff changeset
   555
    legend cell align=left]
Chengsong
parents: 538
diff changeset
   556
\addplot[red,mark=*, mark options={fill=white}] table {NaiveMatcher.data};
Chengsong
parents: 538
diff changeset
   557
\end{axis}
Chengsong
parents: 538
diff changeset
   558
\end{tikzpicture} 
583
Chengsong
parents: 579
diff changeset
   559
\caption{Matching the regular expression $(a^*)^*b$ against strings of the form
Chengsong
parents: 579
diff changeset
   560
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}
Chengsong
parents: 579
diff changeset
   561
$ using Brzozowski's original algorithm}\label{NaiveMatcher}
601
Chengsong
parents: 591
diff changeset
   562
\end{center}
539
Chengsong
parents: 538
diff changeset
   563
\end{figure}
Chengsong
parents: 538
diff changeset
   564
\noindent
564
Chengsong
parents: 543
diff changeset
   565
If we implement the above algorithm naively, however,
Chengsong
parents: 543
diff changeset
   566
the algorithm can be excruciatingly slow, as shown in 
Chengsong
parents: 543
diff changeset
   567
\ref{NaiveMatcher}.
Chengsong
parents: 543
diff changeset
   568
Note that both axes are in logarithmic scale.
Chengsong
parents: 543
diff changeset
   569
Around two dozens characters
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   570
would already ``explode'' the matcher with the regular expression 
564
Chengsong
parents: 543
diff changeset
   571
$(a^*)^*b$.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   572
To improve this situation, we need to introduce simplification
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   573
rules for the intermediate results,
539
Chengsong
parents: 538
diff changeset
   574
such as $r + r \rightarrow r$,
Chengsong
parents: 538
diff changeset
   575
and make sure those rules do not change the 
Chengsong
parents: 538
diff changeset
   576
language of the regular expression.
564
Chengsong
parents: 543
diff changeset
   577
One simpled-minded simplification function
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   578
that achieves these requirements 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   579
is given below (see Ausaf et al. \cite{AusafDyckhoffUrban2016}):
564
Chengsong
parents: 543
diff changeset
   580
\begin{center}
Chengsong
parents: 543
diff changeset
   581
	\begin{tabular}{lcl}
Chengsong
parents: 543
diff changeset
   582
		$\simp \; r_1 \cdot r_2 $ & $ \dn$ & 
Chengsong
parents: 543
diff changeset
   583
		$(\simp \; r_1,  \simp \; r_2) \; \textit{match}$\\
Chengsong
parents: 543
diff changeset
   584
					  & & $\quad \case \; (\ZERO, \_) \Rightarrow \ZERO$\\
Chengsong
parents: 543
diff changeset
   585
					  & & $\quad \case \; (\_, \ZERO) \Rightarrow \ZERO$\\
Chengsong
parents: 543
diff changeset
   586
					  & & $\quad \case \; (\ONE, r_2') \Rightarrow r_2'$\\
Chengsong
parents: 543
diff changeset
   587
					  & & $\quad \case \; (r_1', \ONE) \Rightarrow r_1'$\\
Chengsong
parents: 543
diff changeset
   588
					  & & $\quad \case \; (r_1', r_2') \Rightarrow r_1'\cdot r_2'$\\
Chengsong
parents: 543
diff changeset
   589
		$\simp \; r_1 + r_2$ & $\dn$ & $(\simp \; r_1, \simp \; r_2) \textit{match}$\\
Chengsong
parents: 543
diff changeset
   590
				     & & $\quad \; \case \; (\ZERO, r_2') \Rightarrow r_2'$\\
Chengsong
parents: 543
diff changeset
   591
				     & & $\quad \; \case \; (r_1', \ZERO) \Rightarrow r_1'$\\
Chengsong
parents: 543
diff changeset
   592
				     & & $\quad \; \case \; (r_1', r_2') \Rightarrow r_1' + r_2'$\\
583
Chengsong
parents: 579
diff changeset
   593
		$\simp \; r$ & $\dn$ & $r\quad\quad (otherwise)$
564
Chengsong
parents: 543
diff changeset
   594
				   
Chengsong
parents: 543
diff changeset
   595
	\end{tabular}
Chengsong
parents: 543
diff changeset
   596
\end{center}
Chengsong
parents: 543
diff changeset
   597
If we repeatedly apply this simplification  
Chengsong
parents: 543
diff changeset
   598
function during the matching algorithm, 
Chengsong
parents: 543
diff changeset
   599
we have a matcher with simplification:
Chengsong
parents: 543
diff changeset
   600
\begin{center}
Chengsong
parents: 543
diff changeset
   601
	\begin{tabular}{lcl}
Chengsong
parents: 543
diff changeset
   602
		$\derssimp \; [] \; r$ & $\dn$ & $r$\\
Chengsong
parents: 543
diff changeset
   603
		$\derssimp \; c :: cs \; r$ & $\dn$ & $\derssimp \; cs \; (\simp \; (r \backslash c))$\\
Chengsong
parents: 543
diff changeset
   604
		$\textit{matcher}_{simp}\; s \; r $ & $\dn$ & $\nullable \; (\derssimp \; s\;r)$
Chengsong
parents: 543
diff changeset
   605
	\end{tabular}
Chengsong
parents: 543
diff changeset
   606
\end{center}
Chengsong
parents: 543
diff changeset
   607
\begin{figure}
539
Chengsong
parents: 538
diff changeset
   608
\begin{tikzpicture}
Chengsong
parents: 538
diff changeset
   609
\begin{axis}[
Chengsong
parents: 538
diff changeset
   610
    xlabel={$n$},
Chengsong
parents: 538
diff changeset
   611
    ylabel={time in secs},
601
Chengsong
parents: 591
diff changeset
   612
    %ymode = log,
Chengsong
parents: 591
diff changeset
   613
    %xmode = log,
564
Chengsong
parents: 543
diff changeset
   614
    grid = both,
539
Chengsong
parents: 538
diff changeset
   615
    legend entries={Matcher With Simp},  
Chengsong
parents: 538
diff changeset
   616
    legend pos=north west,
Chengsong
parents: 538
diff changeset
   617
    legend cell align=left]
Chengsong
parents: 538
diff changeset
   618
\addplot[red,mark=*, mark options={fill=white}] table {BetterMatcher.data};
Chengsong
parents: 538
diff changeset
   619
\end{axis}
564
Chengsong
parents: 543
diff changeset
   620
\end{tikzpicture} 
Chengsong
parents: 543
diff changeset
   621
\caption{$(a^*)^*b$ 
Chengsong
parents: 543
diff changeset
   622
against 
Chengsong
parents: 543
diff changeset
   623
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$ Using $\textit{matcher}_{simp}$}\label{BetterMatcher}
Chengsong
parents: 543
diff changeset
   624
\end{figure}
Chengsong
parents: 543
diff changeset
   625
\noindent
Chengsong
parents: 543
diff changeset
   626
The running time of $\textit{ders}\_\textit{simp}$
583
Chengsong
parents: 579
diff changeset
   627
on the same example of Figure \ref{NaiveMatcher}
Chengsong
parents: 579
diff changeset
   628
is now ``tame''  in terms of the length of inputs,
Chengsong
parents: 579
diff changeset
   629
as shown in Figure \ref{BetterMatcher}.
539
Chengsong
parents: 538
diff changeset
   630
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   631
So far the story is use Brzozowski derivatives and
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   632
simplify as much as possible and at the end test
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   633
whether the empty string is recognised 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   634
by the final derivative.
583
Chengsong
parents: 579
diff changeset
   635
But what if we want to 
Chengsong
parents: 579
diff changeset
   636
do lexing instead of just getting a true/false answer?
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   637
Sulzmann and Lu \cite{Sulzmann2014} first came up with a nice and 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   638
elegant (arguably as beautiful as the definition of the 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   639
Brzozowski derivative) solution for this.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   640
539
Chengsong
parents: 538
diff changeset
   641
\section{Values and the Lexing Algorithm by Sulzmann and Lu}
564
Chengsong
parents: 543
diff changeset
   642
In this section, we present a two-phase regular expression lexing 
Chengsong
parents: 543
diff changeset
   643
algorithm.
Chengsong
parents: 543
diff changeset
   644
The first phase takes successive derivatives with 
Chengsong
parents: 543
diff changeset
   645
respect to the input string,
Chengsong
parents: 543
diff changeset
   646
and the second phase does the reverse, \emph{injecting} back
Chengsong
parents: 543
diff changeset
   647
characters, in the meantime constructing a lexing result.
Chengsong
parents: 543
diff changeset
   648
We will introduce the injection phase in detail slightly
Chengsong
parents: 543
diff changeset
   649
later, but as a preliminary we have to first define 
Chengsong
parents: 543
diff changeset
   650
the datatype for lexing results, 
Chengsong
parents: 543
diff changeset
   651
called \emph{value} or
608
37b6fd310a16 added related work chap
Chengsong
parents: 601
diff changeset
   652
sometimes also \emph{lexical value}.  
37b6fd310a16 added related work chap
Chengsong
parents: 601
diff changeset
   653
Values and regular
37b6fd310a16 added related work chap
Chengsong
parents: 601
diff changeset
   654
expressions correspond to each other 
37b6fd310a16 added related work chap
Chengsong
parents: 601
diff changeset
   655
as illustrated in the following
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   656
table:
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   657
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   658
\begin{center}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   659
	\begin{tabular}{c@{\hspace{20mm}}c}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   660
		\begin{tabular}{@{}rrl@{}}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   661
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   662
			$r$ & $::=$  & $\ZERO$\\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   663
			& $\mid$ & $\ONE$   \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   664
			& $\mid$ & $c$          \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   665
			& $\mid$ & $r_1 \cdot r_2$\\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   666
			& $\mid$ & $r_1 + r_2$   \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   667
			\\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   668
			& $\mid$ & $r^*$         \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   669
		\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   670
		&
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   671
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   672
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   673
			$v$ & $::=$  & \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   674
			&        & $\Empty$   \\
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   675
			& $\mid$ & $\Char \; c$          \\
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   676
			& $\mid$ & $\Seq\,v_1\, v_2$\\
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   677
			& $\mid$ & $\Left \;v$   \\
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   678
			& $\mid$ & $\Right\;v$  \\
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   679
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   680
		\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   681
	\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   682
\end{center}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   683
\noindent
564
Chengsong
parents: 543
diff changeset
   684
A value has an underlying string, which 
Chengsong
parents: 543
diff changeset
   685
can be calculated by the ``flatten" function $|\_|$:
Chengsong
parents: 543
diff changeset
   686
\begin{center}
Chengsong
parents: 543
diff changeset
   687
	\begin{tabular}{lcl}
Chengsong
parents: 543
diff changeset
   688
		$|\Empty|$ & $\dn$ &  $[]$\\
Chengsong
parents: 543
diff changeset
   689
		$|\Char \; c|$ & $ \dn$ & $ [c]$\\
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   690
		$|\Seq \; v_1, \;v_2|$ & $ \dn$ & $ v_1| @ |v_2|$\\
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   691
		$|\Left \; v|$ & $ \dn$ & $ |v|$\\
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   692
		$|\Right \; v|$ & $ \dn$ & $ |v|$\\
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   693
		$|\Stars \; []|$ & $\dn$ & $[]$\\
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   694
		$|\Stars \; v::vs|$ &  $\dn$ & $ |v| @ |\Stars(vs)|$
564
Chengsong
parents: 543
diff changeset
   695
	\end{tabular}
Chengsong
parents: 543
diff changeset
   696
\end{center}
Chengsong
parents: 543
diff changeset
   697
Sulzmann and Lu used a binary predicate, written $\vdash v:r $,
Chengsong
parents: 543
diff changeset
   698
to indicate that a value $v$ could be generated from a lexing algorithm
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   699
with input $r$. They call it the value inhabitation relation,
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   700
defined by the rules.
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 626
diff changeset
   701
\begin{figure}[H]
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   702
\begin{mathpar}
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   703
	\inferrule{\mbox{}}{\vdash \Char \; c : \mathbf{c}} \hspace{2em}
564
Chengsong
parents: 543
diff changeset
   704
Chengsong
parents: 543
diff changeset
   705
	\inferrule{\mbox{}}{\vdash \Empty :  \ONE} \hspace{2em}
Chengsong
parents: 543
diff changeset
   706
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   707
\inferrule{\vdash v_1 : r_1 \;\; \vdash v_2 : r_2 }{\vdash \Seq \; v_1,\; v_2 : (r_1 \cdot r_2)}
564
Chengsong
parents: 543
diff changeset
   708
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   709
\inferrule{\vdash v_1 : r_1}{\vdash \Left \; v_1 : r_1+r_2}
564
Chengsong
parents: 543
diff changeset
   710
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   711
\inferrule{\vdash v_2 : r_2}{\vdash \Right \; v_2:r_1 + r_2}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   712
591
b2d0de6aee18 more polishing integrated comments chap2
Chengsong
parents: 585
diff changeset
   713
\inferrule{\forall v \in vs. \vdash v:r \land  |v| \neq []}{\vdash \Stars \; vs : r^*}
564
Chengsong
parents: 543
diff changeset
   714
\end{mathpar}
628
7af4e2420a8c ready to submit~~
Chengsong
parents: 626
diff changeset
   715
\caption{The inhabitation relation for values and regular expressions}\label{fig:inhab}
626
1c8525061545 finished!
Chengsong
parents: 623
diff changeset
   716
\end{figure}
564
Chengsong
parents: 543
diff changeset
   717
\noindent
Chengsong
parents: 543
diff changeset
   718
The condition $|v| \neq []$ in the premise of star's rule
Chengsong
parents: 543
diff changeset
   719
is to make sure that for a given pair of regular 
Chengsong
parents: 543
diff changeset
   720
expression $r$ and string $s$, the number of values 
Chengsong
parents: 543
diff changeset
   721
satisfying $|v| = s$ and $\vdash v:r$ is finite.
601
Chengsong
parents: 591
diff changeset
   722
This additional condition was
Chengsong
parents: 591
diff changeset
   723
imposed by Ausaf and Urban to make their proofs easier.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   724
Given a string and a regular expression, there can be
564
Chengsong
parents: 543
diff changeset
   725
multiple values for it. For example, both
Chengsong
parents: 543
diff changeset
   726
$\vdash \Seq(\Left \; ab)(\Right \; c):(ab+a)(bc+c)$ and
Chengsong
parents: 543
diff changeset
   727
$\vdash \Seq(\Right\; a)(\Left \; bc ):(ab+a)(bc+c)$ hold
Chengsong
parents: 543
diff changeset
   728
and the values both flatten to $abc$.
Chengsong
parents: 543
diff changeset
   729
Lexers therefore have to disambiguate and choose only
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   730
one of the values be generated. $\POSIX$ is one of the
564
Chengsong
parents: 543
diff changeset
   731
disambiguation strategies that is widely adopted.
Chengsong
parents: 543
diff changeset
   732
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   733
Ausaf et al.\parencite{AusafDyckhoffUrban2016} 
564
Chengsong
parents: 543
diff changeset
   734
formalised the property 
Chengsong
parents: 543
diff changeset
   735
as a ternary relation.
Chengsong
parents: 543
diff changeset
   736
The $\POSIX$ value $v$ for a regular expression
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   737
$r$ and string $s$, denoted as $(s, r) \rightarrow v$, can be specified 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   738
in the following rules\footnote{The names of the rules are used
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   739
as they were originally given in \cite{AusafDyckhoffUrban2016}.}:
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   740
\begin{figure}[p]
564
Chengsong
parents: 543
diff changeset
   741
\begin{mathpar}
Chengsong
parents: 543
diff changeset
   742
	\inferrule[P1]{\mbox{}}{([], \ONE) \rightarrow \Empty}
Chengsong
parents: 543
diff changeset
   743
		
Chengsong
parents: 543
diff changeset
   744
	\inferrule[PC]{\mbox{}}{([c], c) \rightarrow \Char \; c}
Chengsong
parents: 543
diff changeset
   745
Chengsong
parents: 543
diff changeset
   746
	\inferrule[P+L]{(s,r_1)\rightarrow v_1}{(s, r_1+r_2)\rightarrow \Left \; v_1}
Chengsong
parents: 543
diff changeset
   747
Chengsong
parents: 543
diff changeset
   748
	\inferrule[P+R]{(s,r_2)\rightarrow v_2\\ s \notin L \; r_1}{(s, r_1+r_2)\rightarrow \Right \; v_2}
Chengsong
parents: 543
diff changeset
   749
Chengsong
parents: 543
diff changeset
   750
	\inferrule[PS]{(s_1, v_1) \rightarrow r_1 \\ (s_2, v_2)\rightarrow r_2\\
Chengsong
parents: 543
diff changeset
   751
		\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land 
Chengsong
parents: 543
diff changeset
   752
		s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2}{(s_1 @ s_2, r_1\cdot r_2) \rightarrow
Chengsong
parents: 543
diff changeset
   753
	\Seq \; v_1 \; v_2}
Chengsong
parents: 543
diff changeset
   754
Chengsong
parents: 543
diff changeset
   755
	\inferrule[P{[]}]{\mbox{}}{([], r^*) \rightarrow \Stars([])}
Chengsong
parents: 543
diff changeset
   756
Chengsong
parents: 543
diff changeset
   757
	\inferrule[P*]{(s_1, v) \rightarrow v \\ (s_2, r^*) \rightarrow \Stars \; vs \\
Chengsong
parents: 543
diff changeset
   758
		|v| \neq []\\ \nexists s_3 \; s_4. s_3 \neq [] \land s_3@s_4 = s_2 \land
Chengsong
parents: 543
diff changeset
   759
		s_1@s_3 \in L \; r \land s_4 \in L \; r^*}{(s_1@s_2, r^*)\rightarrow \Stars \;
Chengsong
parents: 543
diff changeset
   760
	(v::vs)}
Chengsong
parents: 543
diff changeset
   761
\end{mathpar}
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   762
\caption{The inductive POSIX rules given by Ausaf et al.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   763
	\cite{AusafDyckhoffUrban2016}.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   764
This ternary relation, written $(s, r) \rightarrow v$, 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   765
formalises the POSIX constraints on the
601
Chengsong
parents: 591
diff changeset
   766
value $v$ given a string $s$ and 
Chengsong
parents: 591
diff changeset
   767
regular expression $r$.
Chengsong
parents: 591
diff changeset
   768
}
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   769
\end{figure}\afterpage{\clearpage}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   770
\noindent
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   771
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   772
%\begin{figure}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   773
%\begin{tikzpicture}[]
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   774
%    \node [minimum width = 6cm, rectangle split, rectangle split horizontal, 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   775
%	    rectangle split parts=2, rectangle split part fill={red!30,blue!20}, style={draw, rounded corners, inner sep=10pt}]
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   776
%	    (node1)
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   777
%	    {$r_{token1}$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   778
%	    \nodepart{two}  $\;\;\; \quad r_{token2}\;\;\;\quad$ };
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   779
%	    %\node [left = 6.0cm of node1] (start1) {hi};
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   780
%	    \node [left = 0.2cm of node1] (middle) {$v.s.$};
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   781
%    \node [minimum width = 6cm, left = 0.2cm of middle, rectangle split, rectangle split horizontal, 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   782
%	    rectangle split parts=2, rectangle split part fill={red!30,blue!20}, style={draw, rounded corners, inner sep=10pt}]
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   783
%	    (node2)
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   784
%	    {$\quad\;\;\;r_{token1}\quad\;\;\;$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   785
%	    \nodepart{two}  $r_{token2}$ };
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   786
%	    \node [below = 0.1cm of node2] (text1) {\checkmark preferred by POSIX};
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   787
%		\node [above = 1.5cm of middle, minimum width = 6cm, 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   788
%			rectangle, style={draw, rounded corners, inner sep=10pt}] 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   789
%			(topNode) {$s$};
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   790
%	    \path[->,draw]
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   791
%	        (topNode) edge node {split $A$} (node2)
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   792
%	        (topNode) edge node {split $B$} (node1)
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   793
%		;
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   794
%			
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   795
%
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   796
%\end{tikzpicture}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   797
%\caption{Maximum munch example: $s$ matches $r_{token1} \cdot r_{token2}$}\label{munch}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   798
%\end{figure}
564
Chengsong
parents: 543
diff changeset
   799
The above $\POSIX$ rules follows the intuition described below: 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   800
\begin{itemize}
564
Chengsong
parents: 543
diff changeset
   801
	\item (Left Priority)\\
Chengsong
parents: 543
diff changeset
   802
		Match the leftmost regular expression when multiple options of matching
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   803
		are available. See P+L and P+R where in P+R $s$ cannot
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   804
		be in the language of $L \; r_1$.
564
Chengsong
parents: 543
diff changeset
   805
	\item (Maximum munch)\\
Chengsong
parents: 543
diff changeset
   806
		Always match a subpart as much as possible before proceeding
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   807
		to the next part of the string.
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
   808
		For example, when the string $s$ matches 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   809
		$r_{part1}\cdot r_{part2}$, and we have two ways $s$ can be split:
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   810
		Then the split that matches a longer string for the first part
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   811
		$r_{part1}$ is preferred by this maximum munch rule.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   812
		This is caused by the side-condition 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   813
		\begin{center}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   814
		$\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   815
		s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2$
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   816
		\end{center}
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   817
		in PS.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   818
		%(See
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   819
		%\ref{munch} for an illustration).
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   820
\end{itemize}
564
Chengsong
parents: 543
diff changeset
   821
\noindent
Chengsong
parents: 543
diff changeset
   822
These disambiguation strategies can be 
Chengsong
parents: 543
diff changeset
   823
quite practical.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   824
For instance, when lexing a code snippet 
564
Chengsong
parents: 543
diff changeset
   825
\[ 
Chengsong
parents: 543
diff changeset
   826
	\textit{iffoo} = 3
Chengsong
parents: 543
diff changeset
   827
\]
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   828
using a regular expression 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   829
for keywords and 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   830
identifiers:
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   831
%(for example, keyword is a nonempty string starting with letters 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   832
%followed by alphanumeric characters or underscores):
564
Chengsong
parents: 543
diff changeset
   833
\[
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   834
	r_{keyword} + r_{identifier}.
564
Chengsong
parents: 543
diff changeset
   835
\]
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   836
If we want $\textit{iffoo}$ to be recognized
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   837
as an identifier
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   838
where identifiers are defined as usual (letters
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   839
followed by letters, numbers or underscores),
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   840
then a match with a keyword (if)
564
Chengsong
parents: 543
diff changeset
   841
followed by
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   842
an identifier (foo) would be incorrect.
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   843
POSIX lexing would generate what we want.
564
Chengsong
parents: 543
diff changeset
   844
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   845
\noindent
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   846
We know that a POSIX 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   847
value for regular expression $r$ is inhabited by $r$.
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
   848
\begin{lemma}
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
   849
$(r, s) \rightarrow v \implies \vdash v: r$
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
   850
\end{lemma}
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
   851
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   852
The main property about a $\POSIX$ value is that 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   853
given the same regular expression $r$ and string $s$,
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   854
one can always uniquely determine the $\POSIX$ value for it:
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   855
\begin{lemma}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   856
$\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
   857
\end{lemma}
539
Chengsong
parents: 538
diff changeset
   858
\begin{proof}
564
Chengsong
parents: 543
diff changeset
   859
By induction on $s$, $r$ and $v_1$. The inductive cases
Chengsong
parents: 543
diff changeset
   860
are all the POSIX rules. 
Chengsong
parents: 543
diff changeset
   861
Probably the most cumbersome cases are 
Chengsong
parents: 543
diff changeset
   862
the sequence and star with non-empty iterations.
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   863
We shall give the details for proving the sequence case here.
539
Chengsong
parents: 538
diff changeset
   864
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   865
When we have 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   866
\[
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   867
	(s_1, r_1) \rightarrow v_1 \;\, and \;\, 
601
Chengsong
parents: 591
diff changeset
   868
	(s_2, r_2) \rightarrow v_2  \;\, and \;\, 
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   869
	\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   870
		s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   871
\]
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   872
we know that the last condition 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   873
excludes the possibility of a 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   874
string $s_1'$ longer than $s_1$ such that 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   875
\[
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   876
(s_1', r_1) \rightarrow v_1'   \;\; 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   877
and\;\; (s_2', r_2) \rightarrow v_2'\;\; and \;\;s_1' @s_2' = s 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   878
\]
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   879
hold.
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   880
A shorter string $s_1''$ with $s_2''$ satisfying
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   881
\[
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   882
(s_1'', r_1) \rightarrow v_1''
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   883
\;\;and\;\; (s_2'', r_2) \rightarrow v_2'' \;\;and \;\;s_1'' @s_2'' = s 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   884
\]
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   885
cannot possibly form a $\POSIX$ value either, because
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   886
by definition there is a candidate
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   887
with longer initial string
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   888
$s_1$. Therefore, we know that the POSIX
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   889
value $\Seq \; a \; b$ for $r_1 \cdot r_2$ matching
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   890
$s$ must have the 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   891
property that 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   892
\[
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   893
	|a| = s_1 \;\; and \;\; |b| = s_2.
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   894
\]
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   895
The goal is to prove that $a = v_1 $ and $b = v_2$.
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   896
If we have some other POSIX values $v_{10}$ and $v_{20}$ such that 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   897
$(s_1, r_1) \rightarrow v_{10}$ and $(s_2, r_2) \rightarrow v_{20}$ hold,
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   898
then by induction hypothesis $v_{10} = v_1$ and $v_{20}= v_2$, 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   899
which means this "other" $\POSIX$ value $\Seq(v_{10}, v_{20})$
539
Chengsong
parents: 538
diff changeset
   900
is the same as $\Seq(v_1, v_2)$. 
Chengsong
parents: 538
diff changeset
   901
\end{proof}
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   902
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   903
We have now defined what a POSIX value is and shown that it is unique,
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   904
the problem is to generate
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   905
such a value in a lexing algorithm using derivatives.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   906
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   907
\subsection{Sulzmann and Lu's Injection-based Lexing Algorithm}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   908
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   909
Sulzmann and Lu extended Brzozowski's 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   910
derivative-based matching
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   911
to a lexing algorithm by a second phase 
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   912
after the initial phase of successive derivatives.
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   913
This second phase generates a POSIX value 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   914
if the regular expression matches the string.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   915
Two functions are involved: $\inj$ and $\mkeps$.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   916
The function $\mkeps$ constructs a POSIX value from the last
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   917
derivative $r_n$:
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   918
\begin{ceqn}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   919
\begin{equation}\label{graph:mkeps}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   920
\begin{tikzcd}
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   921
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed, "\ldots"] & r_n \arrow[d, "mkeps" description] \\
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   922
	        & 	              & 	            & v_n       
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   923
\end{tikzcd}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   924
\end{equation}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   925
\end{ceqn}
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   926
\noindent
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   927
In the above diagram, again we assume that
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   928
the input string $s$ is made of $n$ characters
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   929
$c_0c_1 \ldots c_{n-1}$ 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   930
The last derivative operation 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   931
$\backslash c_{n-1}$ generates the derivative $r_n$, for which
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   932
$\mkeps$ produces the value $v_n$. This value
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   933
tells us how the empty string is matched by the (nullable)
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   934
regular expression $r_n$, in a POSIX way.
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   935
The definition of $\mkeps$ is
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   936
	\begin{center}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   937
		\begin{tabular}{lcl}
564
Chengsong
parents: 543
diff changeset
   938
			$\mkeps \; \ONE$ 		& $\dn$ & $\Empty$ \\
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   939
			$\mkeps \; (r_{1}+r_{2})$	& $\dn$ 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   940
						& $\textit{if}\; (\nullable \; r_{1}) \;\,
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   941
							\textit{then}\;\, \Left \; (\mkeps \; r_{1})$\\ 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   942
						& & $\phantom{if}\; \textit{else}\;\, \Right \;(\mkeps \; r_{2})$\\
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   943
			$\mkeps \; (r_1 \cdot r_2)$ 	& $\dn$ & $\Seq\;(\mkeps\;r_1)\;(\mkeps \; r_2)$\\
564
Chengsong
parents: 543
diff changeset
   944
			$\mkeps \; r^* $	        & $\dn$ & $\Stars\;[]$
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   945
		\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   946
	\end{center}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   947
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   948
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   949
\noindent 
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   950
The function prefers the left child $r_1$ of $r_1 + r_2$ 
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   951
to match an empty string if there is a choice.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   952
When there is a star to match the empty string,
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   953
we give the $\Stars$ constructor an empty list, meaning
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   954
no iteration is taken.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   955
The result of $\mkeps$ on a $\nullable$ $r$ 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   956
is a POSIX value for $r$ and the empty string:
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   957
\begin{lemma}\label{mePosix}
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   958
$\nullable\; r \implies (r, []) \rightarrow (\mkeps\; v)$
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   959
\end{lemma}
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   960
\begin{proof}
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   961
	By induction on $r$.
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   962
\end{proof}
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   963
\noindent
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   964
After the $\mkeps$-call, Sulzmann and Lu inject back the characters one by one
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   965
in reverse order as they were chopped off in the derivative phase.
622
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   966
The fucntion for this is called $\inj$. This function 
4b1149fb5aec incorporated more comments, bib
Chengsong
parents: 608
diff changeset
   967
operates on values, unlike $\backslash$ which operates on regular expressions.
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   968
In the diagram below, $v_i$ stands for the (POSIX) value 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   969
for how the regular expression 
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   970
$r_i$ matches the string $s_i$ consisting of the last $n-i$ characters
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   971
of $s$ (i.e. $s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   972
After injecting back $n$ characters, we get the lexical value for how $r_0$
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   973
matches $s$. 
601
Chengsong
parents: 591
diff changeset
   974
\begin{figure}[H]
Chengsong
parents: 591
diff changeset
   975
\begin{center}	
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   976
\begin{ceqn}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   977
\begin{tikzcd}
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   978
r_0 \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] \\
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   979
v_0           \arrow[u]                 & v_i  \arrow[l, dashed]                              & v_{i+1} \arrow[l,"inj_{r_i} c_i"]                 & v_n \arrow[l, dashed]         
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   980
\end{tikzcd}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   981
\end{ceqn}
601
Chengsong
parents: 591
diff changeset
   982
\end{center}
Chengsong
parents: 591
diff changeset
   983
\caption{The two-phase lexing algorithm by Sulzmann and Lu \cite{AusafDyckhoffUrban2016},
Chengsong
parents: 591
diff changeset
   984
	matching the regular expression $r_0$ and string of the form $[c_0, c_1, \ldots, c_{n-1}]$.
Chengsong
parents: 591
diff changeset
   985
	The first phase involves taking successive derivatives w.r.t the characters $c_0$,
Chengsong
parents: 591
diff changeset
   986
	$c_1$, and so on. These are the same operations as they have appeared in the matcher
Chengsong
parents: 591
diff changeset
   987
	\ref{matcher}. When the final derivative regular expression is nullable (contains the empty string),
Chengsong
parents: 591
diff changeset
   988
	then the second phase starts. First $\mkeps$ generates a POSIX value which tells us how $r_n$ matches
Chengsong
parents: 591
diff changeset
   989
	the empty string , by always selecting the leftmost 
Chengsong
parents: 591
diff changeset
   990
	nullable regular expression. After that $\inj$ ``injects'' back the character in reverse order as they 
Chengsong
parents: 591
diff changeset
   991
	appeared in the string, always preserving POSIXness.}\label{graph:inj}
Chengsong
parents: 591
diff changeset
   992
\end{figure}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
   993
\noindent
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
   994
The function $\textit{inj}$ as defined by Sulzmann and Lu
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
   995
takes three arguments: a regular
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   996
expression ${r_{i}}$, before the character is chopped off, 
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
   997
a character ${c_{i}}$ (the character we want to inject back) and 
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
   998
the third argument $v_{i+1}$ the value we want to inject into. 
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
   999
The result of an application 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1000
$\inj \; r_i \; c_i \; v_{i+1}$ is a new value $v_i$ such that
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1001
\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1002
	(s_i, r_i) \rightarrow v_i
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1003
\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1004
holds.
567
28cb8089ec36 more updaates
Chengsong
parents: 564
diff changeset
  1005
The definition of $\textit{inj}$ is as follows: 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1006
\begin{center}
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1007
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{5mm}}l}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1008
  $\textit{inj}\;(c)\;c\,Empty$            & $\dn$ & $\Char\,c$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1009
  $\textit{inj}\;(r_1 + r_2)\;c\; (\Left\; v)$ & $\dn$ & $\Left  \; (\textit{inj}\; r_1 \; c\,v)$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1010
  $\textit{inj}\;(r_1 + r_2)\,c\; (\Right\;v)$ & $\dn$ & $\Right \; (\textit{inj}\;r_2\;c  \; v)$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1011
  $\textit{inj}\;(r_1 \cdot r_2)\; c\;(\Seq \; v_1 \; v_2)$ & $\dn$  & 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1012
  $\Seq \; (\textit{inj}\;r_1\;c\;v_1) \; v_2$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1013
  $\textit{inj}\;(r_1 \cdot r_2)\; c\;(\Left \; (\Seq \; v_1\;v_2) )$ & 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1014
  $\dn$  & $\Seq \; (\textit{inj}\,r_1\,c\,v_1)\; v_2$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1015
  $\textit{inj}\;(r_1 \cdot r_2)\; c\; (\Right\; v)$ & $\dn$  & $\Seq\; (\textit{mkeps}\; r_1) \; (\textit{inj} \; r_2\;c\;v)$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1016
  $\textit{inj}\;(r^*)\; c \; (\Seq \; v\; (\Stars\;vs))$         & $\dn$  & $\Stars\;\,((\textit{inj}\;r\;c\;v)\,::\,vs)$\\
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1017
\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1018
\end{center}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1019
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1020
\noindent 
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1021
The function recurses on 
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1022
the shape of regular
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1023
expressionsw  and values.
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1024
Intuitively, each clause analyses 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1025
how $r_i$ could have transformed when being 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1026
derived by $c$, identifying which subpart
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1027
of $v_{i+1}$ has the ``hole'' 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1028
to inject the character back into.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1029
Once the character is
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1030
injected back to that sub-value; 
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1031
$\inj$ assembles all parts together
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1032
to form a new value.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1033
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1034
For instance, the last clause is an
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1035
injection into a sequence value $v_{i+1}$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1036
whose second child
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1037
value is a star, and the shape of the 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1038
regular expression $r_i$ before injection 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1039
is a star.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1040
We therefore know 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1041
the derivative 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1042
starts on a star and ends as a sequence:
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1043
\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1044
	(r^*) \backslash c \longrightarrow r\backslash c \cdot r^*
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1045
\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1046
during which an iteration of the star
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1047
had just been unfolded, giving the below
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1048
value inhabitation relation:
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1049
\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1050
	\vdash \Seq \; v \; (\Stars \; vs) : (r\backslash c) \cdot r^*.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1051
\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1052
The value list $vs$ corresponds to
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1053
matched star iterations,
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1054
and the ``hole'' lies in $v$ because
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1055
\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1056
	\vdash v: r\backslash c.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1057
\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1058
Finally, 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1059
$\inj \; r \;c \; v$ is prepended
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1060
to the previous list of iterations, and then
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1061
wrapped under the $\Stars$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1062
constructor, giving us $\Stars \; ((\inj \; r \; c \; v) ::vs)$.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1063
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1064
Recall that lemma 
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1065
\ref{mePosix} tells us that
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1066
$\mkeps$ always generates the POSIX value.
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1067
The function $\inj$ preserves the POSIXness, provided
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1068
the value before injection is POSIX, namely
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1069
\begin{lemma}\label{injPosix}
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1070
	If$(r \backslash c, s) \rightarrow v $,
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1071
	then $(r, c :: s) \rightarrow (\inj r \; c\; v)$.
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1072
\end{lemma}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1073
\begin{proof}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1074
	By induction on $r$.
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1075
	The non-trivial cases are sequence and star.
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1076
	When $r = a \cdot b$, there can be
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1077
	three cases for the value $v$ satisfying $\vdash v:a\backslash c$.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1078
	We give the reasoning why $\inj \; r \; c \; v$ is POSIX in each
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1079
	case.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1080
	\begin{itemize}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1081
		\item
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1082
			$v = \Seq \; v_a \; v_b$.\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1083
			The ``not nullable'' clause of the $\inj$ function is taken:
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1084
			\begin{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1085
				\begin{tabular}{lcl}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1086
					$\inj \; r \; c \; v$ &   $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; (\Seq \; v_a \; v_b) $\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1087
					& $=$ & $\Seq \; (\inj \;a \; c \; v_a) \; v_b$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1088
				\end{tabular}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1089
			\end{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1090
			We know that there exists a unique pair of
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1091
			$s_a$ and $s_b$ satisfaying	
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1092
				$(a \backslash c, s_a) \rightarrow v_a$,
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1093
				$(b , s_b) \rightarrow v_b$, and
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1094
				$\nexists s_3 \; s_4. s_3 \neq [] \land s_a @ s_3 \in 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1095
				L \; (a\backslash c) \land
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1096
				s_4 \in L \; b$.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1097
			The last condition gives us
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1098
			$\nexists s_3 \; s_4. s_3 \neq [] \land (c :: s_a )@ s_3 \in 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1099
				L \; a \land
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1100
				s_4 \in L \; b$.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1101
			By induction hypothesis, $(a, c::s_a) \rightarrow \inj \; a \; c \; v_a $ holds,
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1102
			and this gives us
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1103
			\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1104
				(a\cdot b, (c::s_a)@s_b) \rightarrow \Seq \; (\inj \; a\;c \;v_a) \; v_b.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1105
			\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1106
		\item
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1107
			$v = \Left \; (\Seq \; v_a \; v_b)$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1108
			The argument is almost identical to the above case,	
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1109
			except that a different clause of $\inj$ is taken:
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1110
			\begin{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1111
				\begin{tabular}{lcl}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1112
					$\inj \; r \; c \; v$ &   $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; (\Left \; (\Seq \; v_a \; v_b)) $\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1113
					& $=$ & $\Seq \; (\inj \;a \; c \; v_a) \; v_b$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1114
				\end{tabular}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1115
			\end{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1116
			With a similar reasoning, 
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1117
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1118
			\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1119
				(a\cdot b, (c::s_a)@s_b) \rightarrow \Seq \; (\inj \; a\;c \;v_a) \; v_b.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1120
			\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1121
			again holds.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1122
		\item 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1123
			$v = \Right \; v_b$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1124
			Again the injection result would be 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1125
			\begin{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1126
				\begin{tabular}{lcl}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1127
					$\inj \; r \; c \; v$ &   $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; \Right \; (v_b) $\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1128
					& $=$ & $\Seq \; (\mkeps \; a) \; (\inj \;b \; c\; v_b)$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1129
				\end{tabular}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1130
			\end{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1131
			We know that $a$ must be nullable,
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1132
			allowing us to call $\mkeps$ and get
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1133
			\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1134
				(a, []) \rightarrow \mkeps \; a.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1135
			\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1136
			Also by inductive hypothesis
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1137
			\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1138
				(b, c::s) \rightarrow \inj\; b \; c \; v_b
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1139
			\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1140
			holds.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1141
			In addition, as
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1142
			$\Right \;v_b$  instead of $\Left \ldots$ is 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1143
			the POSIX value for $v$, it must be the case
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1144
			that $s \notin L \;( (a\backslash c)\cdot b)$.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1145
			This tells us that 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1146
			\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1147
				\nexists s_3 \; s_4.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1148
				s_3 @s_4 = s  \land s_3 \in L \; (a\backslash c) 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1149
				\land s_4 \in L \; b
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1150
			\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1151
			which translates to
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1152
			\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1153
				\nexists s_3 \; s_4. \; s_3 \neq [] \land
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1154
				s_3 @s_4 = c::s  \land s_3 \in L \; a 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1155
				\land s_4 \in L \; b.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1156
			\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1157
			(Which basically says there cannot be a longer 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1158
			initial split for $s$ other than the empty string.)
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1159
			Therefore we have $\Seq \; (\mkeps \; a) \;(\inj \;b \; c\; v_b)$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1160
			as the POSIX value for $a\cdot b$.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1161
	\end{itemize}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1162
	The star case can be proven similarly.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1163
\end{proof}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1164
\noindent
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1165
Putting all together, Sulzmann and Lu obtained the following algorithm
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1166
outlined in the diagram \ref{graph:inj}:
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1167
\begin{center}
539
Chengsong
parents: 538
diff changeset
  1168
\begin{tabular}{lcl}
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1169
	$\lexer \; r \; [] $ & $=$ & $\textit{if} \; (\nullable \; r)\; \textit{then}\;  \Some(\mkeps \; r) \; \textit{else} \; \None$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1170
	$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer \; (r\backslash c) \; s) \;\textit{of}\; $\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1171
	& & $\quad \phantom{\mid}\; \None \implies \None$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1172
	& & $\quad \mid           \Some(v) \implies \Some(\inj \; r\; c\; v)$
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1173
\end{tabular}
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1174
\end{center}
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1175
\noindent
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1176
The central property of the $\lexer$ is that it gives the correct result
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1177
according to
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1178
POSIX rules. 
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1179
\begin{theorem}\label{lexerCorrectness}
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1180
	The $\lexer$ based on derivatives and injections is correct: 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1181
	\begin{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1182
		\begin{tabular}{lcl}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1183
			$\lexer \; r \; s = \Some(v)$ & $ \Longleftrightarrow$ & $ (r, \; s) \rightarrow v$\\
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1184
			$\lexer \;r \; s = \None $ & $\Longleftrightarrow$ & $ \neg(\exists v. (r, s) \rightarrow v)$
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1185
		\end{tabular}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1186
	\end{center}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1187
\end{theorem} 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1188
\begin{proof}
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1189
By induction on $s$. $r$ generalising over an arbitrary regular expression.
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1190
The $[]$ case is proven by  an application of lemma \ref{mePosix}, and the inductive case
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1191
by lemma \ref{injPosix}.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1192
\end{proof}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1193
\noindent
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1194
As we did earlier in this chapter with the matcher, one can 
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1195
introduce simplification on the regular expression in each derivative step.
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1196
However, now one needs to do a backward phase and make sure
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1197
the values align with the regular expression.
539
Chengsong
parents: 538
diff changeset
  1198
Therefore one has to
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1199
be careful not to break the correctness, as the injection 
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1200
function heavily relies on the structure of 
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1201
the regular expressions and values being aligned.
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1202
This can be achieved by recording some extra rectification functions
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1203
during the derivatives step, and applying these rectifications in 
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1204
each run during the injection phase.
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1205
With extra care
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1206
one can show that POSIXness will not be affected
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1207
by the simplifications listed here \cite{AusafDyckhoffUrban2016}. 
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1208
\begin{center}
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1209
	\begin{tabular}{lcl}
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1210
		$\simp \; r_1 \cdot r_2 $ & $ \dn$ & 
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1211
		$(\simp \; r_1,  \simp \; r_2) \; \textit{match}$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1212
					  & & $\quad \case \; (\ZERO, \_) \Rightarrow \ZERO$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1213
					  & & $\quad \case \; (\_, \ZERO) \Rightarrow \ZERO$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1214
					  & & $\quad \case \; (\ONE, r_2') \Rightarrow r_2'$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1215
					  & & $\quad \case \; (r_1', \ONE) \Rightarrow r_1'$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1216
					  & & $\quad \case \; (r_1', r_2') \Rightarrow r_1'\cdot r_2'$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1217
		$\simp \; r_1 + r_2$ & $\dn$ & $(\simp \; r_1, \simp \; r_2) \textit{match}$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1218
				     & & $\quad \; \case \; (\ZERO, r_2') \Rightarrow r_2'$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1219
				     & & $\quad \; \case \; (r_1', \ZERO) \Rightarrow r_1'$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1220
				     & & $\quad \; \case \; (r_1', r_2') \Rightarrow r_1' + r_2'$\\
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1221
		$\simp \; r$ & $\dn$ & $r\quad\quad (otherwise)$
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1222
				   
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1223
	\end{tabular}
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1224
\end{center}
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1225
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1226
However, with the simple-minded simplification rules allowed
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1227
in an injection-based lexer, one can still end up
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1228
with exploding derivatives.
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1229
\section{A Case Requring More Aggressive Simplifications}
539
Chengsong
parents: 538
diff changeset
  1230
For example, when starting with the regular
585
4969ef817d92 chap4 more
Chengsong
parents: 583
diff changeset
  1231
expression $(a^* \cdot a^*)^*$ and building just over
4969ef817d92 chap4 more
Chengsong
parents: 583
diff changeset
  1232
a dozen successive derivatives 
539
Chengsong
parents: 538
diff changeset
  1233
w.r.t.~the character $a$, one obtains a derivative regular expression
585
4969ef817d92 chap4 more
Chengsong
parents: 583
diff changeset
  1234
with millions of nodes (when viewed as a tree)
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1235
even with the simple-minded simplification.
585
4969ef817d92 chap4 more
Chengsong
parents: 583
diff changeset
  1236
\begin{figure}[H]
601
Chengsong
parents: 591
diff changeset
  1237
\begin{center}
539
Chengsong
parents: 538
diff changeset
  1238
\begin{tikzpicture}
Chengsong
parents: 538
diff changeset
  1239
\begin{axis}[
Chengsong
parents: 538
diff changeset
  1240
    xlabel={$n$},
Chengsong
parents: 538
diff changeset
  1241
    ylabel={size},
585
4969ef817d92 chap4 more
Chengsong
parents: 583
diff changeset
  1242
    legend entries={Simple-Minded Simp, Naive Matcher},  
539
Chengsong
parents: 538
diff changeset
  1243
    legend pos=north west,
Chengsong
parents: 538
diff changeset
  1244
    legend cell align=left]
Chengsong
parents: 538
diff changeset
  1245
\addplot[red,mark=*, mark options={fill=white}] table {BetterWaterloo.data};
585
4969ef817d92 chap4 more
Chengsong
parents: 583
diff changeset
  1246
\addplot[blue,mark=*, mark options={fill=white}] table {BetterWaterloo1.data};
539
Chengsong
parents: 538
diff changeset
  1247
\end{axis}
Chengsong
parents: 538
diff changeset
  1248
\end{tikzpicture} 
601
Chengsong
parents: 591
diff changeset
  1249
\end{center}
539
Chengsong
parents: 538
diff changeset
  1250
\caption{Size of $(a^*\cdot a^*)^*$ against $\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$}
Chengsong
parents: 538
diff changeset
  1251
\end{figure}\label{fig:BetterWaterloo}
Chengsong
parents: 538
diff changeset
  1252
   
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1253
That is because Sulzmann and Lu's 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1254
injection-based lexing algorithm keeps a lot of 
541
5bf9f94c02e1 some comments implemented
Chengsong
parents: 539
diff changeset
  1255
"useless" values that will not be used. 
539
Chengsong
parents: 538
diff changeset
  1256
These different ways of matching will grow exponentially with the string length.
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1257
Consider the case
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1258
\[
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1259
	r= (a^*\cdot a^*)^* \quad and \quad
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1260
	s=\underbrace{aa\ldots a}_\text{n \textit{a}s}
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1261
\]
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1262
as an example.
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1263
This is a highly ambiguous regular expression, with
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1264
many ways to split up the string into multiple segments for
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1265
different star iteratioins,
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1266
and for each segment 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1267
multiple ways of splitting between 
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1268
the two $a^*$ sub-expressions.
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1269
When $n$ is equal to $1$, there are two lexical values for
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1270
the match:
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1271
\[
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1272
	\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [])] \quad (v1)
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1273
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1274
and
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1275
\[
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1276
	\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])] \quad (v2)
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1277
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1278
The derivative of $\derssimp \;s \; r$ is
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1279
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1280
	(a^*a^* + a^*)\cdot(a^*a^*)^*.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1281
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1282
The $a^*a^*$ and $a^*$ in the first child of the above sequence
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1283
correspond to value 1 and value 2, respectively.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1284
When $n=2$, the number goes up to 7:
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1285
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1286
	\Stars \; [\Seq \; (\Stars \; [\Char \; a, \Char \; a])\; (\Stars \; [])]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1287
\]
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1288
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1289
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1290
	\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [\Char \; a])]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1291
\]
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1292
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1293
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1294
	\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a, \Char \; a])]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1295
\]
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1296
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1297
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1298
	\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; []), \Seq \; (\Stars \; [\Char\;a])\; (\Stars\; []) ]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1299
\]
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1300
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1301
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1302
	\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; []), 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1303
	 	   \Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1304
		  ] 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1305
\]
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1306
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1307
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1308
	\Stars \; [
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1309
	 	   \Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a]),
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1310
	 	   \Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1311
		  ] 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1312
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1313
and
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1314
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1315
	\Stars \; [
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1316
	 	   \Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a]),
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1317
		   \Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [])
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1318
		  ] 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1319
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1320
And $\derssimp \; aa \; (a^*a^*)^*$ would be
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1321
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1322
	((a^*a^* + a^*)+a^*)\cdot(a^*a^*)^* + 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1323
	(a^*a^* + a^*)\cdot(a^*a^*)^*.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1324
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1325
which removes two out of the seven terms corresponding to the
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1326
seven distinct lexical values.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1327
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1328
It is not surprising that there are exponentially many 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1329
distinct lexical values that cannot be eliminated by 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1330
the simple-minded simplification of $\derssimp$. 
568
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1331
A lexer without a good enough strategy to 
7a579f5533f8 more chapter2 modifications
Chengsong
parents: 567
diff changeset
  1332
deduplicate will naturally
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1333
have an exponential runtime on highly
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1334
ambiguous regular expressions, because there
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1335
are exponentially many matches.
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1336
For this particular example, it seems
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1337
that the number of distinct matches growth
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1338
speed is proportional to $(2n)!/(n!(n+1)!)$ ($n$ being the input length).
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1339
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1340
On the other hand, the
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1341
 $\POSIX$ value for $r= (a^*\cdot a^*)^*$  and 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1342
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$ is  
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1343
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1344
	\Stars\,
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1345
	[\Seq \; (\Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}]), \Stars\,[]]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1346
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1347
and at any moment the  subterms in a regular expression
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1348
that will result in a POSIX value is only
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1349
a minority among the many other terms,
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1350
and one can remove ones that are absolutely not possible to 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1351
be POSIX.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1352
In the above example,
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
  1353
\begin{equation}\label{eqn:growth2}
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1354
	((a^*a^* + \underbrace{a^*}_\text{A})+\underbrace{a^*}_\text{duplicate of A})\cdot(a^*a^*)^* + 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1355
	\underbrace{(a^*a^* + a^*)\cdot(a^*a^*)^*}_\text{further simp removes this}.
579
35df9cdd36ca more chap3
Chengsong
parents: 577
diff changeset
  1356
\end{equation}
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1357
can be further simplified by 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1358
removing the underlined term first,
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1359
which would open up possibilities
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1360
of further simplification that removes the
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1361
underbraced part.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1362
The result would be 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1363
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1364
	(\underbrace{a^*a^*}_\text{term 1} + \underbrace{a^*}_\text{term 2})\cdot(a^*a^*)^*.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1365
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1366
with corresponding values
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1367
\begin{center}
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1368
	\begin{tabular}{lr}
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1369
		$\Stars \; [\Seq \; (\Stars \; [\Char \; a, \Char \; a])\; (\Stars \; [])]$  & $(\text{term 1})$\\
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1370
		$\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [\Char \; a])]  $ &  $(\text{term 2})$
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1371
	\end{tabular}
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1372
\end{center}
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1373
Other terms with an underlying value such as
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1374
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1375
	\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a, \Char \; a])]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1376
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1377
is simply too hopeless to contribute a POSIX lexical value,
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1378
and is therefore thrown away.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1379
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1380
Ausaf et al. \cite{AusafDyckhoffUrban2016} 
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1381
have come up with some simplification steps, however those steps
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1382
are not yet sufficiently strong so that they achieve the above effects.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1383
And even with these relatively mild simplifications the proof
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1384
is already quite a bit complicated than the theorem \ref{lexerCorrectness}.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1385
One would prove something like: 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1386
\[
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1387
	\textit{If}\; (\textit{snd} \; (\textit{simp} \; r\backslash c), s) \rightarrow  v  \;\;
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1388
	\textit{then}\;\; (r, c::s) \rightarrow 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1389
	\inj\;\, r\,  \;c \;\, ((\textit{fst} \; (\textit{simp} \; r \backslash c))\; v) 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1390
\]
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1391
instead of the simple lemma \ref{injPosix}, where now $\textit{simp}$
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1392
not only has to return a simplified regular expression,
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1393
but also what specific simplifications 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1394
has been done as a function on values
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1395
showing how one can transform the value
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1396
underlying the simplified regular expression
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1397
to the unsimplified one.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1398
623
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1399
We therefore choose a slightly different approach
c0c1ebe09c7d finished injchap2
Chengsong
parents: 622
diff changeset
  1400
also described by Sulzmann and Lu to
573
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1401
get better simplifications, which uses
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1402
some augmented data structures compared to 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1403
plain regular expressions.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1404
We call them \emph{annotated}
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1405
regular expressions.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1406
With annotated regular expressions,
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1407
we can avoid creating the intermediate values $v_1,\ldots v_n$ and a 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1408
second phase altogether.
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1409
We introduce this new datatype and the 
454ced557605 chapter2 finished polishing
Chengsong
parents: 568
diff changeset
  1410
corresponding algorithm in the next chapter.
538
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1411
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1412
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1413
8016a2480704 intro and chap2
Chengsong
parents: 536
diff changeset
  1414