author | Chengsong |
Mon, 30 May 2022 14:41:09 +0100 | |
changeset 528 | 28751de4b4ba |
parent 526 | cb702fb4227f |
child 529 | 96e93df60954 |
permissions | -rwxr-xr-x |
468 | 1 |
% Chapter Template |
2 |
||
528 | 3 |
\chapter{Regular Expressions and POSIX Lexing} % Main chapter title |
519 | 4 |
|
5 |
\label{Chapter2} % In chapter 2 \ref{Chapter2} we will introduce the concepts |
|
6 |
%and notations we |
|
7 |
%use for describing the lexing algorithm by Sulzmann and Lu, |
|
8 |
%and then give the algorithm and its variant, and discuss |
|
9 |
%why more aggressive simplifications are needed. |
|
10 |
||
11 |
||
528 | 12 |
\section{Basic Concepts and Notations for Strings, Languages, and Regular Expressions} |
13 |
We have a built-in datatype char, made up of characters, which we do not define |
|
14 |
on top of anything else. |
|
15 |
\begin{center} |
|
16 |
\begin{tabular}{lcl} |
|
17 |
$\textit{char}$ & $\dn$ & $a | b | c | \ldots$\\ |
|
18 |
\end{tabular} |
|
19 |
\end{center} |
|
20 |
They can form strings by lists: |
|
21 |
\begin{center} |
|
22 |
\begin{tabular}{lcl} |
|
23 |
$\textit{string}$ & $\dn$ & $[] | c :: cs$\\ |
|
24 |
& & $(c\; \text{has char type})$ |
|
25 |
\end{tabular} |
|
26 |
\end{center} |
|
27 |
And strings can be concatenated to form longer strings: |
|
28 |
\begin{center} |
|
29 |
\begin{tabular}{lcl} |
|
30 |
$s_1 @ s_2$ & $\rightarrow$ & $s'$\\ |
|
31 |
\end{tabular} |
|
32 |
\end{center} |
|
519 | 33 |
|
528 | 34 |
A set of strings can operate with another set of strings: |
35 |
\begin{center} |
|
36 |
\begin{tabular}{lcl} |
|
37 |
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A; s_B \in B \}$\\ |
|
38 |
\end{tabular} |
|
39 |
\end{center} |
|
40 |
We also call the above "language concatenation". |
|
41 |
The power of a language is defined recursively, using the |
|
42 |
concatenation operator $@$: |
|
43 |
\begin{center} |
|
44 |
\begin{tabular}{lcl} |
|
45 |
$A^0 $ & $\dn$ & $\{ [] \}$\\ |
|
46 |
$A^{n+1}$ & $\dn$ & $A^n @ A$ |
|
47 |
\end{tabular} |
|
48 |
\end{center} |
|
49 |
The infinite set of all the power of a language unioned together |
|
50 |
is defined using the power operator, also in recursive function: |
|
51 |
\begin{center} |
|
52 |
\begin{tabular}{lcl} |
|
53 |
$A^*$ & $\dn$ & $\bigcup_{i \geq 0} A^i$\\ |
|
54 |
\end{tabular} |
|
55 |
\end{center} |
|
56 |
We also define an operation of chopping off a character from all the strings |
|
57 |
in a set: |
|
58 |
\begin{center} |
|
59 |
\begin{tabular}{lcl} |
|
60 |
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\ |
|
61 |
\end{tabular} |
|
62 |
\end{center} |
|
63 |
With the above definitions, it becomes natural to define regular expressions |
|
64 |
as a concise way for expressing the languages. |
|
65 |
\section{Regular Expressions and Their Language Interpretation} |
|
519 | 66 |
Suppose we have an alphabet $\Sigma$, the strings whose characters |
67 |
are from $\Sigma$ |
|
68 |
can be expressed as $\Sigma^*$. |
|
69 |
||
70 |
We use patterns to define a set of strings concisely. Regular expressions |
|
71 |
are one of such patterns systems: |
|
72 |
The basic regular expressions are defined inductively |
|
73 |
by the following grammar: |
|
74 |
\[ r ::= \ZERO \mid \ONE |
|
75 |
\mid c |
|
76 |
\mid r_1 \cdot r_2 |
|
77 |
\mid r_1 + r_2 |
|
78 |
\mid r^* |
|
79 |
\] |
|
80 |
||
81 |
The language or set of strings defined by regular expressions are defined as |
|
82 |
%TODO: FILL in the other defs |
|
83 |
\begin{center} |
|
84 |
\begin{tabular}{lcl} |
|
85 |
$L \; (r_1 + r_2)$ & $\dn$ & $ L \; (r_1) \cup L \; ( r_2)$\\ |
|
86 |
$L \; (r_1 \cdot r_2)$ & $\dn$ & $ L \; (r_1) \cap L \; (r_2)$\\ |
|
87 |
\end{tabular} |
|
88 |
\end{center} |
|
89 |
Which are also called the "language interpretation". |
|
90 |
||
91 |
||
92 |
||
93 |
The Brzozowski derivative w.r.t character $c$ is an operation on the regex, |
|
94 |
where the operation transforms the regex to a new one containing |
|
95 |
strings without the head character $c$. |
|
96 |
||
97 |
Formally, we define first such a transformation on any string set, which |
|
98 |
we call semantic derivative: |
|
99 |
\begin{center} |
|
100 |
$\Der \; c\; \textit{A} = \{s \mid c :: s \in A\}$ |
|
101 |
\end{center} |
|
102 |
Mathematically, it can be expressed as the |
|
103 |
||
528 | 104 |
If the $\textit{A}$ happen to have some structure, for example, |
519 | 105 |
if it is regular, then we have that it |
106 |
||
107 |
% Derivatives of a |
|
108 |
%regular expression, written $r \backslash c$, give a simple solution |
|
109 |
%to the problem of matching a string $s$ with a regular |
|
110 |
%expression $r$: if the derivative of $r$ w.r.t.\ (in |
|
111 |
%succession) all the characters of the string matches the empty string, |
|
112 |
%then $r$ matches $s$ (and {\em vice versa}). |
|
113 |
||
528 | 114 |
|
115 |
\section{Brzozowski Derivatives of Regular Expressions} |
|
519 | 116 |
The the derivative of regular expression, denoted as |
117 |
$r \backslash c$, is a function that takes parameters |
|
118 |
$r$ and $c$, and returns another regular expression $r'$, |
|
119 |
which is computed by the following recursive function: |
|
120 |
||
121 |
\begin{center} |
|
122 |
\begin{tabular}{lcl} |
|
123 |
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\ |
|
124 |
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\ |
|
125 |
$d \backslash c$ & $\dn$ & |
|
126 |
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\ |
|
127 |
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\ |
|
128 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, nullable(r_1)$\\ |
|
129 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\ |
|
130 |
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\ |
|
131 |
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\ |
|
132 |
\end{tabular} |
|
133 |
\end{center} |
|
134 |
\noindent |
|
528 | 135 |
The function derivative, written $r\backslash c$, |
136 |
defines how a regular expression evolves into |
|
137 |
a new regular expression after all the string it contains |
|
138 |
is chopped off a certain head character $c$. |
|
139 |
The most involved cases are the sequence |
|
140 |
and star case. |
|
141 |
The sequence case says that if the first regular expression |
|
142 |
contains an empty string then the second component of the sequence |
|
143 |
might be chosen as the target regular expression to be chopped |
|
144 |
off its head character. |
|
145 |
The star regular expression's derivative unwraps the iteration of |
|
146 |
regular expression and attaches the star regular expression |
|
147 |
to the sequence's second element to make sure a copy is retained |
|
148 |
for possible more iterations in later phases of lexing. |
|
149 |
||
519 | 150 |
|
151 |
The $\nullable$ function tests whether the empty string $""$ |
|
152 |
is in the language of $r$: |
|
153 |
||
154 |
||
155 |
\begin{center} |
|
156 |
\begin{tabular}{lcl} |
|
157 |
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\ |
|
158 |
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\ |
|
159 |
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\ |
|
160 |
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\ |
|
161 |
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\ |
|
162 |
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\ |
|
163 |
\end{tabular} |
|
164 |
\end{center} |
|
165 |
\noindent |
|
166 |
The empty set does not contain any string and |
|
167 |
therefore not the empty string, the empty string |
|
168 |
regular expression contains the empty string |
|
169 |
by definition, the character regular expression |
|
170 |
is the singleton that contains character only, |
|
171 |
and therefore does not contain the empty string, |
|
528 | 172 |
the alternative regular expression (or "or" expression) |
519 | 173 |
might have one of its children regular expressions |
174 |
being nullable and any one of its children being nullable |
|
175 |
would suffice. The sequence regular expression |
|
176 |
would require both children to have the empty string |
|
177 |
to compose an empty string and the Kleene star |
|
528 | 178 |
operation naturally introduced the empty string. |
519 | 179 |
|
528 | 180 |
We have the following property where the derivative on regular |
181 |
expressions coincides with the derivative on a set of strings: |
|
182 |
||
183 |
\begin{lemma} |
|
184 |
$\textit{Der} \; c \; L(r) = L (r\backslash c)$ |
|
185 |
\end{lemma} |
|
186 |
||
519 | 187 |
\noindent |
188 |
||
189 |
||
190 |
The main property of the derivative operation |
|
191 |
that enables us to reason about the correctness of |
|
192 |
an algorithm using derivatives is |
|
193 |
||
194 |
\begin{center} |
|
195 |
$c\!::\!s \in L(r)$ holds |
|
196 |
if and only if $s \in L(r\backslash c)$. |
|
197 |
\end{center} |
|
198 |
||
199 |
\noindent |
|
200 |
We can generalise the derivative operation shown above for single characters |
|
201 |
to strings as follows: |
|
202 |
||
203 |
\begin{center} |
|
204 |
\begin{tabular}{lcl} |
|
205 |
$r \backslash (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash s$ \\ |
|
206 |
$r \backslash [\,] $ & $\dn$ & $r$ |
|
207 |
\end{tabular} |
|
208 |
\end{center} |
|
209 |
||
210 |
\noindent |
|
211 |
and then define Brzozowski's regular-expression matching algorithm as: |
|
212 |
||
528 | 213 |
\begin{definition} |
214 |
$match\;s\;r \;\dn\; nullable(r\backslash s)$ |
|
215 |
\end{definition} |
|
519 | 216 |
|
217 |
\noindent |
|
218 |
Assuming the a string is given as a sequence of characters, say $c_0c_1..c_n$, |
|
219 |
this algorithm presented graphically is as follows: |
|
220 |
||
221 |
\begin{equation}\label{graph:*} |
|
222 |
\begin{tikzcd} |
|
223 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed] & r_n \arrow[r,"\textit{nullable}?"] & \;\textrm{YES}/\textrm{NO} |
|
224 |
\end{tikzcd} |
|
225 |
\end{equation} |
|
226 |
||
227 |
\noindent |
|
228 |
where we start with a regular expression $r_0$, build successive |
|
229 |
derivatives until we exhaust the string and then use \textit{nullable} |
|
230 |
to test whether the result can match the empty string. It can be |
|
231 |
relatively easily shown that this matcher is correct (that is given |
|
232 |
an $s = c_0...c_{n-1}$ and an $r_0$, it generates YES if and only if $s \in L(r_0)$). |
|
233 |
||
234 |
Beautiful and simple definition. |
|
235 |
||
236 |
If we implement the above algorithm naively, however, |
|
237 |
the algorithm can be excruciatingly slow. |
|
238 |
||
239 |
||
240 |
\begin{figure} |
|
241 |
\centering |
|
242 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}} |
|
243 |
\begin{tikzpicture} |
|
244 |
\begin{axis}[ |
|
245 |
xlabel={$n$}, |
|
246 |
x label style={at={(1.05,-0.05)}}, |
|
247 |
ylabel={time in secs}, |
|
248 |
enlargelimits=false, |
|
249 |
xtick={0,5,...,30}, |
|
250 |
xmax=33, |
|
251 |
ymax=10000, |
|
252 |
ytick={0,1000,...,10000}, |
|
253 |
scaled ticks=false, |
|
254 |
axis lines=left, |
|
255 |
width=5cm, |
|
256 |
height=4cm, |
|
257 |
legend entries={JavaScript}, |
|
258 |
legend pos=north west, |
|
259 |
legend cell align=left] |
|
260 |
\addplot[red,mark=*, mark options={fill=white}] table {EightThousandNodes.data}; |
|
261 |
\end{axis} |
|
262 |
\end{tikzpicture}\\ |
|
263 |
\multicolumn{3}{c}{Graphs: Runtime for matching $(a^*)^*\,b$ with strings |
|
264 |
of the form $\underbrace{aa..a}_{n}$.} |
|
265 |
\end{tabular} |
|
266 |
\caption{EightThousandNodes} \label{fig:EightThousandNodes} |
|
267 |
\end{figure} |
|
268 |
||
269 |
||
270 |
(8000 node data to be added here) |
|
271 |
For example, when starting with the regular |
|
272 |
expression $(a + aa)^*$ and building a few successive derivatives (around 10) |
|
273 |
w.r.t.~the character $a$, one obtains a derivative regular expression |
|
274 |
with more than 8000 nodes (when viewed as a tree)\ref{EightThousandNodes}. |
|
275 |
The reason why $(a + aa) ^*$ explodes so drastically is that without |
|
276 |
pruning, the algorithm will keep records of all possible ways of matching: |
|
277 |
\begin{center} |
|
528 | 278 |
$(a + aa) ^* \backslash [aa] = (\ZERO + \ONE \ONE)\cdot(a + aa)^* + (\ONE + \ONE a) \cdot (a + aa)^*$ |
519 | 279 |
\end{center} |
280 |
||
281 |
\noindent |
|
282 |
Each of the above alternative branches correspond to the match |
|
283 |
$aa $, $a \quad a$ and $a \quad a \cdot (a)$(incomplete). |
|
284 |
These different ways of matching will grow exponentially with the string length, |
|
285 |
and without simplifications that throw away some of these very similar matchings, |
|
286 |
it is no surprise that these expressions grow so quickly. |
|
287 |
Operations like |
|
288 |
$\backslash$ and $\nullable$ need to traverse such trees and |
|
289 |
consequently the bigger the size of the derivative the slower the |
|
290 |
algorithm. |
|
291 |
||
292 |
Brzozowski was quick in finding that during this process a lot useless |
|
293 |
$\ONE$s and $\ZERO$s are generated and therefore not optimal. |
|
294 |
He also introduced some "similarity rules" such |
|
295 |
as $P+(Q+R) = (P+Q)+R$ to merge syntactically |
|
296 |
different but language-equivalent sub-regexes to further decrease the size |
|
297 |
of the intermediate regexes. |
|
298 |
||
299 |
More simplifications are possible, such as deleting duplicates |
|
300 |
and opening up nested alternatives to trigger even more simplifications. |
|
301 |
And suppose we apply simplification after each derivative step, and compose |
|
302 |
these two operations together as an atomic one: $a \backslash_{simp}\,c \dn |
|
303 |
\textit{simp}(a \backslash c)$. Then we can build |
|
528 | 304 |
a matcher with simpler regular expressions. |
519 | 305 |
|
306 |
If we want the size of derivatives in the algorithm to |
|
307 |
stay even lower, we would need more aggressive simplifications. |
|
308 |
Essentially we need to delete useless $\ZERO$s and $\ONE$s, as well as |
|
309 |
deleting duplicates whenever possible. For example, the parentheses in |
|
310 |
$(a+b) \cdot c + b\cdot c$ can be opened up to get $a\cdot c + b \cdot c + b |
|
311 |
\cdot c$, and then simplified to just $a \cdot c + b \cdot c$. Another |
|
312 |
example is simplifying $(a^*+a) + (a^*+ \ONE) + (a +\ONE)$ to just |
|
528 | 313 |
$a^*+a+\ONE$. These more aggressive simplification rules are for |
314 |
a very tight size bound, possibly as low |
|
315 |
as that of the \emph{partial derivatives}\parencite{Antimirov1995}. |
|
519 | 316 |
|
317 |
Building derivatives and then simplify them. |
|
318 |
So far so good. But what if we want to |
|
528 | 319 |
do lexing instead of just getting a YES/NO answer? |
519 | 320 |
This requires us to go back again to the world |
321 |
without simplification first for a moment. |
|
322 |
Sulzmann and Lu~\cite{Sulzmann2014} first came up with a nice and |
|
323 |
elegant(arguably as beautiful as the original |
|
324 |
derivatives definition) solution for this. |
|
325 |
||
326 |
\subsection*{Values and the Lexing Algorithm by Sulzmann and Lu} |
|
327 |
||
328 |
||
329 |
They first defined the datatypes for storing the |
|
330 |
lexing information called a \emph{value} or |
|
331 |
sometimes also \emph{lexical value}. These values and regular |
|
332 |
expressions correspond to each other as illustrated in the following |
|
333 |
table: |
|
334 |
||
335 |
\begin{center} |
|
336 |
\begin{tabular}{c@{\hspace{20mm}}c} |
|
337 |
\begin{tabular}{@{}rrl@{}} |
|
338 |
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\ |
|
339 |
$r$ & $::=$ & $\ZERO$\\ |
|
340 |
& $\mid$ & $\ONE$ \\ |
|
341 |
& $\mid$ & $c$ \\ |
|
342 |
& $\mid$ & $r_1 \cdot r_2$\\ |
|
343 |
& $\mid$ & $r_1 + r_2$ \\ |
|
344 |
\\ |
|
345 |
& $\mid$ & $r^*$ \\ |
|
346 |
\end{tabular} |
|
347 |
& |
|
348 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}} |
|
349 |
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\ |
|
350 |
$v$ & $::=$ & \\ |
|
351 |
& & $\Empty$ \\ |
|
352 |
& $\mid$ & $\Char(c)$ \\ |
|
353 |
& $\mid$ & $\Seq\,v_1\, v_2$\\ |
|
354 |
& $\mid$ & $\Left(v)$ \\ |
|
355 |
& $\mid$ & $\Right(v)$ \\ |
|
356 |
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\ |
|
357 |
\end{tabular} |
|
358 |
\end{tabular} |
|
359 |
\end{center} |
|
360 |
||
361 |
\noindent |
|
362 |
||
363 |
Building on top of Sulzmann and Lu's attempt to formalize the |
|
364 |
notion of POSIX lexing rules \parencite{Sulzmann2014}, |
|
365 |
Ausaf and Urban\parencite{AusafDyckhoffUrban2016} modelled |
|
366 |
POSIX matching as a ternary relation recursively defined in a |
|
367 |
natural deduction style. |
|
368 |
With the formally-specified rules for what a POSIX matching is, |
|
369 |
they proved in Isabelle/HOL that the algorithm gives correct results. |
|
370 |
||
371 |
But having a correct result is still not enough, |
|
372 |
we want at least some degree of $\mathbf{efficiency}$. |
|
373 |
||
374 |
||
375 |
||
376 |
One regular expression can have multiple lexical values. For example |
|
377 |
for the regular expression $(a+b)^*$, it has a infinite list of |
|
378 |
values corresponding to it: $\Stars\,[]$, $\Stars\,[\Left(Char(a))]$, |
|
379 |
$\Stars\,[\Right(Char(b))]$, $\Stars\,[\Left(Char(a),\,\Right(Char(b))]$, |
|
380 |
$\ldots$, and vice versa. |
|
381 |
Even for the regular expression matching a certain string, there could |
|
382 |
still be more than one value corresponding to it. |
|
383 |
Take the example where $r= (a^*\cdot a^*)^*$ and the string |
|
384 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$. |
|
528 | 385 |
If we do not allow any empty iterations in its lexical values, |
386 |
there will be $n - 1$ "splitting points" on $s$ we can choose to |
|
387 |
split or not so that each sub-string |
|
388 |
segmented by those chosen splitting points will form different iterations: |
|
389 |
\begin{center} |
|
390 |
\begin{tabular}{lcr} |
|
391 |
$a \mid aaa $ & $\rightarrow$ & $\Stars\, [v_{iteration \,a},\, v_{iteration \,aaa}]$\\ |
|
392 |
$aa \mid aa $ & $\rightarrow$ & $\Stars\, [v_{iteration \, aa},\, v_{iteration \, aa}]$\\ |
|
393 |
$a \mid aa\mid a $ & $\rightarrow$ & $\Stars\, [v_{iteration \, a},\, v_{iteration \, aa}, \, v_{iteration \, a}]$\\ |
|
394 |
& $\textit{etc}.$ & |
|
395 |
\end{tabular} |
|
396 |
\end{center} |
|
397 |
||
398 |
And for each iteration, there are still multiple ways to split |
|
399 |
between the two $a^*$s. |
|
400 |
It is not surprising there are exponentially many lexical values |
|
401 |
that are distinct for the regex and string pair $r= (a^*\cdot a^*)^*$ and |
|
402 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$. |
|
519 | 403 |
|
404 |
A lexer aimed at getting all the possible values has an exponential |
|
405 |
worst case runtime. Therefore it is impractical to try to generate |
|
406 |
all possible matches in a run. In practice, we are usually |
|
407 |
interested about POSIX values, which by intuition always |
|
408 |
\begin{itemize} |
|
409 |
\item |
|
410 |
match the leftmost regular expression when multiple options of matching |
|
411 |
are available |
|
412 |
\item |
|
413 |
always match a subpart as much as possible before proceeding |
|
414 |
to the next token. |
|
415 |
\end{itemize} |
|
416 |
||
417 |
||
418 |
For example, the above example has the POSIX value |
|
419 |
$ \Stars\,[\Seq(Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}], Stars\,[])]$. |
|
420 |
The output of an algorithm we want would be a POSIX matching |
|
421 |
encoded as a value. |
|
422 |
The reason why we are interested in $\POSIX$ values is that they can |
|
423 |
be practically used in the lexing phase of a compiler front end. |
|
424 |
For instance, when lexing a code snippet |
|
425 |
$\textit{iffoo} = 3$ with the regular expression $\textit{keyword} + \textit{identifier}$, we want $\textit{iffoo}$ to be recognized |
|
426 |
as an identifier rather than a keyword. |
|
427 |
||
428 |
The contribution of Sulzmann and Lu is an extension of Brzozowski's |
|
429 |
algorithm by a second phase (the first phase being building successive |
|
430 |
derivatives---see \eqref{graph:*}). In this second phase, a POSIX value |
|
431 |
is generated in case the regular expression matches the string. |
|
432 |
Pictorially, the Sulzmann and Lu algorithm is as follows: |
|
433 |
||
434 |
\begin{ceqn} |
|
435 |
\begin{equation}\label{graph:2} |
|
436 |
\begin{tikzcd} |
|
437 |
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] \\ |
|
438 |
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] |
|
439 |
\end{tikzcd} |
|
440 |
\end{equation} |
|
441 |
\end{ceqn} |
|
442 |
||
443 |
||
444 |
\noindent |
|
445 |
For convenience, we shall employ the following notations: the regular |
|
446 |
expression we start with is $r_0$, and the given string $s$ is composed |
|
447 |
of characters $c_0 c_1 \ldots c_{n-1}$. In the first phase from the |
|
448 |
left to right, we build the derivatives $r_1$, $r_2$, \ldots according |
|
449 |
to the characters $c_0$, $c_1$ until we exhaust the string and obtain |
|
450 |
the derivative $r_n$. We test whether this derivative is |
|
451 |
$\textit{nullable}$ or not. If not, we know the string does not match |
|
452 |
$r$ and no value needs to be generated. If yes, we start building the |
|
453 |
values incrementally by \emph{injecting} back the characters into the |
|
454 |
earlier values $v_n, \ldots, v_0$. This is the second phase of the |
|
455 |
algorithm from the right to left. For the first value $v_n$, we call the |
|
456 |
function $\textit{mkeps}$, which builds a POSIX lexical value |
|
457 |
for how the empty string has been matched by the (nullable) regular |
|
458 |
expression $r_n$. This function is defined as |
|
459 |
||
460 |
\begin{center} |
|
461 |
\begin{tabular}{lcl} |
|
462 |
$\mkeps(\ONE)$ & $\dn$ & $\Empty$ \\ |
|
463 |
$\mkeps(r_{1}+r_{2})$ & $\dn$ |
|
464 |
& \textit{if} $\nullable(r_{1})$\\ |
|
465 |
& & \textit{then} $\Left(\mkeps(r_{1}))$\\ |
|
466 |
& & \textit{else} $\Right(\mkeps(r_{2}))$\\ |
|
467 |
$\mkeps(r_1\cdot r_2)$ & $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\ |
|
468 |
$mkeps(r^*)$ & $\dn$ & $\Stars\,[]$ |
|
469 |
\end{tabular} |
|
470 |
\end{center} |
|
471 |
||
472 |
||
473 |
\noindent |
|
474 |
After the $\mkeps$-call, we inject back the characters one by one in order to build |
|
475 |
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$ |
|
476 |
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$. |
|
477 |
After injecting back $n$ characters, we get the lexical value for how $r_0$ |
|
478 |
matches $s$. The POSIX value is maintained throught out the process. |
|
479 |
For this Sulzmann and Lu defined a function that reverses |
|
480 |
the ``chopping off'' of characters during the derivative phase. The |
|
481 |
corresponding function is called \emph{injection}, written |
|
482 |
$\textit{inj}$; it takes three arguments: the first one is a regular |
|
483 |
expression ${r_{i-1}}$, before the character is chopped off, the second |
|
484 |
is a character ${c_{i-1}}$, the character we want to inject and the |
|
485 |
third argument is the value ${v_i}$, into which one wants to inject the |
|
486 |
character (it corresponds to the regular expression after the character |
|
487 |
has been chopped off). The result of this function is a new value. The |
|
488 |
definition of $\textit{inj}$ is as follows: |
|
468 | 489 |
|
519 | 490 |
\begin{center} |
491 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
|
492 |
$\textit{inj}\,(c)\,c\,Empty$ & $\dn$ & $Char\,c$\\ |
|
493 |
$\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\ |
|
494 |
$\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\ |
|
495 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\ |
|
496 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,\Left(Seq(v_1,v_2))$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\ |
|
497 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$ & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\ |
|
498 |
$\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$ & $\dn$ & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\ |
|
499 |
\end{tabular} |
|
500 |
\end{center} |
|
501 |
||
502 |
\noindent This definition is by recursion on the ``shape'' of regular |
|
503 |
expressions and values. |
|
504 |
The clauses basically do one thing--identifying the ``holes'' on |
|
505 |
value to inject the character back into. |
|
506 |
For instance, in the last clause for injecting back to a value |
|
507 |
that would turn into a new star value that corresponds to a star, |
|
508 |
we know it must be a sequence value. And we know that the first |
|
509 |
value of that sequence corresponds to the child regex of the star |
|
510 |
with the first character being chopped off--an iteration of the star |
|
511 |
that had just been unfolded. This value is followed by the already |
|
512 |
matched star iterations we collected before. So we inject the character |
|
513 |
back to the first value and form a new value with this new iteration |
|
514 |
being added to the previous list of iterations, all under the $Stars$ |
|
515 |
top level. |
|
516 |
||
517 |
We have mentioned before that derivatives without simplification |
|
518 |
can get clumsy, and this is true for values as well--they reflect |
|
519 |
the regular expressions size by definition. |
|
520 |
||
521 |
One can introduce simplification on the regex and values, but have to |
|
522 |
be careful in not breaking the correctness as the injection |
|
523 |
function heavily relies on the structure of the regexes and values |
|
524 |
being correct and match each other. |
|
525 |
It can be achieved by recording some extra rectification functions |
|
526 |
during the derivatives step, and applying these rectifications in |
|
527 |
each run during the injection phase. |
|
528 |
And we can prove that the POSIX value of how |
|
529 |
regular expressions match strings will not be affected---although is much harder |
|
530 |
to establish. |
|
531 |
Some initial results in this regard have been |
|
532 |
obtained in \cite{AusafDyckhoffUrban2016}. |
|
533 |
||
534 |
||
535 |
||
536 |
%Brzozowski, after giving the derivatives and simplification, |
|
537 |
%did not explore lexing with simplification or he may well be |
|
538 |
%stuck on an efficient simplificaiton with a proof. |
|
539 |
%He went on to explore the use of derivatives together with |
|
540 |
%automaton, and did not try lexing using derivatives. |
|
541 |
||
542 |
We want to get rid of complex and fragile rectification of values. |
|
543 |
Can we not create those intermediate values $v_1,\ldots v_n$, |
|
544 |
and get the lexing information that should be already there while |
|
545 |
doing derivatives in one pass, without a second phase of injection? |
|
546 |
In the meantime, can we make sure that simplifications |
|
547 |
are easily handled without breaking the correctness of the algorithm? |
|
548 |
||
549 |
Sulzmann and Lu solved this problem by |
|
550 |
introducing additional informtaion to the |
|
551 |
regular expressions called \emph{bitcodes}. |
|
552 |
||
553 |
\subsection*{Bit-coded Algorithm} |
|
554 |
Bits and bitcodes (lists of bits) are defined as: |
|
555 |
||
556 |
\begin{center} |
|
557 |
$b ::= 1 \mid 0 \qquad |
|
558 |
bs ::= [] \mid b::bs |
|
559 |
$ |
|
560 |
\end{center} |
|
561 |
||
562 |
\noindent |
|
563 |
The $1$ and $0$ are not in bold in order to avoid |
|
564 |
confusion with the regular expressions $\ZERO$ and $\ONE$. Bitcodes (or |
|
565 |
bit-lists) can be used to encode values (or potentially incomplete values) in a |
|
566 |
compact form. This can be straightforwardly seen in the following |
|
567 |
coding function from values to bitcodes: |
|
568 |
||
569 |
\begin{center} |
|
570 |
\begin{tabular}{lcl} |
|
571 |
$\textit{code}(\Empty)$ & $\dn$ & $[]$\\ |
|
572 |
$\textit{code}(\Char\,c)$ & $\dn$ & $[]$\\ |
|
573 |
$\textit{code}(\Left\,v)$ & $\dn$ & $0 :: code(v)$\\ |
|
574 |
$\textit{code}(\Right\,v)$ & $\dn$ & $1 :: code(v)$\\ |
|
575 |
$\textit{code}(\Seq\,v_1\,v_2)$ & $\dn$ & $code(v_1) \,@\, code(v_2)$\\ |
|
576 |
$\textit{code}(\Stars\,[])$ & $\dn$ & $[0]$\\ |
|
577 |
$\textit{code}(\Stars\,(v\!::\!vs))$ & $\dn$ & $1 :: code(v) \;@\; |
|
578 |
code(\Stars\,vs)$ |
|
579 |
\end{tabular} |
|
580 |
\end{center} |
|
581 |
||
582 |
\noindent |
|
583 |
Here $\textit{code}$ encodes a value into a bitcodes by converting |
|
584 |
$\Left$ into $0$, $\Right$ into $1$, and marks the start of a non-empty |
|
585 |
star iteration by $1$. The border where a local star terminates |
|
586 |
is marked by $0$. This coding is lossy, as it throws away the information about |
|
587 |
characters, and also does not encode the ``boundary'' between two |
|
588 |
sequence values. Moreover, with only the bitcode we cannot even tell |
|
589 |
whether the $1$s and $0$s are for $\Left/\Right$ or $\Stars$. The |
|
590 |
reason for choosing this compact way of storing information is that the |
|
591 |
relatively small size of bits can be easily manipulated and ``moved |
|
592 |
around'' in a regular expression. In order to recover values, we will |
|
593 |
need the corresponding regular expression as an extra information. This |
|
594 |
means the decoding function is defined as: |
|
595 |
||
596 |
||
597 |
%\begin{definition}[Bitdecoding of Values]\mbox{} |
|
598 |
\begin{center} |
|
599 |
\begin{tabular}{@{}l@{\hspace{1mm}}c@{\hspace{1mm}}l@{}} |
|
600 |
$\textit{decode}'\,bs\,(\ONE)$ & $\dn$ & $(\Empty, bs)$\\ |
|
601 |
$\textit{decode}'\,bs\,(c)$ & $\dn$ & $(\Char\,c, bs)$\\ |
|
602 |
$\textit{decode}'\,(0\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
603 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}\; |
|
604 |
(\Left\,v, bs_1)$\\ |
|
605 |
$\textit{decode}'\,(1\!::\!bs)\;(r_1 + r_2)$ & $\dn$ & |
|
606 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r_2\;\textit{in}\; |
|
607 |
(\Right\,v, bs_1)$\\ |
|
608 |
$\textit{decode}'\,bs\;(r_1\cdot r_2)$ & $\dn$ & |
|
609 |
$\textit{let}\,(v_1, bs_1) = \textit{decode}'\,bs\,r_1\;\textit{in}$\\ |
|
610 |
& & $\textit{let}\,(v_2, bs_2) = \textit{decode}'\,bs_1\,r_2$\\ |
|
611 |
& & \hspace{35mm}$\textit{in}\;(\Seq\,v_1\,v_2, bs_2)$\\ |
|
612 |
$\textit{decode}'\,(0\!::\!bs)\,(r^*)$ & $\dn$ & $(\Stars\,[], bs)$\\ |
|
613 |
$\textit{decode}'\,(1\!::\!bs)\,(r^*)$ & $\dn$ & |
|
614 |
$\textit{let}\,(v, bs_1) = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
|
615 |
& & $\textit{let}\,(\Stars\,vs, bs_2) = \textit{decode}'\,bs_1\,r^*$\\ |
|
616 |
& & \hspace{35mm}$\textit{in}\;(\Stars\,v\!::\!vs, bs_2)$\bigskip\\ |
|
617 |
||
618 |
$\textit{decode}\,bs\,r$ & $\dn$ & |
|
619 |
$\textit{let}\,(v, bs') = \textit{decode}'\,bs\,r\;\textit{in}$\\ |
|
620 |
& & $\textit{if}\;bs' = []\;\textit{then}\;\textit{Some}\,v\; |
|
621 |
\textit{else}\;\textit{None}$ |
|
622 |
\end{tabular} |
|
623 |
\end{center} |
|
624 |
%\end{definition} |
|
625 |
||
626 |
Sulzmann and Lu's integrated the bitcodes into regular expressions to |
|
627 |
create annotated regular expressions \cite{Sulzmann2014}. |
|
628 |
\emph{Annotated regular expressions} are defined by the following |
|
629 |
grammar:%\comment{ALTS should have an $as$ in the definitions, not just $a_1$ and $a_2$} |
|
630 |
||
631 |
\begin{center} |
|
632 |
\begin{tabular}{lcl} |
|
633 |
$\textit{a}$ & $::=$ & $\ZERO$\\ |
|
634 |
& $\mid$ & $_{bs}\ONE$\\ |
|
635 |
& $\mid$ & $_{bs}{\bf c}$\\ |
|
636 |
& $\mid$ & $_{bs}\sum\,as$\\ |
|
637 |
& $\mid$ & $_{bs}a_1\cdot a_2$\\ |
|
638 |
& $\mid$ & $_{bs}a^*$ |
|
639 |
\end{tabular} |
|
640 |
\end{center} |
|
641 |
%(in \textit{ALTS}) |
|
642 |
||
643 |
\noindent |
|
644 |
where $bs$ stands for bitcodes, $a$ for $\mathbf{a}$nnotated regular |
|
645 |
expressions and $as$ for a list of annotated regular expressions. |
|
646 |
The alternative constructor($\sum$) has been generalized to |
|
647 |
accept a list of annotated regular expressions rather than just 2. |
|
648 |
We will show that these bitcodes encode information about |
|
649 |
the (POSIX) value that should be generated by the Sulzmann and Lu |
|
650 |
algorithm. |
|
651 |
||
652 |
||
653 |
To do lexing using annotated regular expressions, we shall first |
|
654 |
transform the usual (un-annotated) regular expressions into annotated |
|
655 |
regular expressions. This operation is called \emph{internalisation} and |
|
656 |
defined as follows: |
|
657 |
||
658 |
%\begin{definition} |
|
659 |
\begin{center} |
|
660 |
\begin{tabular}{lcl} |
|
661 |
$(\ZERO)^\uparrow$ & $\dn$ & $\ZERO$\\ |
|
662 |
$(\ONE)^\uparrow$ & $\dn$ & $_{[]}\ONE$\\ |
|
663 |
$(c)^\uparrow$ & $\dn$ & $_{[]}{\bf c}$\\ |
|
664 |
$(r_1 + r_2)^\uparrow$ & $\dn$ & |
|
665 |
$_{[]}\sum[\textit{fuse}\,[0]\,r_1^\uparrow,\, |
|
666 |
\textit{fuse}\,[1]\,r_2^\uparrow]$\\ |
|
667 |
$(r_1\cdot r_2)^\uparrow$ & $\dn$ & |
|
668 |
$_{[]}r_1^\uparrow \cdot r_2^\uparrow$\\ |
|
669 |
$(r^*)^\uparrow$ & $\dn$ & |
|
670 |
$_{[]}(r^\uparrow)^*$\\ |
|
671 |
\end{tabular} |
|
672 |
\end{center} |
|
673 |
%\end{definition} |
|
674 |
||
675 |
\noindent |
|
676 |
We use up arrows here to indicate that the basic un-annotated regular |
|
677 |
expressions are ``lifted up'' into something slightly more complex. In the |
|
678 |
fourth clause, $\textit{fuse}$ is an auxiliary function that helps to |
|
679 |
attach bits to the front of an annotated regular expression. Its |
|
680 |
definition is as follows: |
|
681 |
||
682 |
\begin{center} |
|
683 |
\begin{tabular}{lcl} |
|
684 |
$\textit{fuse}\;bs \; \ZERO$ & $\dn$ & $\ZERO$\\ |
|
685 |
$\textit{fuse}\;bs\; _{bs'}\ONE$ & $\dn$ & |
|
686 |
$_{bs @ bs'}\ONE$\\ |
|
687 |
$\textit{fuse}\;bs\;_{bs'}{\bf c}$ & $\dn$ & |
|
688 |
$_{bs@bs'}{\bf c}$\\ |
|
689 |
$\textit{fuse}\;bs\,_{bs'}\sum\textit{as}$ & $\dn$ & |
|
690 |
$_{bs@bs'}\sum\textit{as}$\\ |
|
691 |
$\textit{fuse}\;bs\; _{bs'}a_1\cdot a_2$ & $\dn$ & |
|
692 |
$_{bs@bs'}a_1 \cdot a_2$\\ |
|
693 |
$\textit{fuse}\;bs\,_{bs'}a^*$ & $\dn$ & |
|
694 |
$_{bs @ bs'}a^*$ |
|
695 |
\end{tabular} |
|
696 |
\end{center} |
|
697 |
||
698 |
\noindent |
|
699 |
After internalising the regular expression, we perform successive |
|
700 |
derivative operations on the annotated regular expressions. This |
|
701 |
derivative operation is the same as what we had previously for the |
|
702 |
basic regular expressions, except that we beed to take care of |
|
703 |
the bitcodes: |
|
704 |
||
705 |
||
706 |
\iffalse |
|
707 |
%\begin{definition}{bder} |
|
708 |
\begin{center} |
|
709 |
\begin{tabular}{@{}lcl@{}} |
|
710 |
$(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\ |
|
711 |
$(\textit{ONE}\;bs)\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\ |
|
712 |
$(\textit{CHAR}\;bs\,d)\,\backslash c$ & $\dn$ & |
|
713 |
$\textit{if}\;c=d\; \;\textit{then}\; |
|
714 |
\textit{ONE}\;bs\;\textit{else}\;\textit{ZERO}$\\ |
|
715 |
$(\textit{ALTS}\;bs\,as)\,\backslash c$ & $\dn$ & |
|
716 |
$\textit{ALTS}\;bs\,(map (\backslash c) as)$\\ |
|
717 |
$(\textit{SEQ}\;bs\,a_1\,a_2)\,\backslash c$ & $\dn$ & |
|
718 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
719 |
& &$\textit{then}\;\textit{ALTS}\,bs\,List((\textit{SEQ}\,[]\,(a_1\,\backslash c)\,a_2),$\\ |
|
720 |
& &$\phantom{\textit{then}\;\textit{ALTS}\,bs\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\ |
|
721 |
& &$\textit{else}\;\textit{SEQ}\,bs\,(a_1\,\backslash c)\,a_2$\\ |
|
722 |
$(\textit{STAR}\,bs\,a)\,\backslash c$ & $\dn$ & |
|
723 |
$\textit{SEQ}\;bs\,(\textit{fuse}\, [\Z] (r\,\backslash c))\, |
|
724 |
(\textit{STAR}\,[]\,r)$ |
|
725 |
\end{tabular} |
|
726 |
\end{center} |
|
727 |
%\end{definition} |
|
728 |
||
729 |
\begin{center} |
|
730 |
\begin{tabular}{@{}lcl@{}} |
|
731 |
$(\textit{ZERO})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\ |
|
732 |
$(_{bs}\textit{ONE})\,\backslash c$ & $\dn$ & $\textit{ZERO}$\\ |
|
733 |
$(_{bs}\textit{CHAR}\;d)\,\backslash c$ & $\dn$ & |
|
734 |
$\textit{if}\;c=d\; \;\textit{then}\; |
|
735 |
_{bs}\textit{ONE}\;\textit{else}\;\textit{ZERO}$\\ |
|
736 |
$(_{bs}\textit{ALTS}\;\textit{as})\,\backslash c$ & $\dn$ & |
|
737 |
$_{bs}\textit{ALTS}\;(\textit{as}.\textit{map}(\backslash c))$\\ |
|
738 |
$(_{bs}\textit{SEQ}\;a_1\,a_2)\,\backslash c$ & $\dn$ & |
|
739 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
740 |
& &$\textit{then}\;_{bs}\textit{ALTS}\,List((_{[]}\textit{SEQ}\,(a_1\,\backslash c)\,a_2),$\\ |
|
741 |
& &$\phantom{\textit{then}\;_{bs}\textit{ALTS}\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c)))$\\ |
|
742 |
& &$\textit{else}\;_{bs}\textit{SEQ}\,(a_1\,\backslash c)\,a_2$\\ |
|
743 |
$(_{bs}\textit{STAR}\,a)\,\backslash c$ & $\dn$ & |
|
744 |
$_{bs}\textit{SEQ}\;(\textit{fuse}\, [0] \; r\,\backslash c )\, |
|
745 |
(_{bs}\textit{STAR}\,[]\,r)$ |
|
746 |
\end{tabular} |
|
747 |
\end{center} |
|
748 |
%\end{definition} |
|
749 |
\fi |
|
750 |
||
751 |
\begin{center} |
|
752 |
\begin{tabular}{@{}lcl@{}} |
|
753 |
$(\ZERO)\,\backslash c$ & $\dn$ & $\ZERO$\\ |
|
754 |
$(_{bs}\ONE)\,\backslash c$ & $\dn$ & $\ZERO$\\ |
|
755 |
$(_{bs}{\bf d})\,\backslash c$ & $\dn$ & |
|
756 |
$\textit{if}\;c=d\; \;\textit{then}\; |
|
757 |
_{bs}\ONE\;\textit{else}\;\ZERO$\\ |
|
758 |
$(_{bs}\sum \;\textit{as})\,\backslash c$ & $\dn$ & |
|
759 |
$_{bs}\sum\;(\textit{as.map}(\backslash c))$\\ |
|
760 |
$(_{bs}\;a_1\cdot a_2)\,\backslash c$ & $\dn$ & |
|
761 |
$\textit{if}\;\textit{bnullable}\,a_1$\\ |
|
762 |
& &$\textit{then}\;_{bs}\sum\,[(_{[]}\,(a_1\,\backslash c)\cdot\,a_2),$\\ |
|
763 |
& &$\phantom{\textit{then},\;_{bs}\sum\,}(\textit{fuse}\,(\textit{bmkeps}\,a_1)\,(a_2\,\backslash c))]$\\ |
|
764 |
& &$\textit{else}\;_{bs}\,(a_1\,\backslash c)\cdot a_2$\\ |
|
765 |
$(_{bs}a^*)\,\backslash c$ & $\dn$ & |
|
766 |
$_{bs}(\textit{fuse}\, [0] \; r\,\backslash c)\cdot |
|
767 |
(_{[]}r^*))$ |
|
768 |
\end{tabular} |
|
769 |
\end{center} |
|
770 |
||
771 |
%\end{definition} |
|
772 |
\noindent |
|
773 |
For instance, when we do derivative of $_{bs}a^*$ with respect to c, |
|
774 |
we need to unfold it into a sequence, |
|
775 |
and attach an additional bit $0$ to the front of $r \backslash c$ |
|
776 |
to indicate one more star iteration. Also the sequence clause |
|
777 |
is more subtle---when $a_1$ is $\textit{bnullable}$ (here |
|
778 |
\textit{bnullable} is exactly the same as $\textit{nullable}$, except |
|
779 |
that it is for annotated regular expressions, therefore we omit the |
|
780 |
definition). Assume that $\textit{bmkeps}$ correctly extracts the bitcode for how |
|
781 |
$a_1$ matches the string prior to character $c$ (more on this later), |
|
782 |
then the right branch of alternative, which is $\textit{fuse} \; \bmkeps \; a_1 (a_2 |
|
783 |
\backslash c)$ will collapse the regular expression $a_1$(as it has |
|
784 |
already been fully matched) and store the parsing information at the |
|
785 |
head of the regular expression $a_2 \backslash c$ by fusing to it. The |
|
786 |
bitsequence $\textit{bs}$, which was initially attached to the |
|
787 |
first element of the sequence $a_1 \cdot a_2$, has |
|
788 |
now been elevated to the top-level of $\sum$, as this information will be |
|
789 |
needed whichever way the sequence is matched---no matter whether $c$ belongs |
|
790 |
to $a_1$ or $ a_2$. After building these derivatives and maintaining all |
|
791 |
the lexing information, we complete the lexing by collecting the |
|
792 |
bitcodes using a generalised version of the $\textit{mkeps}$ function |
|
793 |
for annotated regular expressions, called $\textit{bmkeps}$: |
|
794 |
||
795 |
||
796 |
%\begin{definition}[\textit{bmkeps}]\mbox{} |
|
797 |
\begin{center} |
|
798 |
\begin{tabular}{lcl} |
|
799 |
$\textit{bmkeps}\,(_{bs}\ONE)$ & $\dn$ & $bs$\\ |
|
800 |
$\textit{bmkeps}\,(_{bs}\sum a::\textit{as})$ & $\dn$ & |
|
801 |
$\textit{if}\;\textit{bnullable}\,a$\\ |
|
802 |
& &$\textit{then}\;bs\,@\,\textit{bmkeps}\,a$\\ |
|
803 |
& &$\textit{else}\;bs\,@\,\textit{bmkeps}\,(_{bs}\sum \textit{as})$\\ |
|
804 |
$\textit{bmkeps}\,(_{bs} a_1 \cdot a_2)$ & $\dn$ & |
|
805 |
$bs \,@\,\textit{bmkeps}\,a_1\,@\, \textit{bmkeps}\,a_2$\\ |
|
806 |
$\textit{bmkeps}\,(_{bs}a^*)$ & $\dn$ & |
|
807 |
$bs \,@\, [0]$ |
|
808 |
\end{tabular} |
|
809 |
\end{center} |
|
810 |
%\end{definition} |
|
811 |
||
812 |
\noindent |
|
813 |
This function completes the value information by travelling along the |
|
814 |
path of the regular expression that corresponds to a POSIX value and |
|
815 |
collecting all the bitcodes, and using $S$ to indicate the end of star |
|
816 |
iterations. If we take the bitcodes produced by $\textit{bmkeps}$ and |
|
817 |
decode them, we get the value we expect. The corresponding lexing |
|
818 |
algorithm looks as follows: |
|
819 |
||
820 |
\begin{center} |
|
821 |
\begin{tabular}{lcl} |
|
822 |
$\textit{blexer}\;r\,s$ & $\dn$ & |
|
823 |
$\textit{let}\;a = (r^\uparrow)\backslash s\;\textit{in}$\\ |
|
824 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
825 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
826 |
& & $\;\;\textit{else}\;\textit{None}$ |
|
827 |
\end{tabular} |
|
828 |
\end{center} |
|
829 |
||
830 |
\noindent |
|
831 |
In this definition $\_\backslash s$ is the generalisation of the derivative |
|
832 |
operation from characters to strings (just like the derivatives for un-annotated |
|
833 |
regular expressions). |
|
834 |
||
835 |
Now we introduce the simplifications, which is why we introduce the |
|
836 |
bitcodes in the first place. |
|
837 |
||
838 |
\subsection*{Simplification Rules} |
|
839 |
||
840 |
This section introduces aggressive (in terms of size) simplification rules |
|
841 |
on annotated regular expressions |
|
842 |
to keep derivatives small. Such simplifications are promising |
|
843 |
as we have |
|
844 |
generated test data that show |
|
845 |
that a good tight bound can be achieved. We could only |
|
846 |
partially cover the search space as there are infinitely many regular |
|
847 |
expressions and strings. |
|
848 |
||
849 |
One modification we introduced is to allow a list of annotated regular |
|
850 |
expressions in the $\sum$ constructor. This allows us to not just |
|
851 |
delete unnecessary $\ZERO$s and $\ONE$s from regular expressions, but |
|
852 |
also unnecessary ``copies'' of regular expressions (very similar to |
|
853 |
simplifying $r + r$ to just $r$, but in a more general setting). Another |
|
854 |
modification is that we use simplification rules inspired by Antimirov's |
|
855 |
work on partial derivatives. They maintain the idea that only the first |
|
856 |
``copy'' of a regular expression in an alternative contributes to the |
|
857 |
calculation of a POSIX value. All subsequent copies can be pruned away from |
|
858 |
the regular expression. A recursive definition of our simplification function |
|
859 |
that looks somewhat similar to our Scala code is given below: |
|
860 |
%\comment{Use $\ZERO$, $\ONE$ and so on. |
|
861 |
%Is it $ALTS$ or $ALTS$?}\\ |
|
862 |
||
863 |
\begin{center} |
|
864 |
\begin{tabular}{@{}lcl@{}} |
|
865 |
||
866 |
$\textit{simp} \; (_{bs}a_1\cdot a_2)$ & $\dn$ & $ (\textit{simp} \; a_1, \textit{simp} \; a_2) \; \textit{match} $ \\ |
|
867 |
&&$\quad\textit{case} \; (\ZERO, \_) \Rightarrow \ZERO$ \\ |
|
868 |
&&$\quad\textit{case} \; (\_, \ZERO) \Rightarrow \ZERO$ \\ |
|
869 |
&&$\quad\textit{case} \; (\ONE, a_2') \Rightarrow \textit{fuse} \; bs \; a_2'$ \\ |
|
870 |
&&$\quad\textit{case} \; (a_1', \ONE) \Rightarrow \textit{fuse} \; bs \; a_1'$ \\ |
|
871 |
&&$\quad\textit{case} \; (a_1', a_2') \Rightarrow _{bs}a_1' \cdot a_2'$ \\ |
|
872 |
||
873 |
$\textit{simp} \; (_{bs}\sum \textit{as})$ & $\dn$ & $\textit{distinct}( \textit{flatten} ( \textit{as.map(simp)})) \; \textit{match} $ \\ |
|
874 |
&&$\quad\textit{case} \; [] \Rightarrow \ZERO$ \\ |
|
875 |
&&$\quad\textit{case} \; a :: [] \Rightarrow \textit{fuse bs a}$ \\ |
|
876 |
&&$\quad\textit{case} \; as' \Rightarrow _{bs}\sum \textit{as'}$\\ |
|
877 |
||
878 |
$\textit{simp} \; a$ & $\dn$ & $\textit{a} \qquad \textit{otherwise}$ |
|
879 |
\end{tabular} |
|
880 |
\end{center} |
|
881 |
||
882 |
\noindent |
|
883 |
The simplification does a pattern matching on the regular expression. |
|
884 |
When it detected that the regular expression is an alternative or |
|
885 |
sequence, it will try to simplify its child regular expressions |
|
886 |
recursively and then see if one of the children turns into $\ZERO$ or |
|
887 |
$\ONE$, which might trigger further simplification at the current level. |
|
888 |
The most involved part is the $\sum$ clause, where we use two |
|
889 |
auxiliary functions $\textit{flatten}$ and $\textit{distinct}$ to open up nested |
|
890 |
alternatives and reduce as many duplicates as possible. Function |
|
891 |
$\textit{distinct}$ keeps the first occurring copy only and removes all later ones |
|
892 |
when detected duplicates. Function $\textit{flatten}$ opens up nested $\sum$s. |
|
893 |
Its recursive definition is given below: |
|
894 |
||
895 |
\begin{center} |
|
896 |
\begin{tabular}{@{}lcl@{}} |
|
897 |
$\textit{flatten} \; (_{bs}\sum \textit{as}) :: \textit{as'}$ & $\dn$ & $(\textit{map} \; |
|
898 |
(\textit{fuse}\;bs)\; \textit{as}) \; @ \; \textit{flatten} \; as' $ \\ |
|
899 |
$\textit{flatten} \; \ZERO :: as'$ & $\dn$ & $ \textit{flatten} \; \textit{as'} $ \\ |
|
900 |
$\textit{flatten} \; a :: as'$ & $\dn$ & $a :: \textit{flatten} \; \textit{as'}$ \quad(otherwise) |
|
901 |
\end{tabular} |
|
902 |
\end{center} |
|
903 |
||
904 |
\noindent |
|
905 |
Here $\textit{flatten}$ behaves like the traditional functional programming flatten |
|
906 |
function, except that it also removes $\ZERO$s. Or in terms of regular expressions, it |
|
907 |
removes parentheses, for example changing $a+(b+c)$ into $a+b+c$. |
|
908 |
||
909 |
Having defined the $\simp$ function, |
|
910 |
we can use the previous notation of natural |
|
911 |
extension from derivative w.r.t.~character to derivative |
|
912 |
w.r.t.~string:%\comment{simp in the [] case?} |
|
913 |
||
914 |
\begin{center} |
|
915 |
\begin{tabular}{lcl} |
|
916 |
$r \backslash_{simp} (c\!::\!s) $ & $\dn$ & $(r \backslash_{simp}\, c) \backslash_{simp}\, s$ \\ |
|
917 |
$r \backslash_{simp} [\,] $ & $\dn$ & $r$ |
|
918 |
\end{tabular} |
|
919 |
\end{center} |
|
920 |
||
921 |
\noindent |
|
922 |
to obtain an optimised version of the algorithm: |
|
923 |
||
924 |
\begin{center} |
|
925 |
\begin{tabular}{lcl} |
|
926 |
$\textit{blexer\_simp}\;r\,s$ & $\dn$ & |
|
927 |
$\textit{let}\;a = (r^\uparrow)\backslash_{simp}\, s\;\textit{in}$\\ |
|
928 |
& & $\;\;\textit{if}\; \textit{bnullable}(a)$\\ |
|
929 |
& & $\;\;\textit{then}\;\textit{decode}\,(\textit{bmkeps}\,a)\,r$\\ |
|
930 |
& & $\;\;\textit{else}\;\textit{None}$ |
|
931 |
\end{tabular} |
|
932 |
\end{center} |
|
933 |
||
934 |
\noindent |
|
935 |
This algorithm keeps the regular expression size small, for example, |
|
936 |
with this simplification our previous $(a + aa)^*$ example's 8000 nodes |
|
937 |
will be reduced to just 6 and stays constant, no matter how long the |
|
938 |
input string is. |
|
939 |
||
940 |
||
468 | 941 |
|
500 | 942 |
|
943 |
||
944 |
||
468 | 945 |
|
946 |
%----------------------------------- |
|
947 |
% SUBSECTION 1 |
|
948 |
%----------------------------------- |
|
518 | 949 |
\section{Specifications of Certain Functions to be Used} |
524 | 950 |
Here we give some functions' definitions, |
951 |
which we will use later. |
|
952 |
\begin{center} |
|
953 |
\begin{tabular}{ccc} |
|
525
d8740017324c
fixed latex problems
Christian Urban <christian.urban@kcl.ac.uk>
parents:
524
diff
changeset
|
954 |
$\retrieve \; \ACHAR \, \textit{bs} \, c \; \Char(c) = \textit{bs}$ |
524 | 955 |
\end{tabular} |
956 |
\end{center} |
|
500 | 957 |
|
958 |
||
518 | 959 |
|
960 |