ChengsongTanPhdThesis/Chapters/Chapter2.tex
author Chengsong
Mon, 30 May 2022 17:24:52 +0100
changeset 529 96e93df60954
parent 528 28751de4b4ba
child 530 823d9b19d21c
permissions -rwxr-xr-x
more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     1
% Chapter Template
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
     2
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
     3
\chapter{Regular Expressions and POSIX Lexing} % Main chapter title
519
Chengsong
parents: 518
diff changeset
     4
Chengsong
parents: 518
diff changeset
     5
\label{Chapter2} % In chapter 2 \ref{Chapter2} we will introduce the concepts
Chengsong
parents: 518
diff changeset
     6
%and notations we 
Chengsong
parents: 518
diff changeset
     7
%use for describing the lexing algorithm by Sulzmann and Lu,
Chengsong
parents: 518
diff changeset
     8
%and then give the algorithm and its variant, and discuss
Chengsong
parents: 518
diff changeset
     9
%why more aggressive simplifications are needed. 
Chengsong
parents: 518
diff changeset
    10
Chengsong
parents: 518
diff changeset
    11
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    12
\section{Basic Concepts and Notations for Strings, Languages, and Regular Expressions}
529
Chengsong
parents: 528
diff changeset
    13
We have a primitive datatype char, denoting characters.
Chengsong
parents: 528
diff changeset
    14
\[			char ::=  a
Chengsong
parents: 528
diff changeset
    15
			 \mid b
Chengsong
parents: 528
diff changeset
    16
			 \mid c
Chengsong
parents: 528
diff changeset
    17
			 \mid  \ldots
Chengsong
parents: 528
diff changeset
    18
			 \mid z       
Chengsong
parents: 528
diff changeset
    19
\]
Chengsong
parents: 528
diff changeset
    20
(which one is better?)
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    21
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    22
\begin{tabular}{lcl}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    23
$\textit{char}$ & $\dn$ & $a | b | c | \ldots$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    24
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    25
\end{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    26
They can form strings by lists:
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    27
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    28
\begin{tabular}{lcl}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    29
$\textit{string}$ & $\dn$ & $[] | c  :: cs$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    30
& & $(c\; \text{has char type})$
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    31
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    32
\end{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    33
And strings can be concatenated to form longer strings:
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    34
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    35
\begin{tabular}{lcl}
529
Chengsong
parents: 528
diff changeset
    36
$[] @ s_2$ & $\dn$ & $s_2$\\
Chengsong
parents: 528
diff changeset
    37
$(c :: s_1) @ s_2$ & $\dn$ & $c :: (s_1 @ s_2)$
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    38
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    39
\end{center}
519
Chengsong
parents: 518
diff changeset
    40
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    41
A set of strings can operate with another set of strings:
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    42
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    43
\begin{tabular}{lcl}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    44
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A; s_B \in B \}$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    45
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    46
\end{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    47
We also call the above "language concatenation".
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    48
The power of a language is defined recursively, using the 
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    49
concatenation operator $@$:
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    50
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    51
\begin{tabular}{lcl}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    52
$A^0 $ & $\dn$ & $\{ [] \}$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    53
$A^{n+1}$ & $\dn$ & $A^n @ A$
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    54
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    55
\end{center}
529
Chengsong
parents: 528
diff changeset
    56
The union of all the natural number powers of a language   
Chengsong
parents: 528
diff changeset
    57
is denoted by the Kleene star operator:
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    58
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    59
\begin{tabular}{lcl}
529
Chengsong
parents: 528
diff changeset
    60
$\bigcup_{i \geq 0} A^i$ & $\denote$ & $A^*$\\
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    61
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    62
\end{center}
529
Chengsong
parents: 528
diff changeset
    63
Chengsong
parents: 528
diff changeset
    64
In Isabelle of course we cannot easily get a counterpart of
Chengsong
parents: 528
diff changeset
    65
the $\bigcup_{i \geq 0}$ operator, so we instead define the Kleene star
Chengsong
parents: 528
diff changeset
    66
as an inductive set: 
Chengsong
parents: 528
diff changeset
    67
\begin{center}
Chengsong
parents: 528
diff changeset
    68
\begin{tabular}{lcl}
Chengsong
parents: 528
diff changeset
    69
$[] \in A^*$  & &\\
Chengsong
parents: 528
diff changeset
    70
$s_1 \in A \land \; s_2 \in A^* $ & $\implies$ & $s_1 @ s_2 \in A^*$\\
Chengsong
parents: 528
diff changeset
    71
\end{tabular}
Chengsong
parents: 528
diff changeset
    72
\end{center}
Chengsong
parents: 528
diff changeset
    73
Chengsong
parents: 528
diff changeset
    74
We also define an operation of chopping off a character from
Chengsong
parents: 528
diff changeset
    75
a language:
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    76
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    77
\begin{tabular}{lcl}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    78
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    79
\end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
    80
\end{center}
529
Chengsong
parents: 528
diff changeset
    81
Chengsong
parents: 528
diff changeset
    82
This can be generalised to chopping off a string from all strings within set $A$:
Chengsong
parents: 528
diff changeset
    83
\begin{center}
Chengsong
parents: 528
diff changeset
    84
\begin{tabular}{lcl}
Chengsong
parents: 528
diff changeset
    85
$\textit{Ders} \;w \;A$ & $\dn$ & $\{ s \mid w@s \in A \}$\\
Chengsong
parents: 528
diff changeset
    86
\end{tabular}
Chengsong
parents: 528
diff changeset
    87
\end{center}
Chengsong
parents: 528
diff changeset
    88
Chengsong
parents: 528
diff changeset
    89
which is essentially the left quotient $A \backslash L'$ of $A$ against 
Chengsong
parents: 528
diff changeset
    90
the singleton language $L' = \{w\}$
Chengsong
parents: 528
diff changeset
    91
in formal language theory.
Chengsong
parents: 528
diff changeset
    92
For this dissertation the $\textit{Ders}$ notation would suffice, there is
Chengsong
parents: 528
diff changeset
    93
no need for a more general derivative definition.
Chengsong
parents: 528
diff changeset
    94
Chengsong
parents: 528
diff changeset
    95
With the  sequencing, Kleene star, and $\textit{Der}$ operator on languages,
Chengsong
parents: 528
diff changeset
    96
we have a  few properties of how the language derivative can be defined using 
Chengsong
parents: 528
diff changeset
    97
sub-languages.
Chengsong
parents: 528
diff changeset
    98
\begin{lemma}
Chengsong
parents: 528
diff changeset
    99
$\Der \; c \; (A @ B) = \textit{if} \;  [] \in A \; \textit{then} ((\Der \; c \; A) @ B ) \cup \Der \; c\; B \quad \textit{else}\; (\Der \; c \; A) @ B$
Chengsong
parents: 528
diff changeset
   100
\end{lemma}
Chengsong
parents: 528
diff changeset
   101
\noindent
Chengsong
parents: 528
diff changeset
   102
This lemma states that if $A$ contains the empty string, $\Der$ can "pierce" through it
Chengsong
parents: 528
diff changeset
   103
and get to $B$.
Chengsong
parents: 528
diff changeset
   104
Chengsong
parents: 528
diff changeset
   105
The language $A^*$'s derivative can be described using the language derivative
Chengsong
parents: 528
diff changeset
   106
of $A$:
Chengsong
parents: 528
diff changeset
   107
\begin{lemma}
Chengsong
parents: 528
diff changeset
   108
$\textit{Der} \;c \;A^* = (\textit{Der}\; c A) @ (A^*)$\\
Chengsong
parents: 528
diff changeset
   109
\end{lemma}
Chengsong
parents: 528
diff changeset
   110
\begin{proof}
Chengsong
parents: 528
diff changeset
   111
\begin{itemize}
Chengsong
parents: 528
diff changeset
   112
\item{$\subseteq$}
Chengsong
parents: 528
diff changeset
   113
The set 
Chengsong
parents: 528
diff changeset
   114
\[ \{s \mid c :: s \in A^*\} \]
Chengsong
parents: 528
diff changeset
   115
is enclosed in the set
Chengsong
parents: 528
diff changeset
   116
\[ \{s_1 @ s_2 \mid s_1 \, s_2. s_1 \in \{s \mid c :: s \in A\} \land s_2 \in A^* \} \]
Chengsong
parents: 528
diff changeset
   117
because whenever you have a string starting with a character 
Chengsong
parents: 528
diff changeset
   118
in the language of a Kleene star $A^*$, then that character together with some sub-string
Chengsong
parents: 528
diff changeset
   119
immediately after it will form the first iteration, and the rest of the string will 
Chengsong
parents: 528
diff changeset
   120
be still in $A^*$.
Chengsong
parents: 528
diff changeset
   121
\item{$\supseteq$}
Chengsong
parents: 528
diff changeset
   122
Note that
Chengsong
parents: 528
diff changeset
   123
\[ \Der \; c \; A^* = \Der \; c \;  (\{ [] \} \cup (A @ A^*) ) \]
Chengsong
parents: 528
diff changeset
   124
and 
Chengsong
parents: 528
diff changeset
   125
\[ \Der \; c \;  (\{ [] \} \cup (A @ A^*) ) = \Der\; c \; (A @ A^*) \]
Chengsong
parents: 528
diff changeset
   126
where the $\textit{RHS}$ of the above equatioin can be rewritten
Chengsong
parents: 528
diff changeset
   127
as \[ (\Der \; c\; A) @ A^* \cup A' \], $A'$ being a possibly empty set.
Chengsong
parents: 528
diff changeset
   128
\end{itemize}
Chengsong
parents: 528
diff changeset
   129
\end{proof}
Chengsong
parents: 528
diff changeset
   130
Before we define the $\textit{Der}$ and $\textit{Ders}$ counterpart
Chengsong
parents: 528
diff changeset
   131
for regular languages, we need to first give definitions for regular expressions.
Chengsong
parents: 528
diff changeset
   132
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   133
\section{Regular Expressions and Their Language Interpretation}
519
Chengsong
parents: 518
diff changeset
   134
Suppose we have an alphabet $\Sigma$, the strings  whose characters
Chengsong
parents: 518
diff changeset
   135
are from $\Sigma$
Chengsong
parents: 518
diff changeset
   136
can be expressed as $\Sigma^*$.
Chengsong
parents: 518
diff changeset
   137
Chengsong
parents: 518
diff changeset
   138
We use patterns to define a set of strings concisely. Regular expressions
Chengsong
parents: 518
diff changeset
   139
are one of such patterns systems:
Chengsong
parents: 518
diff changeset
   140
The basic regular expressions  are defined inductively
Chengsong
parents: 518
diff changeset
   141
 by the following grammar:
Chengsong
parents: 518
diff changeset
   142
\[			r ::=   \ZERO \mid  \ONE
Chengsong
parents: 518
diff changeset
   143
			 \mid  c  
Chengsong
parents: 518
diff changeset
   144
			 \mid  r_1 \cdot r_2
Chengsong
parents: 518
diff changeset
   145
			 \mid  r_1 + r_2   
Chengsong
parents: 518
diff changeset
   146
			 \mid r^*         
Chengsong
parents: 518
diff changeset
   147
\]
Chengsong
parents: 518
diff changeset
   148
Chengsong
parents: 518
diff changeset
   149
The language or set of strings defined by regular expressions are defined as
Chengsong
parents: 518
diff changeset
   150
%TODO: FILL in the other defs
Chengsong
parents: 518
diff changeset
   151
\begin{center}
Chengsong
parents: 518
diff changeset
   152
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   153
$L \; (r_1 + r_2)$ & $\dn$ & $ L \; (r_1) \cup L \; ( r_2)$\\
Chengsong
parents: 518
diff changeset
   154
$L \; (r_1 \cdot r_2)$ & $\dn$ & $ L \; (r_1) \cap L \; (r_2)$\\
Chengsong
parents: 518
diff changeset
   155
\end{tabular}
Chengsong
parents: 518
diff changeset
   156
\end{center}
Chengsong
parents: 518
diff changeset
   157
Which are also called the "language interpretation".
Chengsong
parents: 518
diff changeset
   158
Chengsong
parents: 518
diff changeset
   159
Chengsong
parents: 518
diff changeset
   160
Chengsong
parents: 518
diff changeset
   161
Chengsong
parents: 518
diff changeset
   162
% Derivatives of a
Chengsong
parents: 518
diff changeset
   163
%regular expression, written $r \backslash c$, give a simple solution
Chengsong
parents: 518
diff changeset
   164
%to the problem of matching a string $s$ with a regular
Chengsong
parents: 518
diff changeset
   165
%expression $r$: if the derivative of $r$ w.r.t.\ (in
Chengsong
parents: 518
diff changeset
   166
%succession) all the characters of the string matches the empty string,
Chengsong
parents: 518
diff changeset
   167
%then $r$ matches $s$ (and {\em vice versa}).  
Chengsong
parents: 518
diff changeset
   168
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   169
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   170
\section{Brzozowski Derivatives of Regular Expressions}
529
Chengsong
parents: 528
diff changeset
   171
Now with semantic derivatives of a language and regular expressions and
Chengsong
parents: 528
diff changeset
   172
their language interpretations, we are ready to define derivatives on regexes.
Chengsong
parents: 528
diff changeset
   173
Chengsong
parents: 528
diff changeset
   174
The Brzozowski derivative w.r.t character $c$ is an operation on the regex,
Chengsong
parents: 528
diff changeset
   175
where the operation transforms the regex to a new one containing
Chengsong
parents: 528
diff changeset
   176
strings without the head character $c$.
Chengsong
parents: 528
diff changeset
   177
Chengsong
parents: 528
diff changeset
   178
The  derivative of regular expression, denoted as
519
Chengsong
parents: 518
diff changeset
   179
$r \backslash c$, is a function that takes parameters
Chengsong
parents: 518
diff changeset
   180
$r$ and $c$, and returns another regular expression $r'$,
Chengsong
parents: 518
diff changeset
   181
which is computed by the following recursive function:
Chengsong
parents: 518
diff changeset
   182
Chengsong
parents: 518
diff changeset
   183
\begin{center}
Chengsong
parents: 518
diff changeset
   184
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   185
		$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 518
diff changeset
   186
		$\ONE \backslash c$  & $\dn$ & $\ZERO$\\
Chengsong
parents: 518
diff changeset
   187
		$d \backslash c$     & $\dn$ & 
Chengsong
parents: 518
diff changeset
   188
		$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
Chengsong
parents: 518
diff changeset
   189
$(r_1 + r_2)\backslash c$     & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
Chengsong
parents: 518
diff changeset
   190
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\
Chengsong
parents: 518
diff changeset
   191
	&   & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
Chengsong
parents: 518
diff changeset
   192
	&   & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
Chengsong
parents: 518
diff changeset
   193
	$(r^*)\backslash c$           & $\dn$ & $(r\backslash c) \cdot r^*$\\
Chengsong
parents: 518
diff changeset
   194
\end{tabular}
Chengsong
parents: 518
diff changeset
   195
\end{center}
Chengsong
parents: 518
diff changeset
   196
\noindent
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   197
The function derivative, written $r\backslash c$, 
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   198
defines how a regular expression evolves into
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   199
a new regular expression after all the string it contains
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   200
is chopped off a certain head character $c$.
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   201
The most involved cases are the sequence 
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   202
and star case.
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   203
The sequence case says that if the first regular expression
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   204
contains an empty string then the second component of the sequence
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   205
might be chosen as the target regular expression to be chopped
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   206
off its head character.
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   207
The star regular expression's derivative unwraps the iteration of
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   208
regular expression and attaches the star regular expression
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   209
to the sequence's second element to make sure a copy is retained
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   210
for possible more iterations in later phases of lexing.
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   211
519
Chengsong
parents: 518
diff changeset
   212
Chengsong
parents: 518
diff changeset
   213
The $\nullable$ function tests whether the empty string $""$ 
Chengsong
parents: 518
diff changeset
   214
is in the language of $r$:
Chengsong
parents: 518
diff changeset
   215
Chengsong
parents: 518
diff changeset
   216
Chengsong
parents: 518
diff changeset
   217
\begin{center}
Chengsong
parents: 518
diff changeset
   218
		\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   219
			$\nullable(\ZERO)$     & $\dn$ & $\mathit{false}$ \\  
Chengsong
parents: 518
diff changeset
   220
			$\nullable(\ONE)$      & $\dn$ & $\mathit{true}$ \\
Chengsong
parents: 518
diff changeset
   221
			$\nullable(c)$ 	       & $\dn$ & $\mathit{false}$ \\
Chengsong
parents: 518
diff changeset
   222
			$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
Chengsong
parents: 518
diff changeset
   223
			$\nullable(r_1\cdot r_2)$  & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
Chengsong
parents: 518
diff changeset
   224
			$\nullable(r^*)$       & $\dn$ & $\mathit{true}$ \\
Chengsong
parents: 518
diff changeset
   225
		\end{tabular}
Chengsong
parents: 518
diff changeset
   226
\end{center}
Chengsong
parents: 518
diff changeset
   227
\noindent
Chengsong
parents: 518
diff changeset
   228
The empty set does not contain any string and
Chengsong
parents: 518
diff changeset
   229
therefore not the empty string, the empty string 
Chengsong
parents: 518
diff changeset
   230
regular expression contains the empty string
Chengsong
parents: 518
diff changeset
   231
by definition, the character regular expression
Chengsong
parents: 518
diff changeset
   232
is the singleton that contains character only,
Chengsong
parents: 518
diff changeset
   233
and therefore does not contain the empty string,
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   234
the alternative regular expression (or "or" expression)
519
Chengsong
parents: 518
diff changeset
   235
might have one of its children regular expressions
Chengsong
parents: 518
diff changeset
   236
being nullable and any one of its children being nullable
Chengsong
parents: 518
diff changeset
   237
would suffice. The sequence regular expression
Chengsong
parents: 518
diff changeset
   238
would require both children to have the empty string
Chengsong
parents: 518
diff changeset
   239
to compose an empty string and the Kleene star
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   240
operation naturally introduced the empty string. 
519
Chengsong
parents: 518
diff changeset
   241
  
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   242
We have the following property where the derivative on regular 
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   243
expressions coincides with the derivative on a set of strings:
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   244
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   245
\begin{lemma}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   246
$\textit{Der} \; c \; L(r) = L (r\backslash c)$
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   247
\end{lemma}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   248
519
Chengsong
parents: 518
diff changeset
   249
\noindent
Chengsong
parents: 518
diff changeset
   250
Chengsong
parents: 518
diff changeset
   251
Chengsong
parents: 518
diff changeset
   252
The main property of the derivative operation
Chengsong
parents: 518
diff changeset
   253
that enables us to reason about the correctness of
Chengsong
parents: 518
diff changeset
   254
an algorithm using derivatives is 
Chengsong
parents: 518
diff changeset
   255
Chengsong
parents: 518
diff changeset
   256
\begin{center}
Chengsong
parents: 518
diff changeset
   257
$c\!::\!s \in L(r)$ holds
Chengsong
parents: 518
diff changeset
   258
if and only if $s \in L(r\backslash c)$.
Chengsong
parents: 518
diff changeset
   259
\end{center}
Chengsong
parents: 518
diff changeset
   260
Chengsong
parents: 518
diff changeset
   261
\noindent
Chengsong
parents: 518
diff changeset
   262
We can generalise the derivative operation shown above for single characters
Chengsong
parents: 518
diff changeset
   263
to strings as follows:
Chengsong
parents: 518
diff changeset
   264
Chengsong
parents: 518
diff changeset
   265
\begin{center}
Chengsong
parents: 518
diff changeset
   266
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   267
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\
Chengsong
parents: 518
diff changeset
   268
$r \backslash [\,] $ & $\dn$ & $r$
Chengsong
parents: 518
diff changeset
   269
\end{tabular}
Chengsong
parents: 518
diff changeset
   270
\end{center}
Chengsong
parents: 518
diff changeset
   271
Chengsong
parents: 518
diff changeset
   272
\noindent
Chengsong
parents: 518
diff changeset
   273
and then define Brzozowski's  regular-expression matching algorithm as:
Chengsong
parents: 518
diff changeset
   274
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   275
\begin{definition}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   276
$match\;s\;r \;\dn\; nullable(r\backslash s)$
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   277
\end{definition}
519
Chengsong
parents: 518
diff changeset
   278
Chengsong
parents: 518
diff changeset
   279
\noindent
Chengsong
parents: 518
diff changeset
   280
Assuming the a string is given as a sequence of characters, say $c_0c_1..c_n$, 
Chengsong
parents: 518
diff changeset
   281
this algorithm presented graphically is as follows:
Chengsong
parents: 518
diff changeset
   282
Chengsong
parents: 518
diff changeset
   283
\begin{equation}\label{graph:*}
Chengsong
parents: 518
diff changeset
   284
\begin{tikzcd}
Chengsong
parents: 518
diff changeset
   285
r_0 \arrow[r, "\backslash c_0"]  & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed]  & r_n  \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO}
Chengsong
parents: 518
diff changeset
   286
\end{tikzcd}
Chengsong
parents: 518
diff changeset
   287
\end{equation}
Chengsong
parents: 518
diff changeset
   288
Chengsong
parents: 518
diff changeset
   289
\noindent
Chengsong
parents: 518
diff changeset
   290
where we start with  a regular expression  $r_0$, build successive
Chengsong
parents: 518
diff changeset
   291
derivatives until we exhaust the string and then use \textit{nullable}
Chengsong
parents: 518
diff changeset
   292
to test whether the result can match the empty string. It can  be
Chengsong
parents: 518
diff changeset
   293
relatively  easily shown that this matcher is correct  (that is given
Chengsong
parents: 518
diff changeset
   294
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$).
Chengsong
parents: 518
diff changeset
   295
Chengsong
parents: 518
diff changeset
   296
Beautiful and simple definition.
Chengsong
parents: 518
diff changeset
   297
Chengsong
parents: 518
diff changeset
   298
If we implement the above algorithm naively, however,
Chengsong
parents: 518
diff changeset
   299
the algorithm can be excruciatingly slow. 
Chengsong
parents: 518
diff changeset
   300
Chengsong
parents: 518
diff changeset
   301
Chengsong
parents: 518
diff changeset
   302
\begin{figure}
Chengsong
parents: 518
diff changeset
   303
\centering
Chengsong
parents: 518
diff changeset
   304
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
Chengsong
parents: 518
diff changeset
   305
\begin{tikzpicture}
Chengsong
parents: 518
diff changeset
   306
\begin{axis}[
Chengsong
parents: 518
diff changeset
   307
    xlabel={$n$},
Chengsong
parents: 518
diff changeset
   308
    x label style={at={(1.05,-0.05)}},
Chengsong
parents: 518
diff changeset
   309
    ylabel={time in secs},
Chengsong
parents: 518
diff changeset
   310
    enlargelimits=false,
Chengsong
parents: 518
diff changeset
   311
    xtick={0,5,...,30},
Chengsong
parents: 518
diff changeset
   312
    xmax=33,
Chengsong
parents: 518
diff changeset
   313
    ymax=10000,
Chengsong
parents: 518
diff changeset
   314
    ytick={0,1000,...,10000},
Chengsong
parents: 518
diff changeset
   315
    scaled ticks=false,
Chengsong
parents: 518
diff changeset
   316
    axis lines=left,
Chengsong
parents: 518
diff changeset
   317
    width=5cm,
Chengsong
parents: 518
diff changeset
   318
    height=4cm, 
Chengsong
parents: 518
diff changeset
   319
    legend entries={JavaScript},  
Chengsong
parents: 518
diff changeset
   320
    legend pos=north west,
Chengsong
parents: 518
diff changeset
   321
    legend cell align=left]
Chengsong
parents: 518
diff changeset
   322
\addplot[red,mark=*, mark options={fill=white}] table {EightThousandNodes.data};
Chengsong
parents: 518
diff changeset
   323
\end{axis}
Chengsong
parents: 518
diff changeset
   324
\end{tikzpicture}\\
Chengsong
parents: 518
diff changeset
   325
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings 
Chengsong
parents: 518
diff changeset
   326
           of the form $\underbrace{aa..a}_{n}$.}
Chengsong
parents: 518
diff changeset
   327
\end{tabular}    
Chengsong
parents: 518
diff changeset
   328
\caption{EightThousandNodes} \label{fig:EightThousandNodes}
Chengsong
parents: 518
diff changeset
   329
\end{figure}
Chengsong
parents: 518
diff changeset
   330
Chengsong
parents: 518
diff changeset
   331
Chengsong
parents: 518
diff changeset
   332
(8000 node data to be added here)
Chengsong
parents: 518
diff changeset
   333
For example, when starting with the regular
Chengsong
parents: 518
diff changeset
   334
expression $(a + aa)^*$ and building a few successive derivatives (around 10)
Chengsong
parents: 518
diff changeset
   335
w.r.t.~the character $a$, one obtains a derivative regular expression
Chengsong
parents: 518
diff changeset
   336
with more than 8000 nodes (when viewed as a tree)\ref{EightThousandNodes}.
Chengsong
parents: 518
diff changeset
   337
The reason why $(a + aa) ^*$ explodes so drastically is that without
Chengsong
parents: 518
diff changeset
   338
pruning, the algorithm will keep records of all possible ways of matching:
Chengsong
parents: 518
diff changeset
   339
\begin{center}
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   340
$(a + aa) ^* \backslash [aa] = (\ZERO + \ONE \ONE)\cdot(a + aa)^* + (\ONE + \ONE a) \cdot (a + aa)^*$
519
Chengsong
parents: 518
diff changeset
   341
\end{center}
Chengsong
parents: 518
diff changeset
   342
Chengsong
parents: 518
diff changeset
   343
\noindent
Chengsong
parents: 518
diff changeset
   344
Each of the above alternative branches correspond to the match 
Chengsong
parents: 518
diff changeset
   345
$aa $, $a \quad a$ and $a \quad a \cdot (a)$(incomplete).
Chengsong
parents: 518
diff changeset
   346
These different ways of matching will grow exponentially with the string length,
Chengsong
parents: 518
diff changeset
   347
and without simplifications that throw away some of these very similar matchings,
Chengsong
parents: 518
diff changeset
   348
it is no surprise that these expressions grow so quickly.
Chengsong
parents: 518
diff changeset
   349
Operations like
Chengsong
parents: 518
diff changeset
   350
$\backslash$ and $\nullable$ need to traverse such trees and
Chengsong
parents: 518
diff changeset
   351
consequently the bigger the size of the derivative the slower the
Chengsong
parents: 518
diff changeset
   352
algorithm. 
Chengsong
parents: 518
diff changeset
   353
Chengsong
parents: 518
diff changeset
   354
Brzozowski was quick in finding that during this process a lot useless
Chengsong
parents: 518
diff changeset
   355
$\ONE$s and $\ZERO$s are generated and therefore not optimal.
Chengsong
parents: 518
diff changeset
   356
He also introduced some "similarity rules" such
Chengsong
parents: 518
diff changeset
   357
as $P+(Q+R) = (P+Q)+R$ to merge syntactically 
Chengsong
parents: 518
diff changeset
   358
different but language-equivalent sub-regexes to further decrease the size
Chengsong
parents: 518
diff changeset
   359
of the intermediate regexes. 
Chengsong
parents: 518
diff changeset
   360
Chengsong
parents: 518
diff changeset
   361
More simplifications are possible, such as deleting duplicates
Chengsong
parents: 518
diff changeset
   362
and opening up nested alternatives to trigger even more simplifications.
Chengsong
parents: 518
diff changeset
   363
And suppose we apply simplification after each derivative step, and compose
Chengsong
parents: 518
diff changeset
   364
these two operations together as an atomic one: $a \backslash_{simp}\,c \dn
Chengsong
parents: 518
diff changeset
   365
\textit{simp}(a \backslash c)$. Then we can build
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   366
a matcher with simpler regular expressions.
519
Chengsong
parents: 518
diff changeset
   367
Chengsong
parents: 518
diff changeset
   368
If we want the size of derivatives in the algorithm to
Chengsong
parents: 518
diff changeset
   369
stay even lower, we would need more aggressive simplifications.
Chengsong
parents: 518
diff changeset
   370
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as
Chengsong
parents: 518
diff changeset
   371
deleting duplicates whenever possible. For example, the parentheses in
Chengsong
parents: 518
diff changeset
   372
$(a+b) \cdot c + b\cdot c$ can be opened up to get $a\cdot c + b \cdot c + b
Chengsong
parents: 518
diff changeset
   373
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another
Chengsong
parents: 518
diff changeset
   374
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   375
$a^*+a+\ONE$.  These more aggressive simplification rules are for
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   376
 a very tight size bound, possibly as low
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   377
  as that of the \emph{partial derivatives}\parencite{Antimirov1995}. 
519
Chengsong
parents: 518
diff changeset
   378
Chengsong
parents: 518
diff changeset
   379
Building derivatives and then simplify them.
Chengsong
parents: 518
diff changeset
   380
So far so good. But what if we want to 
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   381
do lexing instead of just  getting a YES/NO answer?
519
Chengsong
parents: 518
diff changeset
   382
This requires us to go back again to the world 
Chengsong
parents: 518
diff changeset
   383
without simplification first for a moment.
Chengsong
parents: 518
diff changeset
   384
Sulzmann and Lu~\cite{Sulzmann2014} first came up with a nice and 
Chengsong
parents: 518
diff changeset
   385
elegant(arguably as beautiful as the original
Chengsong
parents: 518
diff changeset
   386
derivatives definition) solution for this.
Chengsong
parents: 518
diff changeset
   387
Chengsong
parents: 518
diff changeset
   388
\subsection*{Values and the Lexing Algorithm by Sulzmann and Lu}
Chengsong
parents: 518
diff changeset
   389
Chengsong
parents: 518
diff changeset
   390
Chengsong
parents: 518
diff changeset
   391
They first defined the datatypes for storing the 
Chengsong
parents: 518
diff changeset
   392
lexing information called a \emph{value} or
Chengsong
parents: 518
diff changeset
   393
sometimes also \emph{lexical value}.  These values and regular
Chengsong
parents: 518
diff changeset
   394
expressions correspond to each other as illustrated in the following
Chengsong
parents: 518
diff changeset
   395
table:
Chengsong
parents: 518
diff changeset
   396
Chengsong
parents: 518
diff changeset
   397
\begin{center}
Chengsong
parents: 518
diff changeset
   398
	\begin{tabular}{c@{\hspace{20mm}}c}
Chengsong
parents: 518
diff changeset
   399
		\begin{tabular}{@{}rrl@{}}
Chengsong
parents: 518
diff changeset
   400
			\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
Chengsong
parents: 518
diff changeset
   401
			$r$ & $::=$  & $\ZERO$\\
Chengsong
parents: 518
diff changeset
   402
			& $\mid$ & $\ONE$   \\
Chengsong
parents: 518
diff changeset
   403
			& $\mid$ & $c$          \\
Chengsong
parents: 518
diff changeset
   404
			& $\mid$ & $r_1 \cdot r_2$\\
Chengsong
parents: 518
diff changeset
   405
			& $\mid$ & $r_1 + r_2$   \\
Chengsong
parents: 518
diff changeset
   406
			\\
Chengsong
parents: 518
diff changeset
   407
			& $\mid$ & $r^*$         \\
Chengsong
parents: 518
diff changeset
   408
		\end{tabular}
Chengsong
parents: 518
diff changeset
   409
		&
Chengsong
parents: 518
diff changeset
   410
		\begin{tabular}{@{\hspace{0mm}}rrl@{}}
Chengsong
parents: 518
diff changeset
   411
			\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
Chengsong
parents: 518
diff changeset
   412
			$v$ & $::=$  & \\
Chengsong
parents: 518
diff changeset
   413
			&        & $\Empty$   \\
Chengsong
parents: 518
diff changeset
   414
			& $\mid$ & $\Char(c)$          \\
Chengsong
parents: 518
diff changeset
   415
			& $\mid$ & $\Seq\,v_1\, v_2$\\
Chengsong
parents: 518
diff changeset
   416
			& $\mid$ & $\Left(v)$   \\
Chengsong
parents: 518
diff changeset
   417
			& $\mid$ & $\Right(v)$  \\
Chengsong
parents: 518
diff changeset
   418
			& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
Chengsong
parents: 518
diff changeset
   419
		\end{tabular}
Chengsong
parents: 518
diff changeset
   420
	\end{tabular}
Chengsong
parents: 518
diff changeset
   421
\end{center}
Chengsong
parents: 518
diff changeset
   422
Chengsong
parents: 518
diff changeset
   423
\noindent
Chengsong
parents: 518
diff changeset
   424
Chengsong
parents: 518
diff changeset
   425
Building on top of Sulzmann and Lu's attempt to formalize the 
Chengsong
parents: 518
diff changeset
   426
notion of POSIX lexing rules \parencite{Sulzmann2014}, 
Chengsong
parents: 518
diff changeset
   427
Ausaf and Urban\parencite{AusafDyckhoffUrban2016} modelled
Chengsong
parents: 518
diff changeset
   428
POSIX matching as a ternary relation recursively defined in a
Chengsong
parents: 518
diff changeset
   429
natural deduction style.
Chengsong
parents: 518
diff changeset
   430
With the formally-specified rules for what a POSIX matching is,
Chengsong
parents: 518
diff changeset
   431
they proved in Isabelle/HOL that the algorithm gives correct results.
Chengsong
parents: 518
diff changeset
   432
Chengsong
parents: 518
diff changeset
   433
But having a correct result is still not enough, 
Chengsong
parents: 518
diff changeset
   434
we want at least some degree of $\mathbf{efficiency}$.
Chengsong
parents: 518
diff changeset
   435
Chengsong
parents: 518
diff changeset
   436
Chengsong
parents: 518
diff changeset
   437
Chengsong
parents: 518
diff changeset
   438
One regular expression can have multiple lexical values. For example
Chengsong
parents: 518
diff changeset
   439
for the regular expression $(a+b)^*$, it has a infinite list of
Chengsong
parents: 518
diff changeset
   440
values corresponding to it: $\Stars\,[]$, $\Stars\,[\Left(Char(a))]$,
Chengsong
parents: 518
diff changeset
   441
$\Stars\,[\Right(Char(b))]$, $\Stars\,[\Left(Char(a),\,\Right(Char(b))]$,
Chengsong
parents: 518
diff changeset
   442
$\ldots$, and vice versa.
Chengsong
parents: 518
diff changeset
   443
Even for the regular expression matching a certain string, there could 
Chengsong
parents: 518
diff changeset
   444
still be more than one value corresponding to it.
Chengsong
parents: 518
diff changeset
   445
Take the example where $r= (a^*\cdot a^*)^*$ and the string 
Chengsong
parents: 518
diff changeset
   446
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$.
528
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   447
If we do not allow any empty iterations in its lexical values,
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   448
there will be $n - 1$ "splitting points" on $s$ we can choose to 
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   449
split or not so that each sub-string
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   450
segmented by those chosen splitting points will form different iterations:
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   451
\begin{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   452
\begin{tabular}{lcr}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   453
$a \mid aaa $ & $\rightarrow$ & $\Stars\, [v_{iteration \,a},\,  v_{iteration \,aaa}]$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   454
$aa \mid aa $ & $\rightarrow$ & $\Stars\, [v_{iteration \, aa},\,  v_{iteration \, aa}]$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   455
$a \mid aa\mid a $ & $\rightarrow$ & $\Stars\, [v_{iteration \, a},\,  v_{iteration \, aa}, \, v_{iteration \, a}]$\\
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   456
 & $\textit{etc}.$ &
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   457
 \end{tabular}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   458
\end{center}
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   459
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   460
And for each iteration, there are still multiple ways to split
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   461
between the two $a^*$s.
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   462
It is not surprising there are exponentially many lexical values
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   463
that are distinct for the regex and string pair $r= (a^*\cdot a^*)^*$  and 
28751de4b4ba revised according to comments
Chengsong
parents: 526
diff changeset
   464
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$.
519
Chengsong
parents: 518
diff changeset
   465
529
Chengsong
parents: 528
diff changeset
   466
A lexer aimed at keeping all the possible values will naturally 
Chengsong
parents: 528
diff changeset
   467
have an exponential runtime on ambiguous regular expressions.
Chengsong
parents: 528
diff changeset
   468
Somehow one has to decide which lexical value to keep and
Chengsong
parents: 528
diff changeset
   469
output in a lexing algorithm.
Chengsong
parents: 528
diff changeset
   470
In practice, we are usually 
519
Chengsong
parents: 518
diff changeset
   471
interested about POSIX values, which by intuition always
Chengsong
parents: 518
diff changeset
   472
\begin{itemize}
Chengsong
parents: 518
diff changeset
   473
\item
Chengsong
parents: 518
diff changeset
   474
match the leftmost regular expression when multiple options of matching
Chengsong
parents: 518
diff changeset
   475
are available  
Chengsong
parents: 518
diff changeset
   476
\item 
Chengsong
parents: 518
diff changeset
   477
always match a subpart as much as possible before proceeding
Chengsong
parents: 518
diff changeset
   478
to the next token.
Chengsong
parents: 518
diff changeset
   479
\end{itemize}
529
Chengsong
parents: 528
diff changeset
   480
The formal definition of a $\POSIX$ value can be described 
Chengsong
parents: 528
diff changeset
   481
in the following set of rules:
Chengsong
parents: 528
diff changeset
   482
\
519
Chengsong
parents: 518
diff changeset
   483
Chengsong
parents: 518
diff changeset
   484
Chengsong
parents: 518
diff changeset
   485
 For example, the above example has the POSIX value
Chengsong
parents: 518
diff changeset
   486
$ \Stars\,[\Seq(Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}], Stars\,[])]$.
Chengsong
parents: 518
diff changeset
   487
The output of an algorithm we want would be a POSIX matching
Chengsong
parents: 518
diff changeset
   488
encoded as a value.
Chengsong
parents: 518
diff changeset
   489
The reason why we are interested in $\POSIX$ values is that they can
Chengsong
parents: 518
diff changeset
   490
be practically used in the lexing phase of a compiler front end.
Chengsong
parents: 518
diff changeset
   491
For instance, when lexing a code snippet 
Chengsong
parents: 518
diff changeset
   492
$\textit{iffoo} = 3$ with the regular expression $\textit{keyword} + \textit{identifier}$, we want $\textit{iffoo}$ to be recognized
Chengsong
parents: 518
diff changeset
   493
as an identifier rather than a keyword.
Chengsong
parents: 518
diff changeset
   494
Chengsong
parents: 518
diff changeset
   495
The contribution of Sulzmann and Lu is an extension of Brzozowski's
Chengsong
parents: 518
diff changeset
   496
algorithm by a second phase (the first phase being building successive
Chengsong
parents: 518
diff changeset
   497
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value 
Chengsong
parents: 518
diff changeset
   498
is generated in case the regular expression matches  the string. 
Chengsong
parents: 518
diff changeset
   499
Pictorially, the Sulzmann and Lu algorithm is as follows:
Chengsong
parents: 518
diff changeset
   500
Chengsong
parents: 518
diff changeset
   501
\begin{ceqn}
Chengsong
parents: 518
diff changeset
   502
\begin{equation}\label{graph:2}
Chengsong
parents: 518
diff changeset
   503
\begin{tikzcd}
Chengsong
parents: 518
diff changeset
   504
r_0 \arrow[r, "\backslash c_0"]  \arrow[d] & r_1 \arrow[r, "\backslash c_1"] \arrow[d] & r_2 \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
Chengsong
parents: 518
diff changeset
   505
v_0           & v_1 \arrow[l,"inj_{r_0} c_0"]                & v_2 \arrow[l, "inj_{r_1} c_1"]              & v_n \arrow[l, dashed]         
Chengsong
parents: 518
diff changeset
   506
\end{tikzcd}
Chengsong
parents: 518
diff changeset
   507
\end{equation}
Chengsong
parents: 518
diff changeset
   508
\end{ceqn}
Chengsong
parents: 518
diff changeset
   509
Chengsong
parents: 518
diff changeset
   510
Chengsong
parents: 518
diff changeset
   511
\noindent
Chengsong
parents: 518
diff changeset
   512
For convenience, we shall employ the following notations: the regular
Chengsong
parents: 518
diff changeset
   513
expression we start with is $r_0$, and the given string $s$ is composed
Chengsong
parents: 518
diff changeset
   514
of characters $c_0 c_1 \ldots c_{n-1}$. In  the first phase from the
Chengsong
parents: 518
diff changeset
   515
left to right, we build the derivatives $r_1$, $r_2$, \ldots  according
Chengsong
parents: 518
diff changeset
   516
to the characters $c_0$, $c_1$  until we exhaust the string and obtain
Chengsong
parents: 518
diff changeset
   517
the derivative $r_n$. We test whether this derivative is
Chengsong
parents: 518
diff changeset
   518
$\textit{nullable}$ or not. If not, we know the string does not match
Chengsong
parents: 518
diff changeset
   519
$r$ and no value needs to be generated. If yes, we start building the
Chengsong
parents: 518
diff changeset
   520
values incrementally by \emph{injecting} back the characters into the
Chengsong
parents: 518
diff changeset
   521
earlier values $v_n, \ldots, v_0$. This is the second phase of the
Chengsong
parents: 518
diff changeset
   522
algorithm from the right to left. For the first value $v_n$, we call the
Chengsong
parents: 518
diff changeset
   523
function $\textit{mkeps}$, which builds a POSIX lexical value
Chengsong
parents: 518
diff changeset
   524
for how the empty string has been matched by the (nullable) regular
Chengsong
parents: 518
diff changeset
   525
expression $r_n$. This function is defined as
Chengsong
parents: 518
diff changeset
   526
Chengsong
parents: 518
diff changeset
   527
	\begin{center}
Chengsong
parents: 518
diff changeset
   528
		\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   529
			$\mkeps(\ONE)$ 		& $\dn$ & $\Empty$ \\
Chengsong
parents: 518
diff changeset
   530
			$\mkeps(r_{1}+r_{2})$	& $\dn$ 
Chengsong
parents: 518
diff changeset
   531
			& \textit{if} $\nullable(r_{1})$\\ 
Chengsong
parents: 518
diff changeset
   532
			& & \textit{then} $\Left(\mkeps(r_{1}))$\\ 
Chengsong
parents: 518
diff changeset
   533
			& & \textit{else} $\Right(\mkeps(r_{2}))$\\
Chengsong
parents: 518
diff changeset
   534
			$\mkeps(r_1\cdot r_2)$ 	& $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
Chengsong
parents: 518
diff changeset
   535
			$mkeps(r^*)$	        & $\dn$ & $\Stars\,[]$
Chengsong
parents: 518
diff changeset
   536
		\end{tabular}
Chengsong
parents: 518
diff changeset
   537
	\end{center}
Chengsong
parents: 518
diff changeset
   538
Chengsong
parents: 518
diff changeset
   539
Chengsong
parents: 518
diff changeset
   540
\noindent 
Chengsong
parents: 518
diff changeset
   541
After the $\mkeps$-call, we inject back the characters one by one in order to build
Chengsong
parents: 518
diff changeset
   542
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
Chengsong
parents: 518
diff changeset
   543
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
Chengsong
parents: 518
diff changeset
   544
After injecting back $n$ characters, we get the lexical value for how $r_0$
Chengsong
parents: 518
diff changeset
   545
matches $s$. The POSIX value is maintained throught out the process.
Chengsong
parents: 518
diff changeset
   546
For this Sulzmann and Lu defined a function that reverses
Chengsong
parents: 518
diff changeset
   547
the ``chopping off'' of characters during the derivative phase. The
Chengsong
parents: 518
diff changeset
   548
corresponding function is called \emph{injection}, written
Chengsong
parents: 518
diff changeset
   549
$\textit{inj}$; it takes three arguments: the first one is a regular
Chengsong
parents: 518
diff changeset
   550
expression ${r_{i-1}}$, before the character is chopped off, the second
Chengsong
parents: 518
diff changeset
   551
is a character ${c_{i-1}}$, the character we want to inject and the
Chengsong
parents: 518
diff changeset
   552
third argument is the value ${v_i}$, into which one wants to inject the
Chengsong
parents: 518
diff changeset
   553
character (it corresponds to the regular expression after the character
Chengsong
parents: 518
diff changeset
   554
has been chopped off). The result of this function is a new value. The
Chengsong
parents: 518
diff changeset
   555
definition of $\textit{inj}$ is as follows: 
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
   556
519
Chengsong
parents: 518
diff changeset
   557
\begin{center}
Chengsong
parents: 518
diff changeset
   558
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
Chengsong
parents: 518
diff changeset
   559
  $\textit{inj}\,(c)\,c\,Empty$            & $\dn$ & $Char\,c$\\
Chengsong
parents: 518
diff changeset
   560
  $\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
Chengsong
parents: 518
diff changeset
   561
  $\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
Chengsong
parents: 518
diff changeset
   562
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
Chengsong
parents: 518
diff changeset
   563
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$  & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
Chengsong
parents: 518
diff changeset
   564
  $\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
Chengsong
parents: 518
diff changeset
   565
  $\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$         & $\dn$  & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
Chengsong
parents: 518
diff changeset
   566
\end{tabular}
Chengsong
parents: 518
diff changeset
   567
\end{center}
Chengsong
parents: 518
diff changeset
   568
Chengsong
parents: 518
diff changeset
   569
\noindent This definition is by recursion on the ``shape'' of regular
Chengsong
parents: 518
diff changeset
   570
expressions and values. 
Chengsong
parents: 518
diff changeset
   571
The clauses basically do one thing--identifying the ``holes'' on 
Chengsong
parents: 518
diff changeset
   572
value to inject the character back into.
Chengsong
parents: 518
diff changeset
   573
For instance, in the last clause for injecting back to a value
Chengsong
parents: 518
diff changeset
   574
that would turn into a new star value that corresponds to a star,
Chengsong
parents: 518
diff changeset
   575
we know it must be a sequence value. And we know that the first 
Chengsong
parents: 518
diff changeset
   576
value of that sequence corresponds to the child regex of the star
Chengsong
parents: 518
diff changeset
   577
with the first character being chopped off--an iteration of the star
Chengsong
parents: 518
diff changeset
   578
that had just been unfolded. This value is followed by the already
Chengsong
parents: 518
diff changeset
   579
matched star iterations we collected before. So we inject the character 
Chengsong
parents: 518
diff changeset
   580
back to the first value and form a new value with this new iteration
Chengsong
parents: 518
diff changeset
   581
being added to the previous list of iterations, all under the $Stars$
Chengsong
parents: 518
diff changeset
   582
top level.
Chengsong
parents: 518
diff changeset
   583
Chengsong
parents: 518
diff changeset
   584
We have mentioned before that derivatives without simplification 
Chengsong
parents: 518
diff changeset
   585
can get clumsy, and this is true for values as well--they reflect
Chengsong
parents: 518
diff changeset
   586
the regular expressions size by definition.
Chengsong
parents: 518
diff changeset
   587
Chengsong
parents: 518
diff changeset
   588
One can introduce simplification on the regex and values, but have to
Chengsong
parents: 518
diff changeset
   589
be careful in not breaking the correctness as the injection 
Chengsong
parents: 518
diff changeset
   590
function heavily relies on the structure of the regexes and values
Chengsong
parents: 518
diff changeset
   591
being correct and match each other.
Chengsong
parents: 518
diff changeset
   592
It can be achieved by recording some extra rectification functions
Chengsong
parents: 518
diff changeset
   593
during the derivatives step, and applying these rectifications in 
Chengsong
parents: 518
diff changeset
   594
each run during the injection phase.
Chengsong
parents: 518
diff changeset
   595
And we can prove that the POSIX value of how
Chengsong
parents: 518
diff changeset
   596
regular expressions match strings will not be affected---although is much harder
Chengsong
parents: 518
diff changeset
   597
to establish. 
Chengsong
parents: 518
diff changeset
   598
Some initial results in this regard have been
Chengsong
parents: 518
diff changeset
   599
obtained in \cite{AusafDyckhoffUrban2016}. 
Chengsong
parents: 518
diff changeset
   600
Chengsong
parents: 518
diff changeset
   601
Chengsong
parents: 518
diff changeset
   602
Chengsong
parents: 518
diff changeset
   603
%Brzozowski, after giving the derivatives and simplification,
Chengsong
parents: 518
diff changeset
   604
%did not explore lexing with simplification or he may well be 
Chengsong
parents: 518
diff changeset
   605
%stuck on an efficient simplificaiton with a proof.
Chengsong
parents: 518
diff changeset
   606
%He went on to explore the use of derivatives together with 
Chengsong
parents: 518
diff changeset
   607
%automaton, and did not try lexing using derivatives.
Chengsong
parents: 518
diff changeset
   608
Chengsong
parents: 518
diff changeset
   609
We want to get rid of complex and fragile rectification of values.
Chengsong
parents: 518
diff changeset
   610
Can we not create those intermediate values $v_1,\ldots v_n$,
Chengsong
parents: 518
diff changeset
   611
and get the lexing information that should be already there while
Chengsong
parents: 518
diff changeset
   612
doing derivatives in one pass, without a second phase of injection?
Chengsong
parents: 518
diff changeset
   613
In the meantime, can we make sure that simplifications
Chengsong
parents: 518
diff changeset
   614
are easily handled without breaking the correctness of the algorithm?
Chengsong
parents: 518
diff changeset
   615
Chengsong
parents: 518
diff changeset
   616
Sulzmann and Lu solved this problem by
Chengsong
parents: 518
diff changeset
   617
introducing additional informtaion to the 
Chengsong
parents: 518
diff changeset
   618
regular expressions called \emph{bitcodes}.
Chengsong
parents: 518
diff changeset
   619
Chengsong
parents: 518
diff changeset
   620
\subsection*{Bit-coded Algorithm}
Chengsong
parents: 518
diff changeset
   621
Bits and bitcodes (lists of bits) are defined as:
Chengsong
parents: 518
diff changeset
   622
Chengsong
parents: 518
diff changeset
   623
\begin{center}
Chengsong
parents: 518
diff changeset
   624
		$b ::=   1 \mid  0 \qquad
Chengsong
parents: 518
diff changeset
   625
bs ::= [] \mid b::bs    
Chengsong
parents: 518
diff changeset
   626
$
Chengsong
parents: 518
diff changeset
   627
\end{center}
Chengsong
parents: 518
diff changeset
   628
Chengsong
parents: 518
diff changeset
   629
\noindent
Chengsong
parents: 518
diff changeset
   630
The $1$ and $0$ are not in bold in order to avoid 
Chengsong
parents: 518
diff changeset
   631
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or
Chengsong
parents: 518
diff changeset
   632
bit-lists) can be used to encode values (or potentially incomplete values) in a
Chengsong
parents: 518
diff changeset
   633
compact form. This can be straightforwardly seen in the following
Chengsong
parents: 518
diff changeset
   634
coding function from values to bitcodes: 
Chengsong
parents: 518
diff changeset
   635
Chengsong
parents: 518
diff changeset
   636
\begin{center}
Chengsong
parents: 518
diff changeset
   637
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   638
  $\textit{code}(\Empty)$ & $\dn$ & $[]$\\
Chengsong
parents: 518
diff changeset
   639
  $\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
Chengsong
parents: 518
diff changeset
   640
  $\textit{code}(\Left\,v)$ & $\dn$ & $0 :: code(v)$\\
Chengsong
parents: 518
diff changeset
   641
  $\textit{code}(\Right\,v)$ & $\dn$ & $1 :: code(v)$\\
Chengsong
parents: 518
diff changeset
   642
  $\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
Chengsong
parents: 518
diff changeset
   643
  $\textit{code}(\Stars\,[])$ & $\dn$ & $[0]$\\
Chengsong
parents: 518
diff changeset
   644
  $\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $1 :: code(v) \;@\;
Chengsong
parents: 518
diff changeset
   645
                                                 code(\Stars\,vs)$
Chengsong
parents: 518
diff changeset
   646
\end{tabular}    
Chengsong
parents: 518
diff changeset
   647
\end{center} 
Chengsong
parents: 518
diff changeset
   648
Chengsong
parents: 518
diff changeset
   649
\noindent
Chengsong
parents: 518
diff changeset
   650
Here $\textit{code}$ encodes a value into a bitcodes by converting
Chengsong
parents: 518
diff changeset
   651
$\Left$ into $0$, $\Right$ into $1$, and marks the start of a non-empty
Chengsong
parents: 518
diff changeset
   652
star iteration by $1$. The border where a local star terminates
Chengsong
parents: 518
diff changeset
   653
is marked by $0$. This coding is lossy, as it throws away the information about
Chengsong
parents: 518
diff changeset
   654
characters, and also does not encode the ``boundary'' between two
Chengsong
parents: 518
diff changeset
   655
sequence values. Moreover, with only the bitcode we cannot even tell
Chengsong
parents: 518
diff changeset
   656
whether the $1$s and $0$s are for $\Left/\Right$ or $\Stars$. The
Chengsong
parents: 518
diff changeset
   657
reason for choosing this compact way of storing information is that the
Chengsong
parents: 518
diff changeset
   658
relatively small size of bits can be easily manipulated and ``moved
Chengsong
parents: 518
diff changeset
   659
around'' in a regular expression. In order to recover values, we will 
Chengsong
parents: 518
diff changeset
   660
need the corresponding regular expression as an extra information. This
Chengsong
parents: 518
diff changeset
   661
means the decoding function is defined as:
Chengsong
parents: 518
diff changeset
   662
Chengsong
parents: 518
diff changeset
   663
Chengsong
parents: 518
diff changeset
   664
%\begin{definition}[Bitdecoding of Values]\mbox{}
Chengsong
parents: 518
diff changeset
   665
\begin{center}
Chengsong
parents: 518
diff changeset
   666
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
Chengsong
parents: 518
diff changeset
   667
  $\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
Chengsong
parents: 518
diff changeset
   668
  $\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
Chengsong
parents: 518
diff changeset
   669
  $\textit{decode}'\,(0\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   670
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
Chengsong
parents: 518
diff changeset
   671
       (\Left\,v, bs_1)$\\
Chengsong
parents: 518
diff changeset
   672
  $\textit{decode}'\,(1\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   673
     $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
Chengsong
parents: 518
diff changeset
   674
       (\Right\,v, bs_1)$\\                           
Chengsong
parents: 518
diff changeset
   675
  $\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   676
        $\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
Chengsong
parents: 518
diff changeset
   677
  & &   $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
Chengsong
parents: 518
diff changeset
   678
  & &   \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
Chengsong
parents: 518
diff changeset
   679
  $\textit{decode}'\,(0\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
Chengsong
parents: 518
diff changeset
   680
  $\textit{decode}'\,(1\!::\!bs)\,(r^*)$ & $\dn$ & 
Chengsong
parents: 518
diff changeset
   681
         $\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents: 518
diff changeset
   682
  & &   $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
Chengsong
parents: 518
diff changeset
   683
  & &   \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
Chengsong
parents: 518
diff changeset
   684
  
Chengsong
parents: 518
diff changeset
   685
  $\textit{decode}\,bs\,r$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   686
     $\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
Chengsong
parents: 518
diff changeset
   687
  & & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
Chengsong
parents: 518
diff changeset
   688
       \textit{else}\;\textit{None}$                       
Chengsong
parents: 518
diff changeset
   689
\end{tabular}    
Chengsong
parents: 518
diff changeset
   690
\end{center}    
Chengsong
parents: 518
diff changeset
   691
%\end{definition}
Chengsong
parents: 518
diff changeset
   692
Chengsong
parents: 518
diff changeset
   693
Sulzmann and Lu's integrated the bitcodes into regular expressions to
Chengsong
parents: 518
diff changeset
   694
create annotated regular expressions \cite{Sulzmann2014}.
Chengsong
parents: 518
diff changeset
   695
\emph{Annotated regular expressions} are defined by the following
Chengsong
parents: 518
diff changeset
   696
grammar:%\comment{ALTS should have  an $as$ in  the definitions, not  just $a_1$ and $a_2$}
Chengsong
parents: 518
diff changeset
   697
Chengsong
parents: 518
diff changeset
   698
\begin{center}
Chengsong
parents: 518
diff changeset
   699
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   700
  $\textit{a}$ & $::=$  & $\ZERO$\\
Chengsong
parents: 518
diff changeset
   701
                  & $\mid$ & $_{bs}\ONE$\\
Chengsong
parents: 518
diff changeset
   702
                  & $\mid$ & $_{bs}{\bf c}$\\
Chengsong
parents: 518
diff changeset
   703
                  & $\mid$ & $_{bs}\sum\,as$\\
Chengsong
parents: 518
diff changeset
   704
                  & $\mid$ & $_{bs}a_1\cdot a_2$\\
Chengsong
parents: 518
diff changeset
   705
                  & $\mid$ & $_{bs}a^*$
Chengsong
parents: 518
diff changeset
   706
\end{tabular}    
Chengsong
parents: 518
diff changeset
   707
\end{center}  
Chengsong
parents: 518
diff changeset
   708
%(in \textit{ALTS})
Chengsong
parents: 518
diff changeset
   709
Chengsong
parents: 518
diff changeset
   710
\noindent
Chengsong
parents: 518
diff changeset
   711
where $bs$ stands for bitcodes, $a$  for $\mathbf{a}$nnotated regular
Chengsong
parents: 518
diff changeset
   712
expressions and $as$ for a list of annotated regular expressions.
Chengsong
parents: 518
diff changeset
   713
The alternative constructor($\sum$) has been generalized to 
Chengsong
parents: 518
diff changeset
   714
accept a list of annotated regular expressions rather than just 2.
Chengsong
parents: 518
diff changeset
   715
We will show that these bitcodes encode information about
Chengsong
parents: 518
diff changeset
   716
the (POSIX) value that should be generated by the Sulzmann and Lu
Chengsong
parents: 518
diff changeset
   717
algorithm.
Chengsong
parents: 518
diff changeset
   718
Chengsong
parents: 518
diff changeset
   719
Chengsong
parents: 518
diff changeset
   720
To do lexing using annotated regular expressions, we shall first
Chengsong
parents: 518
diff changeset
   721
transform the usual (un-annotated) regular expressions into annotated
Chengsong
parents: 518
diff changeset
   722
regular expressions. This operation is called \emph{internalisation} and
Chengsong
parents: 518
diff changeset
   723
defined as follows:
Chengsong
parents: 518
diff changeset
   724
Chengsong
parents: 518
diff changeset
   725
%\begin{definition}
Chengsong
parents: 518
diff changeset
   726
\begin{center}
Chengsong
parents: 518
diff changeset
   727
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   728
  $(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
Chengsong
parents: 518
diff changeset
   729
  $(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
Chengsong
parents: 518
diff changeset
   730
  $(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
Chengsong
parents: 518
diff changeset
   731
  $(r_1 + r_2)^\uparrow$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   732
  $_{[]}\sum[\textit{fuse}\,[0]\,r_1^\uparrow,\,
Chengsong
parents: 518
diff changeset
   733
  \textit{fuse}\,[1]\,r_2^\uparrow]$\\
Chengsong
parents: 518
diff changeset
   734
  $(r_1\cdot r_2)^\uparrow$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   735
         $_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
Chengsong
parents: 518
diff changeset
   736
  $(r^*)^\uparrow$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   737
         $_{[]}(r^\uparrow)^*$\\
Chengsong
parents: 518
diff changeset
   738
\end{tabular}    
Chengsong
parents: 518
diff changeset
   739
\end{center}    
Chengsong
parents: 518
diff changeset
   740
%\end{definition}
Chengsong
parents: 518
diff changeset
   741
Chengsong
parents: 518
diff changeset
   742
\noindent
Chengsong
parents: 518
diff changeset
   743
We use up arrows here to indicate that the basic un-annotated regular
Chengsong
parents: 518
diff changeset
   744
expressions are ``lifted up'' into something slightly more complex. In the
Chengsong
parents: 518
diff changeset
   745
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
Chengsong
parents: 518
diff changeset
   746
attach bits to the front of an annotated regular expression. Its
Chengsong
parents: 518
diff changeset
   747
definition is as follows:
Chengsong
parents: 518
diff changeset
   748
Chengsong
parents: 518
diff changeset
   749
\begin{center}
Chengsong
parents: 518
diff changeset
   750
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   751
  $\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
Chengsong
parents: 518
diff changeset
   752
  $\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   753
     $_{bs @ bs'}\ONE$\\
Chengsong
parents: 518
diff changeset
   754
  $\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   755
     $_{bs@bs'}{\bf c}$\\
Chengsong
parents: 518
diff changeset
   756
  $\textit{fuse}\;bs\,_{bs'}\sum\textit{as}$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   757
     $_{bs@bs'}\sum\textit{as}$\\
Chengsong
parents: 518
diff changeset
   758
  $\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   759
     $_{bs@bs'}a_1 \cdot a_2$\\
Chengsong
parents: 518
diff changeset
   760
  $\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   761
     $_{bs @ bs'}a^*$
Chengsong
parents: 518
diff changeset
   762
\end{tabular}    
Chengsong
parents: 518
diff changeset
   763
\end{center}  
Chengsong
parents: 518
diff changeset
   764
Chengsong
parents: 518
diff changeset
   765
\noindent
Chengsong
parents: 518
diff changeset
   766
After internalising the regular expression, we perform successive
Chengsong
parents: 518
diff changeset
   767
derivative operations on the annotated regular expressions. This
Chengsong
parents: 518
diff changeset
   768
derivative operation is the same as what we had previously for the
Chengsong
parents: 518
diff changeset
   769
basic regular expressions, except that we beed to take care of
Chengsong
parents: 518
diff changeset
   770
the bitcodes:
Chengsong
parents: 518
diff changeset
   771
Chengsong
parents: 518
diff changeset
   772
Chengsong
parents: 518
diff changeset
   773
\iffalse
Chengsong
parents: 518
diff changeset
   774
 %\begin{definition}{bder}
Chengsong
parents: 518
diff changeset
   775
\begin{center}
Chengsong
parents: 518
diff changeset
   776
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 518
diff changeset
   777
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 518
diff changeset
   778
  $(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 518
diff changeset
   779
  $(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   780
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 518
diff changeset
   781
         \textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\  
Chengsong
parents: 518
diff changeset
   782
  $(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   783
  $\textit{ALTS}\;bs\,(map (\backslash c) as)$\\
Chengsong
parents: 518
diff changeset
   784
  $(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   785
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 518
diff changeset
   786
					       & &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\
Chengsong
parents: 518
diff changeset
   787
					       & &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
Chengsong
parents: 518
diff changeset
   788
  & &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\
Chengsong
parents: 518
diff changeset
   789
  $(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   790
      $\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\,
Chengsong
parents: 518
diff changeset
   791
       (\textit{STAR}\,[]\,r)$
Chengsong
parents: 518
diff changeset
   792
\end{tabular}    
Chengsong
parents: 518
diff changeset
   793
\end{center}    
Chengsong
parents: 518
diff changeset
   794
%\end{definition}
Chengsong
parents: 518
diff changeset
   795
Chengsong
parents: 518
diff changeset
   796
\begin{center}
Chengsong
parents: 518
diff changeset
   797
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 518
diff changeset
   798
  $(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 518
diff changeset
   799
  $(_{bs}\textit{ONE})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\  
Chengsong
parents: 518
diff changeset
   800
  $(_{bs}\textit{CHAR}\;d)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   801
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 518
diff changeset
   802
         _{bs}\textit{ONE}\;\textit{else}\;\textit{ZERO}$\\  
Chengsong
parents: 518
diff changeset
   803
  $(_{bs}\textit{ALTS}\;\textit{as})\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   804
  $_{bs}\textit{ALTS}\;(\textit{as}.\textit{map}(\backslash c))$\\
Chengsong
parents: 518
diff changeset
   805
  $(_{bs}\textit{SEQ}\;a_1\,a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   806
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 518
diff changeset
   807
					       & &$\textit{then}\;_{bs}\textit{ALTS}\,List((_{[]}\textit{SEQ}\,(a_1\,\backslash c)\,a_2),$\\
Chengsong
parents: 518
diff changeset
   808
					       & &$\phantom{\textit{then}\;_{bs}\textit{ALTS}\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\
Chengsong
parents: 518
diff changeset
   809
  & &$\textit{else}\;_{bs}\textit{SEQ}\,(a_1\,\backslash c)\,a_2$\\
Chengsong
parents: 518
diff changeset
   810
  $(_{bs}\textit{STAR}\,a)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   811
      $_{bs}\textit{SEQ}\;(\textit{fuse}\, [0] \; r\,\backslash c )\,
Chengsong
parents: 518
diff changeset
   812
       (_{bs}\textit{STAR}\,[]\,r)$
Chengsong
parents: 518
diff changeset
   813
\end{tabular}    
Chengsong
parents: 518
diff changeset
   814
\end{center}    
Chengsong
parents: 518
diff changeset
   815
%\end{definition}
Chengsong
parents: 518
diff changeset
   816
\fi
Chengsong
parents: 518
diff changeset
   817
Chengsong
parents: 518
diff changeset
   818
\begin{center}
Chengsong
parents: 518
diff changeset
   819
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 518
diff changeset
   820
  $(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 518
diff changeset
   821
  $(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\  
Chengsong
parents: 518
diff changeset
   822
  $(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   823
        $\textit{if}\;c=d\; \;\textit{then}\;
Chengsong
parents: 518
diff changeset
   824
         _{bs}\ONE\;\textit{else}\;\ZERO$\\  
Chengsong
parents: 518
diff changeset
   825
  $(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   826
  $_{bs}\sum\;(\textit{as.map}(\backslash c))$\\
Chengsong
parents: 518
diff changeset
   827
  $(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   828
     $\textit{if}\;\textit{bnullable}\,a_1$\\
Chengsong
parents: 518
diff changeset
   829
					       & &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
Chengsong
parents: 518
diff changeset
   830
					       & &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
Chengsong
parents: 518
diff changeset
   831
  & &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
Chengsong
parents: 518
diff changeset
   832
  $(_{bs}a^*)\,\backslash c$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   833
      $_{bs}(\textit{fuse}\, [0] \; r\,\backslash c)\cdot
Chengsong
parents: 518
diff changeset
   834
       (_{[]}r^*))$
Chengsong
parents: 518
diff changeset
   835
\end{tabular}    
Chengsong
parents: 518
diff changeset
   836
\end{center}    
Chengsong
parents: 518
diff changeset
   837
Chengsong
parents: 518
diff changeset
   838
%\end{definition}
Chengsong
parents: 518
diff changeset
   839
\noindent
Chengsong
parents: 518
diff changeset
   840
For instance, when we do derivative of  $_{bs}a^*$ with respect to c,
Chengsong
parents: 518
diff changeset
   841
we need to unfold it into a sequence,
Chengsong
parents: 518
diff changeset
   842
and attach an additional bit $0$ to the front of $r \backslash c$
Chengsong
parents: 518
diff changeset
   843
to indicate one more star iteration. Also the sequence clause
Chengsong
parents: 518
diff changeset
   844
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
Chengsong
parents: 518
diff changeset
   845
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
Chengsong
parents: 518
diff changeset
   846
that it is for annotated regular expressions, therefore we omit the
Chengsong
parents: 518
diff changeset
   847
definition). Assume that $\textit{bmkeps}$ correctly extracts the bitcode for how
Chengsong
parents: 518
diff changeset
   848
$a_1$ matches the string prior to character $c$ (more on this later),
Chengsong
parents: 518
diff changeset
   849
then the right branch of alternative, which is $\textit{fuse} \; \bmkeps \;  a_1 (a_2
Chengsong
parents: 518
diff changeset
   850
\backslash c)$ will collapse the regular expression $a_1$(as it has
Chengsong
parents: 518
diff changeset
   851
already been fully matched) and store the parsing information at the
Chengsong
parents: 518
diff changeset
   852
head of the regular expression $a_2 \backslash c$ by fusing to it. The
Chengsong
parents: 518
diff changeset
   853
bitsequence $\textit{bs}$, which was initially attached to the
Chengsong
parents: 518
diff changeset
   854
first element of the sequence $a_1 \cdot a_2$, has
Chengsong
parents: 518
diff changeset
   855
now been elevated to the top-level of $\sum$, as this information will be
Chengsong
parents: 518
diff changeset
   856
needed whichever way the sequence is matched---no matter whether $c$ belongs
Chengsong
parents: 518
diff changeset
   857
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
Chengsong
parents: 518
diff changeset
   858
the lexing information, we complete the lexing by collecting the
Chengsong
parents: 518
diff changeset
   859
bitcodes using a generalised version of the $\textit{mkeps}$ function
Chengsong
parents: 518
diff changeset
   860
for annotated regular expressions, called $\textit{bmkeps}$:
Chengsong
parents: 518
diff changeset
   861
Chengsong
parents: 518
diff changeset
   862
Chengsong
parents: 518
diff changeset
   863
%\begin{definition}[\textit{bmkeps}]\mbox{}
Chengsong
parents: 518
diff changeset
   864
\begin{center}
Chengsong
parents: 518
diff changeset
   865
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   866
  $\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
Chengsong
parents: 518
diff changeset
   867
  $\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   868
     $\textit{if}\;\textit{bnullable}\,a$\\
Chengsong
parents: 518
diff changeset
   869
  & &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
Chengsong
parents: 518
diff changeset
   870
  & &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{bs}\sum \textit{as})$\\
Chengsong
parents: 518
diff changeset
   871
  $\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   872
     $bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
Chengsong
parents: 518
diff changeset
   873
  $\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   874
     $bs \,@\, [0]$
Chengsong
parents: 518
diff changeset
   875
\end{tabular}    
Chengsong
parents: 518
diff changeset
   876
\end{center}    
Chengsong
parents: 518
diff changeset
   877
%\end{definition}
Chengsong
parents: 518
diff changeset
   878
Chengsong
parents: 518
diff changeset
   879
\noindent
Chengsong
parents: 518
diff changeset
   880
This function completes the value information by travelling along the
Chengsong
parents: 518
diff changeset
   881
path of the regular expression that corresponds to a POSIX value and
Chengsong
parents: 518
diff changeset
   882
collecting all the bitcodes, and using $S$ to indicate the end of star
Chengsong
parents: 518
diff changeset
   883
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
Chengsong
parents: 518
diff changeset
   884
decode them, we get the value we expect. The corresponding lexing
Chengsong
parents: 518
diff changeset
   885
algorithm looks as follows:
Chengsong
parents: 518
diff changeset
   886
Chengsong
parents: 518
diff changeset
   887
\begin{center}
Chengsong
parents: 518
diff changeset
   888
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   889
  $\textit{blexer}\;r\,s$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   890
      $\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\                
Chengsong
parents: 518
diff changeset
   891
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents: 518
diff changeset
   892
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents: 518
diff changeset
   893
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents: 518
diff changeset
   894
\end{tabular}
Chengsong
parents: 518
diff changeset
   895
\end{center}
Chengsong
parents: 518
diff changeset
   896
Chengsong
parents: 518
diff changeset
   897
\noindent
Chengsong
parents: 518
diff changeset
   898
In this definition $\_\backslash s$ is the  generalisation  of the derivative
Chengsong
parents: 518
diff changeset
   899
operation from characters to strings (just like the derivatives for un-annotated
Chengsong
parents: 518
diff changeset
   900
regular expressions).
Chengsong
parents: 518
diff changeset
   901
Chengsong
parents: 518
diff changeset
   902
Now we introduce the simplifications, which is why we introduce the 
Chengsong
parents: 518
diff changeset
   903
bitcodes in the first place.
Chengsong
parents: 518
diff changeset
   904
Chengsong
parents: 518
diff changeset
   905
\subsection*{Simplification Rules}
Chengsong
parents: 518
diff changeset
   906
Chengsong
parents: 518
diff changeset
   907
This section introduces aggressive (in terms of size) simplification rules
Chengsong
parents: 518
diff changeset
   908
on annotated regular expressions
Chengsong
parents: 518
diff changeset
   909
to keep derivatives small. Such simplifications are promising
Chengsong
parents: 518
diff changeset
   910
as we have
Chengsong
parents: 518
diff changeset
   911
generated test data that show
Chengsong
parents: 518
diff changeset
   912
that a good tight bound can be achieved. We could only
Chengsong
parents: 518
diff changeset
   913
partially cover the search space as there are infinitely many regular
Chengsong
parents: 518
diff changeset
   914
expressions and strings. 
Chengsong
parents: 518
diff changeset
   915
Chengsong
parents: 518
diff changeset
   916
One modification we introduced is to allow a list of annotated regular
Chengsong
parents: 518
diff changeset
   917
expressions in the $\sum$ constructor. This allows us to not just
Chengsong
parents: 518
diff changeset
   918
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but
Chengsong
parents: 518
diff changeset
   919
also unnecessary ``copies'' of regular expressions (very similar to
Chengsong
parents: 518
diff changeset
   920
simplifying $r + r$ to just $r$, but in a more general setting). Another
Chengsong
parents: 518
diff changeset
   921
modification is that we use simplification rules inspired by Antimirov's
Chengsong
parents: 518
diff changeset
   922
work on partial derivatives. They maintain the idea that only the first
Chengsong
parents: 518
diff changeset
   923
``copy'' of a regular expression in an alternative contributes to the
Chengsong
parents: 518
diff changeset
   924
calculation of a POSIX value. All subsequent copies can be pruned away from
Chengsong
parents: 518
diff changeset
   925
the regular expression. A recursive definition of our  simplification function 
Chengsong
parents: 518
diff changeset
   926
that looks somewhat similar to our Scala code is given below:
Chengsong
parents: 518
diff changeset
   927
%\comment{Use $\ZERO$, $\ONE$ and so on. 
Chengsong
parents: 518
diff changeset
   928
%Is it $ALTS$ or $ALTS$?}\\
Chengsong
parents: 518
diff changeset
   929
Chengsong
parents: 518
diff changeset
   930
\begin{center}
Chengsong
parents: 518
diff changeset
   931
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 518
diff changeset
   932
   
Chengsong
parents: 518
diff changeset
   933
  $\textit{simp} \; (_{bs}a_1\cdot a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp}  \; a_2) \; \textit{match} $ \\
Chengsong
parents: 518
diff changeset
   934
   &&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow  \ZERO$ \\
Chengsong
parents: 518
diff changeset
   935
   &&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow  \ZERO$ \\
Chengsong
parents: 518
diff changeset
   936
   &&$\quad\textit{case} \;  (\ONE, a_2') \Rightarrow  \textit{fuse} \; bs \;  a_2'$ \\
Chengsong
parents: 518
diff changeset
   937
   &&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow  \textit{fuse} \; bs \;  a_1'$ \\
Chengsong
parents: 518
diff changeset
   938
   &&$\quad\textit{case} \; (a_1', a_2') \Rightarrow   _{bs}a_1' \cdot a_2'$ \\
Chengsong
parents: 518
diff changeset
   939
Chengsong
parents: 518
diff changeset
   940
  $\textit{simp} \; (_{bs}\sum \textit{as})$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{as.map(simp)})) \; \textit{match} $ \\
Chengsong
parents: 518
diff changeset
   941
  &&$\quad\textit{case} \; [] \Rightarrow  \ZERO$ \\
Chengsong
parents: 518
diff changeset
   942
   &&$\quad\textit{case} \; a :: [] \Rightarrow  \textit{fuse bs a}$ \\
Chengsong
parents: 518
diff changeset
   943
   &&$\quad\textit{case} \;  as' \Rightarrow _{bs}\sum \textit{as'}$\\ 
Chengsong
parents: 518
diff changeset
   944
Chengsong
parents: 518
diff changeset
   945
   $\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$   
Chengsong
parents: 518
diff changeset
   946
\end{tabular}    
Chengsong
parents: 518
diff changeset
   947
\end{center}    
Chengsong
parents: 518
diff changeset
   948
Chengsong
parents: 518
diff changeset
   949
\noindent
Chengsong
parents: 518
diff changeset
   950
The simplification does a pattern matching on the regular expression.
Chengsong
parents: 518
diff changeset
   951
When it detected that the regular expression is an alternative or
Chengsong
parents: 518
diff changeset
   952
sequence, it will try to simplify its child regular expressions
Chengsong
parents: 518
diff changeset
   953
recursively and then see if one of the children turns into $\ZERO$ or
Chengsong
parents: 518
diff changeset
   954
$\ONE$, which might trigger further simplification at the current level.
Chengsong
parents: 518
diff changeset
   955
The most involved part is the $\sum$ clause, where we use two
Chengsong
parents: 518
diff changeset
   956
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested
Chengsong
parents: 518
diff changeset
   957
alternatives and reduce as many duplicates as possible. Function
Chengsong
parents: 518
diff changeset
   958
$\textit{distinct}$  keeps the first occurring copy only and removes all later ones
Chengsong
parents: 518
diff changeset
   959
when detected duplicates. Function $\textit{flatten}$ opens up nested $\sum$s.
Chengsong
parents: 518
diff changeset
   960
Its recursive definition is given below:
Chengsong
parents: 518
diff changeset
   961
Chengsong
parents: 518
diff changeset
   962
 \begin{center}
Chengsong
parents: 518
diff changeset
   963
  \begin{tabular}{@{}lcl@{}}
Chengsong
parents: 518
diff changeset
   964
  $\textit{flatten} \; (_{bs}\sum \textit{as}) :: \textit{as'}$ & $\dn$ & $(\textit{map} \;
Chengsong
parents: 518
diff changeset
   965
     (\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\
Chengsong
parents: 518
diff changeset
   966
  $\textit{flatten} \; \ZERO :: as'$ & $\dn$ & $ \textit{flatten} \;  \textit{as'} $ \\
Chengsong
parents: 518
diff changeset
   967
    $\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; \textit{as'}$ \quad(otherwise) 
Chengsong
parents: 518
diff changeset
   968
\end{tabular}    
Chengsong
parents: 518
diff changeset
   969
\end{center}  
Chengsong
parents: 518
diff changeset
   970
Chengsong
parents: 518
diff changeset
   971
\noindent
Chengsong
parents: 518
diff changeset
   972
Here $\textit{flatten}$ behaves like the traditional functional programming flatten
Chengsong
parents: 518
diff changeset
   973
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it
Chengsong
parents: 518
diff changeset
   974
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$.
Chengsong
parents: 518
diff changeset
   975
Chengsong
parents: 518
diff changeset
   976
Having defined the $\simp$ function,
Chengsong
parents: 518
diff changeset
   977
we can use the previous notation of  natural
Chengsong
parents: 518
diff changeset
   978
extension from derivative w.r.t.~character to derivative
Chengsong
parents: 518
diff changeset
   979
w.r.t.~string:%\comment{simp in  the [] case?}
Chengsong
parents: 518
diff changeset
   980
Chengsong
parents: 518
diff changeset
   981
\begin{center}
Chengsong
parents: 518
diff changeset
   982
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   983
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\
Chengsong
parents: 518
diff changeset
   984
$r \backslash_{simp} [\,] $ & $\dn$ & $r$
Chengsong
parents: 518
diff changeset
   985
\end{tabular}
Chengsong
parents: 518
diff changeset
   986
\end{center}
Chengsong
parents: 518
diff changeset
   987
Chengsong
parents: 518
diff changeset
   988
\noindent
Chengsong
parents: 518
diff changeset
   989
to obtain an optimised version of the algorithm:
Chengsong
parents: 518
diff changeset
   990
Chengsong
parents: 518
diff changeset
   991
 \begin{center}
Chengsong
parents: 518
diff changeset
   992
\begin{tabular}{lcl}
Chengsong
parents: 518
diff changeset
   993
  $\textit{blexer\_simp}\;r\,s$ & $\dn$ &
Chengsong
parents: 518
diff changeset
   994
      $\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\                
Chengsong
parents: 518
diff changeset
   995
  & & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
Chengsong
parents: 518
diff changeset
   996
  & & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
Chengsong
parents: 518
diff changeset
   997
  & & $\;\;\textit{else}\;\textit{None}$
Chengsong
parents: 518
diff changeset
   998
\end{tabular}
Chengsong
parents: 518
diff changeset
   999
\end{center}
Chengsong
parents: 518
diff changeset
  1000
Chengsong
parents: 518
diff changeset
  1001
\noindent
Chengsong
parents: 518
diff changeset
  1002
This algorithm keeps the regular expression size small, for example,
Chengsong
parents: 518
diff changeset
  1003
with this simplification our previous $(a + aa)^*$ example's 8000 nodes
Chengsong
parents: 518
diff changeset
  1004
will be reduced to just 6 and stays constant, no matter how long the
Chengsong
parents: 518
diff changeset
  1005
input string is.
Chengsong
parents: 518
diff changeset
  1006
Chengsong
parents: 518
diff changeset
  1007
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
  1008
500
Chengsong
parents: 468
diff changeset
  1009
Chengsong
parents: 468
diff changeset
  1010
Chengsong
parents: 468
diff changeset
  1011
468
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
  1012
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
  1013
%-----------------------------------
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
  1014
%	SUBSECTION 1
a0f27e21b42c all texrelated
Chengsong
parents:
diff changeset
  1015
%-----------------------------------
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
  1016
\section{Specifications of Certain Functions to be Used}
524
947cbbd4e4a7 more data
Chengsong
parents: 519
diff changeset
  1017
Here we give some functions' definitions, 
947cbbd4e4a7 more data
Chengsong
parents: 519
diff changeset
  1018
which we will use later.
947cbbd4e4a7 more data
Chengsong
parents: 519
diff changeset
  1019
\begin{center}
947cbbd4e4a7 more data
Chengsong
parents: 519
diff changeset
  1020
\begin{tabular}{ccc}
525
d8740017324c fixed latex problems
Christian Urban <christian.urban@kcl.ac.uk>
parents: 524
diff changeset
  1021
$\retrieve \; \ACHAR \, \textit{bs} \, c \; \Char(c) = \textit{bs}$
524
947cbbd4e4a7 more data
Chengsong
parents: 519
diff changeset
  1022
\end{tabular}
947cbbd4e4a7 more data
Chengsong
parents: 519
diff changeset
  1023
\end{center}
500
Chengsong
parents: 468
diff changeset
  1024
Chengsong
parents: 468
diff changeset
  1025
518
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
  1026
ff7945a988a3 more to thesis
Chengsong
parents: 516
diff changeset
  1027