532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
% Main chapter title
|
538
|
4 |
\chapter{Bit-coded Algorithm of Sulzmann and Lu}
|
532
|
5 |
|
|
6 |
\label{Bitcoded1} % Change X to a consecutive number; for referencing this chapter elsewhere, use \ref{ChapterX}
|
|
7 |
%Then we illustrate how the algorithm without bitcodes falls short for such aggressive
|
|
8 |
%simplifications and therefore introduce our version of the bitcoded algorithm and
|
|
9 |
%its correctness proof in
|
|
10 |
%Chapter 3\ref{Chapter3}.
|
538
|
11 |
In this chapter, we are going to introduce the bit-coded algorithm
|
|
12 |
introduced by Sulzmann and Lu to address the problem of
|
|
13 |
under-simplified regular expressions.
|
537
|
14 |
\section{Bit-coded Algorithm}
|
|
15 |
The lexer algorithm in Chapter \ref{Inj}, as shown in \ref{InjFigure},
|
|
16 |
stores information of previous lexing steps
|
|
17 |
on a stack, in the form of regular expressions
|
|
18 |
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc.
|
|
19 |
\begin{envForCaption}
|
|
20 |
\begin{ceqn}
|
|
21 |
\begin{equation}%\label{graph:injLexer}
|
|
22 |
\begin{tikzcd}
|
|
23 |
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] \\
|
|
24 |
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]
|
|
25 |
\end{tikzcd}
|
|
26 |
\end{equation}
|
|
27 |
\end{ceqn}
|
|
28 |
\caption{Injection-based Lexing from Chapter\ref{Inj}}\label{InjFigure}
|
|
29 |
\end{envForCaption}
|
|
30 |
\noindent
|
|
31 |
This is both inefficient and prone to stack overflow.
|
|
32 |
A natural question arises as to whether we can store lexing
|
|
33 |
information on the fly, while still using regular expression
|
|
34 |
derivatives?
|
|
35 |
|
|
36 |
In a lexing algorithm's run, split by the current input position,
|
|
37 |
we have a sub-string that has been consumed,
|
|
38 |
and the sub-string that has yet to come.
|
|
39 |
We already know what was before, and this should be reflected in the value
|
|
40 |
and the regular expression at that step as well. But this is not the
|
|
41 |
case for injection-based regular expression derivatives.
|
|
42 |
Take the regex $(aa)^* \cdot bc$ matching the string $aabc$
|
|
43 |
as an example, if we have just read the two former characters $aa$:
|
532
|
44 |
|
|
45 |
\begin{center}
|
537
|
46 |
\begin{envForCaption}
|
|
47 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
48 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
49 |
{Consumed: $aa$
|
|
50 |
\nodepart{two} Not Yet Reached: $bc$ };
|
|
51 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
52 |
\end{tikzpicture}
|
|
53 |
\caption{Partially matched String}
|
|
54 |
\end{envForCaption}
|
|
55 |
\end{center}
|
|
56 |
%\caption{Input String}\label{StringPartial}
|
|
57 |
%\end{figure}
|
|
58 |
|
|
59 |
\noindent
|
|
60 |
We have the value that has already been partially calculated,
|
|
61 |
and the part that has yet to come:
|
|
62 |
\begin{center}
|
|
63 |
\begin{envForCaption}
|
|
64 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
65 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
66 |
{$\Seq(\Stars[\Char(a), \Char(a)], ???)$
|
|
67 |
\nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
|
|
68 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
69 |
\end{tikzpicture}
|
|
70 |
\caption{Partially constructed Value}
|
|
71 |
\end{envForCaption}
|
|
72 |
\end{center}
|
|
73 |
|
|
74 |
In the regex derivative part , (after simplification)
|
|
75 |
all we have is just what is about to come:
|
|
76 |
\begin{center}
|
|
77 |
\begin{envForCaption}
|
|
78 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
79 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={white!30,blue!20},]
|
|
80 |
{$???$
|
|
81 |
\nodepart{two} To Come: $b c$};
|
|
82 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
83 |
\end{tikzpicture}
|
|
84 |
\caption{Derivative}
|
|
85 |
\end{envForCaption}
|
|
86 |
\end{center}
|
|
87 |
\noindent
|
|
88 |
The previous part is missing.
|
|
89 |
How about keeping the partially constructed value
|
|
90 |
attached to the front of the regular expression?
|
|
91 |
\begin{center}
|
|
92 |
\begin{envForCaption}
|
|
93 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
94 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
95 |
{$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$
|
|
96 |
\nodepart{two} To Come: $b c$};
|
|
97 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
98 |
\end{tikzpicture}
|
|
99 |
\caption{Derivative}
|
|
100 |
\end{envForCaption}
|
|
101 |
\end{center}
|
|
102 |
\noindent
|
|
103 |
If we do this kind of "attachment"
|
|
104 |
and each time augment the attached partially
|
|
105 |
constructed value when taking off a
|
|
106 |
character:
|
|
107 |
\begin{center}
|
|
108 |
\begin{envForCaption}
|
|
109 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
110 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
111 |
{$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$
|
|
112 |
\nodepart{two} To Come: $c$};
|
|
113 |
\end{tikzpicture}\\
|
|
114 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
115 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
116 |
{$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$
|
|
117 |
\nodepart{two} EOF};
|
|
118 |
\end{tikzpicture}
|
|
119 |
\caption{After $\backslash b$ and $\backslash c$}
|
|
120 |
\end{envForCaption}
|
|
121 |
\end{center}
|
|
122 |
\noindent
|
|
123 |
In the end we could recover the value without a backward phase.
|
|
124 |
But (partial) values are a bit clumsy to stick together with a regular expression, so
|
|
125 |
we instead use bit-codes to encode them.
|
|
126 |
|
|
127 |
Bits and bitcodes (lists of bits) are defined as:
|
|
128 |
\begin{envForCaption}
|
|
129 |
\begin{center}
|
|
130 |
$b ::= S \mid Z \qquad
|
532
|
131 |
bs ::= [] \mid b::bs
|
|
132 |
$
|
|
133 |
\end{center}
|
537
|
134 |
\caption{Bit-codes datatype}
|
|
135 |
\end{envForCaption}
|
532
|
136 |
|
|
137 |
\noindent
|
538
|
138 |
Using $S$ and $Z$ rather than $1$ and $0$ is to avoid
|
|
139 |
confusion with the regular expressions $\ZERO$ and $\ONE$.
|
|
140 |
Bitcodes (or
|
532
|
141 |
bit-lists) can be used to encode values (or potentially incomplete values) in a
|
|
142 |
compact form. This can be straightforwardly seen in the following
|
|
143 |
coding function from values to bitcodes:
|
537
|
144 |
\begin{envForCaption}
|
532
|
145 |
\begin{center}
|
|
146 |
\begin{tabular}{lcl}
|
|
147 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\
|
|
148 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
|
537
|
149 |
$\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\
|
|
150 |
$\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\
|
532
|
151 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
|
537
|
152 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\
|
|
153 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\;
|
532
|
154 |
code(\Stars\,vs)$
|
|
155 |
\end{tabular}
|
|
156 |
\end{center}
|
537
|
157 |
\caption{Coding Function for Values}
|
|
158 |
\end{envForCaption}
|
532
|
159 |
|
|
160 |
\noindent
|
537
|
161 |
Here $\textit{code}$ encodes a value into a bit-code by converting
|
|
162 |
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty
|
|
163 |
star iteration by $S$. The border where a local star terminates
|
|
164 |
is marked by $Z$.
|
|
165 |
This coding is lossy, as it throws away the information about
|
532
|
166 |
characters, and also does not encode the ``boundary'' between two
|
|
167 |
sequence values. Moreover, with only the bitcode we cannot even tell
|
537
|
168 |
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$. The
|
532
|
169 |
reason for choosing this compact way of storing information is that the
|
|
170 |
relatively small size of bits can be easily manipulated and ``moved
|
537
|
171 |
around'' in a regular expression.
|
|
172 |
|
|
173 |
|
|
174 |
We define the reverse operation of $\code$, which is $\decode$.
|
|
175 |
As expected, $\decode$ not only requires the bit-codes,
|
|
176 |
but also a regular expression to guide the decoding and
|
|
177 |
fill the gaps of characters:
|
532
|
178 |
|
|
179 |
|
|
180 |
%\begin{definition}[Bitdecoding of Values]\mbox{}
|
537
|
181 |
\begin{envForCaption}
|
532
|
182 |
\begin{center}
|
|
183 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
|
|
184 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
|
|
185 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
|
537
|
186 |
$\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
532
|
187 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
|
|
188 |
(\Left\,v, bs_1)$\\
|
537
|
189 |
$\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
532
|
190 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
|
|
191 |
(\Right\,v, bs_1)$\\
|
|
192 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
|
|
193 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
|
|
194 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
|
|
195 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
|
537
|
196 |
$\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
|
|
197 |
$\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ &
|
532
|
198 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
199 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
|
|
200 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
|
|
201 |
|
|
202 |
$\textit{decode}\,bs\,r$ & $\dn$ &
|
|
203 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
204 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
|
|
205 |
\textit{else}\;\textit{None}$
|
|
206 |
\end{tabular}
|
538
|
207 |
\end{center}
|
537
|
208 |
\end{envForCaption}
|
532
|
209 |
%\end{definition}
|
|
210 |
|
538
|
211 |
\noindent
|
|
212 |
$\decode'$ does most of the job while $\decode$ throws
|
|
213 |
away leftover bit-codes and returns the value only.
|
|
214 |
$\decode$ is terminating as $\decode'$ is terminating.
|
|
215 |
We have the property that $\decode$ and $\code$ are
|
|
216 |
reverse operations of one another:
|
|
217 |
\begin{lemma}
|
|
218 |
\[\vdash v : r \implies \decode \; (\code \; v) \; r = \textit{Some}(v) \]
|
|
219 |
\end{lemma}
|
|
220 |
\begin{proof}
|
|
221 |
By proving a more general version of the lemma, on $\decode'$:
|
|
222 |
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \]
|
|
223 |
Then setting $ds$ to be $[]$ and unfolding $\decode$ definition
|
|
224 |
we get the lemma.
|
|
225 |
\end{proof}
|
|
226 |
With the $\code$ and $\decode$ functions in hand, we know how to
|
|
227 |
switch between bit-codes and value--the two different representations of
|
|
228 |
lexing information.
|
|
229 |
The next step is to integrate this information into the working regular expression.
|
|
230 |
Attaching bits to the front of regular expressions is the solution Sulzamann and Lu
|
|
231 |
gave for storing partial values on the fly:
|
532
|
232 |
|
|
233 |
\begin{center}
|
|
234 |
\begin{tabular}{lcl}
|
|
235 |
$\textit{a}$ & $::=$ & $\ZERO$\\
|
|
236 |
& $\mid$ & $_{bs}\ONE$\\
|
|
237 |
& $\mid$ & $_{bs}{\bf c}$\\
|
|
238 |
& $\mid$ & $_{bs}\sum\,as$\\
|
|
239 |
& $\mid$ & $_{bs}a_1\cdot a_2$\\
|
|
240 |
& $\mid$ & $_{bs}a^*$
|
|
241 |
\end{tabular}
|
|
242 |
\end{center}
|
|
243 |
%(in \textit{ALTS})
|
|
244 |
|
|
245 |
\noindent
|
538
|
246 |
We call these regular expressions carrying bit-codes \emph{Annotated regular expressions}.
|
|
247 |
$bs$ stands for bit-codes, $a$ for $\mathbf{a}$nnotated regular
|
532
|
248 |
expressions and $as$ for a list of annotated regular expressions.
|
538
|
249 |
The alternative constructor ($\sum$) has been generalised to
|
532
|
250 |
accept a list of annotated regular expressions rather than just 2.
|
538
|
251 |
%We will show that these bitcodes encode information about
|
|
252 |
%the ($\POSIX$) value that should be generated by the Sulzmann and Lu
|
|
253 |
%algorithm.
|
|
254 |
The most central question is how these partial lexing information
|
|
255 |
represented as bit-codes is augmented and carried around
|
|
256 |
during a derivative is taken.
|
|
257 |
|
|
258 |
This is done by adding bitcodes to the
|
|
259 |
derivatives, for example when one more star iteratoin is taken (we
|
|
260 |
call the operation of derivatives on annotated regular expressions $\bder$
|
|
261 |
because it is derivatives on regexes with bitcodes):
|
|
262 |
\begin{center}
|
|
263 |
\begin{tabular}{@{}lcl@{}}
|
|
264 |
$\bder \; c\; (_{bs}a^*) $ & $\dn$ &
|
|
265 |
$_{bs}(\textit{fuse}\, [Z] \; \bder \; c \; a)\cdot
|
|
266 |
(_{[]}a^*))$
|
|
267 |
\end{tabular}
|
|
268 |
\end{center}
|
|
269 |
|
|
270 |
\noindent
|
|
271 |
For most time we use the infix notation $\backslash$ to mean $\bder$ for brevity when
|
|
272 |
there is no danger of confusion with derivatives on plain regular expressions,
|
|
273 |
for example, the above can be expressed as
|
|
274 |
\begin{center}
|
|
275 |
\begin{tabular}{@{}lcl@{}}
|
|
276 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
277 |
$_{bs}(\textit{fuse}\, [Z] \; a\,\backslash c)\cdot
|
|
278 |
(_{[]}a^*))$
|
|
279 |
\end{tabular}
|
|
280 |
\end{center}
|
|
281 |
|
|
282 |
|
|
283 |
Using the picture we used earlier to depict this, the transformation when
|
|
284 |
taking a derivative w.r.t a star is like below:
|
|
285 |
\centering
|
|
286 |
\begin{tabular}{@{}l@{\hspace{1mm}}l@{\hspace{0mm}}c@{}}
|
|
287 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
288 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
289 |
{$bs$
|
|
290 |
\nodepart{two} $a^*$ };
|
|
291 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
292 |
\end{tikzpicture}
|
|
293 |
&
|
|
294 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
295 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
296 |
{$v_{\text{previous iterations}}$
|
|
297 |
\nodepart{two} $a^*$};
|
|
298 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
299 |
\end{tikzpicture}
|
|
300 |
\\
|
|
301 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
302 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
303 |
{ $bs$ + [Z]
|
|
304 |
\nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
305 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
306 |
\end{tikzpicture}
|
|
307 |
&
|
|
308 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
309 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
310 |
{$v_{\text{previous iterations}}$ + 1 more iteration
|
|
311 |
\nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
312 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
313 |
\end{tikzpicture}
|
|
314 |
\end{tabular}
|
|
315 |
\noindent
|
|
316 |
The operation $\fuse$ is just to attach bit-codes
|
|
317 |
to the front of an annotated regular expression:
|
|
318 |
\begin{center}
|
|
319 |
\begin{tabular}{lcl}
|
|
320 |
$\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
|
|
321 |
$\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
|
|
322 |
$_{bs @ bs'}\ONE$\\
|
|
323 |
$\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
|
|
324 |
$_{bs@bs'}{\bf c}$\\
|
|
325 |
$\textit{fuse}\;bs\,_{bs'}\sum\textit{as}$ & $\dn$ &
|
|
326 |
$_{bs@bs'}\sum\textit{as}$\\
|
|
327 |
$\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
|
|
328 |
$_{bs@bs'}a_1 \cdot a_2$\\
|
|
329 |
$\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
|
|
330 |
$_{bs @ bs'}a^*$
|
|
331 |
\end{tabular}
|
|
332 |
\end{center}
|
|
333 |
|
|
334 |
\noindent
|
|
335 |
Another place in the $\bder$ function where it differs
|
|
336 |
from normal derivatives on un-annotated regular expressions
|
|
337 |
is the sequence case:
|
|
338 |
\begin{center}
|
|
339 |
\begin{tabular}{@{}lcl@{}}
|
|
340 |
|
|
341 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
342 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
343 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
344 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
345 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$
|
|
346 |
\end{tabular}
|
|
347 |
\end{center}
|
|
348 |
Here
|
|
349 |
|
|
350 |
|
|
351 |
\begin{center}
|
|
352 |
\begin{tabular}{@{}lcl@{}}
|
|
353 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
354 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
355 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
|
|
356 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
357 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\
|
|
358 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ &
|
|
359 |
$_{bs}\sum\;(\textit{map} (\_\backslash c) as )$\\
|
|
360 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
361 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
362 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
363 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
364 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
|
|
365 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
366 |
$_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot
|
|
367 |
(_{[]}r^*))$
|
|
368 |
\end{tabular}
|
|
369 |
\end{center}
|
532
|
370 |
|
|
371 |
|
|
372 |
To do lexing using annotated regular expressions, we shall first
|
|
373 |
transform the usual (un-annotated) regular expressions into annotated
|
|
374 |
regular expressions. This operation is called \emph{internalisation} and
|
|
375 |
defined as follows:
|
|
376 |
|
|
377 |
%\begin{definition}
|
|
378 |
\begin{center}
|
|
379 |
\begin{tabular}{lcl}
|
|
380 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
|
|
381 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
|
|
382 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
|
|
383 |
$(r_1 + r_2)^\uparrow$ & $\dn$ &
|
537
|
384 |
$_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\,
|
|
385 |
\textit{fuse}\,[S]\,r_2^\uparrow]$\\
|
532
|
386 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ &
|
|
387 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
|
|
388 |
$(r^*)^\uparrow$ & $\dn$ &
|
|
389 |
$_{[]}(r^\uparrow)^*$\\
|
|
390 |
\end{tabular}
|
|
391 |
\end{center}
|
|
392 |
%\end{definition}
|
|
393 |
|
|
394 |
\noindent
|
|
395 |
We use up arrows here to indicate that the basic un-annotated regular
|
|
396 |
expressions are ``lifted up'' into something slightly more complex. In the
|
|
397 |
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to
|
|
398 |
attach bits to the front of an annotated regular expression. Its
|
|
399 |
definition is as follows:
|
|
400 |
|
538
|
401 |
|
532
|
402 |
|
|
403 |
\noindent
|
|
404 |
After internalising the regular expression, we perform successive
|
|
405 |
derivative operations on the annotated regular expressions. This
|
|
406 |
derivative operation is the same as what we had previously for the
|
|
407 |
basic regular expressions, except that we beed to take care of
|
|
408 |
the bitcodes:
|
|
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
|
413 |
|
|
414 |
%\end{definition}
|
|
415 |
\noindent
|
|
416 |
For instance, when we do derivative of $_{bs}a^*$ with respect to c,
|
|
417 |
we need to unfold it into a sequence,
|
537
|
418 |
and attach an additional bit $Z$ to the front of $r \backslash c$
|
532
|
419 |
to indicate one more star iteration. Also the sequence clause
|
|
420 |
is more subtle---when $a_1$ is $\textit{bnullable}$ (here
|
|
421 |
\textit{bnullable} is exactly the same as $\textit{nullable}$, except
|
|
422 |
that it is for annotated regular expressions, therefore we omit the
|
|
423 |
definition). Assume that $\textit{bmkeps}$ correctly extracts the bitcode for how
|
|
424 |
$a_1$ matches the string prior to character $c$ (more on this later),
|
|
425 |
then the right branch of alternative, which is $\textit{fuse} \; \bmkeps \; a_1 (a_2
|
|
426 |
\backslash c)$ will collapse the regular expression $a_1$(as it has
|
|
427 |
already been fully matched) and store the parsing information at the
|
|
428 |
head of the regular expression $a_2 \backslash c$ by fusing to it. The
|
|
429 |
bitsequence $\textit{bs}$, which was initially attached to the
|
|
430 |
first element of the sequence $a_1 \cdot a_2$, has
|
|
431 |
now been elevated to the top-level of $\sum$, as this information will be
|
|
432 |
needed whichever way the sequence is matched---no matter whether $c$ belongs
|
|
433 |
to $a_1$ or $ a_2$. After building these derivatives and maintaining all
|
|
434 |
the lexing information, we complete the lexing by collecting the
|
|
435 |
bitcodes using a generalised version of the $\textit{mkeps}$ function
|
|
436 |
for annotated regular expressions, called $\textit{bmkeps}$:
|
|
437 |
|
|
438 |
|
|
439 |
%\begin{definition}[\textit{bmkeps}]\mbox{}
|
|
440 |
\begin{center}
|
|
441 |
\begin{tabular}{lcl}
|
|
442 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
|
|
443 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ &
|
|
444 |
$\textit{if}\;\textit{bnullable}\,a$\\
|
|
445 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
|
|
446 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{bs}\sum \textit{as})$\\
|
|
447 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
|
|
448 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
|
|
449 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
|
537
|
450 |
$bs \,@\, [Z]$
|
532
|
451 |
\end{tabular}
|
|
452 |
\end{center}
|
|
453 |
%\end{definition}
|
|
454 |
|
|
455 |
\noindent
|
|
456 |
This function completes the value information by travelling along the
|
|
457 |
path of the regular expression that corresponds to a POSIX value and
|
|
458 |
collecting all the bitcodes, and using $S$ to indicate the end of star
|
|
459 |
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and
|
|
460 |
decode them, we get the value we expect. The corresponding lexing
|
|
461 |
algorithm looks as follows:
|
|
462 |
|
|
463 |
\begin{center}
|
|
464 |
\begin{tabular}{lcl}
|
|
465 |
$\textit{blexer}\;r\,s$ & $\dn$ &
|
|
466 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\
|
|
467 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
468 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
469 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
470 |
\end{tabular}
|
|
471 |
\end{center}
|
|
472 |
|
|
473 |
\noindent
|
|
474 |
In this definition $\_\backslash s$ is the generalisation of the derivative
|
|
475 |
operation from characters to strings (just like the derivatives for un-annotated
|
|
476 |
regular expressions).
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
%-----------------------------------
|
|
481 |
% SUBSECTION 1
|
|
482 |
%-----------------------------------
|
|
483 |
\section{Specifications of Some Helper Functions}
|
|
484 |
Here we give some functions' definitions,
|
|
485 |
which we will use later.
|
|
486 |
\begin{center}
|
|
487 |
\begin{tabular}{ccc}
|
|
488 |
$\retrieve \; \ACHAR \, \textit{bs} \, c \; \Char(c) = \textit{bs}$
|
|
489 |
\end{tabular}
|
|
490 |
\end{center}
|
|
491 |
|
|
492 |
|
|
493 |
%----------------------------------------------------------------------------------------
|
|
494 |
% SECTION correctness proof
|
|
495 |
%----------------------------------------------------------------------------------------
|
|
496 |
\section{Correctness of Bit-coded Algorithm (Without Simplification)}
|
|
497 |
We now give the proof the correctness of the algorithm with bit-codes.
|
|
498 |
|
|
499 |
Ausaf and Urban cleverly defined an auxiliary function called $\flex$,
|
|
500 |
defined as
|
536
|
501 |
\begin{center}
|
|
502 |
\begin{tabular}{lcr}
|
|
503 |
$\flex \; r \; f \; [] \; v$ & $=$ & $f\; v$\\
|
|
504 |
$\flex \; r \; f \; c :: s \; v$ & $=$ & $\flex \; r \; \lambda v. \, f (\inj \; r\; c\; v)\; s \; v$
|
|
505 |
\end{tabular}
|
|
506 |
\end{center}
|
532
|
507 |
which accumulates the characters that needs to be injected back,
|
|
508 |
and does the injection in a stack-like manner (last taken derivative first injected).
|
|
509 |
$\flex$ is connected to the $\lexer$:
|
|
510 |
\begin{lemma}
|
|
511 |
$\flex \; r \; \textit{id}\; s \; \mkeps (r\backslash s) = \lexer \; r \; s$
|
|
512 |
\end{lemma}
|
|
513 |
$\flex$ provides us a bridge between $\lexer$ and $\blexer$.
|
|
514 |
What is even better about $\flex$ is that it allows us to
|
|
515 |
directly operate on the value $\mkeps (r\backslash v)$,
|
|
516 |
which is pivotal in the definition of $\lexer $ and $\blexer$, but not visible as an argument.
|
|
517 |
When the value created by $\mkeps$ becomes available, one can
|
|
518 |
prove some stepwise properties of lexing nicely:
|
|
519 |
\begin{lemma}\label{flexStepwise}
|
|
520 |
$\textit{flex} \; r \; f \; s@[c] \; v= \flex \; r \; f\; s \; (\inj \; (r\backslash s) \; c \; v) $
|
|
521 |
\end{lemma}
|
|
522 |
|
|
523 |
And for $\blexer$ we have a function with stepwise properties like $\flex$ as well,
|
|
524 |
called $\retrieve$\ref{retrieveDef}.
|
|
525 |
$\retrieve$ takes bit-codes from annotated regular expressions
|
|
526 |
guided by a value.
|
|
527 |
$\retrieve$ is connected to the $\blexer$ in the following way:
|
|
528 |
\begin{lemma}\label{blexer_retrieve}
|
|
529 |
$\blexer \; r \; s = \decode \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$
|
|
530 |
\end{lemma}
|
|
531 |
If you take derivative of an annotated regular expression,
|
|
532 |
you can $\retrieve$ the same bit-codes as before the derivative took place,
|
|
533 |
provided that you use the corresponding value:
|
|
534 |
|
|
535 |
\begin{lemma}\label{retrieveStepwise}
|
|
536 |
$\retrieve \; (r \backslash c) \; v= \retrieve \; r \; (\inj \; r\; c\; v)$
|
|
537 |
\end{lemma}
|
|
538 |
The other good thing about $\retrieve$ is that it can be connected to $\flex$:
|
|
539 |
%centralLemma1
|
|
540 |
\begin{lemma}\label{flex_retrieve}
|
|
541 |
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$
|
|
542 |
\end{lemma}
|
|
543 |
\begin{proof}
|
|
544 |
By induction on $s$. The induction tactic is reverse induction on strings.
|
|
545 |
$v$ is allowed to be arbitrary.
|
|
546 |
The crucial point is to rewrite
|
|
547 |
\[
|
|
548 |
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c])
|
|
549 |
\]
|
|
550 |
as
|
|
551 |
\[
|
|
552 |
\retrieve \; (r \backslash s) \; (\inj \; (r \backslash s) \; c\; \mkeps (r \backslash s@[c]))
|
|
553 |
\].
|
|
554 |
This enables us to equate
|
|
555 |
\[
|
|
556 |
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c])
|
|
557 |
\]
|
|
558 |
with
|
|
559 |
\[
|
|
560 |
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c])))
|
|
561 |
\],
|
|
562 |
which in turn can be rewritten as
|
|
563 |
\[
|
|
564 |
\flex \; r \; \textit{id} \; s@[c] \; (\mkeps (r\backslash s@[c]))
|
|
565 |
\].
|
|
566 |
\end{proof}
|
|
567 |
|
|
568 |
With the above lemma we can now link $\flex$ and $\blexer$.
|
|
569 |
|
|
570 |
\begin{lemma}\label{flex_blexer}
|
|
571 |
$\textit{flex} \; r \; \textit{id} \; s \; \mkeps(r \backslash s) = \blexer \; r \; s$
|
|
572 |
\end{lemma}
|
|
573 |
\begin{proof}
|
|
574 |
Using two of the above lemmas: \ref{flex_retrieve} and \ref{blexer_retrieve}.
|
|
575 |
\end{proof}
|
|
576 |
Finally
|
|
577 |
|
|
578 |
|
|
579 |
|