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