author | Chengsong |
Sun, 18 Jun 2023 17:54:52 +0100 | |
changeset 649 | ef2b8abcbc55 |
parent 640 | bd1354127574 |
child 653 | bc5571c38d1f |
permissions | -rwxr-xr-x |
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 |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
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 |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
116 |
As the number of derivative steps increases, |
579 | 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 |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
330 |
intermediate expressions, but only the most recent one |
581 | 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 |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
335 |
that the bitcodes are auxiliary information that does 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. |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
455 |
The characters used in each derivative are written as $c_i$. |
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
456 |
The size of the derivatives typically increases during each derivative step. |
638 | 457 |
The process starts with an internalise phase and concludes with a decoding phase.} |
580 | 458 |
\end{figure} |
459 |
\noindent |
|
460 |
The plain regular expressions |
|
461 |
are first ``lifted'' to an annotated regular expression, |
|
624 | 462 |
with the function called \emph{internalise} ($r \rightarrow _{bs}a$). |
580 | 463 |
Then the annotated regular expression $_{bs}a$ will |
624 | 464 |
be transformed by successive derivatives with respect |
580 | 465 |
to the input stream of characters $c_1, c_2$ etc. |
466 |
Each time a derivative is taken, the bitcodes |
|
467 |
are moved around, discarded or augmented together |
|
468 |
with the regular expression they are attached to. |
|
469 |
After all input has been consumed, the |
|
470 |
bitcodes are collected by $\bmkeps$, |
|
471 |
which traverses the nullable part of the regular expression |
|
624 | 472 |
and collects the bitcodes along the way. |
580 | 473 |
The collected bitcodes are then $\decode$d with the guidance |
624 | 474 |
of the input regular expression $r$ ( $_{bs}a \rightarrow r$). |
580 | 475 |
The most notable improvements of $\blexer$ |
476 |
over $\lexer$ are |
|
477 |
\begin{itemize} |
|
478 |
\item |
|
479 |
||
638 | 480 |
the absence of the second injection phase. |
580 | 481 |
\item |
638 | 482 |
one does not need to store each pair of the |
580 | 483 |
intermediate regular expressions $_{bs_i}a_i$ and $c_{i+1}$. |
484 |
The previous annotated regular expression $_{bs_i}a_i$'s information is passed |
|
485 |
on without loss to its successor $_{bs_{i+1}}a_{i+1}$, |
|
624 | 486 |
and $c_{i+1}$'s information is stored in $L\;r$.\footnote{ |
487 |
which will be used during the decode phase, where we use $r$ as |
|
488 |
a source of information.} |
|
580 | 489 |
\item |
638 | 490 |
simplification works much smoother--one can maintain correctness |
624 | 491 |
while applying quite aggressive simplifications. |
580 | 492 |
\end{itemize} |
624 | 493 |
%In the next section we will |
494 |
%give the operations needed in $\blexer$, |
|
495 |
%with some remarks on the idea behind their definitions. |
|
580 | 496 |
\subsection{Operations in $\textit{Blexer}$} |
497 |
The first operation we define related to bit-coded regular expressions |
|
498 |
is how we move bits to the inside of regular expressions. |
|
624 | 499 |
This operation is called $\fuse$: |
580 | 500 |
\begin{center} |
501 |
\begin{tabular}{lcl} |
|
624 | 502 |
$\textit{fuse}\;bs \; (\ZERO)$ & $\dn$ & $\ZERO$\\ |
503 |
$\textit{fuse}\;bs\; (_{bs'})\ONE$ & $\dn$ & |
|
580 | 504 |
$_{bs @ bs'}\ONE$\\ |
624 | 505 |
$\textit{fuse}\;bs\;(_{bs'}{\bf c})$ & $\dn$ & |
580 | 506 |
$_{bs@bs'}{\bf c}$\\ |
624 | 507 |
$\textit{fuse}\;bs\;(_{bs'}\sum\textit{as})$ & $\dn$ & |
580 | 508 |
$_{bs@bs'}\sum\textit{as}$\\ |
624 | 509 |
$\textit{fuse}\;bs\; (_{bs'}a_1\cdot a_2)$ & $\dn$ & |
580 | 510 |
$_{bs@bs'}a_1 \cdot a_2$\\ |
624 | 511 |
$\textit{fuse}\;bs\;(_{bs'}a^*)$ & $\dn$ & |
580 | 512 |
$_{bs @ bs'}a^*$ |
513 |
\end{tabular} |
|
514 |
\end{center} |
|
515 |
||
516 |
\noindent |
|
624 | 517 |
With \emph{fuse} we are able to define the $\internalise$ function, |
518 |
written $(\_)^\uparrow$, |
|
580 | 519 |
that translates a ``standard'' regular expression into an |
520 |
annotated regular expression. |
|
521 |
This function will be applied before we start |
|
522 |
with the derivative phase of the algorithm. |
|
523 |
||
524 |
\begin{center} |
|
525 |
\begin{tabular}{lcl} |
|
526 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\ |
|
527 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\ |
|
528 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\ |
|
529 |
$(r_1 + r_2)^\uparrow$ & $\dn$ & |
|
530 |
$_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\, |
|
531 |
\textit{fuse}\,[S]\,r_2^\uparrow]$\\ |
|
532 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ & |
|
533 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\ |
|
534 |
$(r^*)^\uparrow$ & $\dn$ & |
|
535 |
$_{[]}(r^\uparrow)^*$\\ |
|
536 |
\end{tabular} |
|
537 |
\end{center} |
|
538 |
\noindent |
|
539 |
The opposite of $\textit{internalise}$ is |
|
624 | 540 |
$\erase$, where all bit-codes are removed, |
580 | 541 |
and the alternative operator $\sum$ for annotated |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
542 |
regular expressions is transformed into the binary version |
624 | 543 |
in (plain) regular expressions. This can be defined as follows: |
544 |
\begin{center}\label{eraseDef} |
|
580 | 545 |
\begin{tabular}{lcl} |
546 |
$\ZERO_\downarrow$ & $\dn$ & $\ZERO$\\ |
|
547 |
$( _{bs}\ONE )_\downarrow$ & $\dn$ & $\ONE$\\ |
|
548 |
$( _{bs}\mathbf{c} )_\downarrow$ & $\dn$ & $\mathbf{c}$\\ |
|
549 |
$( _{bs} a_1 \cdot a_2 )_\downarrow$ & $\dn$ & |
|
550 |
$ (a_1) _\downarrow \cdot (a_2) _\downarrow$\\ |
|
551 |
$( _{bs} [])_\downarrow $ & $\dn$ & $\ZERO $\\ |
|
552 |
$( _{bs} [a] )_\downarrow$ & $\dn$ & $a_\downarrow$\\ |
|
624 | 553 |
$(_{bs} \sum [a_1, \; a_2])_\downarrow$ & $\dn$ & $ (a_1) _\downarrow + ( a_2 ) _\downarrow $\\ |
580 | 554 |
$(_{bs} \sum (a :: as))_\downarrow$ & $\dn$ & $ a_\downarrow + \; (_{[]} \sum as)_\downarrow$\\ |
555 |
$( _{bs} a^* )_\downarrow$ & $\dn$ & $(a_\downarrow)^*$ |
|
556 |
\end{tabular} |
|
557 |
\end{center} |
|
558 |
\noindent |
|
638 | 559 |
Where we abbreviate the $\erase\; a$ operation |
560 |
as $(a)_\downarrow$, for conciseness. |
|
580 | 561 |
|
624 | 562 |
Determining whether an annotated regular expression |
638 | 563 |
contains the empty string in its language requires |
580 | 564 |
a dedicated function $\bnullable$. |
624 | 565 |
In our formalisation this function is defined by simply calling $\erase$ |
566 |
before $\nullable$. |
|
580 | 567 |
\begin{definition} |
568 |
$\bnullable \; a \dn \nullable \; (a_\downarrow)$ |
|
569 |
\end{definition} |
|
570 |
The function for collecting |
|
571 |
bitcodes from a |
|
572 |
(b)nullable regular expression is called $\bmkeps$. |
|
624 | 573 |
This function is a lifted version of the $\textit{mkeps}$ function, |
580 | 574 |
which follows the path $\mkeps$ takes to traverse the |
575 |
$\nullable$ branches when visiting a regular expression, |
|
624 | 576 |
but collects bitcodes instead of generating a value. |
580 | 577 |
\begin{center} |
578 |
\begin{tabular}{lcl} |
|
579 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\ |
|
580 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ & |
|
581 |
$\textit{if}\;\textit{bnullable}\,a$\\ |
|
582 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\ |
|
583 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{[]}\sum \textit{as})$\\ |
|
584 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ & |
|
585 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\ |
|
586 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ & |
|
585 | 587 |
$bs \,@\, [S]$ |
580 | 588 |
\end{tabular} |
589 |
\end{center} |
|
590 |
\noindent |
|
624 | 591 |
This function, just like $\mkeps$, |
581 | 592 |
visits a regular expression tree respecting |
624 | 593 |
the POSIX rules. |
581 | 594 |
It traverses each child of the sequence regular expression |
595 |
from left to right and creates a bitcode by stitching |
|
596 |
together bitcodes obtained from the children expressions. |
|
597 |
In the case of alternative regular expressions, |
|
598 |
it looks for the leftmost |
|
599 |
$\nullable$ branch |
|
638 | 600 |
to visit and ignores the other siblings. |
581 | 601 |
%Whenever there is some bitcodes attached to a |
602 |
%node, it returns the bitcodes concatenated with whatever |
|
603 |
%child recursive calls return. |
|
604 |
The only time when $\bmkeps$ creates new bitcodes |
|
605 |
is when it completes a star's iterations by attaching a $S$ to the end of the bitcode |
|
624 | 606 |
list it returns. |
607 |
||
580 | 608 |
The bitcodes extracted by $\bmkeps$ need to be |
638 | 609 |
$\decode$d (with the guidance of a ``plain'' regular expression): |
580 | 610 |
%\begin{definition}[Bitdecoding of Values]\mbox{} |
611 |
\begin{center} |
|
612 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}} |
|
613 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\ |
|
614 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\ |
|
615 |
$\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
616 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\; |
|
617 |
(\Left\,v, bs_1)$\\ |
|
618 |
$\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
619 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\; |
|
620 |
(\Right\,v, bs_1)$\\ |
|
621 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ & |
|
622 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\ |
|
623 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\ |
|
624 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\ |
|
585 | 625 |
$\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\ |
626 |
$\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ & |
|
580 | 627 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
628 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\ |
|
629 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\ |
|
630 |
||
631 |
$\textit{decode}\,bs\,r$ & $\dn$ & |
|
632 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
|
633 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\; |
|
634 |
\textit{else}\;\textit{None}$ |
|
635 |
\end{tabular} |
|
636 |
\end{center} |
|
537 | 637 |
\noindent |
580 | 638 |
The function $\decode'$ returns a pair consisting of |
624 | 639 |
a partially decoded value and some leftover bit-list. |
580 | 640 |
The function $\decode'$ succeeds if the left-over |
641 |
bit-sequence is empty. |
|
624 | 642 |
|
643 |
%$\decode$ is terminating as $\decode'$ is terminating. |
|
644 |
%$\decode'$ is terminating |
|
645 |
%because at least one of $\decode'$'s parameters will go down in terms |
|
646 |
%of size.\\ |
|
580 | 647 |
The reverse operation of $\decode$ is $\code$. |
624 | 648 |
This function encodes a value into a bitcode by converting |
580 | 649 |
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
650 |
star iteration by $S$. $ Z$ marks the border where a star iteration |
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
651 |
terminates. |
580 | 652 |
This coding is lossy, as it throws away the information about |
653 |
characters, and does not encode the ``boundary'' between two |
|
654 |
sequence values. Moreover, with only the bitcode we cannot even tell |
|
624 | 655 |
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$, |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
656 |
but this will not be necessary for our correctness proof. |
580 | 657 |
\begin{center} |
658 |
\begin{tabular}{lcl} |
|
659 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\ |
|
660 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\ |
|
661 |
$\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\ |
|
662 |
$\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\ |
|
663 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\ |
|
664 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\ |
|
665 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\; |
|
666 |
code(\Stars\,vs)$ |
|
667 |
\end{tabular} |
|
668 |
\end{center} |
|
669 |
\noindent |
|
624 | 670 |
We can prove that given a value $v$ and regular expression $r$ |
580 | 671 |
with $\vdash v:r$, |
672 |
then we have the property that $\decode$ and $\code$ are |
|
673 |
reverse operations of one another: |
|
674 |
\begin{lemma} |
|
675 |
\[If \vdash v : r \; then \;\decode \; (\code \; v) \; r = \textit{Some}(v) \] |
|
676 |
\end{lemma} |
|
677 |
\begin{proof} |
|
678 |
By proving a more general version of the lemma, on $\decode'$: |
|
679 |
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \] |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
680 |
Then we obtain the property by setting $ds$ to be $[]$ and unfolding the $\decode$ definition. |
580 | 681 |
\end{proof} |
682 |
\noindent |
|
638 | 683 |
Now we define the central part of Sulzmann and Lu's second lexing algorithm, |
624 | 684 |
the $\bder$ function (which stands for \emph{b}itcoded-derivative): |
580 | 685 |
\begin{center} |
686 |
\begin{tabular}{@{}lcl@{}} |
|
687 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\ |
|
688 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\ |
|
689 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ & |
|
690 |
$\textit{if}\;c=d\; \;\textit{then}\; |
|
691 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\ |
|
692 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ & |
|
693 |
$_{bs}\sum\;(\textit{map} \; (\_\backslash c) \; as )$\\ |
|
694 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ & |
|
695 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
696 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\ |
|
697 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\ |
|
698 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\ |
|
699 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ & |
|
700 |
$_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot |
|
701 |
(_{[]}r^*))$ |
|
702 |
\end{tabular} |
|
703 |
\end{center} |
|
704 |
\noindent |
|
639 | 705 |
For $\bder \; c\; a$, we use the infix notation $a\backslash c$ for readability. |
624 | 706 |
The $\bder$ function tells us how regular expressions can be recursively traversed, |
580 | 707 |
where the bitcodes are augmented and carried around |
708 |
when a derivative is taken. |
|
709 |
We give the intuition behind some of the more involved cases in |
|
638 | 710 |
$\bder$. |
711 |
||
580 | 712 |
For example, |
713 |
in the \emph{star} case, |
|
624 | 714 |
a derivative of $_{bs}a^*$ means |
715 |
that one more star iteration needs to be taken. |
|
716 |
We therefore need to unfold it into a sequence, |
|
717 |
and attach an additional bit $Z$ to the front of $a \backslash c$ |
|
580 | 718 |
as a record to indicate one new star iteration is unfolded. |
719 |
||
720 |
\noindent |
|
721 |
\begin{center} |
|
722 |
\begin{tabular}{@{}lcl@{}} |
|
723 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ & |
|
724 |
$_{bs}(\underbrace{\textit{fuse}\, [Z] \; a\,\backslash c}_{\text{One more iteration}})\cdot |
|
725 |
(_{[]}a^*))$ |
|
726 |
\end{tabular} |
|
727 |
\end{center} |
|
728 |
||
729 |
\noindent |
|
730 |
This information will be recovered later by the $\decode$ function. |
|
624 | 731 |
%The intuition is that the bit $Z$ will be decoded at the right location, |
732 |
%because we accumulate bits from left to right (a rigorous proof will be given |
|
733 |
%later). |
|
580 | 734 |
|
735 |
%\begin{tikzpicture}[ > = stealth, % arrow head style |
|
736 |
% shorten > = 1pt, % don't touch arrow head to node |
|
737 |
% semithick % line style |
|
738 |
% ] |
|
739 |
% |
|
740 |
% \tikzstyle{every state}=[ |
|
741 |
% draw = black, |
|
742 |
% thin, |
|
743 |
% fill = cyan!29, |
|
744 |
% minimum size = 7mm |
|
745 |
% ] |
|
746 |
% \begin{scope}[node distance=1cm and 0cm, every node/.style=state] |
|
747 |
% \node (k) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
748 |
% {$bs$ |
|
749 |
% \nodepart{two} $a^*$ }; |
|
750 |
% \node (l) [below =of k, rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
751 |
% { $bs$ + [Z] |
|
752 |
% \nodepart{two} $(a\backslash c )\cdot a^*$ }; |
|
753 |
% \end{scope} |
|
754 |
% \path[->] |
|
755 |
% (k) edge (l); |
|
756 |
%\end{tikzpicture} |
|
757 |
% |
|
758 |
%Pictorially the process looks like below. |
|
759 |
%Like before, the red region denotes |
|
760 |
%previous lexing information (stored as bitcodes in $bs$). |
|
761 |
||
762 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
763 |
% \begin{scope}[node distance=1cm] |
|
764 |
% \node (a) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
765 |
% {$bs$ |
|
766 |
% \nodepart{two} $a^*$ }; |
|
767 |
% \node (b) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
768 |
% { $bs$ + [Z] |
|
769 |
% \nodepart{two} $(a\backslash c )\cdot a^*$ }; |
|
770 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
771 |
% \end{scope} |
|
772 |
%\end{tikzpicture} |
|
773 |
||
774 |
\noindent |
|
775 |
Another place the $\bder$ function differs |
|
624 | 776 |
from derivatives on regular expressions |
580 | 777 |
is the sequence case: |
778 |
\begin{center} |
|
779 |
\begin{tabular}{@{}lcl@{}} |
|
780 |
||
781 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ & |
|
782 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
783 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\ |
|
784 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\ |
|
785 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$ |
|
786 |
\end{tabular} |
|
787 |
\end{center} |
|
788 |
\noindent |
|
624 | 789 |
%We assume that $\bmkeps$ |
790 |
%correctly extracts the bitcode for how $a_1$ |
|
791 |
%matches the string prior to $c$ (more on this later). |
|
792 |
%The bitsequence $\textit{bs}$ which was initially |
|
793 |
%attached to the first element of the sequence |
|
794 |
%$a_1 \cdot a_2$, has now been |
|
795 |
%elevated to the top level of the $\sum$ constructor. |
|
796 |
%This is because this piece of information will be needed |
|
797 |
%whichever way the sequence is matched, |
|
798 |
%regardless of whether $c$ belongs to $a_1$ or $a_2$. |
|
799 |
The difference mainly lies in the $\textit{if}$ branch (when $a_1$ is $\bnullable$): |
|
580 | 800 |
we use $\bmkeps$ to store the lexing information |
801 |
in $a_1$ before collapsing |
|
802 |
it (as it has been fully matched by string prior to $c$), |
|
803 |
and attach the collected bit-codes to the front of $a_2$ |
|
624 | 804 |
before throwing away $a_1$. |
580 | 805 |
In the injection-based lexer, $r_1$ is immediately thrown away in |
638 | 806 |
the $\textit{if}$ branch, |
624 | 807 |
the information $r_1$ stores is therefore lost: |
580 | 808 |
\begin{center} |
624 | 809 |
\begin{tabular}{lcl} |
810 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\ |
|
811 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\ |
|
812 |
& & $\mathit{else}\; \ldots$\\ |
|
813 |
\end{tabular} |
|
580 | 814 |
\end{center} |
815 |
\noindent |
|
624 | 816 |
|
817 |
%\noindent |
|
818 |
%this loss of information makes it necessary |
|
819 |
%to store on stack all the regular expressions' |
|
820 |
%``snapshots'' before they were |
|
821 |
%taken derivative of. |
|
822 |
%So that the related information will be available |
|
823 |
%once the child recursive |
|
824 |
%call finishes.\\ |
|
580 | 825 |
The rest of the clauses of $\bder$ is rather similar to |
826 |
$\der$. \\ |
|
827 |
Generalising the derivative operation with bitcodes to strings, we have |
|
828 |
\begin{center} |
|
829 |
\begin{tabular}{@{}lcl@{}} |
|
830 |
$a\backslash_s [] $ & $\dn$ & $a$\\ |
|
831 |
$a\backslash (c :: s) $ & $\dn$ & $(a \backslash c) \backslash_s s$ |
|
832 |
\end{tabular} |
|
833 |
\end{center} |
|
834 |
\noindent |
|
835 |
As we did earlier, we omit the $s$ subscript at $\backslash_s$ when there is no danger |
|
836 |
of confusion. |
|
837 |
\subsection{Putting Things Together} |
|
838 |
Putting these operations altogether, we obtain a lexer with bit-coded regular expressions |
|
839 |
as its internal data structures, which we call $\blexer$: |
|
840 |
||
841 |
\begin{center} |
|
842 |
\begin{tabular}{lcl} |
|
843 |
$\textit{blexer}\;r\,s$ & $\dn$ & |
|
844 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\ |
|
845 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
846 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
847 |
& & $\;\;\textit{else}\;\textit{None}$ |
|
848 |
\end{tabular} |
|
849 |
\end{center} |
|
850 |
\noindent |
|
624 | 851 |
This function first attaches bitcodes to a |
852 |
plain regular expression $r$, and then builds successive derivatives |
|
581 | 853 |
with respect to the input string $s$, and |
624 | 854 |
then test whether the result is (b)nullable. |
855 |
If yes, then extract the bitcodes from the nullable expression, |
|
581 | 856 |
and decodes the bitcodes into a lexical value. |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
857 |
If there does not exist a match between $r$ and $s$, the lexer |
638 | 858 |
outputs $\None$ indicating a mismatch. |
580 | 859 |
Ausaf and Urban formally proved the correctness of the $\blexer$, namely |
860 |
\begin{property} |
|
861 |
$\blexer \;r \; s = \lexer \; r \; s$. |
|
862 |
\end{property} |
|
581 | 863 |
\noindent |
580 | 864 |
This was claimed but not formalised in Sulzmann and Lu's work. |
865 |
We introduce the proof later, after we give all |
|
866 |
the needed auxiliary functions and definitions. |
|
581 | 867 |
\subsection{An Example $\blexer$ Run} |
624 | 868 |
Before introducing the proof we shall first walk |
580 | 869 |
through a concrete example of our $\blexer$ calculating the right |
638 | 870 |
lexical information through bit-coded regular expressions. |
871 |
||
581 | 872 |
Consider the regular expression $(aa)^* \cdot (b+c)$ matching the string $aab$. |
873 |
We give again the bird's eye view of this particular example |
|
874 |
in each stage of the algorithm: |
|
875 |
||
876 |
\tikzset{three sided/.style={ |
|
877 |
draw=none, |
|
878 |
append after command={ |
|
879 |
[-,shorten <= -0.5\pgflinewidth] |
|
880 |
([shift={(-1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east) |
|
881 |
edge([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) |
|
882 |
([shift={( 0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) |
|
883 |
edge([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west) |
|
884 |
([shift={( 0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west) |
|
885 |
edge([shift={(-1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east) |
|
886 |
} |
|
887 |
} |
|
888 |
} |
|
889 |
||
890 |
\tikzset{three sided1/.style={ |
|
891 |
draw=none, |
|
892 |
append after command={ |
|
893 |
[-,shorten <= -0.5\pgflinewidth] |
|
894 |
([shift={(1.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north west) |
|
895 |
edge([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east) |
|
896 |
([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]\tikzlastnode.north east) |
|
897 |
edge([shift={(-0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east) |
|
898 |
([shift={(-0.5\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south east) |
|
899 |
edge([shift={(1.0\pgflinewidth,+0.5\pgflinewidth)}]\tikzlastnode.south west) |
|
900 |
} |
|
901 |
} |
|
902 |
} |
|
903 |
||
904 |
\begin{figure}[H] |
|
905 |
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, auto, thick] |
|
906 |
\node [rectangle, draw] (r) at (-6, -1) {$(aa)^*(b+c)$}; |
|
907 |
\node [rectangle, draw] (a) at (-6, 4) {$(aa)^*(_{Z}b + _{S}c)$}; |
|
908 |
\path (r) |
|
909 |
edge [] node {$\internalise$} (a); |
|
910 |
\node [rectangle, draw] (a1) at (-3, 1) {$(_{Z}(\ONE \cdot a) \cdot (aa)^*) (_{Z}b + _Sc)$}; |
|
911 |
\path (a) |
|
912 |
edge [] node {$\backslash a$} (a1); |
|
913 |
||
914 |
\node [rectangle, draw, three sided] (a21) at (-2.5, 4) {$(_{Z}\ONE \cdot (aa)^*)$}; |
|
915 |
\node [rectangle, draw, three sided1] (a22) at (-0.8, 4) {$(_{Z}b + _{S}c)$}; |
|
916 |
\path (a1) |
|
917 |
edge [] node {$\backslash a$} (a21); |
|
918 |
\node [rectangle, draw] (a3) at (0.5, 2) {$_{ZS}(_{Z}\ONE + \ZERO)$}; |
|
919 |
\path (a22) |
|
920 |
edge [] node {$\backslash b$} (a3); |
|
921 |
\path (a21) |
|
922 |
edge [dashed, bend right] node {} (a3); |
|
923 |
\node [rectangle, draw] (bs) at (2, 4) {$ZSZ$}; |
|
924 |
\path (a3) |
|
925 |
edge [below] node {$\bmkeps$} (bs); |
|
926 |
\node [rectangle, draw] (v) at (3, 0) {$\Seq \; (\Stars\; [\Seq \; a \; a]) \; (\Left \; b)$}; |
|
927 |
\path (bs) |
|
928 |
edge [] node {$\decode$} (v); |
|
929 |
||
930 |
||
931 |
\end{tikzpicture} |
|
638 | 932 |
\caption{$\blexer$ with the regular expression $(aa)^*(b+c)$ and $aab$} |
581 | 933 |
\end{figure} |
537 | 934 |
\noindent |
581 | 935 |
The one dashed arrow indicates that $_Z(\ONE \cdot (aa)^*)$ |
936 |
turned into $ZS$ after derivative w.r.t $b$ |
|
638 | 937 |
is taken, which calls $\bmkeps$ on the nullable $_Z\ONE\cdot (aa)^*$ |
938 |
before processing $_Zb+_Sc$. |
|
939 |
||
581 | 940 |
The annotated regular expressions |
624 | 941 |
would look overwhelming if we explicitly indicate all the |
581 | 942 |
locations where bitcodes are attached. |
943 |
For example, |
|
944 |
$(aa)^*\cdot (b+c)$ would |
|
945 |
look like $_{[]}(_{[]}(_{[]}a \cdot _{[]}a)^*\cdot _{[]}(_{[]}b+_{[]}c))$ |
|
946 |
after |
|
947 |
internalise. |
|
948 |
Therefore for readability we omit bitcodes if they are empty. |
|
624 | 949 |
This applies to all annotated |
638 | 950 |
regular expressions in this thesis. |
951 |
||
581 | 952 |
%and assume we have just read the first character $a$: |
953 |
%\begin{center} |
|
954 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
955 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
956 |
% {$(_{[Z]}(\ONE \cdot a) \cdot (aa)^* )\cdot bc$ |
|
957 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq\; a \; ??, \;??]) \; ??$}; |
|
958 |
%\end{tikzpicture} |
|
959 |
%\end{center} |
|
960 |
%\noindent |
|
961 |
%We use the red area for (annotated) regular expressions and the blue |
|
962 |
%area the (partially calculated) bitcodes |
|
963 |
%and its corresponding (partial) values. |
|
964 |
%The first derivative |
|
965 |
%generates a $Z$ bitcode to indicate |
|
966 |
%a new iteration has been started. |
|
967 |
%This bitcode is attached to the front of |
|
968 |
%the unrolled star iteration $\ONE\cdot a$ |
|
969 |
%for later decoding. |
|
970 |
%\begin{center} |
|
971 |
%\begin{tikzpicture}[] |
|
972 |
% \node [rectangle split, rectangle split horizontal, |
|
973 |
% rectangle split parts=2, rectangle split part fill={red!30,blue!20}, draw, rounded corners, inner sep=10pt] |
|
974 |
% (der2) at (0,0) |
|
975 |
% {$(_{[Z]}(\ONE \cdot \ONE) \cdot (aa)^*) \cdot bc $ |
|
976 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq \; a\;a, ??]) \; ??$}; |
|
977 |
% |
|
978 |
%\node [draw=none, minimum size = 0.1, ] (r) at (-7, 0) {$a_1$}; |
|
979 |
%\path |
|
980 |
% (r) |
|
981 |
% edge [->, >=stealth',shorten >=1pt, above] node {$\backslash a$} (der2); |
|
982 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
983 |
%\end{tikzpicture} |
|
984 |
%\end{center} |
|
985 |
%\noindent |
|
986 |
%After we take derivative with respect to |
|
987 |
%second input character $a$, the annotated |
|
988 |
%regular expression has the second $a$ chopped off. |
|
989 |
%The second derivative does not involve any |
|
990 |
%new bitcodes being generated, because |
|
991 |
%there are no new iterations or bifurcations |
|
992 |
%in the regular expression requiring any $S$ or $Z$ marker |
|
993 |
%to indicate choices. |
|
994 |
%\begin{center} |
|
995 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
996 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
997 |
% {$(_{[Z]}(\ONE \cdot \ONE) \cdot (aa)^*) \cdot (\ONE \cdot c) $ |
|
998 |
% \nodepart{two} $[Z] \iff \Seq \; (\Stars \; [\Seq \; a\;a, ??]) \; ??$}; |
|
999 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
1000 |
%\end{tikzpicture} |
|
1001 |
%\end{center} |
|
1002 |
%\noindent |
|
1003 |
% |
|
1004 |
% |
|
1005 |
%\begin{center} |
|
1006 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1007 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1008 |
% {$\stackrel{Bitcoded}{\longrightarrow} \Seq(\Stars[\Char(a), \Char(a)], ???)$ |
|
1009 |
% \nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$ $\stackrel{Inj}{\longleftarrow}$}; |
|
1010 |
%%\caption{term 1 \ref{term:1}'s matching configuration} |
|
1011 |
%\end{tikzpicture} |
|
1012 |
%\end{center} |
|
1013 |
%\noindent |
|
1014 |
%If we do this kind of "attachment" |
|
1015 |
%and each time augment the attached partially |
|
1016 |
%constructed value when taking off a |
|
1017 |
%character: |
|
1018 |
%\begin{center} |
|
1019 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1020 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] (spPoint) |
|
1021 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$ |
|
1022 |
% \nodepart{two} Remaining: $b c$}; |
|
1023 |
%\end{tikzpicture}\\ |
|
1024 |
%$\downarrow$\\ |
|
1025 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1026 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1027 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$ |
|
1028 |
% \nodepart{two} Remaining: $c$}; |
|
1029 |
%\end{tikzpicture}\\ |
|
1030 |
%$\downarrow$\\ |
|
1031 |
%\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}] |
|
1032 |
% \node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] |
|
1033 |
% {$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$ |
|
1034 |
% \nodepart{two} EOF}; |
|
1035 |
%\end{tikzpicture} |
|
1036 |
%\end{center} |
|
638 | 1037 |
|
581 | 1038 |
In the next section we introduce the correctness proof |
1039 |
found by Ausaf and Urban |
|
1040 |
of the bitcoded lexer. |
|
532 | 1041 |
%----------------------------------- |
1042 |
% SUBSECTION 1 |
|
1043 |
%----------------------------------- |
|
581 | 1044 |
\section{Correctness Proof of $\textit{Blexer}$} |
1045 |
Why is $\blexer$ correct? |
|
1046 |
In other words, why is it the case that |
|
1047 |
$\blexer$ outputs the same value as $\lexer$? |
|
1048 |
Intuitively, |
|
1049 |
that is because |
|
1050 |
\begin{itemize} |
|
1051 |
\item |
|
1052 |
$\blexer$ follows an almost identical |
|
1053 |
path as that of $\lexer$, |
|
1054 |
for example $r_1, r_2, \ldots$ and $a_1, a_2, \ldots$ being produced, |
|
1055 |
which are the same up to the application of $\erase$. |
|
1056 |
\item |
|
1057 |
The bit-encodings work properly, |
|
1058 |
allowing the possibility of |
|
1059 |
pulling out the right lexical value from an |
|
1060 |
annotated regular expression at |
|
624 | 1061 |
any stage, using $\bmkeps$ or $\retrieve$ (details in |
1062 |
lemmas \ref{bmkepsRetrieve} and \ref{blexer_retrieve}). |
|
581 | 1063 |
\end{itemize} |
1064 |
We will elaborate on this, with the help of |
|
1065 |
some helper functions such as $\retrieve$ and |
|
1066 |
$\flex$. |
|
1067 |
\subsection{Specifications of Some Helper Functions} |
|
622 | 1068 |
The functions we introduce will give a more detailed view into |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
1069 |
the lexing process, which is not possible |
581 | 1070 |
using $\lexer$ or $\blexer$ alone. |
1071 |
\subsubsection{$\textit{Retrieve}$} |
|
1072 |
The first function we shall introduce is $\retrieve$. |
|
1073 |
Sulzmann and Lu gave its definition, and |
|
638 | 1074 |
Ausaf and Urban found it |
624 | 1075 |
useful in their mechanised proofs. |
581 | 1076 |
Our bit-coded lexer ``retrieve''s the bitcodes using $\bmkeps$, |
624 | 1077 |
after all the derivatives have been taken: |
532 | 1078 |
\begin{center} |
542 | 1079 |
\begin{tabular}{lcl} |
1080 |
& & $\ldots$\\ |
|
1081 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
1082 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
1083 |
& & $\ldots$ |
|
532 | 1084 |
\end{tabular} |
1085 |
\end{center} |
|
542 | 1086 |
\noindent |
624 | 1087 |
The function $\bmkeps$ retrieves the value $v$'s |
581 | 1088 |
information in the format |
1089 |
of bitcodes, by travelling along the |
|
1090 |
path of the regular expression that corresponds to a POSIX match, |
|
1091 |
collecting all the bitcodes. |
|
542 | 1092 |
We know that this "retrieved" bitcode leads to the correct value after decoding, |
624 | 1093 |
which is $v_0$ in the injection-based lexing diagram: |
542 | 1094 |
\vspace{20mm} |
581 | 1095 |
\begin{figure}[H]%\label{graph:injLexer} |
1096 |
\begin{center} |
|
542 | 1097 |
\begin{tikzcd}[ |
1098 |
every matrix/.append style = {name=p}, |
|
1099 |
remember picture, overlay, |
|
1100 |
] |
|
1101 |
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] \\ |
|
1102 |
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] |
|
1103 |
\end{tikzcd} |
|
581 | 1104 |
\end{center} |
542 | 1105 |
\begin{tikzpicture}[ |
1106 |
remember picture, overlay, |
|
1107 |
E/.style = {ellipse, draw=blue, dashed, |
|
1108 |
inner xsep=4mm,inner ysep=-4mm, rotate=90, fit=#1} |
|
1109 |
] |
|
1110 |
\node[E = (p-1-1) (p-2-1)] {}; |
|
1111 |
\node[E = (p-1-4) (p-2-4)] {}; |
|
581 | 1112 |
\node[E = (p-1-2) (p-2-2)] {}; |
1113 |
\node[E = (p-1-3) (p-2-3)] {}; |
|
542 | 1114 |
\end{tikzpicture} |
624 | 1115 |
\caption{Injection-based lexing diagram, |
1116 |
with matching value and regular expression pairs |
|
1117 |
encircled}\label{Inj2} |
|
581 | 1118 |
\end{figure} |
542 | 1119 |
\vspace{20mm} |
1120 |
\noindent |
|
624 | 1121 |
We have that $\vdash v_i:(a_i)_\downarrow$, |
1122 |
where $v_i$ are the intermediate values generated step by step in $\lexer$. |
|
1123 |
These values are not explicitly created in $\blexer$. |
|
1124 |
||
1125 |
We encircled in the diagram all the pairs $v_i, a_i$ to show that these values |
|
582 | 1126 |
and regular expressions share the same structure. |
1127 |
These pairs all contain adequate information to |
|
1128 |
obtain the final lexing result. |
|
1129 |
For example, in the leftmost pair the |
|
581 | 1130 |
lexical information is condensed in |
582 | 1131 |
$v_0$, whereas for the rightmost pair, |
638 | 1132 |
the lexing result hides is in the bitcodes of $a_n$. |
1133 |
||
624 | 1134 |
The function $\bmkeps$ is able to extract from $a_n$ the result |
582 | 1135 |
by looking for nullable parts of the regular expression. |
1136 |
However for regular expressions $a_i$ in general, |
|
1137 |
they might not necessarily be nullable. |
|
1138 |
For those regular expressions that are not nullable, |
|
1139 |
$\bmkeps$ will not work. |
|
1140 |
Therefore they need some additional ``help'' |
|
1141 |
finding the POSIX bit-encoding. |
|
581 | 1142 |
The most straightforward ``help'' comes from $a_i$'s corresponding |
1143 |
value $v_i$, and this suggests a function $f$ satisfying the |
|
1144 |
following properties: |
|
542 | 1145 |
\begin{itemize} |
1146 |
\item |
|
581 | 1147 |
$f \; a_i\;v_i = f \; a_n \; v_n = \bmkeps \; a_n$%\decode \; (\bmkeps \; a_n) \; (\erase \; a_0)$ |
542 | 1148 |
\item |
582 | 1149 |
$f \; a_i\;v_i = f \; a_0 \; v_0 = \code \; v_0$ %\decode \;(\code \; v_0) \; (\erase \; a_0)$ |
542 | 1150 |
\end{itemize} |
1151 |
\noindent |
|
624 | 1152 |
Sulzmann and Lu came up with the following definition for $f$ satisfying |
582 | 1153 |
the above |
624 | 1154 |
requirements, and named it \emph{retrieve}: |
1155 |
\vspace{-7mm} |
|
542 | 1156 |
\begin{center} |
582 | 1157 |
\begin{tabular}{llcl} |
1158 |
$\retrieve \; \, _{bs} \ONE$ & $ \Empty$ & $\dn$ & $\textit{bs}$\\ |
|
1159 |
||
1160 |
$\retrieve \; \, _{bs} \mathbf{c}$ & $ (\Char \; c) $ & $\dn$ & $ \textit{bs}$\\ |
|
1161 |
||
1162 |
$\retrieve \; \, _{bs} a_1 \cdot a_2$ & $ (\Seq \; v_1 \; v_2) $ & |
|
1163 |
$\dn$ & $\textit{bs} \; @\; (\retrieve \; a_1\; v_1)\; @ \;(\retrieve \; a_2 \; v_2)$\\ |
|
1164 |
||
1165 |
$\retrieve \; \, _{bs} \Sigma (a :: \textit{as})$ & $ (\Left \; v) $ & |
|
1166 |
$\dn$ & $\textit{bs}\; @\; (\retrieve \; a \; v)$\\ |
|
1167 |
||
1168 |
$\retrieve \; \, _{bs} \Sigma (a :: \textit{as})$ & $ (\Right \; v) $ & |
|
1169 |
$\dn$ & $\textit{bs}\; @\; (\retrieve \; (_{[]}\Sigma \textit{as}) \; v)$\\ |
|
1170 |
||
1171 |
$\retrieve \; \, _{bs} a^* $ & $ (\Stars \; (v :: vs)) $ & $\dn$ & |
|
585 | 1172 |
$\textit{bs}\; @\; [Z] \; @ \; (\retrieve \; a \; v)\; @ \; |
582 | 1173 |
(\retrieve \; (_{[]} a^*) \; (\Stars \; vs) )$\\ |
1174 |
||
585 | 1175 |
$\retrieve \; \, _{bs} a^* $ & $ (\Stars \; []) $ & $\dn$ & $\textit{bs} \; @ \; [S]$ |
542 | 1176 |
\end{tabular} |
1177 |
\end{center} |
|
1178 |
\noindent |
|
624 | 1179 |
As stated, $\retrieve$ collects the right bit-codes from the |
1180 |
final derivative $a_n$, guided by $v_n$. This can be proved |
|
1181 |
as follows: |
|
582 | 1182 |
\begin{lemma}\label{bmkepsRetrieve} |
542 | 1183 |
$\bnullable \; a \implies \bmkeps \; a = \retrieve \; a \; (\mkeps \; (\erase \; a))$ |
1184 |
\end{lemma} |
|
1185 |
\begin{proof} |
|
1186 |
By a routine induction on $a$. |
|
1187 |
\end{proof} |
|
582 | 1188 |
\noindent |
624 | 1189 |
%The design of $\retrieve$ enables us to extract bitcodes |
1190 |
%from both annotated regular expressions and values. |
|
1191 |
%$\retrieve$ always ``sucks up'' all the information. |
|
582 | 1192 |
When more information is stored in the value, we would be able to |
1193 |
extract more from the value, and vice versa. |
|
1194 |
For example in star iterations, $\retrieve$ will be able to extract from $\Stars \; vs$ |
|
1195 |
exactly the same bitcodes as $\code \; (\Stars \; vs)$: |
|
624 | 1196 |
\begin{lemma}\label{retrieveEncodeSTARS} |
582 | 1197 |
If $\forall v \in vs. \vdash v : r$, and $\code \; v = \retrieve \; (\internalise\; r) \; v$\\ |
1198 |
then $\code \; (\Stars \; vs) = \retrieve \; _{[]}((\internalise \; r)^*) \; (\Stars \; vs)$ |
|
624 | 1199 |
\end{lemma} |
582 | 1200 |
\begin{proof} |
1201 |
By induction on the value list $vs$. |
|
1202 |
\end{proof} |
|
1203 |
\noindent |
|
1204 |
Similarly, when the value list does not hold information, |
|
1205 |
only the bitcodes plus some delimiter will be recorded: |
|
1206 |
\begin{center} |
|
1207 |
$\retrieve \; _{bs}((\internalise \; r)^*) \; (\Stars \; [] ) = bs @ [S]$. |
|
1208 |
\end{center} |
|
624 | 1209 |
In general, if we have a regular expression that is just internalised |
1210 |
and the final lexing result value, we can $\retrieve$ the bitcodes |
|
582 | 1211 |
that look as if we have en$\code$-ed the value as bitcodes: |
542 | 1212 |
\begin{lemma} |
1213 |
$\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$ |
|
1214 |
\end{lemma} |
|
1215 |
\begin{proof} |
|
1216 |
By induction on $r$. |
|
582 | 1217 |
The star case uses lemma \ref{retrieveEncodeSTARS}. |
542 | 1218 |
\end{proof} |
1219 |
\noindent |
|
624 | 1220 |
The following property of $\retrieve$ is crucial, as |
582 | 1221 |
it provides a "bridge" between $a_0$ and $a_n $ in the |
1222 |
lexing diagram by linking adjacent regular expressions $a_i$ and |
|
1223 |
$a_{i+1}$. |
|
1224 |
The property says that one |
|
1225 |
can retrieve the same bits from a derivative |
|
1226 |
regular expression as those from |
|
1227 |
before the derivative took place, |
|
624 | 1228 |
provided that the corresponding values are used respectively: |
542 | 1229 |
\begin{lemma}\label{retrieveStepwise} |
1230 |
$\vdash v : (r\backslash c) \implies \retrieve \; (r \backslash c) \; v= \retrieve \; r \; (\inj \; r\; c\; v)$ |
|
1231 |
\end{lemma} |
|
1232 |
\begin{proof} |
|
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
1233 |
We do an induction on $r$, generalising over $v$. |
624 | 1234 |
The induction principle in our formalisation |
1235 |
is function $\erase$'s cases. |
|
542 | 1236 |
\end{proof} |
1237 |
\noindent |
|
649 | 1238 |
Intuitively the lemma can be understood as all lexical value |
1239 |
information being preserved and recoverable throughout each lexing step. |
|
1240 |
We shall see in the next chapter that this no longer |
|
1241 |
holds with simplifications which takes away certain sub-expressions |
|
1242 |
corresponding to non-POSIX lexical values. |
|
1243 |
||
582 | 1244 |
Before we move on to the next |
1245 |
helper function, we rewrite $\blexer$ in |
|
1246 |
the following way which makes later proofs more convenient: |
|
542 | 1247 |
\begin{lemma}\label{blexer_retrieve} |
1248 |
$\blexer \; r \; s = \decode \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$ |
|
1249 |
\end{lemma} |
|
582 | 1250 |
\begin{proof} |
1251 |
Using lemma \ref{bmkepsRetrieve}. |
|
1252 |
\end{proof} |
|
542 | 1253 |
\noindent |
624 | 1254 |
The function $\retrieve$ allows connecting |
582 | 1255 |
between the intermediate |
1256 |
results $a_i$ and $a_{i+1}$ in $\blexer$. |
|
1257 |
For plain regular expressions something similar is required. |
|
542 | 1258 |
|
638 | 1259 |
\subsection{The Function $\flex$} |
624 | 1260 |
Ausaf and Urban defined an auxiliary function called $\flex$ for $\lexer$, |
532 | 1261 |
defined as |
536 | 1262 |
\begin{center} |
582 | 1263 |
\begin{tabular}{lcl} |
536 | 1264 |
$\flex \; r \; f \; [] \; v$ & $=$ & $f\; v$\\ |
582 | 1265 |
$\flex \; r \; f \; (c :: s) \; v$ & $=$ & $\flex \; r \; (\lambda v. \, f (\inj \; r\; c\; v)) \;\; s \; v$ |
536 | 1266 |
\end{tabular} |
1267 |
\end{center} |
|
582 | 1268 |
which accumulates the characters that need to be injected back, |
640
bd1354127574
more proofreading done, last version before submission
Chengsong
parents:
639
diff
changeset
|
1269 |
and does the injection in a stack-like manner (the last character being chopped off in the derivatives phase is first injected). |
624 | 1270 |
The function |
582 | 1271 |
$\flex$ can calculate what $\lexer$ calculates, given the input regular expression |
1272 |
$r$, the identity function $id$, the input string $s$ and the value |
|
1273 |
$v_n= \mkeps \; (r\backslash s)$: |
|
532 | 1274 |
\begin{lemma} |
582 | 1275 |
$\flex \;\; r \;\; \textit{id}\;\; s \;\; (\mkeps \;(r\backslash s)) = \lexer \; r \; s$ |
532 | 1276 |
\end{lemma} |
542 | 1277 |
\begin{proof} |
1278 |
By reverse induction on $s$. |
|
1279 |
\end{proof} |
|
582 | 1280 |
\noindent |
1281 |
The main advantage of using $\flex$ |
|
624 | 1282 |
in $\lexer$ is that |
582 | 1283 |
$\flex$ makes the value $v$ and function $f$ |
1284 |
that manipulates the value explicit parameters, |
|
1285 |
which ``exposes'' $\lexer$'s recursive call |
|
1286 |
arguments and allows us to ``set breakpoints'' and ``resume'' |
|
624 | 1287 |
at any point during $\lexer$'s recursive calls. Therefore |
1288 |
the following stepwise property holds. |
|
532 | 1289 |
\begin{lemma}\label{flexStepwise} |
582 | 1290 |
$\textit{flex} \;\; r \;\; f \;\; (s@[c]) \;\; v = \flex \;\; r \;\; f \;\; s \;\; (\inj \; (r\backslash s) \; c \; v) $ |
532 | 1291 |
\end{lemma} |
1292 |
\begin{proof} |
|
582 | 1293 |
By induction on the shape of $r\backslash s$. |
532 | 1294 |
\end{proof} |
582 | 1295 |
\noindent |
1296 |
With $\flex$ and $\retrieve$, |
|
1297 |
we are ready to connect $\lexer$ and $\blexer$, |
|
624 | 1298 |
giving the correctness proof for $\blexer$. |
532 | 1299 |
|
542 | 1300 |
%---------------------------------------------------------------------------------------- |
1301 |
% SECTION correctness proof |
|
1302 |
%---------------------------------------------------------------------------------------- |
|
624 | 1303 |
\section{Correctness of the Bit-coded Algorithm (Without Simplification)} |
1304 |
We can first show that |
|
638 | 1305 |
$\flex$ and $\blexer$ calculates the same value. |
582 | 1306 |
\begin{lemma}\label{flex_retrieve} |
1307 |
If $\vdash v: (r\backslash s)$, then\\ |
|
1308 |
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$ |
|
532 | 1309 |
\end{lemma} |
1310 |
\begin{proof} |
|
582 | 1311 |
By induction on $s$. |
1312 |
We prove the interesting case where |
|
1313 |
both $\flex$ and $\decode$ successfully terminates |
|
1314 |
with some value. |
|
1315 |
We take advantage of the stepwise properties |
|
1316 |
both sides. |
|
1317 |
The induction tactic is reverse induction on string $s$. |
|
1318 |
The inductive hypothesis says that $\flex \; r \; id \;s \; v = |
|
1319 |
\decode \; (\retrieve \; (r\backslash s) \; v) \; r$ holds, |
|
1320 |
where $v$ can be any value satisfying |
|
1321 |
the assumption $\vdash v: (r\backslash s)$. |
|
1322 |
The crucial point is to rewrite |
|
1323 |
\[ |
|
1324 |
\retrieve \;\; (r \backslash (s@[c])) \;\; (\mkeps\; (r \backslash (s@[c]) )) |
|
1325 |
\] |
|
1326 |
as |
|
1327 |
\[ |
|
1328 |
\retrieve \;\; (r \backslash s) |
|
1329 |
\;\; (\inj \; (r \backslash s) \; c\; \mkeps (r \backslash (s@[c]))) |
|
1330 |
\] |
|
1331 |
using lemma \ref{retrieveStepwise}. |
|
1332 |
This enables us to equate |
|
1333 |
\[ |
|
1334 |
\retrieve \; (r \backslash (s@[c])) \; (\mkeps \; (r \backslash (s@[c]) )) |
|
1335 |
\] |
|
1336 |
with |
|
1337 |
\[ |
|
1338 |
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c]))) |
|
1339 |
\] |
|
1340 |
using IH, |
|
1341 |
which in turn can be rewritten as |
|
1342 |
\[ |
|
1343 |
\flex \; r \; \textit{id} \; (s@[c]) \; (\mkeps \; (r \backslash (s@[c]))) |
|
1344 |
\]. |
|
532 | 1345 |
\end{proof} |
582 | 1346 |
\noindent |
1347 |
With this pivotal lemma we can now link $\flex$ and $\blexer$ |
|
1348 |
and finally give the correctness of $\blexer$--it outputs the same result as $\lexer$: |
|
649 | 1349 |
\begin{theorem}\label{blexerCorrect} |
542 | 1350 |
$\blexer\; r \; s = \lexer \; r \; s$ |
1351 |
\end{theorem} |
|
1352 |
\begin{proof} |
|
582 | 1353 |
From \ref{flex_retrieve} we have that |
1354 |
$\textit{flex} \; r \; \textit{id} \; s \; \mkeps(r \backslash s) = \blexer \; r \; s$, |
|
1355 |
which immediately implies this theorem. |
|
542 | 1356 |
\end{proof} |
1357 |
\noindent |
|
582 | 1358 |
To piece things together and spell out the correctness |
1359 |
result of the bitcoded lexer more explicitly, |
|
576 | 1360 |
we use the fact from the previous chapter that |
1361 |
\[ |
|
624 | 1362 |
(r, s) \rightarrow v \;\; \textit{iff} \;\; \lexer \; r \; s =\Some \; v |
1363 |
\quad \quad |
|
1364 |
\nexists v. (r, s) \rightarrow v\;\; \textit{iff} \;\; \lexer \;r\;s = None |
|
576 | 1365 |
\] |
1366 |
to obtain this corollary: |
|
1367 |
\begin{corollary}\label{blexerPOSIX} |
|
624 | 1368 |
The $\blexer$ function correctly implements a POSIX lexer, namely\\ |
1369 |
$(r, s) \rightarrow v \;\; \textit{iff} \;\; \blexer \; r \; s = \Some \; v$\\ |
|
1370 |
$\nexists v. (r, s) \rightarrow v\;\; \textit{iff} \;\; \blexer \;r\;s = None$ |
|
576 | 1371 |
\end{corollary} |
624 | 1372 |
Our main reason for analysing the bit-coded algorithm over |
1373 |
the injection-based one is that it allows us to define |
|
542 | 1374 |
more aggressive simplifications. |
1375 |
We will elaborate on this in the next chapter. |
|
532 | 1376 |
|
1377 |