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}.
|
564
|
11 |
In this chapter, we are going to describe the bit-coded algorithm
|
|
12 |
introduced by Sulzmann and Lu \parencite{Sulzmann2014} to address the growth problem of
|
624
|
13 |
derivatives of
|
564
|
14 |
regular expressions.
|
624
|
15 |
We have implemented their algorithm in Scala and Isabelle,
|
|
16 |
and found problems
|
579
|
17 |
in their algorithm such as de-duplication not working properly and redundant
|
624
|
18 |
fixpoint construction.
|
580
|
19 |
\section{The Motivation Behind Using Bitcodes}
|
624
|
20 |
Let us give again the definition of $\lexer$ from Chapter \ref{Inj}:
|
579
|
21 |
\begin{center}
|
|
22 |
\begin{tabular}{lcl}
|
|
23 |
$\lexer \; r \; [] $ & $=$ & $\textit{if} \; (\nullable \; r)\; \textit{then}\; \Some(\mkeps \; r) \; \textit{else} \; \None$\\
|
|
24 |
$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer \; (r\backslash c) \; s) \;\textit{of}\; $\\
|
|
25 |
& & $\quad \phantom{\mid}\; \None \implies \None$\\
|
|
26 |
& & $\quad \mid \Some(v) \implies \Some(\inj \; r\; c\; v)$
|
|
27 |
\end{tabular}
|
|
28 |
\end{center}
|
|
29 |
\noindent
|
624
|
30 |
The first problem with this algorithm is that
|
|
31 |
for the function $\inj$ to work properly
|
|
32 |
we cannot destroy the structure of a regular expression,
|
638
|
33 |
and therefore cannot simplify too aggressively.
|
624
|
34 |
For example,
|
|
35 |
\[
|
|
36 |
r + (r + a) \rightarrow r + a
|
|
37 |
\]
|
638
|
38 |
cannot be applied because that would require
|
624
|
39 |
breaking up the inner alternative.
|
638
|
40 |
The $\lexer$ plus $\textit{simp}$ therefore only enables
|
624
|
41 |
same-level de-duplications like
|
|
42 |
\[
|
|
43 |
r + r \rightarrow r.
|
|
44 |
\]
|
|
45 |
Secondly, the algorithm recursively calls $\lexer$ on
|
579
|
46 |
each new character input,
|
|
47 |
and before starting a child call
|
|
48 |
it stores information of previous lexing steps
|
|
49 |
on a stack, in the form of regular expressions
|
|
50 |
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc.
|
|
51 |
Each descent into deeper recursive calls in $\lexer$
|
|
52 |
causes a new pair of $r_i, c_i$ to be pushed to the call stack.
|
580
|
53 |
\begin{figure}[H]
|
579
|
54 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
55 |
%\draw (-6,-6) grid (6,6);
|
|
56 |
\node [ circle ] (r) at (-6, 5) {$r$};
|
|
57 |
|
|
58 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
59 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$};
|
|
60 |
%
|
|
61 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
62 |
\node [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
|
|
63 |
|
|
64 |
\node [minimum width = 2cm, rectangle, draw] (stack) at (0, 3) {Stack};
|
|
65 |
|
|
66 |
\path
|
|
67 |
(r)
|
|
68 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
69 |
|
|
70 |
\path (r1)
|
|
71 |
edge [bend right, dashed] node {saved} (stack);
|
|
72 |
\path (c1)
|
|
73 |
edge [bend right, dashed] node {} (stack);
|
|
74 |
|
|
75 |
|
|
76 |
\end{tikzpicture}
|
638
|
77 |
\caption{First derivative taken}
|
579
|
78 |
\end{figure}
|
|
79 |
|
|
80 |
|
|
81 |
|
580
|
82 |
\begin{figure}[H]
|
579
|
83 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
84 |
%\draw (-6,-6) grid (6,6);
|
|
85 |
\node [ circle ] (r) at (-6, 5) {$r$};
|
|
86 |
|
|
87 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
88 |
\node [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$};
|
|
89 |
%
|
|
90 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
91 |
\node [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$};
|
|
92 |
|
|
93 |
|
|
94 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$};
|
|
95 |
%
|
|
96 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
97 |
\node [circle, draw] (r2) at (2, 5) {$r_2$};
|
|
98 |
\node [minimum width = 3cm, minimum height = 1cm, rectangle, draw] (stack) at (0, 2) {\large Stack};
|
|
99 |
|
|
100 |
\path
|
|
101 |
(r)
|
|
102 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
103 |
|
|
104 |
\path (r2)
|
|
105 |
edge [bend right, dashed] node {} (stack);
|
|
106 |
\path (c2)
|
|
107 |
edge [bend right, dashed] node {} (stack);
|
|
108 |
|
|
109 |
\path (r1)
|
|
110 |
edge [] node {} (r2);
|
|
111 |
|
|
112 |
\end{tikzpicture}
|
638
|
113 |
\caption{Second derivative taken}
|
579
|
114 |
\end{figure}
|
|
115 |
\noindent
|
|
116 |
As the number of derivative steps increase,
|
|
117 |
the stack would increase:
|
|
118 |
|
580
|
119 |
\begin{figure}[H]
|
579
|
120 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
121 |
%\draw (-6,-6) grid (6,6);
|
|
122 |
\node [ circle ] (r) at (-6, 5) {$r$};
|
|
123 |
|
|
124 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
125 |
\node [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$};
|
|
126 |
%
|
|
127 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
128 |
\node [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$};
|
|
129 |
|
|
130 |
|
|
131 |
\node [circle, minimum size = 0.1, ] (c2) at (0, 5.4) {$c_2$};
|
|
132 |
%
|
|
133 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
134 |
\node [circle, ] (r2) at (2, 5) {$r_2$};
|
|
135 |
\node [minimum width = 4cm, minimum height = 2.5cm, rectangle, draw] (stack) at (0, 1) { \large Stack};
|
|
136 |
|
|
137 |
\node [] (ldots) at (3.5, 5) {};
|
|
138 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
|
|
139 |
|
|
140 |
\node [minimum size = 0.5, circle, ] (rn) at (6, 5) {};
|
|
141 |
|
|
142 |
\node (rldots) at ($(ldots)!.4!(rn)$) {\ldots};
|
|
143 |
|
|
144 |
\path
|
|
145 |
(r)
|
|
146 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
147 |
|
|
148 |
\path (rldots)
|
|
149 |
edge [bend left, dashed] node {} (stack);
|
|
150 |
|
|
151 |
\path (r1)
|
|
152 |
edge [] node {} (r2);
|
|
153 |
|
|
154 |
\path (r2)
|
|
155 |
edge [] node {} (ldots);
|
|
156 |
\path (ldots)
|
|
157 |
edge [bend left, dashed] node {} (stack);
|
|
158 |
\path (5.03, 4.9)
|
|
159 |
edge [bend left, dashed] node {} (stack);
|
|
160 |
\end{tikzpicture}
|
638
|
161 |
\caption{More derivatives taken}
|
579
|
162 |
\end{figure}
|
580
|
163 |
|
579
|
164 |
|
580
|
165 |
\begin{figure}[H]
|
579
|
166 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
167 |
%\draw (-6,-6) grid (6,6);
|
|
168 |
\node [radius = 0.5, circle, draw] (r) at (-6, 5) {$r$};
|
|
169 |
|
|
170 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
171 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$};
|
|
172 |
%
|
|
173 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
174 |
\node [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
|
|
175 |
%
|
|
176 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$};
|
|
177 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$};
|
|
178 |
%
|
|
179 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
180 |
\node [circle, draw] (r2) at (2, 5) {$r_2$};
|
|
181 |
%
|
|
182 |
%
|
|
183 |
\node [] (ldots) at (4.5, 5) {};
|
|
184 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
|
|
185 |
|
|
186 |
\node [minimum size = 0.5, circle, draw] (rn) at (6, 5) {$r_n$};
|
|
187 |
|
|
188 |
\node at ($(ldots)!.4!(rn)$) {\ldots};
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack};
|
|
194 |
|
|
195 |
\path
|
|
196 |
(r)
|
|
197 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
198 |
\path
|
|
199 |
(r1)
|
|
200 |
edge [] node {} (r2);
|
|
201 |
\path (r2)
|
|
202 |
edge [] node {} (ldots);
|
|
203 |
\path (r)
|
|
204 |
edge [dashed, bend right] node {} (stack);
|
|
205 |
|
|
206 |
\path (r1)
|
|
207 |
edge [dashed, ] node {} (stack);
|
|
208 |
|
|
209 |
\path (c1)
|
|
210 |
edge [dashed, bend right] node {} (stack);
|
|
211 |
|
|
212 |
\path (c2)
|
|
213 |
edge [dashed] node {} (stack);
|
|
214 |
\path (4.5, 5)
|
|
215 |
edge [dashed, bend left] node {} (stack);
|
|
216 |
\path (4.9, 5)
|
|
217 |
edge [dashed, bend left] node {} (stack);
|
|
218 |
\path (5.3, 5)
|
|
219 |
edge [dashed, bend left] node {} (stack);
|
|
220 |
\path (r2)
|
|
221 |
edge [dashed, ] node {} (stack);
|
|
222 |
\path (rn)
|
|
223 |
edge [dashed, bend left] node {} (stack);
|
|
224 |
\end{tikzpicture}
|
638
|
225 |
\caption{Before injection phase starts}
|
580
|
226 |
\end{figure}
|
|
227 |
|
|
228 |
|
|
229 |
\noindent
|
|
230 |
After all derivatives have been taken, the stack grows to a maximum size
|
|
231 |
and the pair of regular expressions and characters $r_i, c_{i+1}$
|
|
232 |
are then popped out and used in the injection phase.
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
\begin{figure}[H]
|
|
237 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
238 |
%\draw (-6,-6) grid (6,6);
|
|
239 |
\node [radius = 0.5, circle, ] (r) at (-6, 5) {$r$};
|
|
240 |
|
|
241 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
242 |
\node [circle, minimum size = 0.1, ] (c1) at (-4, 5.4) {$c_1$};
|
579
|
243 |
%
|
580
|
244 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
245 |
\node [minimum size = 0.5, circle, ] (r1) at (-2, 5) {$r_1$};
|
579
|
246 |
%
|
580
|
247 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$};
|
|
248 |
\node [circle, minimum size = 0.1, ] (c2) at (0, 5.4) {$c_2$};
|
579
|
249 |
%
|
580
|
250 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
251 |
\node [circle, ] (r2) at (2, 5) {$r_2$};
|
579
|
252 |
%
|
|
253 |
%
|
580
|
254 |
\node [] (ldots) at (4.5, 5) {};
|
|
255 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
|
|
256 |
|
|
257 |
\node [minimum size = 0.5, circle, ] (rn) at (6, 5) {$r_n$};
|
|
258 |
|
|
259 |
\node at ($(ldots)!.4!(rn)$) {\ldots};
|
|
260 |
|
|
261 |
\node [minimum size = 0.5, circle, ] (vn) at (6, -5) {$v_n$};
|
|
262 |
|
|
263 |
\node [] (ldots2) at (3.5, -5) {};
|
|
264 |
|
|
265 |
\node [minimum size = 0.5, circle, ] (v2) at (2, -5) {$v_2$};
|
|
266 |
|
|
267 |
\node at ($(ldots2)!.4!(v2)$) {\ldots};
|
|
268 |
|
|
269 |
|
|
270 |
\node [circle, ] (v1) at (-2, -5) {$v_1$};
|
|
271 |
|
|
272 |
\node [radius = 0.5, circle, ] (v) at (-6, -5) {$v$};
|
|
273 |
|
|
274 |
\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack};
|
|
275 |
|
|
276 |
\path
|
|
277 |
(r)
|
|
278 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
279 |
\path
|
|
280 |
(r1)
|
|
281 |
edge [] node {} (r2);
|
|
282 |
\path (r2)
|
|
283 |
edge [] node {} (ldots);
|
|
284 |
\path (rn)
|
|
285 |
edge [] node {$\mkeps$} (vn);
|
|
286 |
\path (vn)
|
|
287 |
edge [] node {} (ldots2);
|
|
288 |
\path (v2)
|
|
289 |
edge [] node {$\inj \; r_1 \; c_2\;v_2$} (v1);
|
|
290 |
|
|
291 |
\path (v1)
|
|
292 |
edge [] node {$\inj \; r \; c_1 \; v_1$} (v);
|
|
293 |
|
|
294 |
\path (stack)
|
|
295 |
edge [dashed] node {} (-4.2, -5.2);
|
|
296 |
\path (stack)
|
|
297 |
edge [dashed] node {} (-4, -5.2);
|
|
298 |
\path (stack)
|
|
299 |
edge [dashed] node {} (-0.1, -5.2);
|
|
300 |
\path (stack)
|
|
301 |
edge [dashed] node {} (0.2, -5.26);
|
|
302 |
\path (stack)
|
|
303 |
edge [dashed] node {} (3.2, -5);
|
|
304 |
\path (stack)
|
|
305 |
edge [dashed] node {} (2.7, -5);
|
|
306 |
\path (stack)
|
|
307 |
edge [dashed] node {} (3.7, -5);
|
|
308 |
\end{tikzpicture}
|
|
309 |
\caption{Stored $r_i, c_{i+1}$ Used by $\inj$}
|
|
310 |
\end{figure}
|
|
311 |
\noindent
|
|
312 |
Storing all $r_i, c_{i+1}$ pairs recursively
|
|
313 |
allows the algorithm to work in an elegant way, at the expense of
|
624
|
314 |
storing quite a bit of verbose information on the stack.
|
|
315 |
The stack seems to grow at least quadratically with respect
|
580
|
316 |
to the input (not taking into account the size bloat of $r_i$),
|
624
|
317 |
which can be inefficient and prone to stack overflows.
|
580
|
318 |
\section{Bitcoded Algorithm}
|
|
319 |
To address this,
|
624
|
320 |
Sulzmann and Lu defined a new datatype
|
580
|
321 |
called \emph{annotated regular expression},
|
|
322 |
which condenses all the partial lexing information
|
|
323 |
(that was originally stored in $r_i, c_{i+1}$ pairs)
|
|
324 |
into bitcodes.
|
581
|
325 |
The bitcodes are then carried with the regular
|
|
326 |
expression, and augmented or moved around
|
638
|
327 |
as the lexing proceeds.
|
581
|
328 |
It becomes unnecessary
|
|
329 |
to remember all the
|
|
330 |
intermediate expresssions, but only the most recent one
|
|
331 |
with this bit-carrying regular expression.
|
580
|
332 |
Annotated regular expressions
|
|
333 |
are defined as the following
|
638
|
334 |
Isabelle datatype:\footnote{ We use subscript notation to indicate
|
580
|
335 |
that the bitcodes are auxiliary information that do not
|
638
|
336 |
interfere with the structure of the regular expressions }
|
580
|
337 |
\begin{center}
|
|
338 |
\begin{tabular}{lcl}
|
|
339 |
$\textit{a}$ & $::=$ & $\ZERO$\\
|
|
340 |
& $\mid$ & $_{bs}\ONE$\\
|
|
341 |
& $\mid$ & $_{bs}{\bf c}$\\
|
|
342 |
& $\mid$ & $_{bs}\sum\,as$\\
|
|
343 |
& $\mid$ & $_{bs}a_1\cdot a_2$\\
|
|
344 |
& $\mid$ & $_{bs}a^*$
|
|
345 |
\end{tabular}
|
|
346 |
\end{center}
|
|
347 |
\noindent
|
|
348 |
where $bs$ stands for bit-codes, $a$ for $\mathbf{a}$nnotated regular
|
|
349 |
expressions and $as$ for lists of annotated regular expressions.
|
638
|
350 |
The alternative constructor, written $\sum$, has been generalised to
|
624
|
351 |
take a list of annotated regular expressions rather than just two.
|
|
352 |
Why is it generalised? This is because when we analyse nested
|
|
353 |
alternatives, there can be more than two elements at the same level
|
580
|
354 |
after de-duplication, which can no longer be stored in a binary
|
|
355 |
constructor.
|
|
356 |
Bits and bitcodes (lists of bits) are defined as:
|
|
357 |
\begin{center}
|
|
358 |
$b ::= S \mid Z \qquad
|
|
359 |
bs ::= [] \mid b::bs
|
|
360 |
$
|
|
361 |
\end{center}
|
|
362 |
\noindent
|
|
363 |
We use $S$ and $Z$ rather than $1$ and $0$ is to avoid
|
|
364 |
confusion with the regular expressions $\ZERO$ and $\ONE$.
|
624
|
365 |
The idea is to use the bitcodes
|
580
|
366 |
to indicate which choice was made at a certain point
|
|
367 |
during lexing.
|
|
368 |
For example,
|
|
369 |
$(_{Z}a+_{S}b) \backslash a$ gives us
|
624
|
370 |
$_{Z}\ONE + \ZERO$, where the $Z$ bitcode will
|
580
|
371 |
later be used to decode that a left branch was
|
638
|
372 |
selected at the time when the part $a+b$ was analysed by
|
624
|
373 |
derivative.
|
580
|
374 |
\subsection{A Bird's Eye View of the Bit-coded Lexer}
|
624
|
375 |
Before we give the details of the functions and definitions
|
580
|
376 |
related to our
|
|
377 |
$\blexer$ (\emph{b}-itcoded lexer), we first provide a high-level
|
624
|
378 |
view of the algorithm.
|
580
|
379 |
\begin{figure}[H]
|
|
380 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
381 |
%\draw (-6,-6) grid (6,6);
|
|
382 |
|
|
383 |
\node [circle, draw] (r0) at (-6, 2) {$r$};
|
|
384 |
|
|
385 |
\node [radius = 0.5, circle, draw] (r) at (-6, 5) {$_{bs}a$};
|
|
386 |
\path (r0)
|
|
387 |
edge [] node {$\internalise$} (r);
|
|
388 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
389 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$};
|
579
|
390 |
%
|
580
|
391 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
392 |
\node [minimum size = 1.0cm, circle, draw] (r1) at (-2, 5) {$_{bs_1}a_1$};
|
|
393 |
%
|
|
394 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$};
|
|
395 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$};
|
|
396 |
%
|
|
397 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
398 |
\node [circle, draw, minimum size = 1.4cm] (r2) at (2, 5) {$_{bs_2}a_2$};
|
|
399 |
%
|
|
400 |
%
|
|
401 |
\node [] (ldots) at (4.5, 5) {};
|
|
402 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
|
|
403 |
|
|
404 |
\node [minimum size = 2.2cm, circle, draw] (rn) at (6, 5) {$_{bs_n}a_n$};
|
|
405 |
|
|
406 |
\node at ($(ldots)!.1!(rn)$) {\ldots};
|
|
407 |
|
|
408 |
\node [minimum size = 0.5, circle, ] (v) at (6, 2) {$v$};
|
|
409 |
|
|
410 |
%\node [] (v2) at (4, -5) {};
|
|
411 |
%
|
|
412 |
%\node [draw, cross out] (ldots2) at (5, -5) {};
|
579
|
413 |
%
|
580
|
414 |
%\node at ($(ldots2)!.4!(v2)$) {\ldots};
|
|
415 |
|
|
416 |
|
|
417 |
\node [align = center] (decode) at (6.6, 3.2) {$\bmkeps$\\$\decode$};
|
|
418 |
|
|
419 |
\path (c1)
|
|
420 |
edge [dashed, bend left] node {} (r0);
|
|
421 |
|
|
422 |
\path (c2)
|
|
423 |
edge [dashed, bend left] node {} (r0);
|
|
424 |
|
|
425 |
\path (r1)
|
|
426 |
edge [dashed, bend right] node {} (r2);
|
|
427 |
|
|
428 |
|
|
429 |
\path
|
|
430 |
(r)
|
|
431 |
edge [dashed, bend right] node[left] {} (r1);
|
|
432 |
|
|
433 |
\path
|
|
434 |
(r)
|
|
435 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
436 |
|
|
437 |
\path
|
|
438 |
(r1)
|
|
439 |
edge [] node {} (r2);
|
|
440 |
\path (r2)
|
|
441 |
edge [] node {} (ldots);
|
|
442 |
\path (rn)
|
|
443 |
edge [] node {} (v);
|
|
444 |
|
|
445 |
\path (r0)
|
|
446 |
edge [dashed, bend right] node {} (decode);
|
|
447 |
%\path (v)
|
|
448 |
%edge [] node {} (ldots2);
|
579
|
449 |
|
|
450 |
|
|
451 |
|
580
|
452 |
\end{tikzpicture}
|
624
|
453 |
\caption{A bird's eye view of $\blexer$. The $_{bsi}a_{i}$s stand
|
|
454 |
for the annotated regular expressions in each derivative step.
|
|
455 |
The characters used in each derivative is written as $c_i$.
|
638
|
456 |
The size of the derivatives typically increase during each derivative step,
|
|
457 |
therefore we draw the larger circles to indicate that.
|
|
458 |
The process starts with an internalise phase and concludes with a decoding phase.}
|
580
|
459 |
\end{figure}
|
|
460 |
\noindent
|
|
461 |
The plain regular expressions
|
|
462 |
are first ``lifted'' to an annotated regular expression,
|
624
|
463 |
with the function called \emph{internalise} ($r \rightarrow _{bs}a$).
|
580
|
464 |
Then the annotated regular expression $_{bs}a$ will
|
624
|
465 |
be transformed by successive derivatives with respect
|
580
|
466 |
to the input stream of characters $c_1, c_2$ etc.
|
|
467 |
Each time a derivative is taken, the bitcodes
|
|
468 |
are moved around, discarded or augmented together
|
|
469 |
with the regular expression they are attached to.
|
|
470 |
After all input has been consumed, the
|
|
471 |
bitcodes are collected by $\bmkeps$,
|
|
472 |
which traverses the nullable part of the regular expression
|
624
|
473 |
and collects the bitcodes along the way.
|
580
|
474 |
The collected bitcodes are then $\decode$d with the guidance
|
624
|
475 |
of the input regular expression $r$ ( $_{bs}a \rightarrow r$).
|
580
|
476 |
The most notable improvements of $\blexer$
|
|
477 |
over $\lexer$ are
|
|
478 |
\begin{itemize}
|
|
479 |
\item
|
|
480 |
|
638
|
481 |
the absence of the second injection phase.
|
580
|
482 |
\item
|
638
|
483 |
one does not need to store each pair of the
|
580
|
484 |
intermediate regular expressions $_{bs_i}a_i$ and $c_{i+1}$.
|
|
485 |
The previous annotated regular expression $_{bs_i}a_i$'s information is passed
|
|
486 |
on without loss to its successor $_{bs_{i+1}}a_{i+1}$,
|
624
|
487 |
and $c_{i+1}$'s information is stored in $L\;r$.\footnote{
|
|
488 |
which will be used during the decode phase, where we use $r$ as
|
|
489 |
a source of information.}
|
580
|
490 |
\item
|
638
|
491 |
simplification works much smoother--one can maintain correctness
|
624
|
492 |
while applying quite aggressive simplifications.
|
580
|
493 |
\end{itemize}
|
624
|
494 |
%In the next section we will
|
|
495 |
%give the operations needed in $\blexer$,
|
|
496 |
%with some remarks on the idea behind their definitions.
|
580
|
497 |
\subsection{Operations in $\textit{Blexer}$}
|
|
498 |
The first operation we define related to bit-coded regular expressions
|
|
499 |
is how we move bits to the inside of regular expressions.
|
624
|
500 |
This operation is called $\fuse$:
|
580
|
501 |
\begin{center}
|
|
502 |
\begin{tabular}{lcl}
|
624
|
503 |
$\textit{fuse}\;bs \; (\ZERO)$ & $\dn$ & $\ZERO$\\
|
|
504 |
$\textit{fuse}\;bs\; (_{bs'})\ONE$ & $\dn$ &
|
580
|
505 |
$_{bs @ bs'}\ONE$\\
|
624
|
506 |
$\textit{fuse}\;bs\;(_{bs'}{\bf c})$ & $\dn$ &
|
580
|
507 |
$_{bs@bs'}{\bf c}$\\
|
624
|
508 |
$\textit{fuse}\;bs\;(_{bs'}\sum\textit{as})$ & $\dn$ &
|
580
|
509 |
$_{bs@bs'}\sum\textit{as}$\\
|
624
|
510 |
$\textit{fuse}\;bs\; (_{bs'}a_1\cdot a_2)$ & $\dn$ &
|
580
|
511 |
$_{bs@bs'}a_1 \cdot a_2$\\
|
624
|
512 |
$\textit{fuse}\;bs\;(_{bs'}a^*)$ & $\dn$ &
|
580
|
513 |
$_{bs @ bs'}a^*$
|
|
514 |
\end{tabular}
|
|
515 |
\end{center}
|
|
516 |
|
|
517 |
\noindent
|
624
|
518 |
With \emph{fuse} we are able to define the $\internalise$ function,
|
|
519 |
written $(\_)^\uparrow$,
|
580
|
520 |
that translates a ``standard'' regular expression into an
|
|
521 |
annotated regular expression.
|
|
522 |
This function will be applied before we start
|
|
523 |
with the derivative phase of the algorithm.
|
|
524 |
|
|
525 |
\begin{center}
|
|
526 |
\begin{tabular}{lcl}
|
|
527 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
|
|
528 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
|
|
529 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
|
|
530 |
$(r_1 + r_2)^\uparrow$ & $\dn$ &
|
|
531 |
$_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\,
|
|
532 |
\textit{fuse}\,[S]\,r_2^\uparrow]$\\
|
|
533 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ &
|
|
534 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
|
|
535 |
$(r^*)^\uparrow$ & $\dn$ &
|
|
536 |
$_{[]}(r^\uparrow)^*$\\
|
|
537 |
\end{tabular}
|
|
538 |
\end{center}
|
|
539 |
\noindent
|
|
540 |
The opposite of $\textit{internalise}$ is
|
624
|
541 |
$\erase$, where all bit-codes are removed,
|
580
|
542 |
and the alternative operator $\sum$ for annotated
|
|
543 |
regular expressions is transformed to the binary version
|
624
|
544 |
in (plain) regular expressions. This can be defined as follows:
|
|
545 |
\begin{center}\label{eraseDef}
|
580
|
546 |
\begin{tabular}{lcl}
|
|
547 |
$\ZERO_\downarrow$ & $\dn$ & $\ZERO$\\
|
|
548 |
$( _{bs}\ONE )_\downarrow$ & $\dn$ & $\ONE$\\
|
|
549 |
$( _{bs}\mathbf{c} )_\downarrow$ & $\dn$ & $\mathbf{c}$\\
|
|
550 |
$( _{bs} a_1 \cdot a_2 )_\downarrow$ & $\dn$ &
|
|
551 |
$ (a_1) _\downarrow \cdot (a_2) _\downarrow$\\
|
|
552 |
$( _{bs} [])_\downarrow $ & $\dn$ & $\ZERO $\\
|
|
553 |
$( _{bs} [a] )_\downarrow$ & $\dn$ & $a_\downarrow$\\
|
624
|
554 |
$(_{bs} \sum [a_1, \; a_2])_\downarrow$ & $\dn$ & $ (a_1) _\downarrow + ( a_2 ) _\downarrow $\\
|
580
|
555 |
$(_{bs} \sum (a :: as))_\downarrow$ & $\dn$ & $ a_\downarrow + \; (_{[]} \sum as)_\downarrow$\\
|
|
556 |
$( _{bs} a^* )_\downarrow$ & $\dn$ & $(a_\downarrow)^*$
|
|
557 |
\end{tabular}
|
|
558 |
\end{center}
|
|
559 |
\noindent
|
638
|
560 |
Where we abbreviate the $\erase\; a$ operation
|
|
561 |
as $(a)_\downarrow$, for conciseness.
|
580
|
562 |
|
624
|
563 |
Determining whether an annotated regular expression
|
638
|
564 |
contains the empty string in its language requires
|
580
|
565 |
a dedicated function $\bnullable$.
|
624
|
566 |
In our formalisation this function is defined by simply calling $\erase$
|
|
567 |
before $\nullable$.
|
580
|
568 |
\begin{definition}
|
|
569 |
$\bnullable \; a \dn \nullable \; (a_\downarrow)$
|
|
570 |
\end{definition}
|
|
571 |
The function for collecting
|
|
572 |
bitcodes from a
|
|
573 |
(b)nullable regular expression is called $\bmkeps$.
|
624
|
574 |
This function is a lifted version of the $\textit{mkeps}$ function,
|
580
|
575 |
which follows the path $\mkeps$ takes to traverse the
|
|
576 |
$\nullable$ branches when visiting a regular expression,
|
624
|
577 |
but collects bitcodes instead of generating a value.
|
580
|
578 |
\begin{center}
|
|
579 |
\begin{tabular}{lcl}
|
|
580 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
|
|
581 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ &
|
|
582 |
$\textit{if}\;\textit{bnullable}\,a$\\
|
|
583 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
|
|
584 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{[]}\sum \textit{as})$\\
|
|
585 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
|
|
586 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
|
|
587 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
|
585
|
588 |
$bs \,@\, [S]$
|
580
|
589 |
\end{tabular}
|
|
590 |
\end{center}
|
|
591 |
\noindent
|
624
|
592 |
This function, just like $\mkeps$,
|
581
|
593 |
visits a regular expression tree respecting
|
624
|
594 |
the POSIX rules.
|
581
|
595 |
It traverses each child of the sequence regular expression
|
|
596 |
from left to right and creates a bitcode by stitching
|
|
597 |
together bitcodes obtained from the children expressions.
|
|
598 |
In the case of alternative regular expressions,
|
|
599 |
it looks for the leftmost
|
|
600 |
$\nullable$ branch
|
638
|
601 |
to visit and ignores the other siblings.
|
581
|
602 |
%Whenever there is some bitcodes attached to a
|
|
603 |
%node, it returns the bitcodes concatenated with whatever
|
|
604 |
%child recursive calls return.
|
|
605 |
The only time when $\bmkeps$ creates new bitcodes
|
|
606 |
is when it completes a star's iterations by attaching a $S$ to the end of the bitcode
|
624
|
607 |
list it returns.
|
|
608 |
|
580
|
609 |
The bitcodes extracted by $\bmkeps$ need to be
|
638
|
610 |
$\decode$d (with the guidance of a ``plain'' regular expression):
|
580
|
611 |
%\begin{definition}[Bitdecoding of Values]\mbox{}
|
|
612 |
\begin{center}
|
|
613 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
|
|
614 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
|
|
615 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
|
|
616 |
$\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
|
617 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
|
|
618 |
(\Left\,v, bs_1)$\\
|
|
619 |
$\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
|
620 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
|
|
621 |
(\Right\,v, bs_1)$\\
|
|
622 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
|
|
623 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
|
|
624 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
|
|
625 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
|
585
|
626 |
$\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
|
|
627 |
$\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ &
|
580
|
628 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
629 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
|
|
630 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
|
|
631 |
|
|
632 |
$\textit{decode}\,bs\,r$ & $\dn$ &
|
|
633 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
634 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
|
|
635 |
\textit{else}\;\textit{None}$
|
|
636 |
\end{tabular}
|
|
637 |
\end{center}
|
537
|
638 |
\noindent
|
580
|
639 |
The function $\decode'$ returns a pair consisting of
|
624
|
640 |
a partially decoded value and some leftover bit-list.
|
580
|
641 |
The function $\decode'$ succeeds if the left-over
|
|
642 |
bit-sequence is empty.
|
624
|
643 |
|
|
644 |
%$\decode$ is terminating as $\decode'$ is terminating.
|
|
645 |
%$\decode'$ is terminating
|
|
646 |
%because at least one of $\decode'$'s parameters will go down in terms
|
|
647 |
%of size.\\
|
580
|
648 |
The reverse operation of $\decode$ is $\code$.
|
624
|
649 |
This function encodes a value into a bitcode by converting
|
580
|
650 |
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty
|
|
651 |
star iteration by $S$. The border where a star iteration
|
|
652 |
terminates is marked by $Z$.
|
|
653 |
This coding is lossy, as it throws away the information about
|
|
654 |
characters, and does not encode the ``boundary'' between two
|
|
655 |
sequence values. Moreover, with only the bitcode we cannot even tell
|
624
|
656 |
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$,
|
638
|
657 |
but this will not be necessary in our correctness proof.
|
580
|
658 |
\begin{center}
|
|
659 |
\begin{tabular}{lcl}
|
|
660 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\
|
|
661 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
|
|
662 |
$\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\
|
|
663 |
$\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\
|
|
664 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
|
|
665 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\
|
|
666 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\;
|
|
667 |
code(\Stars\,vs)$
|
|
668 |
\end{tabular}
|
|
669 |
\end{center}
|
|
670 |
\noindent
|
624
|
671 |
We can prove that given a value $v$ and regular expression $r$
|
580
|
672 |
with $\vdash v:r$,
|
|
673 |
then we have the property that $\decode$ and $\code$ are
|
|
674 |
reverse operations of one another:
|
|
675 |
\begin{lemma}
|
|
676 |
\[If \vdash v : r \; then \;\decode \; (\code \; v) \; r = \textit{Some}(v) \]
|
|
677 |
\end{lemma}
|
|
678 |
\begin{proof}
|
|
679 |
By proving a more general version of the lemma, on $\decode'$:
|
|
680 |
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \]
|
|
681 |
Then setting $ds$ to be $[]$ and unfolding $\decode$ definition,
|
|
682 |
we obtain the property.
|
|
683 |
\end{proof}
|
|
684 |
\noindent
|
638
|
685 |
Now we define the central part of Sulzmann and Lu's second lexing algorithm,
|
624
|
686 |
the $\bder$ function (which stands for \emph{b}itcoded-derivative):
|
580
|
687 |
\begin{center}
|
|
688 |
\begin{tabular}{@{}lcl@{}}
|
|
689 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
690 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
691 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
|
|
692 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
693 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\
|
|
694 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ &
|
|
695 |
$_{bs}\sum\;(\textit{map} \; (\_\backslash c) \; as )$\\
|
|
696 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
697 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
698 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
699 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
700 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
|
|
701 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
702 |
$_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot
|
|
703 |
(_{[]}r^*))$
|
|
704 |
\end{tabular}
|
|
705 |
\end{center}
|
|
706 |
\noindent
|
639
|
707 |
For $\bder \; c\; a$, we use the infix notation $a\backslash c$ for readability.
|
624
|
708 |
The $\bder$ function tells us how regular expressions can be recursively traversed,
|
580
|
709 |
where the bitcodes are augmented and carried around
|
|
710 |
when a derivative is taken.
|
|
711 |
We give the intuition behind some of the more involved cases in
|
638
|
712 |
$\bder$.
|
|
713 |
|
580
|
714 |
For example,
|
|
715 |
in the \emph{star} case,
|
624
|
716 |
a derivative of $_{bs}a^*$ means
|
|
717 |
that one more star iteration needs to be taken.
|
|
718 |
We therefore need to unfold it into a sequence,
|
|
719 |
and attach an additional bit $Z$ to the front of $a \backslash c$
|
580
|
720 |
as a record to indicate one new star iteration is unfolded.
|
|
721 |
|
|
722 |
\noindent
|
|
723 |
\begin{center}
|
|
724 |
\begin{tabular}{@{}lcl@{}}
|
|
725 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
726 |
$_{bs}(\underbrace{\textit{fuse}\, [Z] \; a\,\backslash c}_{\text{One more iteration}})\cdot
|
|
727 |
(_{[]}a^*))$
|
|
728 |
\end{tabular}
|
|
729 |
\end{center}
|
|
730 |
|
|
731 |
\noindent
|
|
732 |
This information will be recovered later by the $\decode$ function.
|
624
|
733 |
%The intuition is that the bit $Z$ will be decoded at the right location,
|
|
734 |
%because we accumulate bits from left to right (a rigorous proof will be given
|
|
735 |
%later).
|
580
|
736 |
|
|
737 |
%\begin{tikzpicture}[ > = stealth, % arrow head style
|
|
738 |
% shorten > = 1pt, % don't touch arrow head to node
|
|
739 |
% semithick % line style
|
|
740 |
% ]
|
|
741 |
%
|
|
742 |
% \tikzstyle{every state}=[
|
|
743 |
% draw = black,
|
|
744 |
% thin,
|
|
745 |
% fill = cyan!29,
|
|
746 |
% minimum size = 7mm
|
|
747 |
% ]
|
|
748 |
% \begin{scope}[node distance=1cm and 0cm, every node/.style=state]
|
|
749 |
% \node (k) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
750 |
% {$bs$
|
|
751 |
% \nodepart{two} $a^*$ };
|
|
752 |
% \node (l) [below =of k, rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
753 |
% { $bs$ + [Z]
|
|
754 |
% \nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
755 |
% \end{scope}
|
|
756 |
% \path[->]
|
|
757 |
% (k) edge (l);
|
|
758 |
%\end{tikzpicture}
|
|
759 |
%
|
|
760 |
%Pictorially the process looks like below.
|
|
761 |
%Like before, the red region denotes
|
|
762 |
%previous lexing information (stored as bitcodes in $bs$).
|
|
763 |
|
|
764 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
765 |
% \begin{scope}[node distance=1cm]
|
|
766 |
% \node (a) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
767 |
% {$bs$
|
|
768 |
% \nodepart{two} $a^*$ };
|
|
769 |
% \node (b) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
770 |
% { $bs$ + [Z]
|
|
771 |
% \nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
772 |
%%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
773 |
% \end{scope}
|
|
774 |
%\end{tikzpicture}
|
|
775 |
|
|
776 |
\noindent
|
|
777 |
Another place the $\bder$ function differs
|
624
|
778 |
from derivatives on regular expressions
|
580
|
779 |
is the sequence case:
|
|
780 |
\begin{center}
|
|
781 |
\begin{tabular}{@{}lcl@{}}
|
|
782 |
|
|
783 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
784 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
785 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
786 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
787 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$
|
|
788 |
\end{tabular}
|
|
789 |
\end{center}
|
|
790 |
\noindent
|
624
|
791 |
%We assume that $\bmkeps$
|
|
792 |
%correctly extracts the bitcode for how $a_1$
|
|
793 |
%matches the string prior to $c$ (more on this later).
|
|
794 |
%The bitsequence $\textit{bs}$ which was initially
|
|
795 |
%attached to the first element of the sequence
|
|
796 |
%$a_1 \cdot a_2$, has now been
|
|
797 |
%elevated to the top level of the $\sum$ constructor.
|
|
798 |
%This is because this piece of information will be needed
|
|
799 |
%whichever way the sequence is matched,
|
|
800 |
%regardless of whether $c$ belongs to $a_1$ or $a_2$.
|
|
801 |
The difference mainly lies in the $\textit{if}$ branch (when $a_1$ is $\bnullable$):
|
580
|
802 |
we use $\bmkeps$ to store the lexing information
|
|
803 |
in $a_1$ before collapsing
|
|
804 |
it (as it has been fully matched by string prior to $c$),
|
|
805 |
and attach the collected bit-codes to the front of $a_2$
|
624
|
806 |
before throwing away $a_1$.
|
580
|
807 |
In the injection-based lexer, $r_1$ is immediately thrown away in
|
638
|
808 |
the $\textit{if}$ branch,
|
624
|
809 |
the information $r_1$ stores is therefore lost:
|
580
|
810 |
\begin{center}
|
624
|
811 |
\begin{tabular}{lcl}
|
|
812 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\
|
|
813 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
|
|
814 |
& & $\mathit{else}\; \ldots$\\
|
|
815 |
\end{tabular}
|
580
|
816 |
\end{center}
|
|
817 |
\noindent
|
624
|
818 |
|
|
819 |
%\noindent
|
|
820 |
%this loss of information makes it necessary
|
|
821 |
%to store on stack all the regular expressions'
|
|
822 |
%``snapshots'' before they were
|
|
823 |
%taken derivative of.
|
|
824 |
%So that the related information will be available
|
|
825 |
%once the child recursive
|
|
826 |
%call finishes.\\
|
580
|
827 |
The rest of the clauses of $\bder$ is rather similar to
|
|
828 |
$\der$. \\
|
|
829 |
Generalising the derivative operation with bitcodes to strings, we have
|
|
830 |
\begin{center}
|
|
831 |
\begin{tabular}{@{}lcl@{}}
|
|
832 |
$a\backslash_s [] $ & $\dn$ & $a$\\
|
|
833 |
$a\backslash (c :: s) $ & $\dn$ & $(a \backslash c) \backslash_s s$
|
|
834 |
\end{tabular}
|
|
835 |
\end{center}
|
|
836 |
\noindent
|
|
837 |
As we did earlier, we omit the $s$ subscript at $\backslash_s$ when there is no danger
|
|
838 |
of confusion.
|
|
839 |
\subsection{Putting Things Together}
|
|
840 |
Putting these operations altogether, we obtain a lexer with bit-coded regular expressions
|
|
841 |
as its internal data structures, which we call $\blexer$:
|
|
842 |
|
|
843 |
\begin{center}
|
|
844 |
\begin{tabular}{lcl}
|
|
845 |
$\textit{blexer}\;r\,s$ & $\dn$ &
|
|
846 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\
|
|
847 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
848 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
849 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
850 |
\end{tabular}
|
|
851 |
\end{center}
|
|
852 |
\noindent
|
624
|
853 |
This function first attaches bitcodes to a
|
|
854 |
plain regular expression $r$, and then builds successive derivatives
|
581
|
855 |
with respect to the input string $s$, and
|
624
|
856 |
then test whether the result is (b)nullable.
|
|
857 |
If yes, then extract the bitcodes from the nullable expression,
|
581
|
858 |
and decodes the bitcodes into a lexical value.
|
|
859 |
If there does not exists a match between $r$ and $s$ the lexer
|
638
|
860 |
outputs $\None$ indicating a mismatch.
|
580
|
861 |
Ausaf and Urban formally proved the correctness of the $\blexer$, namely
|
|
862 |
\begin{property}
|
|
863 |
$\blexer \;r \; s = \lexer \; r \; s$.
|
|
864 |
\end{property}
|
581
|
865 |
\noindent
|
580
|
866 |
This was claimed but not formalised in Sulzmann and Lu's work.
|
|
867 |
We introduce the proof later, after we give all
|
|
868 |
the needed auxiliary functions and definitions.
|
581
|
869 |
\subsection{An Example $\blexer$ Run}
|
624
|
870 |
Before introducing the proof we shall first walk
|
580
|
871 |
through a concrete example of our $\blexer$ calculating the right
|
638
|
872 |
lexical information through bit-coded regular expressions.
|
|
873 |
|
581
|
874 |
Consider the regular expression $(aa)^* \cdot (b+c)$ matching the string $aab$.
|
|
875 |
We give again the bird's eye view of this particular example
|
|
876 |
in each stage of the algorithm:
|
|
877 |
|
|
878 |
\tikzset{three sided/.style={
|
|
879 |
draw=none,
|
|
880 |
append after command={
|
|
881 |
[-,shorten <= -0.5\pgflinewidth]
|
|
882 |
([shift={(-1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east)
|
|
883 |
edge([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west)
|
|
884 |
([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west)
|
|
885 |
edge([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
|
|
886 |
([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
|
|
887 |
edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
|
|
888 |
}
|
|
889 |
}
|
|
890 |
}
|
|
891 |
|
|
892 |
\tikzset{three sided1/.style={
|
|
893 |
draw=none,
|
|
894 |
append after command={
|
|
895 |
[-,shorten <= -0.5\pgflinewidth]
|
|
896 |
([shift={(1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west)
|
|
897 |
edge([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east)
|
|
898 |
([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east)
|
|
899 |
edge([shift={(-0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
|
|
900 |
([shift={(-0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east)
|
|
901 |
edge([shift={(1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west)
|
|
902 |
}
|
|
903 |
}
|
|
904 |
}
|
|
905 |
|
|
906 |
\begin{figure}[H]
|
|
907 |
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, auto, thick]
|
|
908 |
\node [rectangle, draw] (r) at (-6, -1) {$(aa)^*(b+c)$};
|
|
909 |
\node [rectangle, draw] (a) at (-6, 4) {$(aa)^*(_{Z}b + _{S}c)$};
|
|
910 |
\path (r)
|
|
911 |
edge [] node {$\internalise$} (a);
|
|
912 |
\node [rectangle, draw] (a1) at (-3, 1) {$(_{Z}(\ONE \cdot a) \cdot (aa)^*) (_{Z}b + _Sc)$};
|
|
913 |
\path (a)
|
|
914 |
edge [] node {$\backslash a$} (a1);
|
|
915 |
|
|
916 |
\node [rectangle, draw, three sided] (a21) at (-2.5, 4) {$(_{Z}\ONE \cdot (aa)^*)$};
|
|
917 |
\node [rectangle, draw, three sided1] (a22) at (-0.8, 4) {$(_{Z}b + _{S}c)$};
|
|
918 |
\path (a1)
|
|
919 |
edge [] node {$\backslash a$} (a21);
|
|
920 |
\node [rectangle, draw] (a3) at (0.5, 2) {$_{ZS}(_{Z}\ONE + \ZERO)$};
|
|
921 |
\path (a22)
|
|
922 |
edge [] node {$\backslash b$} (a3);
|
|
923 |
\path (a21)
|
|
924 |
edge [dashed, bend right] node {} (a3);
|
|
925 |
\node [rectangle, draw] (bs) at (2, 4) {$ZSZ$};
|
|
926 |
\path (a3)
|
|
927 |
edge [below] node {$\bmkeps$} (bs);
|
|
928 |
\node [rectangle, draw] (v) at (3, 0) {$\Seq \; (\Stars\; [\Seq \; a \; a]) \; (\Left \; b)$};
|
|
929 |
\path (bs)
|
|
930 |
edge [] node {$\decode$} (v);
|
|
931 |
|
|
932 |
|
|
933 |
\end{tikzpicture}
|
638
|
934 |
\caption{$\blexer$ with the regular expression $(aa)^*(b+c)$ and $aab$}
|
581
|
935 |
\end{figure}
|
537
|
936 |
\noindent
|
581
|
937 |
The one dashed arrow indicates that $_Z(\ONE \cdot (aa)^*)$
|
|
938 |
turned into $ZS$ after derivative w.r.t $b$
|
638
|
939 |
is taken, which calls $\bmkeps$ on the nullable $_Z\ONE\cdot (aa)^*$
|
|
940 |
before processing $_Zb+_Sc$.
|
|
941 |
|
581
|
942 |
The annotated regular expressions
|
624
|
943 |
would look overwhelming if we explicitly indicate all the
|
581
|
944 |
locations where bitcodes are attached.
|
|
945 |
For example,
|
|
946 |
$(aa)^*\cdot (b+c)$ would
|
|
947 |
look like $_{[]}(_{[]}(_{[]}a \cdot _{[]}a)^*\cdot _{[]}(_{[]}b+_{[]}c))$
|
|
948 |
after
|
|
949 |
internalise.
|
|
950 |
Therefore for readability we omit bitcodes if they are empty.
|
624
|
951 |
This applies to all annotated
|
638
|
952 |
regular expressions in this thesis.
|
|
953 |
|
581
|
954 |
%and assume we have just read the first character $a$:
|
|
955 |
%\begin{center}
|
|
956 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
957 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
958 |
% {$(_{[Z]}(\ONE \cdot a) \cdot (aa)^* )\cdot bc$
|
|
959 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq\; a \; ??, \;??]) \; ??$};
|
|
960 |
%\end{tikzpicture}
|
|
961 |
%\end{center}
|
|
962 |
%\noindent
|
|
963 |
%We use the red area for (annotated) regular expressions and the blue
|
|
964 |
%area the (partially calculated) bitcodes
|
|
965 |
%and its corresponding (partial) values.
|
|
966 |
%The first derivative
|
|
967 |
%generates a $Z$ bitcode to indicate
|
|
968 |
%a new iteration has been started.
|
|
969 |
%This bitcode is attached to the front of
|
|
970 |
%the unrolled star iteration $\ONE\cdot a$
|
|
971 |
%for later decoding.
|
|
972 |
%\begin{center}
|
|
973 |
%\begin{tikzpicture}[]
|
|
974 |
% \node [rectangle split, rectangle split horizontal,
|
|
975 |
% rectangle split parts=2, rectangle split part fill={red!30,blue!20}, draw, rounded corners, inner sep=10pt]
|
|
976 |
% (der2) at (0,0)
|
|
977 |
% {$(_{[Z]}(\ONE \cdot \ONE) \cdot (aa)^*) \cdot bc $
|
|
978 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq \; a\;a, ??]) \; ??$};
|
|
979 |
%
|
|
980 |
%\node [draw=none, minimum size = 0.1, ] (r) at (-7, 0) {$a_1$};
|
|
981 |
%\path
|
|
982 |
% (r)
|
|
983 |
% edge [->, >=stealth',shorten >=1pt, above] node {$\backslash a$} (der2);
|
|
984 |
%%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
985 |
%\end{tikzpicture}
|
|
986 |
%\end{center}
|
|
987 |
%\noindent
|
|
988 |
%After we take derivative with respect to
|
|
989 |
%second input character $a$, the annotated
|
|
990 |
%regular expression has the second $a$ chopped off.
|
|
991 |
%The second derivative does not involve any
|
|
992 |
%new bitcodes being generated, because
|
|
993 |
%there are no new iterations or bifurcations
|
|
994 |
%in the regular expression requiring any $S$ or $Z$ marker
|
|
995 |
%to indicate choices.
|
|
996 |
%\begin{center}
|
|
997 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
998 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
999 |
% {$(_{[Z]}(\ONE \cdot \ONE) \cdot (aa)^*) \cdot (\ONE \cdot c) $
|
|
1000 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq \; a\;a, ??]) \; ??$};
|
|
1001 |
%%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
1002 |
%\end{tikzpicture}
|
|
1003 |
%\end{center}
|
|
1004 |
%\noindent
|
|
1005 |
%
|
|
1006 |
%
|
|
1007 |
%\begin{center}
|
|
1008 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
1009 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
1010 |
% {$\stackrel{Bitcoded}{\longrightarrow} \Seq(\Stars[\Char(a), \Char(a)], ???)$
|
|
1011 |
% \nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$ $\stackrel{Inj}{\longleftarrow}$};
|
|
1012 |
%%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
1013 |
%\end{tikzpicture}
|
|
1014 |
%\end{center}
|
|
1015 |
%\noindent
|
|
1016 |
%If we do this kind of "attachment"
|
|
1017 |
%and each time augment the attached partially
|
|
1018 |
%constructed value when taking off a
|
|
1019 |
%character:
|
|
1020 |
%\begin{center}
|
|
1021 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
1022 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] (spPoint)
|
|
1023 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$
|
|
1024 |
% \nodepart{two} Remaining: $b c$};
|
|
1025 |
%\end{tikzpicture}\\
|
|
1026 |
%$\downarrow$\\
|
|
1027 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
1028 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
1029 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$
|
|
1030 |
% \nodepart{two} Remaining: $c$};
|
|
1031 |
%\end{tikzpicture}\\
|
|
1032 |
%$\downarrow$\\
|
|
1033 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
1034 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
1035 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$
|
|
1036 |
% \nodepart{two} EOF};
|
|
1037 |
%\end{tikzpicture}
|
|
1038 |
%\end{center}
|
638
|
1039 |
|
581
|
1040 |
In the next section we introduce the correctness proof
|
|
1041 |
found by Ausaf and Urban
|
|
1042 |
of the bitcoded lexer.
|
532
|
1043 |
%-----------------------------------
|
|
1044 |
% SUBSECTION 1
|
|
1045 |
%-----------------------------------
|
581
|
1046 |
\section{Correctness Proof of $\textit{Blexer}$}
|
|
1047 |
Why is $\blexer$ correct?
|
|
1048 |
In other words, why is it the case that
|
|
1049 |
$\blexer$ outputs the same value as $\lexer$?
|
|
1050 |
Intuitively,
|
|
1051 |
that is because
|
|
1052 |
\begin{itemize}
|
|
1053 |
\item
|
|
1054 |
$\blexer$ follows an almost identical
|
|
1055 |
path as that of $\lexer$,
|
|
1056 |
for example $r_1, r_2, \ldots$ and $a_1, a_2, \ldots$ being produced,
|
|
1057 |
which are the same up to the application of $\erase$.
|
|
1058 |
\item
|
|
1059 |
The bit-encodings work properly,
|
|
1060 |
allowing the possibility of
|
|
1061 |
pulling out the right lexical value from an
|
|
1062 |
annotated regular expression at
|
624
|
1063 |
any stage, using $\bmkeps$ or $\retrieve$ (details in
|
|
1064 |
lemmas \ref{bmkepsRetrieve} and \ref{blexer_retrieve}).
|
581
|
1065 |
\end{itemize}
|
|
1066 |
We will elaborate on this, with the help of
|
|
1067 |
some helper functions such as $\retrieve$ and
|
|
1068 |
$\flex$.
|
|
1069 |
\subsection{Specifications of Some Helper Functions}
|
622
|
1070 |
The functions we introduce will give a more detailed view into
|
581
|
1071 |
the lexing process, which is not be possible
|
|
1072 |
using $\lexer$ or $\blexer$ alone.
|
|
1073 |
\subsubsection{$\textit{Retrieve}$}
|
|
1074 |
The first function we shall introduce is $\retrieve$.
|
|
1075 |
Sulzmann and Lu gave its definition, and
|
638
|
1076 |
Ausaf and Urban found it
|
624
|
1077 |
useful in their mechanised proofs.
|
581
|
1078 |
Our bit-coded lexer ``retrieve''s the bitcodes using $\bmkeps$,
|
624
|
1079 |
after all the derivatives have been taken:
|
532
|
1080 |
\begin{center}
|
542
|
1081 |
\begin{tabular}{lcl}
|
|
1082 |
& & $\ldots$\\
|
|
1083 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
1084 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
1085 |
& & $\ldots$
|
532
|
1086 |
\end{tabular}
|
|
1087 |
\end{center}
|
542
|
1088 |
\noindent
|
624
|
1089 |
The function $\bmkeps$ retrieves the value $v$'s
|
581
|
1090 |
information in the format
|
|
1091 |
of bitcodes, by travelling along the
|
|
1092 |
path of the regular expression that corresponds to a POSIX match,
|
|
1093 |
collecting all the bitcodes.
|
542
|
1094 |
We know that this "retrieved" bitcode leads to the correct value after decoding,
|
624
|
1095 |
which is $v_0$ in the injection-based lexing diagram:
|
542
|
1096 |
\vspace{20mm}
|
581
|
1097 |
\begin{figure}[H]%\label{graph:injLexer}
|
|
1098 |
\begin{center}
|
542
|
1099 |
\begin{tikzcd}[
|
|
1100 |
every matrix/.append style = {name=p},
|
|
1101 |
remember picture, overlay,
|
|
1102 |
]
|
|
1103 |
a_0 \arrow[r, "\backslash c_0"] \arrow[d] & a_1 \arrow[r, "\backslash c_1"] \arrow[d] & a_2 \arrow[r, dashed] \arrow[d] & a_n \arrow[d] \\
|
|
1104 |
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]
|
|
1105 |
\end{tikzcd}
|
581
|
1106 |
\end{center}
|
542
|
1107 |
\begin{tikzpicture}[
|
|
1108 |
remember picture, overlay,
|
|
1109 |
E/.style = {ellipse, draw=blue, dashed,
|
|
1110 |
inner xsep=4mm,inner ysep=-4mm, rotate=90, fit=#1}
|
|
1111 |
]
|
|
1112 |
\node[E = (p-1-1) (p-2-1)] {};
|
|
1113 |
\node[E = (p-1-4) (p-2-4)] {};
|
581
|
1114 |
\node[E = (p-1-2) (p-2-2)] {};
|
|
1115 |
\node[E = (p-1-3) (p-2-3)] {};
|
542
|
1116 |
\end{tikzpicture}
|
624
|
1117 |
\caption{Injection-based lexing diagram,
|
|
1118 |
with matching value and regular expression pairs
|
|
1119 |
encircled}\label{Inj2}
|
581
|
1120 |
\end{figure}
|
542
|
1121 |
\vspace{20mm}
|
|
1122 |
\noindent
|
624
|
1123 |
We have that $\vdash v_i:(a_i)_\downarrow$,
|
|
1124 |
where $v_i$ are the intermediate values generated step by step in $\lexer$.
|
|
1125 |
These values are not explicitly created in $\blexer$.
|
|
1126 |
|
|
1127 |
We encircled in the diagram all the pairs $v_i, a_i$ to show that these values
|
582
|
1128 |
and regular expressions share the same structure.
|
|
1129 |
These pairs all contain adequate information to
|
|
1130 |
obtain the final lexing result.
|
|
1131 |
For example, in the leftmost pair the
|
581
|
1132 |
lexical information is condensed in
|
582
|
1133 |
$v_0$, whereas for the rightmost pair,
|
638
|
1134 |
the lexing result hides is in the bitcodes of $a_n$.
|
|
1135 |
|
624
|
1136 |
The function $\bmkeps$ is able to extract from $a_n$ the result
|
582
|
1137 |
by looking for nullable parts of the regular expression.
|
|
1138 |
However for regular expressions $a_i$ in general,
|
|
1139 |
they might not necessarily be nullable.
|
|
1140 |
For those regular expressions that are not nullable,
|
|
1141 |
$\bmkeps$ will not work.
|
|
1142 |
Therefore they need some additional ``help''
|
|
1143 |
finding the POSIX bit-encoding.
|
581
|
1144 |
The most straightforward ``help'' comes from $a_i$'s corresponding
|
|
1145 |
value $v_i$, and this suggests a function $f$ satisfying the
|
|
1146 |
following properties:
|
542
|
1147 |
\begin{itemize}
|
|
1148 |
\item
|
581
|
1149 |
$f \; a_i\;v_i = f \; a_n \; v_n = \bmkeps \; a_n$%\decode \; (\bmkeps \; a_n) \; (\erase \; a_0)$
|
542
|
1150 |
\item
|
582
|
1151 |
$f \; a_i\;v_i = f \; a_0 \; v_0 = \code \; v_0$ %\decode \;(\code \; v_0) \; (\erase \; a_0)$
|
542
|
1152 |
\end{itemize}
|
|
1153 |
\noindent
|
624
|
1154 |
Sulzmann and Lu came up with the following definition for $f$ satisfying
|
582
|
1155 |
the above
|
624
|
1156 |
requirements, and named it \emph{retrieve}:
|
|
1157 |
\vspace{-7mm}
|
542
|
1158 |
\begin{center}
|
582
|
1159 |
\begin{tabular}{llcl}
|
|
1160 |
$\retrieve \; \, _{bs} \ONE$ & $ \Empty$ & $\dn$ & $\textit{bs}$\\
|
|
1161 |
|
|
1162 |
$\retrieve \; \, _{bs} \mathbf{c}$ & $ (\Char \; c) $ & $\dn$ & $ \textit{bs}$\\
|
|
1163 |
|
|
1164 |
$\retrieve \; \, _{bs} a_1 \cdot a_2$ & $ (\Seq \; v_1 \; v_2) $ &
|
|
1165 |
$\dn$ & $\textit{bs} \; @\; (\retrieve \; a_1\; v_1)\; @ \;(\retrieve \; a_2 \; v_2)$\\
|
|
1166 |
|
|
1167 |
$\retrieve \; \, _{bs} \Sigma (a :: \textit{as})$ & $ (\Left \; v) $ &
|
|
1168 |
$\dn$ & $\textit{bs}\; @\; (\retrieve \; a \; v)$\\
|
|
1169 |
|
|
1170 |
$\retrieve \; \, _{bs} \Sigma (a :: \textit{as})$ & $ (\Right \; v) $ &
|
|
1171 |
$\dn$ & $\textit{bs}\; @\; (\retrieve \; (_{[]}\Sigma \textit{as}) \; v)$\\
|
|
1172 |
|
|
1173 |
$\retrieve \; \, _{bs} a^* $ & $ (\Stars \; (v :: vs)) $ & $\dn$ &
|
585
|
1174 |
$\textit{bs}\; @\; [Z] \; @ \; (\retrieve \; a \; v)\; @ \;
|
582
|
1175 |
(\retrieve \; (_{[]} a^*) \; (\Stars \; vs) )$\\
|
|
1176 |
|
585
|
1177 |
$\retrieve \; \, _{bs} a^* $ & $ (\Stars \; []) $ & $\dn$ & $\textit{bs} \; @ \; [S]$
|
542
|
1178 |
\end{tabular}
|
|
1179 |
\end{center}
|
|
1180 |
\noindent
|
624
|
1181 |
As stated, $\retrieve$ collects the right bit-codes from the
|
|
1182 |
final derivative $a_n$, guided by $v_n$. This can be proved
|
|
1183 |
as follows:
|
582
|
1184 |
\begin{lemma}\label{bmkepsRetrieve}
|
542
|
1185 |
$\bnullable \; a \implies \bmkeps \; a = \retrieve \; a \; (\mkeps \; (\erase \; a))$
|
|
1186 |
\end{lemma}
|
|
1187 |
\begin{proof}
|
|
1188 |
By a routine induction on $a$.
|
|
1189 |
\end{proof}
|
582
|
1190 |
\noindent
|
624
|
1191 |
%The design of $\retrieve$ enables us to extract bitcodes
|
|
1192 |
%from both annotated regular expressions and values.
|
|
1193 |
%$\retrieve$ always ``sucks up'' all the information.
|
582
|
1194 |
When more information is stored in the value, we would be able to
|
|
1195 |
extract more from the value, and vice versa.
|
|
1196 |
For example in star iterations, $\retrieve$ will be able to extract from $\Stars \; vs$
|
|
1197 |
exactly the same bitcodes as $\code \; (\Stars \; vs)$:
|
624
|
1198 |
\begin{lemma}\label{retrieveEncodeSTARS}
|
582
|
1199 |
If $\forall v \in vs. \vdash v : r$, and $\code \; v = \retrieve \; (\internalise\; r) \; v$\\
|
|
1200 |
then $\code \; (\Stars \; vs) = \retrieve \; _{[]}((\internalise \; r)^*) \; (\Stars \; vs)$
|
624
|
1201 |
\end{lemma}
|
582
|
1202 |
\begin{proof}
|
|
1203 |
By induction on the value list $vs$.
|
|
1204 |
\end{proof}
|
|
1205 |
\noindent
|
|
1206 |
Similarly, when the value list does not hold information,
|
|
1207 |
only the bitcodes plus some delimiter will be recorded:
|
|
1208 |
\begin{center}
|
|
1209 |
$\retrieve \; _{bs}((\internalise \; r)^*) \; (\Stars \; [] ) = bs @ [S]$.
|
|
1210 |
\end{center}
|
624
|
1211 |
In general, if we have a regular expression that is just internalised
|
|
1212 |
and the final lexing result value, we can $\retrieve$ the bitcodes
|
582
|
1213 |
that look as if we have en$\code$-ed the value as bitcodes:
|
542
|
1214 |
\begin{lemma}
|
|
1215 |
$\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$
|
|
1216 |
\end{lemma}
|
|
1217 |
\begin{proof}
|
|
1218 |
By induction on $r$.
|
582
|
1219 |
The star case uses lemma \ref{retrieveEncodeSTARS}.
|
542
|
1220 |
\end{proof}
|
|
1221 |
\noindent
|
624
|
1222 |
The following property of $\retrieve$ is crucial, as
|
582
|
1223 |
it provides a "bridge" between $a_0$ and $a_n $ in the
|
|
1224 |
lexing diagram by linking adjacent regular expressions $a_i$ and
|
|
1225 |
$a_{i+1}$.
|
|
1226 |
The property says that one
|
|
1227 |
can retrieve the same bits from a derivative
|
|
1228 |
regular expression as those from
|
|
1229 |
before the derivative took place,
|
624
|
1230 |
provided that the corresponding values are used respectively:
|
542
|
1231 |
\begin{lemma}\label{retrieveStepwise}
|
|
1232 |
$\vdash v : (r\backslash c) \implies \retrieve \; (r \backslash c) \; v= \retrieve \; r \; (\inj \; r\; c\; v)$
|
|
1233 |
\end{lemma}
|
|
1234 |
\begin{proof}
|
624
|
1235 |
By induction on $r$, generalising over $v$.
|
|
1236 |
The induction principle in our formalisation
|
|
1237 |
is function $\erase$'s cases.
|
542
|
1238 |
\end{proof}
|
|
1239 |
\noindent
|
582
|
1240 |
Before we move on to the next
|
|
1241 |
helper function, we rewrite $\blexer$ in
|
|
1242 |
the following way which makes later proofs more convenient:
|
542
|
1243 |
\begin{lemma}\label{blexer_retrieve}
|
|
1244 |
$\blexer \; r \; s = \decode \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$
|
|
1245 |
\end{lemma}
|
582
|
1246 |
\begin{proof}
|
|
1247 |
Using lemma \ref{bmkepsRetrieve}.
|
|
1248 |
\end{proof}
|
542
|
1249 |
\noindent
|
624
|
1250 |
The function $\retrieve$ allows connecting
|
582
|
1251 |
between the intermediate
|
|
1252 |
results $a_i$ and $a_{i+1}$ in $\blexer$.
|
|
1253 |
For plain regular expressions something similar is required.
|
542
|
1254 |
|
638
|
1255 |
\subsection{The Function $\flex$}
|
624
|
1256 |
Ausaf and Urban defined an auxiliary function called $\flex$ for $\lexer$,
|
532
|
1257 |
defined as
|
536
|
1258 |
\begin{center}
|
582
|
1259 |
\begin{tabular}{lcl}
|
536
|
1260 |
$\flex \; r \; f \; [] \; v$ & $=$ & $f\; v$\\
|
582
|
1261 |
$\flex \; r \; f \; (c :: s) \; v$ & $=$ & $\flex \; r \; (\lambda v. \, f (\inj \; r\; c\; v)) \;\; s \; v$
|
536
|
1262 |
\end{tabular}
|
|
1263 |
\end{center}
|
582
|
1264 |
which accumulates the characters that need to be injected back,
|
638
|
1265 |
and does the injection in a stack-like manner (the last taken derivative is first injected).
|
624
|
1266 |
The function
|
582
|
1267 |
$\flex$ can calculate what $\lexer$ calculates, given the input regular expression
|
|
1268 |
$r$, the identity function $id$, the input string $s$ and the value
|
|
1269 |
$v_n= \mkeps \; (r\backslash s)$:
|
532
|
1270 |
\begin{lemma}
|
582
|
1271 |
$\flex \;\; r \;\; \textit{id}\;\; s \;\; (\mkeps \;(r\backslash s)) = \lexer \; r \; s$
|
532
|
1272 |
\end{lemma}
|
542
|
1273 |
\begin{proof}
|
|
1274 |
By reverse induction on $s$.
|
|
1275 |
\end{proof}
|
582
|
1276 |
\noindent
|
|
1277 |
The main advantage of using $\flex$
|
624
|
1278 |
in $\lexer$ is that
|
582
|
1279 |
$\flex$ makes the value $v$ and function $f$
|
|
1280 |
that manipulates the value explicit parameters,
|
|
1281 |
which ``exposes'' $\lexer$'s recursive call
|
|
1282 |
arguments and allows us to ``set breakpoints'' and ``resume''
|
624
|
1283 |
at any point during $\lexer$'s recursive calls. Therefore
|
|
1284 |
the following stepwise property holds.
|
532
|
1285 |
\begin{lemma}\label{flexStepwise}
|
582
|
1286 |
$\textit{flex} \;\; r \;\; f \;\; (s@[c]) \;\; v = \flex \;\; r \;\; f \;\; s \;\; (\inj \; (r\backslash s) \; c \; v) $
|
532
|
1287 |
\end{lemma}
|
|
1288 |
\begin{proof}
|
582
|
1289 |
By induction on the shape of $r\backslash s$.
|
532
|
1290 |
\end{proof}
|
582
|
1291 |
\noindent
|
|
1292 |
With $\flex$ and $\retrieve$,
|
|
1293 |
we are ready to connect $\lexer$ and $\blexer$,
|
624
|
1294 |
giving the correctness proof for $\blexer$.
|
532
|
1295 |
|
542
|
1296 |
%----------------------------------------------------------------------------------------
|
|
1297 |
% SECTION correctness proof
|
|
1298 |
%----------------------------------------------------------------------------------------
|
624
|
1299 |
\section{Correctness of the Bit-coded Algorithm (Without Simplification)}
|
|
1300 |
We can first show that
|
638
|
1301 |
$\flex$ and $\blexer$ calculates the same value.
|
582
|
1302 |
\begin{lemma}\label{flex_retrieve}
|
|
1303 |
If $\vdash v: (r\backslash s)$, then\\
|
|
1304 |
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$
|
532
|
1305 |
\end{lemma}
|
|
1306 |
\begin{proof}
|
582
|
1307 |
By induction on $s$.
|
|
1308 |
We prove the interesting case where
|
|
1309 |
both $\flex$ and $\decode$ successfully terminates
|
|
1310 |
with some value.
|
|
1311 |
We take advantage of the stepwise properties
|
|
1312 |
both sides.
|
|
1313 |
The induction tactic is reverse induction on string $s$.
|
|
1314 |
The inductive hypothesis says that $\flex \; r \; id \;s \; v =
|
|
1315 |
\decode \; (\retrieve \; (r\backslash s) \; v) \; r$ holds,
|
|
1316 |
where $v$ can be any value satisfying
|
|
1317 |
the assumption $\vdash v: (r\backslash s)$.
|
|
1318 |
The crucial point is to rewrite
|
|
1319 |
\[
|
|
1320 |
\retrieve \;\; (r \backslash (s@[c])) \;\; (\mkeps\; (r \backslash (s@[c]) ))
|
|
1321 |
\]
|
|
1322 |
as
|
|
1323 |
\[
|
|
1324 |
\retrieve \;\; (r \backslash s)
|
|
1325 |
\;\; (\inj \; (r \backslash s) \; c\; \mkeps (r \backslash (s@[c])))
|
|
1326 |
\]
|
|
1327 |
using lemma \ref{retrieveStepwise}.
|
|
1328 |
This enables us to equate
|
|
1329 |
\[
|
|
1330 |
\retrieve \; (r \backslash (s@[c])) \; (\mkeps \; (r \backslash (s@[c]) ))
|
|
1331 |
\]
|
|
1332 |
with
|
|
1333 |
\[
|
|
1334 |
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c])))
|
|
1335 |
\]
|
|
1336 |
using IH,
|
|
1337 |
which in turn can be rewritten as
|
|
1338 |
\[
|
|
1339 |
\flex \; r \; \textit{id} \; (s@[c]) \; (\mkeps \; (r \backslash (s@[c])))
|
|
1340 |
\].
|
532
|
1341 |
\end{proof}
|
582
|
1342 |
\noindent
|
|
1343 |
With this pivotal lemma we can now link $\flex$ and $\blexer$
|
|
1344 |
and finally give the correctness of $\blexer$--it outputs the same result as $\lexer$:
|
542
|
1345 |
\begin{theorem}
|
|
1346 |
$\blexer\; r \; s = \lexer \; r \; s$
|
|
1347 |
\end{theorem}
|
|
1348 |
\begin{proof}
|
582
|
1349 |
From \ref{flex_retrieve} we have that
|
|
1350 |
$\textit{flex} \; r \; \textit{id} \; s \; \mkeps(r \backslash s) = \blexer \; r \; s$,
|
|
1351 |
which immediately implies this theorem.
|
542
|
1352 |
\end{proof}
|
|
1353 |
\noindent
|
582
|
1354 |
To piece things together and spell out the correctness
|
|
1355 |
result of the bitcoded lexer more explicitly,
|
576
|
1356 |
we use the fact from the previous chapter that
|
|
1357 |
\[
|
624
|
1358 |
(r, s) \rightarrow v \;\; \textit{iff} \;\; \lexer \; r \; s =\Some \; v
|
|
1359 |
\quad \quad
|
|
1360 |
\nexists v. (r, s) \rightarrow v\;\; \textit{iff} \;\; \lexer \;r\;s = None
|
576
|
1361 |
\]
|
|
1362 |
to obtain this corollary:
|
|
1363 |
\begin{corollary}\label{blexerPOSIX}
|
624
|
1364 |
The $\blexer$ function correctly implements a POSIX lexer, namely\\
|
|
1365 |
$(r, s) \rightarrow v \;\; \textit{iff} \;\; \blexer \; r \; s = \Some \; v$\\
|
|
1366 |
$\nexists v. (r, s) \rightarrow v\;\; \textit{iff} \;\; \blexer \;r\;s = None$
|
576
|
1367 |
\end{corollary}
|
624
|
1368 |
Our main reason for analysing the bit-coded algorithm over
|
|
1369 |
the injection-based one is that it allows us to define
|
542
|
1370 |
more aggressive simplifications.
|
|
1371 |
We will elaborate on this in the next chapter.
|
532
|
1372 |
|
|
1373 |
|