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.
|
537
|
18 |
\section{Bit-coded Algorithm}
|
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.
|
|
38 |
\begin{figure}
|
|
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 |
|
|
67 |
\begin{figure}
|
|
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 |
|
|
104 |
\begin{figure}
|
|
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}
|
|
148 |
\noindent
|
|
149 |
After all derivatives have been taken, the stack grows to a maximum size
|
|
150 |
and the pair of regular expressions and characters $r_i, c_{i+1}$
|
|
151 |
are then popped out and used in the injection phase:
|
|
152 |
|
|
153 |
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
154 |
%\draw (-6,-6) grid (6,6);
|
|
155 |
\node [radius = 0.5, circle, draw] (r) at (-6, 5) {$r$};
|
|
156 |
|
|
157 |
%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
158 |
\node [circle, minimum size = 0.1, draw] (c1) at (-4, 5.4) {$c_1$};
|
|
159 |
%
|
|
160 |
%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
161 |
\node [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
|
|
162 |
%
|
|
163 |
%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$};
|
|
164 |
\node [circle, minimum size = 0.1, draw] (c2) at (0, 5.4) {$c_2$};
|
|
165 |
%
|
|
166 |
%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
167 |
\node [circle, draw] (r2) at (2, 5) {$r_2$};
|
|
168 |
%
|
|
169 |
%
|
|
170 |
\node [] (ldots) at (4.5, 5) {};
|
|
171 |
%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
|
|
172 |
|
|
173 |
\node [minimum size = 0.5, circle, draw] (rn) at (6, 5) {$r_n$};
|
|
174 |
|
|
175 |
\node at ($(ldots)!.4!(rn)$) {\ldots};
|
|
176 |
|
|
177 |
\node [minimum size = 0.5, circle, draw] (vn) at (6, -5) {$v_n$};
|
|
178 |
|
|
179 |
\node [] (ldots2) at (3.5, -5) {};
|
|
180 |
|
|
181 |
\node [minimum size = 0.5, circle, draw] (v2) at (2, -5) {$v_2$};
|
|
182 |
|
|
183 |
\node at ($(ldots2)!.4!(v2)$) {\ldots};
|
|
184 |
|
|
185 |
|
|
186 |
\node [circle, draw] (v1) at (-2, -5) {$v_1$};
|
|
187 |
|
|
188 |
\node [radius = 0.5, circle, draw] (v) at (-6, -5) {$v$};
|
|
189 |
|
|
190 |
\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack};
|
|
191 |
|
|
192 |
\path
|
|
193 |
(r)
|
|
194 |
edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
195 |
\path
|
|
196 |
(r1)
|
|
197 |
edge [] node {} (r2);
|
|
198 |
\path (r2)
|
|
199 |
edge [] node {} (ldots);
|
|
200 |
\path (rn)
|
|
201 |
edge [] node {$\mkeps$} (vn);
|
|
202 |
\path (vn)
|
|
203 |
edge [] node {} (ldots2);
|
|
204 |
\path (v2)
|
|
205 |
edge [] node {$\inj \; r_1 \; c_2\;v_2$} (v1);
|
|
206 |
|
|
207 |
\path (v1)
|
|
208 |
edge [] node {$\inj \; r \; c_1 \; v_1$} (v);
|
|
209 |
\path (r)
|
|
210 |
edge [dashed, bend right] node {} (stack);
|
|
211 |
|
|
212 |
\path (r1)
|
|
213 |
edge [dashed, ] node {} (stack);
|
|
214 |
|
|
215 |
\path (c1)
|
|
216 |
edge [dashed, bend right] node {} (stack);
|
|
217 |
|
|
218 |
\path (c2)
|
|
219 |
edge [dashed] node {} (stack);
|
|
220 |
\path (4.5, 5)
|
|
221 |
edge [dashed, bend left] node {} (stack);
|
|
222 |
\path (4.9, 5)
|
|
223 |
edge [dashed, bend left] node {} (stack);
|
|
224 |
\path (5.3, 5)
|
|
225 |
edge [dashed, bend left] node {} (stack);
|
|
226 |
\path (r2)
|
|
227 |
edge [dashed, ] node {} (stack);
|
|
228 |
\path (rn)
|
|
229 |
edge [dashed, bend left] node {} (stack);
|
|
230 |
\end{tikzpicture}
|
|
231 |
%\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,thick]
|
|
232 |
%%\draw (-6,-6) grid (6,6);
|
|
233 |
%\node [radius = 0.5, circle, draw] (r) at (-6, 5) {$r$};
|
|
234 |
%
|
|
235 |
%%\node (-4, 6) (c1) circle [radius = 0.3] {$c_1$};
|
|
236 |
%\node [circle, minimum size = 0.1] (c1) at (-4, 5.4) {$c_1$};
|
|
237 |
%%
|
|
238 |
%%\node (-2, 5) (r1) circle [radius = 0.5] {$r_1$};
|
|
239 |
%\node [minimum size = 0.5, circle, draw] (r1) at (-2, 5) {$r_1$};
|
|
240 |
%%
|
|
241 |
%%\node (0, 6) (c2) circle [radius = 0.3] {$c_2$};
|
|
242 |
%\node [circle, minimum size = 0.1] (c2) at (0, 5.4) {$c_2$};
|
|
243 |
%%
|
|
244 |
%%\node (2, 5) (r2) circle [radius = 0.5] {$r_2$};
|
|
245 |
%\node [circle, draw] (r2) at (2, 5) {$r_2$};
|
|
246 |
%%
|
|
247 |
%%
|
|
248 |
%\node [] (ldots) at (4.5, 5) {};
|
|
249 |
%%\node (6, 5) (rn) circle [radius = 0.5] {$r_n$};
|
|
250 |
%
|
|
251 |
%\node [minimum size = 0.5, circle, draw] (rn) at (6, 5) {$r_n$};
|
|
252 |
%
|
|
253 |
%\node at ($(ldots)!.4!(rn)$) {\ldots};
|
|
254 |
%
|
|
255 |
%\node [minimum size = 0.5, circle, draw] (vn) at (6, -5) {$v_n$};
|
|
256 |
%
|
|
257 |
%\node [] (ldots2) at (3.5, -5) {};
|
|
258 |
%
|
|
259 |
%\node [minimum size = 0.5, circle, draw] (v2) at (2, -5) {$v_2$};
|
|
260 |
%
|
|
261 |
%\node at ($(ldots2)!.4!(v2)$) {\ldots};
|
|
262 |
%
|
|
263 |
%
|
|
264 |
%\node [circle, draw] (v1) at (-2, -5) {$v_1$};
|
|
265 |
%
|
|
266 |
%\node [radius = 0.5, circle, draw] (v) at (-6, -5) {$v$};
|
|
267 |
%
|
|
268 |
%\node [minimum size = 6cm, rectangle, draw] (stack) at (0, 0) {\Huge Stack};
|
|
269 |
%
|
|
270 |
%\path
|
|
271 |
% (r)
|
|
272 |
% edge [->, >=stealth',shorten >=1pt] node[left] {} (r1);
|
|
273 |
%\path
|
|
274 |
% (r1)
|
|
275 |
% edge [] node {} (r2);
|
|
276 |
%\path (r2)
|
|
277 |
% edge [] node {} (ldots);
|
|
278 |
%\path (rn)
|
|
279 |
% edge [] node {$\mkeps$} (vn);
|
|
280 |
%\path (vn)
|
|
281 |
% edge [] node {} (ldots2);
|
|
282 |
%\path (v2)
|
|
283 |
% edge [] node {} (v1);
|
|
284 |
%
|
|
285 |
%\path (v1)
|
|
286 |
% edge [] node {} (v);
|
|
287 |
%\path (r)
|
|
288 |
% edge [] node {saved} (stack);
|
|
289 |
%
|
|
290 |
%\path (r1)
|
|
291 |
% edge [] node {saved} (stack);
|
|
292 |
%\end{tikzpicture}
|
|
293 |
\noindent
|
|
294 |
The information stored in characters and regular expressions
|
|
295 |
make the algorithm work in an elegant way, at the expense of being
|
|
296 |
storing quite a bit of verbose information.
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
The lexer algorithm in Chapter \ref{Inj},
|
537
|
301 |
stores information of previous lexing steps
|
|
302 |
on a stack, in the form of regular expressions
|
|
303 |
and characters: $r_0$, $c_0$, $r_1$, $c_1$, etc.
|
579
|
304 |
The red part represents what we already know during the first
|
|
305 |
derivative phase,
|
|
306 |
and the blue part represents the unknown part of input.
|
537
|
307 |
\begin{ceqn}
|
|
308 |
\begin{equation}%\label{graph:injLexer}
|
564
|
309 |
\begin{tikzcd}[ampersand replacement=\&, execute at end picture={
|
|
310 |
\begin{scope}[on background layer]
|
|
311 |
\node[rectangle, fill={red!30},
|
|
312 |
pattern=north east lines, pattern color=red,
|
|
313 |
fit={(-3,-1) (-3, 1) (1, -1)
|
|
314 |
(1, 1)}
|
|
315 |
]
|
|
316 |
{}; ,
|
|
317 |
\node[rectangle, fill={blue!20},
|
|
318 |
pattern=north east lines, pattern color=blue,
|
|
319 |
fit= {(1, -1) (1, 1) (3, -1) (3, 1)}
|
|
320 |
]
|
|
321 |
{};
|
|
322 |
\end{scope}}
|
|
323 |
]
|
|
324 |
r_0 \arrow[r, "\backslash c_0"] \arrow[d] \& r_1 \arrow[r, "\backslash c_1"] \arrow[d] \& r_2 \arrow[r, dashed] \arrow[d] \& r_n \arrow[d, "mkeps" description] \\
|
|
325 |
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] \\
|
537
|
326 |
\end{tikzcd}
|
|
327 |
\end{equation}
|
|
328 |
\end{ceqn}
|
|
329 |
\noindent
|
564
|
330 |
The red area expands as we move towards $r_n$,
|
579
|
331 |
indicating more is known about the lexing result.
|
|
332 |
Despite knowing this partial lexing information during
|
564
|
333 |
the forward derivative phase, we choose to store them
|
579
|
334 |
all the way until $r_n$ is reached.
|
|
335 |
Then we reconstruct the value character by character
|
|
336 |
values at a later stage, using information in a Last-In-First-Out
|
|
337 |
manner. Although the algorithm is elegant and natural,
|
|
338 |
it can be inefficient and prone to stack overflow.\\
|
|
339 |
It turns out we can store lexing
|
537
|
340 |
information on the fly, while still using regular expression
|
564
|
341 |
derivatives.
|
579
|
342 |
If we remove the individual
|
564
|
343 |
lexing steps, and use red and blue areas as before
|
|
344 |
to indicate consumed (seen) input and constructed
|
|
345 |
partial value (before recovering the rest of the stack),
|
|
346 |
one could see that the seen part's lexical information
|
|
347 |
is stored in the form of a regular expression.
|
|
348 |
Consider the regular expression $(aa)^* \cdot bc$ matching the string $aabc$
|
|
349 |
and assume we have just read the two characters $aa$:
|
532
|
350 |
\begin{center}
|
537
|
351 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
352 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
564
|
353 |
{Partial lexing info: $\ONE \cdot a \cdot (aa)^* \cdot bc$ etc.
|
537
|
354 |
\nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
|
|
355 |
\end{tikzpicture}
|
|
356 |
\end{center}
|
564
|
357 |
\noindent
|
|
358 |
In the injection-based lexing algorithm, we ``neglect" the red area
|
|
359 |
by putting all the characters we have consumed and
|
|
360 |
intermediate regular expressions on the stack when
|
|
361 |
we go from left to right in the derivative phase.
|
|
362 |
The red area grows till the string is exhausted.
|
|
363 |
During the injection phase, the value in the blue area
|
|
364 |
is built up incrementally, while the red area shrinks.
|
|
365 |
Before we have recovered all characters and intermediate
|
|
366 |
derivative regular expressions from the stack,
|
|
367 |
what values these characters and regular expressions correspond
|
|
368 |
to are unknown:
|
537
|
369 |
\begin{center}
|
|
370 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
371 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={white!30,blue!20},]
|
564
|
372 |
{$(\ONE \cdot \ONE) \cdot (aa)^* \cdot bc $ correspond to:$???$
|
|
373 |
\nodepart{two} $b c$ corresponds to $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$};
|
537
|
374 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
375 |
\end{tikzpicture}
|
|
376 |
\end{center}
|
|
377 |
\noindent
|
564
|
378 |
However, they should be calculable,
|
|
379 |
as characters and regular expression shapes
|
|
380 |
after taking derivative w.r.t those characters
|
|
381 |
have already been known, therefore in our example,
|
|
382 |
we know that the value starts with two $a$s,
|
|
383 |
and makes up to an iteration in a Kleene star:
|
|
384 |
(We have put the injection-based lexing's partial
|
|
385 |
result in the right part of the split rectangle
|
|
386 |
to contrast it with the partial valued produced
|
|
387 |
in a forward manner)
|
537
|
388 |
\begin{center}
|
|
389 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
390 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
564
|
391 |
{$\stackrel{Bitcoded}{\longrightarrow} \Seq(\Stars[\Char(a), \Char(a)], ???)$
|
|
392 |
\nodepart{two} $\Seq(\ldots, \Seq(\Char(b), \Char(c)))$ $\stackrel{Inj}{\longleftarrow}$};
|
537
|
393 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
394 |
\end{tikzpicture}
|
|
395 |
\end{center}
|
|
396 |
\noindent
|
|
397 |
If we do this kind of "attachment"
|
|
398 |
and each time augment the attached partially
|
|
399 |
constructed value when taking off a
|
|
400 |
character:
|
|
401 |
\begin{center}
|
564
|
402 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
403 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},] (spPoint)
|
|
404 |
{$\Seq(\Stars[\Char(a), \Char(a)], \ldots)$
|
|
405 |
\nodepart{two} Remaining: $b c$};
|
|
406 |
\end{tikzpicture}\\
|
|
407 |
$\downarrow$\\
|
537
|
408 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
409 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
410 |
{$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \ldots))$
|
564
|
411 |
\nodepart{two} Remaining: $c$};
|
537
|
412 |
\end{tikzpicture}\\
|
564
|
413 |
$\downarrow$\\
|
537
|
414 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
415 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
416 |
{$\Seq(\Stars[\Char(a), \Char(a)], \Seq(\Char(b), \Char(c)))$
|
|
417 |
\nodepart{two} EOF};
|
|
418 |
\end{tikzpicture}
|
|
419 |
\end{center}
|
|
420 |
\noindent
|
|
421 |
In the end we could recover the value without a backward phase.
|
|
422 |
But (partial) values are a bit clumsy to stick together with a regular expression, so
|
|
423 |
we instead use bit-codes to encode them.
|
|
424 |
|
|
425 |
Bits and bitcodes (lists of bits) are defined as:
|
|
426 |
\begin{center}
|
|
427 |
$b ::= S \mid Z \qquad
|
532
|
428 |
bs ::= [] \mid b::bs
|
|
429 |
$
|
|
430 |
\end{center}
|
|
431 |
|
|
432 |
\noindent
|
538
|
433 |
Using $S$ and $Z$ rather than $1$ and $0$ is to avoid
|
|
434 |
confusion with the regular expressions $\ZERO$ and $\ONE$.
|
|
435 |
Bitcodes (or
|
532
|
436 |
bit-lists) can be used to encode values (or potentially incomplete values) in a
|
|
437 |
compact form. This can be straightforwardly seen in the following
|
|
438 |
coding function from values to bitcodes:
|
|
439 |
\begin{center}
|
|
440 |
\begin{tabular}{lcl}
|
|
441 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\
|
|
442 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\
|
537
|
443 |
$\textit{code}(\Left\,v)$ & $\dn$ & $Z :: code(v)$\\
|
|
444 |
$\textit{code}(\Right\,v)$ & $\dn$ & $S :: code(v)$\\
|
532
|
445 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\
|
537
|
446 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[Z]$\\
|
|
447 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $S :: code(v) \;@\;
|
532
|
448 |
code(\Stars\,vs)$
|
|
449 |
\end{tabular}
|
|
450 |
\end{center}
|
|
451 |
\noindent
|
537
|
452 |
Here $\textit{code}$ encodes a value into a bit-code by converting
|
|
453 |
$\Left$ into $Z$, $\Right$ into $S$, and marks the start of any non-empty
|
|
454 |
star iteration by $S$. The border where a local star terminates
|
|
455 |
is marked by $Z$.
|
|
456 |
This coding is lossy, as it throws away the information about
|
532
|
457 |
characters, and also does not encode the ``boundary'' between two
|
|
458 |
sequence values. Moreover, with only the bitcode we cannot even tell
|
537
|
459 |
whether the $S$s and $Z$s are for $\Left/\Right$ or $\Stars$. The
|
532
|
460 |
reason for choosing this compact way of storing information is that the
|
|
461 |
relatively small size of bits can be easily manipulated and ``moved
|
564
|
462 |
around" in a regular expression.
|
537
|
463 |
|
564
|
464 |
Because of the lossiness, the process of decoding a bitlist requires additionally
|
|
465 |
a regular expression. The function $\decode$ is defined as:
|
532
|
466 |
|
|
467 |
%\begin{definition}[Bitdecoding of Values]\mbox{}
|
|
468 |
\begin{center}
|
|
469 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
|
|
470 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\
|
|
471 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\
|
537
|
472 |
$\textit{decode}'\,(Z\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
532
|
473 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\;
|
|
474 |
(\Left\,v, bs_1)$\\
|
537
|
475 |
$\textit{decode}'\,(S\!::\!bs)\;(r_1 + r_2)$ & $\dn$ &
|
532
|
476 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\;
|
|
477 |
(\Right\,v, bs_1)$\\
|
|
478 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ &
|
|
479 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\
|
|
480 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\
|
|
481 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\
|
537
|
482 |
$\textit{decode}'\,(Z\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\
|
|
483 |
$\textit{decode}'\,(S\!::\!bs)\,(r^*)$ & $\dn$ &
|
532
|
484 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
485 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\
|
|
486 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\
|
|
487 |
|
|
488 |
$\textit{decode}\,bs\,r$ & $\dn$ &
|
|
489 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\
|
|
490 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\;
|
|
491 |
\textit{else}\;\textit{None}$
|
|
492 |
\end{tabular}
|
538
|
493 |
\end{center}
|
532
|
494 |
%\end{definition}
|
|
495 |
|
538
|
496 |
\noindent
|
575
|
497 |
The function $\decode'$ returns a pair consisting of
|
|
498 |
a partially decoded value and some leftover bit list that cannot
|
|
499 |
be decide yet.
|
|
500 |
The function $\decode'$ succeeds if the left-over
|
|
501 |
bit-sequence is empty.
|
538
|
502 |
$\decode$ is terminating as $\decode'$ is terminating.
|
575
|
503 |
$\decode'$ is terminating
|
|
504 |
because at least one of $\decode'$'s parameters will go down in terms
|
|
505 |
of size.
|
|
506 |
Assuming we have a value $v$ and regular expression $r$
|
|
507 |
with $\vdash v:r$,
|
|
508 |
then we have the property that $\decode$ and $\code$ are
|
538
|
509 |
reverse operations of one another:
|
|
510 |
\begin{lemma}
|
575
|
511 |
\[If \vdash v : r \; then \;\decode \; (\code \; v) \; r = \textit{Some}(v) \]
|
538
|
512 |
\end{lemma}
|
|
513 |
\begin{proof}
|
|
514 |
By proving a more general version of the lemma, on $\decode'$:
|
|
515 |
\[\vdash v : r \implies \decode' \; ((\code \; v) @ ds) \; r = (v, ds) \]
|
575
|
516 |
Then setting $ds$ to be $[]$ and unfolding $\decode$ definition,
|
|
517 |
we obtain the property.
|
538
|
518 |
\end{proof}
|
|
519 |
With the $\code$ and $\decode$ functions in hand, we know how to
|
575
|
520 |
switch between bit-codes and values.
|
|
521 |
The next step is to integrate this information into regular expression.
|
538
|
522 |
Attaching bits to the front of regular expressions is the solution Sulzamann and Lu
|
575
|
523 |
gave for storing partial values in regular expressions.
|
|
524 |
Annotated regular expressions are therefore defined as the Isabelle
|
|
525 |
datatype:
|
532
|
526 |
|
|
527 |
\begin{center}
|
|
528 |
\begin{tabular}{lcl}
|
|
529 |
$\textit{a}$ & $::=$ & $\ZERO$\\
|
|
530 |
& $\mid$ & $_{bs}\ONE$\\
|
|
531 |
& $\mid$ & $_{bs}{\bf c}$\\
|
|
532 |
& $\mid$ & $_{bs}\sum\,as$\\
|
|
533 |
& $\mid$ & $_{bs}a_1\cdot a_2$\\
|
|
534 |
& $\mid$ & $_{bs}a^*$
|
|
535 |
\end{tabular}
|
|
536 |
\end{center}
|
|
537 |
%(in \textit{ALTS})
|
|
538 |
|
|
539 |
\noindent
|
575
|
540 |
where $bs$ stands for bit-codes, $a$ for $\mathbf{a}$nnotated regular
|
|
541 |
expressions and $as$ for lists of annotated regular expressions.
|
|
542 |
The alternative constructor, written, $\sum$, has been generalised to
|
|
543 |
accept a list of annotated regular expressions rather than just two.
|
|
544 |
Why is it generalised? This is because when we open up nested
|
|
545 |
alternatives, there could be more than two elements at the same level
|
|
546 |
after de-duplication, which can no longer be stored in a binary
|
|
547 |
constructor.
|
542
|
548 |
|
575
|
549 |
The first operation we define related to bit-coded regular expressions
|
|
550 |
is how we move bits to the inside of regular expressions.
|
|
551 |
Called $\fuse$, this operation is attaches bit-codes
|
542
|
552 |
to the front of an annotated regular expression:
|
|
553 |
\begin{center}
|
|
554 |
\begin{tabular}{lcl}
|
|
555 |
$\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\
|
|
556 |
$\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ &
|
|
557 |
$_{bs @ bs'}\ONE$\\
|
|
558 |
$\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ &
|
|
559 |
$_{bs@bs'}{\bf c}$\\
|
|
560 |
$\textit{fuse}\;bs\,_{bs'}\sum\textit{as}$ & $\dn$ &
|
|
561 |
$_{bs@bs'}\sum\textit{as}$\\
|
|
562 |
$\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ &
|
|
563 |
$_{bs@bs'}a_1 \cdot a_2$\\
|
|
564 |
$\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ &
|
|
565 |
$_{bs @ bs'}a^*$
|
|
566 |
\end{tabular}
|
|
567 |
\end{center}
|
|
568 |
|
|
569 |
\noindent
|
575
|
570 |
With \emph{fuse} we are able to define the $\internalise$ function
|
|
571 |
that translates a ``standard'' regular expression into an
|
|
572 |
annotated regular expression.
|
|
573 |
This function will be applied before we start
|
|
574 |
with the derivative phase of the algorithm.
|
542
|
575 |
|
|
576 |
\begin{center}
|
|
577 |
\begin{tabular}{lcl}
|
|
578 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\
|
|
579 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\
|
|
580 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\
|
|
581 |
$(r_1 + r_2)^\uparrow$ & $\dn$ &
|
|
582 |
$_{[]}\sum[\textit{fuse}\,[Z]\,r_1^\uparrow,\,
|
|
583 |
\textit{fuse}\,[S]\,r_2^\uparrow]$\\
|
|
584 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ &
|
|
585 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\
|
|
586 |
$(r^*)^\uparrow$ & $\dn$ &
|
|
587 |
$_{[]}(r^\uparrow)^*$\\
|
|
588 |
\end{tabular}
|
|
589 |
\end{center}
|
|
590 |
%\end{definition}
|
|
591 |
|
|
592 |
\noindent
|
|
593 |
We use an up arrow with postfix notation
|
575
|
594 |
to denote this operation.
|
542
|
595 |
for convenience. The $\textit{internalise} \; r$
|
|
596 |
notation is more cumbersome.
|
|
597 |
The opposite of $\textit{internalise}$ is
|
|
598 |
$\erase$, where all the bit-codes are removed,
|
|
599 |
and the alternative operator $\sum$ for annotated
|
|
600 |
regular expressions is transformed to the binary alternatives
|
|
601 |
for plain regular expressions.
|
|
602 |
\begin{center}
|
575
|
603 |
\begin{tabular}{lcl}
|
|
604 |
$\ZERO_\downarrow$ & $\dn$ & $\ZERO$\\
|
|
605 |
$( _{bs}\ONE )_\downarrow$ & $\dn$ & $\ONE$\\
|
|
606 |
$( _{bs}\mathbf{c} )_\downarrow$ & $\dn$ & $\mathbf{c}$\\
|
|
607 |
$( _{bs} a_1 \cdot a_2 )_\downarrow$ & $\dn$ &
|
|
608 |
$ (a_1) _\downarrow \cdot (a_2) _\downarrow$\\
|
|
609 |
$( _{bs} [])_\downarrow $ & $\dn$ & $\ZERO $\\
|
|
610 |
$( _{bs} [a] )_\downarrow$ & $\dn$ & $a_\downarrow$\\
|
|
611 |
$_{bs} \sum [a_1, \; a_2]$ & $\dn$ & $ (a_1) _\downarrow + ( a_2 ) _\downarrow $\\
|
|
612 |
$(_{bs} \sum (a :: as))_\downarrow$ & $\dn$ & $ a_\downarrow + \; (_{[]} \sum as)_\downarrow$\\
|
|
613 |
$( _{bs} a^* )_\downarrow$ & $\dn$ & $(a_\downarrow)^*$
|
542
|
614 |
\end{tabular}
|
|
615 |
\end{center}
|
|
616 |
\noindent
|
|
617 |
We also abbreviate the $\erase\; a$ operation
|
|
618 |
as $a_\downarrow$, for conciseness.
|
|
619 |
For bit-coded regular expressions, as a different datatype,
|
|
620 |
testing whether they contain empty string in their lauguage requires
|
|
621 |
a dedicated function $\bnullable$
|
|
622 |
which simply calls $\erase$ first before testing whether it is $\nullable$.
|
575
|
623 |
\begin{definition}
|
|
624 |
$\bnullable \; a \dn \nullable \; (a_\downarrow)$
|
|
625 |
\end{definition}
|
542
|
626 |
The function for collecting the
|
575
|
627 |
bitcodes at the end of the derivative
|
|
628 |
phase from a (b)nullable regular expression
|
542
|
629 |
is a generalised version of the $\textit{mkeps}$ function
|
|
630 |
for annotated regular expressions, called $\textit{bmkeps}$:
|
|
631 |
|
|
632 |
|
|
633 |
%\begin{definition}[\textit{bmkeps}]\mbox{}
|
|
634 |
\begin{center}
|
|
635 |
\begin{tabular}{lcl}
|
|
636 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\
|
|
637 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ &
|
|
638 |
$\textit{if}\;\textit{bnullable}\,a$\\
|
|
639 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\
|
|
640 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{[]}\sum \textit{as})$\\
|
|
641 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ &
|
|
642 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\
|
|
643 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ &
|
|
644 |
$bs \,@\, [Z]$
|
|
645 |
\end{tabular}
|
|
646 |
\end{center}
|
|
647 |
%\end{definition}
|
|
648 |
|
|
649 |
\noindent
|
575
|
650 |
$\bmkeps$ completes the value information by travelling along the
|
542
|
651 |
path of the regular expression that corresponds to a POSIX value and
|
575
|
652 |
collecting all the bitcodes, and attaching $S$ to indicate the end of star
|
542
|
653 |
iterations.
|
|
654 |
|
575
|
655 |
Now we give out the central part of this lexing algorithm,
|
|
656 |
the $\bder$ function (stands for \emph{b}itcoded-derivative).
|
|
657 |
For most time we use the infix notation $(\_\backslash\_)$
|
|
658 |
to mean $\bder$ for brevity when
|
|
659 |
there is no danger of confusion with derivatives on plain regular expressions.
|
|
660 |
For example, we write $( _{[]}r^* ) \backslash c$ instead of $\bder \;c \; _{[]}r^*$,
|
|
661 |
as the bitcodes at the front of $r^*$ indicates that it is
|
|
662 |
a bit-coded regular expression, not a plain one.
|
|
663 |
$\bder$ tells us how regular expressions can be recursively traversed,
|
|
664 |
where the bitcodes are augmented and carried around
|
|
665 |
when a derivative is taken.
|
|
666 |
\begin{center}
|
|
667 |
\begin{tabular}{@{}lcl@{}}
|
|
668 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
669 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\
|
|
670 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ &
|
|
671 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
672 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\
|
|
673 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ &
|
|
674 |
$_{bs}\sum\;(\textit{map} \; (\_\backslash c) \; as )$\\
|
|
675 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
676 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
677 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
678 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
679 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\
|
|
680 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
|
681 |
$_{bs}(\textit{fuse}\, [Z] \; r\,\backslash c)\cdot
|
|
682 |
(_{[]}r^*))$
|
|
683 |
\end{tabular}
|
|
684 |
\end{center}
|
|
685 |
\noindent
|
|
686 |
We give the intuition behind some of the more involved cases in
|
|
687 |
$\bder$. For example,
|
|
688 |
in the \emph{star} case,
|
|
689 |
a derivative on $_{bs}a^*$ means
|
|
690 |
that one more star iteratoin needs to be taken.
|
542
|
691 |
we need to unfold it into a sequence,
|
|
692 |
and attach an additional bit $Z$ to the front of $r \backslash c$
|
575
|
693 |
as a record to indicate one new star iteration is unfolded.
|
538
|
694 |
|
|
695 |
\noindent
|
|
696 |
\begin{center}
|
|
697 |
\begin{tabular}{@{}lcl@{}}
|
|
698 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ &
|
575
|
699 |
$_{bs}(\underbrace{\textit{fuse}\, [Z] \; a\,\backslash c}_{\text{One more iteration}})\cdot
|
538
|
700 |
(_{[]}a^*))$
|
|
701 |
\end{tabular}
|
|
702 |
\end{center}
|
|
703 |
|
542
|
704 |
\noindent
|
575
|
705 |
This information will be recovered later by the $\decode$ function.
|
|
706 |
The intuition is that the bit $Z$ will be decoded at the right location,
|
|
707 |
because we accumulate bits from left to right (a rigorous proof will be given
|
|
708 |
later).
|
542
|
709 |
|
575
|
710 |
\begin{tikzpicture}[ > = stealth, % arrow head style
|
|
711 |
shorten > = 1pt, % don't touch arrow head to node
|
|
712 |
semithick % line style
|
|
713 |
]
|
|
714 |
|
|
715 |
\tikzstyle{every state}=[
|
|
716 |
draw = black,
|
|
717 |
thin,
|
|
718 |
fill = cyan!29,
|
|
719 |
minimum size = 7mm
|
|
720 |
]
|
|
721 |
\begin{scope}[node distance=1cm and 0cm, every node/.style=state]
|
|
722 |
\node (k) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
538
|
723 |
{$bs$
|
|
724 |
\nodepart{two} $a^*$ };
|
575
|
725 |
\node (l) [below =of k, rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
726 |
{ $bs$ + [Z]
|
|
727 |
\nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
728 |
\end{scope}
|
|
729 |
\path[->]
|
|
730 |
(k) edge (l);
|
|
731 |
\end{tikzpicture}
|
|
732 |
|
|
733 |
Pictorially the process looks like below.
|
|
734 |
Like before, the red region denotes
|
|
735 |
previous lexing information (stored as bitcodes in $bs$).
|
|
736 |
|
538
|
737 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
575
|
738 |
\begin{scope}[node distance=1cm]
|
|
739 |
\node (a) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
|
740 |
{$bs$
|
|
741 |
\nodepart{two} $a^*$ };
|
|
742 |
\node (b) [rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={red!30,blue!20},]
|
538
|
743 |
{ $bs$ + [Z]
|
|
744 |
\nodepart{two} $(a\backslash c )\cdot a^*$ };
|
|
745 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
575
|
746 |
\end{scope}
|
538
|
747 |
\end{tikzpicture}
|
575
|
748 |
|
538
|
749 |
\noindent
|
|
750 |
Another place in the $\bder$ function where it differs
|
542
|
751 |
from normal derivatives (on un-annotated regular expressions)
|
538
|
752 |
is the sequence case:
|
|
753 |
\begin{center}
|
|
754 |
\begin{tabular}{@{}lcl@{}}
|
|
755 |
|
|
756 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ &
|
|
757 |
$\textit{if}\;\textit{bnullable}\,a_1$\\
|
|
758 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\
|
|
759 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\
|
|
760 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$
|
|
761 |
\end{tabular}
|
|
762 |
\end{center}
|
542
|
763 |
|
|
764 |
The difference is that (when $a_1$ is $\bnullable$)
|
|
765 |
we use $\bmkeps$ to store the lexing information
|
|
766 |
in $a_1$ before collapsing it (as it has been fully matched by string prior to $c$,
|
|
767 |
and attach the collected bit-codes to the front of $a_2$
|
|
768 |
before throwing away $a_1$. We assume that $\bmkeps$ correctly extracts the bitcode for how $a_1$
|
|
769 |
matches the string prior to $c$ (more on this later).
|
|
770 |
The bitsequence $\textit{bs}$ which was initially attached to the first element of the sequence
|
|
771 |
$a_1 \cdot a_2$, has now been elevated to the top level of teh $\sum$.
|
|
772 |
This is because this piece of information will be needed whichever way the sequence is matched,
|
|
773 |
regardless of whether $c$ belongs to $a_1$ or $a_2$.
|
538
|
774 |
|
542
|
775 |
In the injection-based lexing, $r_1$ is immediately thrown away in
|
|
776 |
subsequent derivatives on the right branch (when $r_1$ is $\nullable$),
|
|
777 |
\begin{center}
|
|
778 |
$(r_1 \cdot r_2 )\backslash c = (r_1 \backslash c) \cdot r_2 + r_2 \backslash c$
|
|
779 |
\end{center}
|
|
780 |
\noindent
|
|
781 |
as it knows $r_1$ is stored on stack and available once the recursive
|
|
782 |
call to later derivatives finish.
|
|
783 |
Therefore, if the $\Right$ branch is taken in a $\POSIX$ match,
|
|
784 |
we construct back the sequence value once step back by
|
|
785 |
calling a on $\mkeps(r_1)$.
|
|
786 |
\begin{center}
|
|
787 |
\begin{tabular}{lcr}
|
|
788 |
$\ldots r_1 \cdot r_2$ & $\rightarrow$ & $r_1\cdot r_2 + r_2 \backslash c \ldots $\\
|
|
789 |
$\ldots \Seq(v_1, v_2) (\Seq(\mkeps(r1), (\inj \; r_2 \; c\; v_{2c})))$ & $\leftarrow$ & $\Right(v_{2c})\ldots$
|
|
790 |
\end{tabular}
|
|
791 |
\end{center}
|
|
792 |
\noindent
|
|
793 |
The rest of the clauses of $\bder$ is rather similar to
|
|
794 |
$\der$, and is put together here as a wholesome definition
|
|
795 |
for $\bder$:
|
|
796 |
\noindent
|
|
797 |
Generalising the derivative operation with bitcodes to strings, we have
|
532
|
798 |
\begin{center}
|
542
|
799 |
\begin{tabular}{@{}lcl@{}}
|
|
800 |
$a\backslash_s [] $ & $\dn$ & $a$\\
|
|
801 |
$a\backslash (c :: s) $ & $\dn$ & $(a \backslash c) \backslash_s s$
|
|
802 |
\end{tabular}
|
|
803 |
\end{center}
|
|
804 |
As we did earlier, we omit the $s$ subscript at $\backslash_s$ when there is no danger
|
|
805 |
of confusion.
|
|
806 |
Putting this all together, we obtain a lexer with bit-coded regular expressions
|
|
807 |
as its internal data structures, which we call $\blexer$:
|
532
|
808 |
|
|
809 |
\begin{center}
|
|
810 |
\begin{tabular}{lcl}
|
|
811 |
$\textit{blexer}\;r\,s$ & $\dn$ &
|
|
812 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\
|
|
813 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
814 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
815 |
& & $\;\;\textit{else}\;\textit{None}$
|
|
816 |
\end{tabular}
|
|
817 |
\end{center}
|
|
818 |
|
|
819 |
\noindent
|
542
|
820 |
Ausaf and Urban formally proved the correctness of the $\blexer$, namely
|
|
821 |
\begin{conjecture}
|
|
822 |
$\blexer \;r \; s = \lexer \; r \; s$.
|
|
823 |
\end{conjecture}
|
|
824 |
This was claimed but not formalised in Sulzmann and Lu's work.
|
|
825 |
We introduce the proof later, after we give out all
|
|
826 |
the needed auxiliary functions and definitions
|
532
|
827 |
|
|
828 |
|
|
829 |
%-----------------------------------
|
|
830 |
% SUBSECTION 1
|
|
831 |
%-----------------------------------
|
|
832 |
\section{Specifications of Some Helper Functions}
|
542
|
833 |
The functions we introduce will give a more detailed glimpse into
|
|
834 |
the lexing process, which might not be possible
|
|
835 |
using $\lexer$ or $\blexer$ themselves.
|
|
836 |
The first function we shall look at is $\retrieve$.
|
543
|
837 |
\subsection{$\textit{Retrieve}$}
|
542
|
838 |
Our bit-coded lexer "retrieve"s the bitcodes using $\bmkeps$
|
|
839 |
after we finished doing all the derivatives:
|
532
|
840 |
\begin{center}
|
542
|
841 |
\begin{tabular}{lcl}
|
|
842 |
& & $\ldots$\\
|
|
843 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\
|
|
844 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\
|
|
845 |
& & $\ldots$
|
532
|
846 |
\end{tabular}
|
|
847 |
\end{center}
|
542
|
848 |
\noindent
|
|
849 |
Recall that $\bmkeps$ looks for the leftmost branch of an alternative
|
|
850 |
and completes a star's iterations by attaching a $Z$ at the end of the bitcodes
|
|
851 |
extracted. It "retrieves" a sequence by visiting both children and then stitch together
|
|
852 |
two bitcodes using concatenation. After the entire tree structure of the regular
|
|
853 |
expression has been traversed using the above manner, we get a bitcode encoding the
|
|
854 |
lexing result.
|
|
855 |
We know that this "retrieved" bitcode leads to the correct value after decoding,
|
|
856 |
which is $v_0$ in the bird's eye view of the injection-based lexing diagram.
|
|
857 |
Now assume we keep every other data structure in the diagram \ref{InjFigure},
|
|
858 |
and only replace all the plain regular expression by their annotated counterparts,
|
|
859 |
computed during a $\blexer$ run.
|
|
860 |
Then we obtain a diagram for the annotated regular expression derivatives and
|
|
861 |
their corresponding values, though the values are never calculated in $\blexer$.
|
|
862 |
We have that $a_n$ contains all the lexing result information.
|
|
863 |
\vspace{20mm}
|
|
864 |
\begin{center}%\label{graph:injLexer}
|
|
865 |
\begin{tikzcd}[
|
|
866 |
every matrix/.append style = {name=p},
|
|
867 |
remember picture, overlay,
|
|
868 |
]
|
|
869 |
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] \\
|
|
870 |
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]
|
|
871 |
\end{tikzcd}
|
|
872 |
\begin{tikzpicture}[
|
|
873 |
remember picture, overlay,
|
|
874 |
E/.style = {ellipse, draw=blue, dashed,
|
|
875 |
inner xsep=4mm,inner ysep=-4mm, rotate=90, fit=#1}
|
|
876 |
]
|
|
877 |
\node[E = (p-1-1) (p-2-1)] {};
|
|
878 |
\node[E = (p-1-4) (p-2-4)] {};
|
|
879 |
\end{tikzpicture}
|
|
880 |
|
|
881 |
\end{center}
|
|
882 |
\vspace{20mm}
|
|
883 |
\noindent
|
|
884 |
On the other hand, $v_0$ also encodes the correct lexing result, as we have proven for $\lexer$.
|
|
885 |
Encircled in the diagram are the two pairs $v_0, a_0$ and $v_n, a_n$, which both
|
|
886 |
encode the correct lexical result. Though for the leftmost pair, we have
|
|
887 |
the information condensed in $v_0$ the value part, whereas for the rightmost pair,
|
|
888 |
the information is concentrated on $a_n$.
|
|
889 |
We know that in the intermediate steps the pairs $v_i, a_i$, must in some way encode the complete
|
|
890 |
lexing information as well. Therefore, we need a unified approach to extract such lexing result
|
|
891 |
from a value $v_i$ and its annotated regular expression $a_i$.
|
|
892 |
And the function $f$ must satisfy these requirements:
|
|
893 |
\begin{itemize}
|
|
894 |
\item
|
|
895 |
$f \; a_i\;v_i = f \; a_n \; v_n = \decode \; (\bmkeps \; a_n) \; (\erase \; a_0)$
|
|
896 |
\item
|
|
897 |
$f \; a_i\;v_i = f \; a_0 \; v_0 = v_0 = \decode \;(\code \; v_0) \; (\erase \; a_0)$
|
|
898 |
\end{itemize}
|
|
899 |
\noindent
|
|
900 |
If we factor out the common part $\decode \; \_ \; (\erase \; a_0)$,
|
|
901 |
The core of the function $f$ is something that produces the bitcodes
|
|
902 |
$\code \; v_0$.
|
|
903 |
It is unclear how, but Sulzmann and Lu came up with a function satisfying all the above
|
|
904 |
requirements, named \emph{retrieve}:
|
532
|
905 |
|
|
906 |
|
|
907 |
|
542
|
908 |
\begin{center}
|
|
909 |
\begin{tabular}{lcr}
|
|
910 |
$\retrieve \; \, (_{bs} \ONE) \; \, (\Empty)$ & $\dn$ & $\textit{bs}$\\
|
|
911 |
$\retrieve \; \, (_{bs} \mathbf{c} ) \; \Char(c)$ & $\dn$ & $ \textit{bs}$\\
|
|
912 |
$\retrieve \; \, (_{bs} a_1 \cdot a_2) \; \Seq(v_1, v_2)$ & $\dn$ & $\textit{bs} @ (\retrieve \; a_1\; v_1) @ (\retrieve \; a_2 \; v_2)$\\
|
|
913 |
$\retrieve \; \, (_{bs} \Sigma (a :: \textit{as}) \; \,\Left(v)$ & $\dn$ & $\textit{bs} @ (\retrieve \; a \; v)$\\
|
|
914 |
$\retrieve \; \, (_{bs} \Sigma (a :: \textit{as} \; \, \Right(v)$ & $\dn$ & $\textit{bs} @ (\retrieve \; (_{[]}\Sigma \textit{as}) \; v)$\\
|
|
915 |
$\retrieve \; \, (_{bs} a^*) \; \, (\Stars(v :: vs)) $ & $\dn$ & $\textit{bs} @ (\retrieve \; a \; v) @ (\retrieve \; (_{[]} a^*) \; (\Stars(vs)))$\\
|
|
916 |
$\retrieve \; \, (_{bs} a^*) \; \, (\Stars([]) $ & $\dn$ & $\textit{bs} @ [Z]$
|
|
917 |
\end{tabular}
|
|
918 |
\end{center}
|
|
919 |
\noindent
|
|
920 |
As promised, $\retrieve$ collects the right bit-codes from the
|
|
921 |
final derivative $a_n$:
|
|
922 |
\begin{lemma}
|
|
923 |
$\bnullable \; a \implies \bmkeps \; a = \retrieve \; a \; (\mkeps \; (\erase \; a))$
|
|
924 |
\end{lemma}
|
|
925 |
\begin{proof}
|
|
926 |
By a routine induction on $a$.
|
|
927 |
\end{proof}
|
|
928 |
The design of $\retrieve$ enables extraction of bit-codes
|
|
929 |
from not only $\bnullable$ (annotated) regular expressions,
|
|
930 |
but also those that are not $\bnullable$.
|
|
931 |
For example, if we have the regular expression just internalised
|
|
932 |
and the lexing result value, we could $\retrieve$ the bitcdoes
|
|
933 |
that look as if we have en$\code$-ed the value:
|
|
934 |
\begin{lemma}
|
|
935 |
$\vdash v : r \implies \retrieve \; (r)^\uparrow \; v = \code \; v$
|
|
936 |
\end{lemma}
|
|
937 |
\begin{proof}
|
|
938 |
By induction on $r$.
|
|
939 |
\end{proof}
|
|
940 |
\noindent
|
|
941 |
The following property is more interesting, as
|
|
942 |
it provides a "bridge" between $a_0, v_0$ and $a_n, v_n$ in the
|
|
943 |
lexing diagram.
|
|
944 |
If you take derivative of an annotated regular expression,
|
|
945 |
you can $\retrieve$ the same bit-codes as before the derivative took place,
|
|
946 |
provided that you use the corresponding value:
|
|
947 |
|
|
948 |
\begin{lemma}\label{retrieveStepwise}
|
|
949 |
$\vdash v : (r\backslash c) \implies \retrieve \; (r \backslash c) \; v= \retrieve \; r \; (\inj \; r\; c\; v)$
|
|
950 |
\end{lemma}
|
|
951 |
\begin{proof}
|
|
952 |
By induction on $r$, where $v$ is allowed to be arbitrary.
|
|
953 |
The induction principle is function $\erase$'s cases.
|
|
954 |
\end{proof}
|
|
955 |
\noindent
|
|
956 |
$\retrieve$ is connected to the $\blexer$ in the following way:
|
|
957 |
\begin{lemma}\label{blexer_retrieve}
|
|
958 |
$\blexer \; r \; s = \decode \; (\retrieve \; (\internalise \; r) \; (\mkeps \; (r \backslash s) )) \; r$
|
|
959 |
\end{lemma}
|
|
960 |
\noindent
|
564
|
961 |
$\retrieve$ allows free navigation on the diagram \ref{InjFigure} for annotated regular expressiones of $\blexer$.
|
542
|
962 |
For plain regular expressions something similar is required as well.
|
|
963 |
|
|
964 |
\subsection{$\flex$}
|
|
965 |
Ausaf and Urban cleverly defined an auxiliary function called $\flex$ for $\lexer$,
|
532
|
966 |
defined as
|
536
|
967 |
\begin{center}
|
|
968 |
\begin{tabular}{lcr}
|
|
969 |
$\flex \; r \; f \; [] \; v$ & $=$ & $f\; v$\\
|
|
970 |
$\flex \; r \; f \; c :: s \; v$ & $=$ & $\flex \; r \; \lambda v. \, f (\inj \; r\; c\; v)\; s \; v$
|
|
971 |
\end{tabular}
|
|
972 |
\end{center}
|
532
|
973 |
which accumulates the characters that needs to be injected back,
|
|
974 |
and does the injection in a stack-like manner (last taken derivative first injected).
|
|
975 |
$\flex$ is connected to the $\lexer$:
|
|
976 |
\begin{lemma}
|
|
977 |
$\flex \; r \; \textit{id}\; s \; \mkeps (r\backslash s) = \lexer \; r \; s$
|
|
978 |
\end{lemma}
|
542
|
979 |
\begin{proof}
|
|
980 |
By reverse induction on $s$.
|
|
981 |
\end{proof}
|
|
982 |
$\flex$ provides us a bridge between $\lexer$'s intermediate steps.
|
532
|
983 |
What is even better about $\flex$ is that it allows us to
|
|
984 |
directly operate on the value $\mkeps (r\backslash v)$,
|
|
985 |
which is pivotal in the definition of $\lexer $ and $\blexer$, but not visible as an argument.
|
|
986 |
When the value created by $\mkeps$ becomes available, one can
|
|
987 |
prove some stepwise properties of lexing nicely:
|
|
988 |
\begin{lemma}\label{flexStepwise}
|
|
989 |
$\textit{flex} \; r \; f \; s@[c] \; v= \flex \; r \; f\; s \; (\inj \; (r\backslash s) \; c \; v) $
|
|
990 |
\end{lemma}
|
542
|
991 |
\begin{proof}
|
|
992 |
By induction on the shape of $r\backslash s$
|
|
993 |
\end{proof}
|
|
994 |
\noindent
|
|
995 |
With $\flex$ and $\retrieve$ ready, we are ready to connect $\lexer$ and $\blexer$ .
|
532
|
996 |
|
542
|
997 |
\subsection{Correctness Proof of Bit-coded Algorithm}
|
532
|
998 |
\begin{lemma}\label{flex_retrieve}
|
|
999 |
$\flex \; r \; \textit{id}\; s\; v = \decode \; (\retrieve \; (r\backslash s )\; v) \; r$
|
|
1000 |
\end{lemma}
|
|
1001 |
\begin{proof}
|
|
1002 |
By induction on $s$. The induction tactic is reverse induction on strings.
|
|
1003 |
$v$ is allowed to be arbitrary.
|
|
1004 |
The crucial point is to rewrite
|
|
1005 |
\[
|
|
1006 |
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c])
|
|
1007 |
\]
|
|
1008 |
as
|
|
1009 |
\[
|
|
1010 |
\retrieve \; (r \backslash s) \; (\inj \; (r \backslash s) \; c\; \mkeps (r \backslash s@[c]))
|
|
1011 |
\].
|
|
1012 |
This enables us to equate
|
|
1013 |
\[
|
|
1014 |
\retrieve \; (r \backslash s@[c]) \; \mkeps (r \backslash s@[c])
|
|
1015 |
\]
|
|
1016 |
with
|
|
1017 |
\[
|
|
1018 |
\flex \; r \; \textit{id} \; s \; (\inj \; (r\backslash s) \; c\; (\mkeps (r\backslash s@[c])))
|
|
1019 |
\],
|
|
1020 |
which in turn can be rewritten as
|
|
1021 |
\[
|
|
1022 |
\flex \; r \; \textit{id} \; s@[c] \; (\mkeps (r\backslash s@[c]))
|
|
1023 |
\].
|
|
1024 |
\end{proof}
|
|
1025 |
|
|
1026 |
With the above lemma we can now link $\flex$ and $\blexer$.
|
|
1027 |
|
542
|
1028 |
%----------------------------------------------------------------------------------------
|
|
1029 |
% SECTION correctness proof
|
|
1030 |
%----------------------------------------------------------------------------------------
|
|
1031 |
\section{Correctness of Bit-coded Algorithm (Without Simplification)}
|
|
1032 |
We now give the proof the correctness of the algorithm with bit-codes.
|
|
1033 |
|
532
|
1034 |
\begin{lemma}\label{flex_blexer}
|
|
1035 |
$\textit{flex} \; r \; \textit{id} \; s \; \mkeps(r \backslash s) = \blexer \; r \; s$
|
|
1036 |
\end{lemma}
|
|
1037 |
\begin{proof}
|
|
1038 |
Using two of the above lemmas: \ref{flex_retrieve} and \ref{blexer_retrieve}.
|
|
1039 |
\end{proof}
|
542
|
1040 |
Finally the correctness of $\blexer$ is given as it outputs the same result as $\lexer$:
|
|
1041 |
\begin{theorem}
|
|
1042 |
$\blexer\; r \; s = \lexer \; r \; s$
|
|
1043 |
\end{theorem}
|
|
1044 |
\begin{proof}
|
|
1045 |
Straightforward corollary of \ref{flex_blexer}.
|
|
1046 |
\end{proof}
|
|
1047 |
\noindent
|
576
|
1048 |
To piece things together and spell out the exact correctness
|
|
1049 |
of the bitcoded lexer
|
|
1050 |
in terms of producing POSIX values,
|
|
1051 |
we use the fact from the previous chapter that
|
|
1052 |
\[
|
|
1053 |
If \; (r, s) \rightarrow v \; then \; \lexer \; r \; s = v
|
|
1054 |
\]
|
|
1055 |
to obtain this corollary:
|
|
1056 |
\begin{corollary}\label{blexerPOSIX}
|
|
1057 |
$If \; (r, s) \rightarrow v \; then \blexer \; r \; s = v$
|
|
1058 |
\end{corollary}
|
542
|
1059 |
Our main reason for wanting a bit-coded algorithm over
|
|
1060 |
the injection-based one is for its capabilities of allowing
|
|
1061 |
more aggressive simplifications.
|
|
1062 |
We will elaborate on this in the next chapter.
|
532
|
1063 |
|
|
1064 |
|