532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
\chapter{Regular Expressions and POSIX Lexing} % Main chapter title
|
|
4 |
|
|
5 |
\label{Inj} % 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 |
|
538
|
11 |
In this chapter, we define the basic notions
|
|
12 |
for regular languages and regular expressions.
|
|
13 |
We also give the definition of what $\POSIX$ lexing means.
|
532
|
14 |
|
538
|
15 |
\section{Basic Concepts}
|
|
16 |
Usually in formal language theory there is an alphabet
|
|
17 |
denoting a set of characters.
|
|
18 |
Here we only use the datatype of characters from Isabelle,
|
|
19 |
which roughly corresponds to the ASCII character.
|
|
20 |
Then using the usual $[]$ notation for lists,
|
|
21 |
we can define strings using chars:
|
532
|
22 |
\begin{center}
|
|
23 |
\begin{tabular}{lcl}
|
|
24 |
$\textit{string}$ & $\dn$ & $[] | c :: cs$\\
|
|
25 |
& & $(c\; \text{has char type})$
|
|
26 |
\end{tabular}
|
|
27 |
\end{center}
|
538
|
28 |
And strings can be concatenated to form longer strings,
|
|
29 |
in the same way as we concatenate two lists,
|
|
30 |
which we denote as $@$. We omit the precise
|
|
31 |
recursive definition here.
|
|
32 |
We overload this concatenation operator for two sets of strings:
|
532
|
33 |
\begin{center}
|
|
34 |
\begin{tabular}{lcl}
|
|
35 |
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A; s_B \in B \}$\\
|
|
36 |
\end{tabular}
|
|
37 |
\end{center}
|
538
|
38 |
We also call the above \emph{language concatenation}.
|
532
|
39 |
The power of a language is defined recursively, using the
|
|
40 |
concatenation operator $@$:
|
|
41 |
\begin{center}
|
|
42 |
\begin{tabular}{lcl}
|
|
43 |
$A^0 $ & $\dn$ & $\{ [] \}$\\
|
|
44 |
$A^{n+1}$ & $\dn$ & $A^n @ A$
|
|
45 |
\end{tabular}
|
|
46 |
\end{center}
|
|
47 |
The union of all the natural number powers of a language
|
538
|
48 |
is defined as the Kleene star operator:
|
532
|
49 |
\begin{center}
|
|
50 |
\begin{tabular}{lcl}
|
536
|
51 |
$A*$ & $\dn$ & $\bigcup_{i \geq 0} A^i$ \\
|
532
|
52 |
\end{tabular}
|
|
53 |
\end{center}
|
|
54 |
|
538
|
55 |
\noindent
|
|
56 |
However, to obtain a convenient induction principle
|
|
57 |
in Isabelle/HOL,
|
536
|
58 |
we instead define the Kleene star
|
532
|
59 |
as an inductive set:
|
538
|
60 |
|
532
|
61 |
\begin{center}
|
538
|
62 |
\begin{mathpar}
|
|
63 |
\inferrule{}{[] \in A*\\}
|
|
64 |
|
|
65 |
\inferrule{\\s_1 \in A \land \; s_2 \in A*}{s_1 @ s_2 \in A*}
|
|
66 |
\end{mathpar}
|
532
|
67 |
\end{center}
|
538
|
68 |
|
|
69 |
We also define an operation of "chopping of" a character from
|
|
70 |
a language, which we call $\Der$, meaning "Derivative for a language":
|
532
|
71 |
\begin{center}
|
|
72 |
\begin{tabular}{lcl}
|
|
73 |
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\
|
|
74 |
\end{tabular}
|
|
75 |
\end{center}
|
538
|
76 |
\noindent
|
|
77 |
This can be generalised to "chopping off" a string from all strings within set $A$,
|
|
78 |
with the help of the concatenation operator:
|
532
|
79 |
\begin{center}
|
|
80 |
\begin{tabular}{lcl}
|
|
81 |
$\textit{Ders} \;w \;A$ & $\dn$ & $\{ s \mid w@s \in A \}$\\
|
|
82 |
\end{tabular}
|
|
83 |
\end{center}
|
538
|
84 |
\noindent
|
532
|
85 |
which is essentially the left quotient $A \backslash L'$ of $A$ against
|
|
86 |
the singleton language $L' = \{w\}$
|
|
87 |
in formal language theory.
|
536
|
88 |
For this dissertation the $\textit{Ders}$ definition with
|
|
89 |
a single string suffices.
|
532
|
90 |
|
|
91 |
With the sequencing, Kleene star, and $\textit{Der}$ operator on languages,
|
|
92 |
we have a few properties of how the language derivative can be defined using
|
|
93 |
sub-languages.
|
|
94 |
\begin{lemma}
|
536
|
95 |
\[
|
|
96 |
\Der \; c \; (A @ B) =
|
|
97 |
\begin{cases}
|
538
|
98 |
((\Der \; c \; A) \, @ \, B ) \cup (\Der \; c\; B) , & \text{if} \; [] \in A \\
|
|
99 |
(\Der \; c \; A) \, @ \, B, & \text{otherwise}
|
536
|
100 |
\end{cases}
|
|
101 |
\]
|
532
|
102 |
\end{lemma}
|
|
103 |
\noindent
|
|
104 |
This lemma states that if $A$ contains the empty string, $\Der$ can "pierce" through it
|
|
105 |
and get to $B$.
|
536
|
106 |
The language $A*$'s derivative can be described using the language derivative
|
532
|
107 |
of $A$:
|
|
108 |
\begin{lemma}
|
538
|
109 |
$\textit{Der} \;c \;(A*) = (\textit{Der}\; c A) @ (A*)$\\
|
532
|
110 |
\end{lemma}
|
|
111 |
\begin{proof}
|
|
112 |
\begin{itemize}
|
|
113 |
\item{$\subseteq$}
|
538
|
114 |
\noindent
|
532
|
115 |
The set
|
536
|
116 |
\[ \{s \mid c :: s \in A*\} \]
|
532
|
117 |
is enclosed in the set
|
536
|
118 |
\[ \{s_1 @ s_2 \mid s_1 \, s_2. s_1 \in \{s \mid c :: s \in A\} \land s_2 \in A* \} \]
|
532
|
119 |
because whenever you have a string starting with a character
|
538
|
120 |
in the language of a Kleene star $A*$,
|
|
121 |
then that character together with some sub-string
|
|
122 |
immediately after it will form the first iteration,
|
|
123 |
and the rest of the string will
|
536
|
124 |
be still in $A*$.
|
532
|
125 |
\item{$\supseteq$}
|
|
126 |
Note that
|
538
|
127 |
\[ \Der \; c \; (A*) = \Der \; c \; (\{ [] \} \cup (A @ A*) ) \]
|
532
|
128 |
and
|
536
|
129 |
\[ \Der \; c \; (\{ [] \} \cup (A @ A*) ) = \Der\; c \; (A @ A*) \]
|
532
|
130 |
where the $\textit{RHS}$ of the above equatioin can be rewritten
|
536
|
131 |
as \[ (\Der \; c\; A) @ A* \cup A' \], $A'$ being a possibly empty set.
|
532
|
132 |
\end{itemize}
|
|
133 |
\end{proof}
|
538
|
134 |
|
|
135 |
\noindent
|
532
|
136 |
Before we define the $\textit{Der}$ and $\textit{Ders}$ counterpart
|
|
137 |
for regular languages, we need to first give definitions for regular expressions.
|
|
138 |
|
536
|
139 |
\subsection{Regular Expressions and Their Meaning}
|
532
|
140 |
The basic regular expressions are defined inductively
|
|
141 |
by the following grammar:
|
|
142 |
\[ r ::= \ZERO \mid \ONE
|
|
143 |
\mid c
|
|
144 |
\mid r_1 \cdot r_2
|
|
145 |
\mid r_1 + r_2
|
|
146 |
\mid r^*
|
|
147 |
\]
|
538
|
148 |
\noindent
|
|
149 |
We call them basic because we might introduce
|
|
150 |
more constructs later such as negation
|
|
151 |
and bounded repetitions.
|
|
152 |
We defined the regular expression containing
|
|
153 |
nothing as $\ZERO$, note that some authors
|
|
154 |
also use $\phi$ for that.
|
|
155 |
Similarly, the regular expression denoting the
|
|
156 |
singleton set with only $[]$ is sometimes
|
|
157 |
denoted by $\epsilon$, but we use $\ONE$ here.
|
532
|
158 |
|
|
159 |
The language or set of strings denoted
|
|
160 |
by regular expressions are defined as
|
|
161 |
%TODO: FILL in the other defs
|
|
162 |
\begin{center}
|
|
163 |
\begin{tabular}{lcl}
|
536
|
164 |
$L \; (\ZERO)$ & $\dn$ & $\phi$\\
|
|
165 |
$L \; (\ONE)$ & $\dn$ & $\{[]\}$\\
|
|
166 |
$L \; (c)$ & $\dn$ & $\{[c]\}$\\
|
532
|
167 |
$L \; (r_1 + r_2)$ & $\dn$ & $ L \; (r_1) \cup L \; ( r_2)$\\
|
|
168 |
$L \; (r_1 \cdot r_2)$ & $\dn$ & $ L \; (r_1) \cap L \; (r_2)$\\
|
536
|
169 |
$L \; (r^*)$ & $\dn$ & $ (L(r))^*$
|
532
|
170 |
\end{tabular}
|
|
171 |
\end{center}
|
536
|
172 |
\noindent
|
532
|
173 |
Which is also called the "language interpretation" of
|
|
174 |
a regular expression.
|
|
175 |
|
|
176 |
Now with semantic derivatives of a language and regular expressions and
|
|
177 |
their language interpretations in place, we are ready to define derivatives on regexes.
|
|
178 |
\subsection{Brzozowski Derivatives and a Regular Expression Matcher}
|
538
|
179 |
|
|
180 |
\ChristianComment{Hi this part I want to keep the ordering as is, so that it keeps the
|
|
181 |
readers engaged with a story how we got to the definition of $\backslash$, rather
|
|
182 |
than first "overwhelming" them with the definition of $\nullable$.}
|
|
183 |
|
536
|
184 |
The language derivative acts on a string set and chops off a character from
|
532
|
185 |
all strings in that set, we want to define a derivative operation on regular expressions
|
|
186 |
so that after derivative $L(r\backslash c)$
|
|
187 |
will look as if it was obtained by doing a language derivative on $L(r)$:
|
538
|
188 |
\begin{center}
|
532
|
189 |
\[
|
538
|
190 |
r\backslash c \dn ?
|
532
|
191 |
\]
|
538
|
192 |
so that
|
|
193 |
\[
|
|
194 |
L(r \backslash c) = \Der \; c \; L(r) ?
|
|
195 |
\]
|
|
196 |
\end{center}
|
532
|
197 |
So we mimic the equalities we have for $\Der$ on language concatenation
|
|
198 |
|
|
199 |
\[
|
|
200 |
\Der \; c \; (A @ B) = \textit{if} \; [] \in A \; \textit{then} ((\Der \; c \; A) @ B ) \cup \Der \; c\; B \quad \textit{else}\; (\Der \; c \; A) @ B\\
|
|
201 |
\]
|
|
202 |
to get the derivative for sequence regular expressions:
|
|
203 |
\[
|
|
204 |
(r_1 \cdot r_2 ) \backslash c = \textit{if}\,([] \in L(r_1)) r_1 \backslash c \cdot r_2 + r_2 \backslash c \textit{else} (r_1 \backslash c) \cdot r_2
|
|
205 |
\]
|
|
206 |
|
|
207 |
\noindent
|
|
208 |
and language Kleene star:
|
|
209 |
\[
|
536
|
210 |
\textit{Der} \;c \;A* = (\textit{Der}\; c A) @ (A*)
|
532
|
211 |
\]
|
|
212 |
to get derivative of the Kleene star regular expression:
|
|
213 |
\[
|
|
214 |
r^* \backslash c = (r \backslash c)\cdot r^*
|
|
215 |
\]
|
|
216 |
Note that although we can formalise the boolean predicate
|
|
217 |
$[] \in L(r_1)$ without problems, if we want a function that works
|
|
218 |
computationally, then we would have to define a function that tests
|
|
219 |
whether an empty string is in the language of a regular expression.
|
|
220 |
We call such a function $\nullable$:
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
\begin{center}
|
|
225 |
\begin{tabular}{lcl}
|
|
226 |
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\
|
|
227 |
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\
|
|
228 |
$d \backslash c$ & $\dn$ &
|
|
229 |
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
|
|
230 |
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
|
538
|
231 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\
|
532
|
232 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
|
|
233 |
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
|
|
234 |
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\
|
|
235 |
\end{tabular}
|
|
236 |
\end{center}
|
|
237 |
\noindent
|
|
238 |
The function derivative, written $r\backslash c$,
|
|
239 |
defines how a regular expression evolves into
|
|
240 |
a new regular expression after all the string it contains
|
|
241 |
is chopped off a certain head character $c$.
|
|
242 |
The most involved cases are the sequence
|
|
243 |
and star case.
|
|
244 |
The sequence case says that if the first regular expression
|
|
245 |
contains an empty string then the second component of the sequence
|
|
246 |
might be chosen as the target regular expression to be chopped
|
|
247 |
off its head character.
|
|
248 |
The star regular expression's derivative unwraps the iteration of
|
|
249 |
regular expression and attaches the star regular expression
|
|
250 |
to the sequence's second element to make sure a copy is retained
|
|
251 |
for possible more iterations in later phases of lexing.
|
|
252 |
|
|
253 |
|
538
|
254 |
To test whether $[] \in L(r_1)$, we need the $\nullable$ function,
|
|
255 |
which tests whether the empty string $""$
|
532
|
256 |
is in the language of $r$:
|
|
257 |
|
|
258 |
|
|
259 |
\begin{center}
|
|
260 |
\begin{tabular}{lcl}
|
|
261 |
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\
|
|
262 |
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\
|
|
263 |
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\
|
|
264 |
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
|
|
265 |
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
|
|
266 |
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\
|
|
267 |
\end{tabular}
|
|
268 |
\end{center}
|
|
269 |
\noindent
|
|
270 |
The empty set does not contain any string and
|
|
271 |
therefore not the empty string, the empty string
|
|
272 |
regular expression contains the empty string
|
|
273 |
by definition, the character regular expression
|
|
274 |
is the singleton that contains character only,
|
|
275 |
and therefore does not contain the empty string,
|
|
276 |
the alternative regular expression (or "or" expression)
|
|
277 |
might have one of its children regular expressions
|
|
278 |
being nullable and any one of its children being nullable
|
|
279 |
would suffice. The sequence regular expression
|
|
280 |
would require both children to have the empty string
|
|
281 |
to compose an empty string and the Kleene star
|
|
282 |
operation naturally introduced the empty string.
|
|
283 |
|
|
284 |
We have the following property where the derivative on regular
|
|
285 |
expressions coincides with the derivative on a set of strings:
|
|
286 |
|
|
287 |
\begin{lemma}
|
|
288 |
$\textit{Der} \; c \; L(r) = L (r\backslash c)$
|
|
289 |
\end{lemma}
|
|
290 |
|
|
291 |
\noindent
|
|
292 |
The main property of the derivative operation
|
|
293 |
that enables us to reason about the correctness of
|
|
294 |
an algorithm using derivatives is
|
|
295 |
|
539
|
296 |
\begin{lemma}\label{derStepwise}
|
532
|
297 |
$c\!::\!s \in L(r)$ holds
|
|
298 |
if and only if $s \in L(r\backslash c)$.
|
539
|
299 |
\end{lemma}
|
532
|
300 |
|
|
301 |
\noindent
|
|
302 |
We can generalise the derivative operation shown above for single characters
|
|
303 |
to strings as follows:
|
|
304 |
|
|
305 |
\begin{center}
|
|
306 |
\begin{tabular}{lcl}
|
|
307 |
$r \backslash_s (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash_s s$ \\
|
|
308 |
$r \backslash [\,] $ & $\dn$ & $r$
|
|
309 |
\end{tabular}
|
|
310 |
\end{center}
|
|
311 |
|
|
312 |
\noindent
|
|
313 |
When there is no ambiguity we will use $\backslash$ to denote
|
|
314 |
string derivatives for brevity.
|
539
|
315 |
Brzozowski's regular-expression matcher algorithm can then be described as:
|
532
|
316 |
|
|
317 |
\begin{definition}
|
538
|
318 |
$\textit{match}\;s\;r \;\dn\; \nullable(r\backslash s)$
|
532
|
319 |
\end{definition}
|
|
320 |
|
|
321 |
\noindent
|
|
322 |
Assuming the string is given as a sequence of characters, say $c_0c_1..c_n$,
|
|
323 |
this algorithm presented graphically is as follows:
|
|
324 |
|
|
325 |
\begin{equation}\label{graph:successive_ders}
|
|
326 |
\begin{tikzcd}
|
|
327 |
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}
|
|
328 |
\end{tikzcd}
|
|
329 |
\end{equation}
|
|
330 |
|
|
331 |
\noindent
|
539
|
332 |
It can be
|
|
333 |
relatively easily shown that this matcher is correct:
|
|
334 |
\begin{lemma}
|
|
335 |
$\textit{match} \; s\; r = \textit{true} \Longleftrightarrow s \in L(r)$
|
|
336 |
\end{lemma}
|
|
337 |
\begin{proof}
|
|
338 |
By the stepwise property of $\backslash$ (\ref{derStepwise})
|
|
339 |
\end{proof}
|
|
340 |
\noindent
|
|
341 |
If we implement the above algorithm naively, however,
|
|
342 |
the algorithm can be excruciatingly slow.
|
538
|
343 |
|
|
344 |
|
539
|
345 |
\begin{figure}
|
|
346 |
\begin{tikzpicture}
|
|
347 |
\begin{axis}[
|
|
348 |
xlabel={$n$},
|
|
349 |
ylabel={time in secs},
|
|
350 |
ymode = log,
|
|
351 |
legend entries={Naive Matcher},
|
|
352 |
legend pos=north west,
|
|
353 |
legend cell align=left]
|
|
354 |
\addplot[red,mark=*, mark options={fill=white}] table {NaiveMatcher.data};
|
|
355 |
\end{axis}
|
|
356 |
\end{tikzpicture}
|
|
357 |
\caption{Matching $(a^*)^*b$ against $\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$}\label{NaiveMatcher}
|
|
358 |
\end{figure}
|
|
359 |
|
|
360 |
\noindent
|
|
361 |
For this we need to introduce certain
|
|
362 |
rewrite rules for the intermediate results,
|
|
363 |
such as $r + r \rightarrow r$,
|
|
364 |
and make sure those rules do not change the
|
|
365 |
language of the regular expression.
|
|
366 |
We have a simplification function (that is as simple as possible
|
|
367 |
while having much power on making a regex simpler):
|
|
368 |
\begin{verbatim}
|
|
369 |
def simp(r: Rexp) : Rexp = r match {
|
|
370 |
case SEQ(r1, r2) =>
|
|
371 |
(simp(r1), simp(r2)) match {
|
|
372 |
case (ZERO, _) => ZERO
|
|
373 |
case (_, ZERO) => ZERO
|
|
374 |
case (ONE, r2s) => r2s
|
|
375 |
case (r1s, ONE) => r1s
|
|
376 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
|
377 |
}
|
|
378 |
case ALTS(r1, r2) => {
|
|
379 |
(simp(r1), simp(r2)) match {
|
|
380 |
case (ZERO, r2s) => r2s
|
|
381 |
case (r1s, ZERO) => r1s
|
|
382 |
case (r1s, r2s) =>
|
|
383 |
if(r1s == r2s) r1s else ALTS(r1s, r2s)
|
|
384 |
}
|
|
385 |
}
|
|
386 |
case r => r
|
|
387 |
}
|
|
388 |
\end{verbatim}
|
|
389 |
If we repeatedly incorporate these
|
|
390 |
rules during the matching algorithm,
|
|
391 |
we have a lexer with simplification:
|
|
392 |
\begin{verbatim}
|
|
393 |
def ders_simp(s: List[Char], r: Rexp) : Rexp = s match {
|
|
394 |
case Nil => simp(r)
|
|
395 |
case c :: cs => ders_simp(cs, simp(der(c, r)))
|
|
396 |
}
|
|
397 |
|
|
398 |
def simp_matcher(s: String, r: Rexp) : Boolean =
|
|
399 |
nullable(ders_simp(s.toList, r))
|
|
400 |
|
|
401 |
\end{verbatim}
|
|
402 |
\noindent
|
|
403 |
After putting in those rules, the example of \ref{NaiveMatcher}
|
|
404 |
is now very tame in the length of inputs:
|
|
405 |
|
|
406 |
|
|
407 |
\begin{tikzpicture}
|
|
408 |
\begin{axis}[
|
|
409 |
xlabel={$n$},
|
|
410 |
ylabel={time in secs},
|
|
411 |
ymode = log,
|
|
412 |
xmode = log,
|
|
413 |
legend entries={Matcher With Simp},
|
|
414 |
legend pos=north west,
|
|
415 |
legend cell align=left]
|
|
416 |
\addplot[red,mark=*, mark options={fill=white}] table {BetterMatcher.data};
|
|
417 |
\end{axis}
|
|
418 |
\end{tikzpicture} \label{fig:BetterMatcher}
|
|
419 |
|
|
420 |
|
|
421 |
\noindent
|
|
422 |
Note how the x-axis is in logarithmic scale.
|
538
|
423 |
Building derivatives and then testing the existence
|
539
|
424 |
of empty string in the resulting regular expression's language,
|
|
425 |
and add simplification rules when necessary.
|
538
|
426 |
So far, so good. But what if we want to
|
|
427 |
do lexing instead of just getting a YES/NO answer?
|
539
|
428 |
\citeauthor{Sulzmann2014} first came up with a nice and
|
538
|
429 |
elegant (arguably as beautiful as the definition of the original derivative) solution for this.
|
|
430 |
|
539
|
431 |
\section{Values and the Lexing Algorithm by Sulzmann and Lu}
|
538
|
432 |
Here we present the hybrid phases of a regular expression lexing
|
|
433 |
algorithm using the function $\inj$, as given by Sulzmann and Lu.
|
|
434 |
They first defined the datatypes for storing the
|
|
435 |
lexing information called a \emph{value} or
|
|
436 |
sometimes also \emph{lexical value}. These values and regular
|
|
437 |
expressions correspond to each other as illustrated in the following
|
|
438 |
table:
|
|
439 |
|
|
440 |
\begin{center}
|
|
441 |
\begin{tabular}{c@{\hspace{20mm}}c}
|
|
442 |
\begin{tabular}{@{}rrl@{}}
|
|
443 |
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
|
|
444 |
$r$ & $::=$ & $\ZERO$\\
|
|
445 |
& $\mid$ & $\ONE$ \\
|
|
446 |
& $\mid$ & $c$ \\
|
|
447 |
& $\mid$ & $r_1 \cdot r_2$\\
|
|
448 |
& $\mid$ & $r_1 + r_2$ \\
|
|
449 |
\\
|
|
450 |
& $\mid$ & $r^*$ \\
|
|
451 |
\end{tabular}
|
|
452 |
&
|
|
453 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}}
|
|
454 |
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
|
|
455 |
$v$ & $::=$ & \\
|
|
456 |
& & $\Empty$ \\
|
|
457 |
& $\mid$ & $\Char(c)$ \\
|
|
458 |
& $\mid$ & $\Seq\,v_1\, v_2$\\
|
|
459 |
& $\mid$ & $\Left(v)$ \\
|
|
460 |
& $\mid$ & $\Right(v)$ \\
|
|
461 |
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
|
|
462 |
\end{tabular}
|
|
463 |
\end{tabular}
|
|
464 |
\end{center}
|
|
465 |
|
|
466 |
\noindent
|
|
467 |
We have a formal binary relation for telling whether the structure
|
|
468 |
of a regular expression agrees with the value.
|
|
469 |
\begin{mathpar}
|
|
470 |
\inferrule{}{\vdash \Char(c) : \mathbf{c}} \hspace{2em}
|
|
471 |
\inferrule{}{\vdash \Empty : \ONE} \hspace{2em}
|
|
472 |
\inferrule{\vdash v_1 : r_1 \\ \vdash v_2 : r_2 }{\vdash \Seq(v_1, v_2) : (r_1 \cdot r_2)}
|
|
473 |
\end{mathpar}
|
|
474 |
|
|
475 |
Building on top of Sulzmann and Lu's attempt to formalise the
|
|
476 |
notion of POSIX lexing rules \parencite{Sulzmann2014},
|
|
477 |
Ausaf and Urban\parencite{AusafDyckhoffUrban2016} modelled
|
|
478 |
POSIX matching as a ternary relation recursively defined in a
|
|
479 |
natural deduction style.
|
|
480 |
The formal definition of a $\POSIX$ value $v$ for a regular expression
|
|
481 |
$r$ and string $s$, denoted as $(s, r) \rightarrow v$, can be specified
|
|
482 |
in the following set of rules:
|
|
483 |
\ChristianComment{Will complete later}
|
|
484 |
\newcommand*{\inference}[3][t]{%
|
|
485 |
\begingroup
|
|
486 |
\def\and{\\}%
|
|
487 |
\begin{tabular}[#1]{@{\enspace}c@{\enspace}}
|
|
488 |
#2 \\
|
|
489 |
\hline
|
|
490 |
#3
|
|
491 |
\end{tabular}%
|
|
492 |
\endgroup
|
|
493 |
}
|
|
494 |
\begin{center}
|
|
495 |
\inference{$s_1 @ s_2 = s$ \and $(\nexists s_3 s_4 s_5. s_1 @ s_5 = s_3 \land s_5 \neq [] \land s_3 @ s_4 = s \land (s_3, r_1) \rightarrow v_3 \land (s_4, r_2) \rightarrow v_4)$ \and $(s_1, r_1) \rightarrow v_1$ \and $(s_2, r_2) \rightarrow v_2$ }{$(s, r_1 \cdot r_2) \rightarrow \Seq(v_1, v_2)$ }
|
|
496 |
\end{center}
|
|
497 |
\noindent
|
|
498 |
The above $\POSIX$ rules could be explained intuitionally as
|
|
499 |
\begin{itemize}
|
|
500 |
\item
|
|
501 |
match the leftmost regular expression when multiple options of matching
|
|
502 |
are available
|
|
503 |
\item
|
|
504 |
always match a subpart as much as possible before proceeding
|
|
505 |
to the next token.
|
|
506 |
\end{itemize}
|
|
507 |
|
|
508 |
The reason why we are interested in $\POSIX$ values is that they can
|
|
509 |
be practically used in the lexing phase of a compiler front end.
|
|
510 |
For instance, when lexing a code snippet
|
|
511 |
$\textit{iffoo} = 3$ with the regular expression $\textit{keyword} + \textit{identifier}$, we want $\textit{iffoo}$ to be recognized
|
|
512 |
as an identifier rather than a keyword.
|
|
513 |
|
|
514 |
The good property about a $\POSIX$ value is that
|
|
515 |
given the same regular expression $r$ and string $s$,
|
|
516 |
one can always uniquely determine the $\POSIX$ value for it:
|
|
517 |
\begin{lemma}
|
|
518 |
$\textit{if} \,(s, r) \rightarrow v_1 \land (s, r) \rightarrow v_2\quad \textit{then} \; v_1 = v_2$
|
|
519 |
\end{lemma}
|
539
|
520 |
\begin{proof}
|
|
521 |
By induction on $s$, $r$ and $v_1$. The induction principle is
|
|
522 |
the \POSIX rules. Each case is proven by a combination of
|
|
523 |
the induction rules for $\POSIX$ values and the inductive hypothesis.
|
|
524 |
Probably the most cumbersome cases are the sequence and star with non-empty iterations.
|
|
525 |
|
|
526 |
We give the reasoning about the sequence case as follows:
|
|
527 |
When we have $(s_1, r_1) \rightarrow v_1$ and $(s_2, r_2) \rightarrow v_2$,
|
|
528 |
we know that there could not be a longer string $r_1'$ such that $(s_1', r_1) \rightarrow v_1'$
|
|
529 |
and $(s_2', r_2) \rightarrow v2'$ and $s_1' @s_2' = s$ all hold.
|
|
530 |
For possible values of $s_1'$ and $s_2'$ where $s_1'$ is shorter, they cannot
|
|
531 |
possibly form a $\POSIX$ for $s$.
|
|
532 |
If we have some other values $v_1'$ and $v_2'$ such that
|
|
533 |
$(s_1, r_1) \rightarrow v_1'$ and $(s_2, r_2) \rightarrow v_2'$,
|
|
534 |
Then by induction hypothesis $v_1' = v_1$ and $v_2'= v_2$,
|
|
535 |
which means this "different" $\POSIX$ value $\Seq(v_1', v_2')$
|
|
536 |
is the same as $\Seq(v_1, v_2)$.
|
|
537 |
\end{proof}
|
538
|
538 |
Now we know what a $\POSIX$ value is, the problem is how do we achieve
|
|
539 |
such a value in a lexing algorithm, using derivatives?
|
|
540 |
|
|
541 |
\subsection{Sulzmann and Lu's Injection-based Lexing Algorithm}
|
|
542 |
|
|
543 |
The contribution of Sulzmann and Lu is an extension of Brzozowski's
|
|
544 |
algorithm by a second phase (the first phase being building successive
|
539
|
545 |
derivatives---see \ref{graph:successive_ders}). In this second phase, a POSIX value
|
538
|
546 |
is generated if the regular expression matches the string.
|
|
547 |
Two functions are involved: $\inj$ and $\mkeps$.
|
|
548 |
The function $\mkeps$ constructs a value from the last
|
|
549 |
one of all the successive derivatives:
|
|
550 |
\begin{ceqn}
|
|
551 |
\begin{equation}\label{graph:mkeps}
|
|
552 |
\begin{tikzcd}
|
|
553 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed] & r_n \arrow[d, "mkeps" description] \\
|
|
554 |
& & & v_n
|
|
555 |
\end{tikzcd}
|
|
556 |
\end{equation}
|
|
557 |
\end{ceqn}
|
|
558 |
|
|
559 |
It tells us how can an empty string be matched by a
|
|
560 |
regular expression, in a $\POSIX$ way:
|
|
561 |
|
|
562 |
\begin{center}
|
|
563 |
\begin{tabular}{lcl}
|
|
564 |
$\mkeps(\ONE)$ & $\dn$ & $\Empty$ \\
|
|
565 |
$\mkeps(r_{1}+r_{2})$ & $\dn$
|
|
566 |
& \textit{if} $\nullable(r_{1})$\\
|
|
567 |
& & \textit{then} $\Left(\mkeps(r_{1}))$\\
|
|
568 |
& & \textit{else} $\Right(\mkeps(r_{2}))$\\
|
|
569 |
$\mkeps(r_1\cdot r_2)$ & $\dn$ & $\Seq\,(\mkeps\,r_1)\,(\mkeps\,r_2)$\\
|
|
570 |
$mkeps(r^*)$ & $\dn$ & $\Stars\,[]$
|
|
571 |
\end{tabular}
|
|
572 |
\end{center}
|
|
573 |
|
|
574 |
|
|
575 |
\noindent
|
|
576 |
We favour the left to match an empty string if there is a choice.
|
|
577 |
When there is a star for us to match the empty string,
|
|
578 |
we give the $\Stars$ constructor an empty list, meaning
|
|
579 |
no iterations are taken.
|
|
580 |
The result of a call to $\mkeps$ on a $\nullable$ $r$ would
|
|
581 |
be a $\POSIX$ value corresponding to $r$:
|
|
582 |
\begin{lemma}
|
|
583 |
$\nullable(r) \implies (r, []) \rightarrow (\mkeps\; v)$
|
|
584 |
\end{lemma}\label{mePosix}
|
|
585 |
|
|
586 |
|
|
587 |
After the $\mkeps$-call, we inject back the characters one by one in order to build
|
|
588 |
the lexical value $v_i$ for how the regex $r_i$ matches the string $s_i$
|
|
589 |
($s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
|
|
590 |
After injecting back $n$ characters, we get the lexical value for how $r_0$
|
|
591 |
matches $s$.
|
|
592 |
To do this, Sulzmann and Lu defined a function that reverses
|
|
593 |
the ``chopping off'' of characters during the derivative phase. The
|
|
594 |
corresponding function is called \emph{injection}, written
|
|
595 |
$\textit{inj}$; it takes three arguments: the first one is a regular
|
|
596 |
expression ${r_{i-1}}$, before the character is chopped off, the second
|
|
597 |
is a character ${c_{i-1}}$, the character we want to inject and the
|
|
598 |
third argument is the value ${v_i}$, into which one wants to inject the
|
|
599 |
character (it corresponds to the regular expression after the character
|
|
600 |
has been chopped off). The result of this function is a new value.
|
|
601 |
\begin{ceqn}
|
|
602 |
\begin{equation}\label{graph:inj}
|
|
603 |
\begin{tikzcd}
|
|
604 |
r_1 \arrow[r, dashed] \arrow[d]& r_i \arrow[r, "\backslash c_i"] \arrow[d] & r_{i+1} \arrow[r, dashed] \arrow[d] & r_n \arrow[d, "mkeps" description] \\
|
|
605 |
v_1 \arrow[u] & v_i \arrow[l, dashed] & v_{i+1} \arrow[l,"inj_{r_i} c_i"] & v_n \arrow[l, dashed]
|
|
606 |
\end{tikzcd}
|
|
607 |
\end{equation}
|
|
608 |
\end{ceqn}
|
|
609 |
|
|
610 |
|
|
611 |
\noindent
|
|
612 |
The
|
|
613 |
definition of $\textit{inj}$ is as follows:
|
|
614 |
|
|
615 |
\begin{center}
|
|
616 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
|
|
617 |
$\textit{inj}\,(c)\,c\,Empty$ & $\dn$ & $Char\,c$\\
|
|
618 |
$\textit{inj}\,(r_1 + r_2)\,c\,\Left(v)$ & $\dn$ & $\Left(\textit{inj}\,r_1\,c\,v)$\\
|
|
619 |
$\textit{inj}\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(\textit{inj}\,r_2\,c\,v)$\\
|
|
620 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$ & $Seq(\textit{inj}\,r_1\,c\,v_1,v_2)$\\
|
|
621 |
$\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)$\\
|
|
622 |
$\textit{inj}\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$ & $Seq(\textit{mkeps}(r_1),\textit{inj}\,r_2\,c\,v)$\\
|
|
623 |
$\textit{inj}\,(r^*)\,c\,Seq(v,Stars\,vs)$ & $\dn$ & $Stars((\textit{inj}\,r\,c\,v)\,::\,vs)$\\
|
|
624 |
\end{tabular}
|
|
625 |
\end{center}
|
|
626 |
|
|
627 |
\noindent
|
|
628 |
This definition is by recursion on the ``shape'' of regular
|
|
629 |
expressions and values.
|
|
630 |
The clauses do one thing--identifying the ``hole'' on a
|
|
631 |
value to inject the character back into.
|
|
632 |
For instance, in the last clause for injecting back to a value
|
|
633 |
that would turn into a new star value that corresponds to a star,
|
|
634 |
we know it must be a sequence value. And we know that the first
|
|
635 |
value of that sequence corresponds to the child regex of the star
|
|
636 |
with the first character being chopped off--an iteration of the star
|
|
637 |
that had just been unfolded. This value is followed by the already
|
|
638 |
matched star iterations we collected before. So we inject the character
|
|
639 |
back to the first value and form a new value with this latest iteration
|
|
640 |
being added to the previous list of iterations, all under the $\Stars$
|
|
641 |
top level.
|
539
|
642 |
The POSIX value is maintained throughout the process:
|
538
|
643 |
\begin{lemma}
|
|
644 |
$(r \backslash c, s) \rightarrow v \implies (r, c :: s) \rightarrow (\inj r \; c\; v)$
|
|
645 |
\end{lemma}\label{injPosix}
|
|
646 |
|
|
647 |
|
|
648 |
Putting all the functions $\inj$, $\mkeps$, $\backslash$ together,
|
|
649 |
and taking into consideration the possibility of a non-match,
|
|
650 |
we have a lexer with the following recursive definition:
|
|
651 |
\begin{center}
|
539
|
652 |
\begin{tabular}{lcl}
|
538
|
653 |
$\lexer \; r \; [] $ & $=$ & $\textit{if} (\nullable \; r)\; \textit{then}\; \Some(\mkeps \; r) \; \textit{else} \; \None$\\
|
|
654 |
$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer (r\backslash c) s) \textit{of} $\\
|
539
|
655 |
& & $\quad \None \implies \None$\\
|
|
656 |
& & $\quad \mid \Some(v) \implies \Some(\inj \; r\; c\; v)$
|
538
|
657 |
\end{tabular}
|
|
658 |
\end{center}
|
|
659 |
\noindent
|
|
660 |
The central property of the $\lexer$ is that it gives the correct result by
|
|
661 |
$\POSIX$ standards:
|
|
662 |
\begin{lemma}
|
|
663 |
\begin{tabular}{l}
|
|
664 |
$s \in L(r) \Longleftrightarrow (\exists v. \; r \; s = \Some(v) \land (r, \; s) \rightarrow v)$\\
|
|
665 |
$s \notin L(r) \Longleftrightarrow (\lexer \; r\; s = \None)$
|
|
666 |
\end{tabular}
|
|
667 |
\end{lemma}
|
|
668 |
|
|
669 |
|
|
670 |
\begin{proof}
|
|
671 |
By induction on $s$. $r$ is allowed to be an arbitrary regular expression.
|
|
672 |
The $[]$ case is proven by lemma \ref{mePosix}, and the inductive case
|
|
673 |
by lemma \ref{injPosix}.
|
|
674 |
\end{proof}
|
|
675 |
|
539
|
676 |
|
|
677 |
Pictorially, the algorithm is as follows (
|
|
678 |
For convenience, we employ the following notations: the regular
|
538
|
679 |
expression we start with is $r_0$, and the given string $s$ is composed
|
539
|
680 |
of characters $c_0 c_1 \ldots c_{n-1}$. The
|
|
681 |
values built incrementally by \emph{injecting} back the characters into the
|
|
682 |
earlier values are $v_n, \ldots, v_0$. Corresponding values and characters
|
|
683 |
are always in the same subscript, i.e. $\vdash v_i : r_i$):
|
538
|
684 |
|
|
685 |
\begin{ceqn}
|
|
686 |
\begin{equation}\label{graph:2}
|
|
687 |
\begin{tikzcd}
|
|
688 |
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] \\
|
|
689 |
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]
|
|
690 |
\end{tikzcd}
|
|
691 |
\end{equation}
|
|
692 |
\end{ceqn}
|
|
693 |
|
|
694 |
\noindent
|
539
|
695 |
As we did earlier in this chapter on the matcher, one can
|
|
696 |
introduce simplification on the regex.
|
|
697 |
However, now we need to do a backward phase and make sure
|
|
698 |
the values align with the regular expressions.
|
|
699 |
Therefore one has to
|
538
|
700 |
be careful not to break the correctness, as the injection
|
|
701 |
function heavily relies on the structure of the regexes and values
|
|
702 |
being correct and matching each other.
|
|
703 |
It can be achieved by recording some extra rectification functions
|
|
704 |
during the derivatives step, and applying these rectifications in
|
|
705 |
each run during the injection phase.
|
539
|
706 |
|
|
707 |
\ChristianComment{Do I introduce the lexer with rectification here?}
|
538
|
708 |
And we can prove that the POSIX value of how
|
|
709 |
regular expressions match strings will not be affected---although it is much harder
|
|
710 |
to establish.
|
|
711 |
Some initial results in this regard have been
|
|
712 |
obtained in \cite{AusafDyckhoffUrban2016}.
|
|
713 |
|
539
|
714 |
However, even with these simplification rules, we could still end up in
|
|
715 |
trouble, when we encounter cases that require more involved and aggressive
|
|
716 |
simplifications.
|
|
717 |
\section{A Case Requring More Aggressive Simplification}
|
|
718 |
For example, when starting with the regular
|
|
719 |
expression $(a^* \cdot a^*)^*$ and building a few successive derivatives (around 10)
|
|
720 |
w.r.t.~the character $a$, one obtains a derivative regular expression
|
|
721 |
with more than 9000 nodes (when viewed as a tree)
|
|
722 |
even with simplification.
|
|
723 |
\begin{figure}
|
|
724 |
\begin{tikzpicture}
|
|
725 |
\begin{axis}[
|
|
726 |
xlabel={$n$},
|
|
727 |
ylabel={size},
|
|
728 |
legend entries={Naive Matcher},
|
|
729 |
legend pos=north west,
|
|
730 |
legend cell align=left]
|
|
731 |
\addplot[red,mark=*, mark options={fill=white}] table {BetterWaterloo.data};
|
|
732 |
\end{axis}
|
|
733 |
\end{tikzpicture}
|
|
734 |
\caption{Size of $(a^*\cdot a^*)^*$ against $\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$}
|
|
735 |
\end{figure}\label{fig:BetterWaterloo}
|
|
736 |
|
|
737 |
That is because our lexing algorithm currently keeps a lot of
|
|
738 |
"useless values that will never not be used.
|
|
739 |
These different ways of matching will grow exponentially with the string length.
|
538
|
740 |
|
539
|
741 |
For $r= (a^*\cdot a^*)^*$ and
|
|
742 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$,
|
|
743 |
if we do not allow any empty iterations in its lexical values,
|
|
744 |
there will be $n - 1$ "splitting points" on $s$ we can independently choose to
|
538
|
745 |
split or not so that each sub-string
|
539
|
746 |
segmented by those chosen splitting points will form different iterations.
|
|
747 |
For example when $n=4$,
|
538
|
748 |
\begin{center}
|
|
749 |
\begin{tabular}{lcr}
|
539
|
750 |
$aaaa $ & $\rightarrow$ & $\Stars\, [v_{iteration \,aaaa}]$ (1 iteration, this iteration will be divided between the inner sequence $a^*\cdot a^*$)\\
|
|
751 |
$a \mid aaa $ & $\rightarrow$ & $\Stars\, [v_{iteration \,a},\, v_{iteration \,aaa}]$ (two iterations)\\
|
|
752 |
$aa \mid aa $ & $\rightarrow$ & $\Stars\, [v_{iteration \, aa},\, v_{iteration \, aa}]$ (two iterations)\\
|
|
753 |
$a \mid aa\mid a $ & $\rightarrow$ & $\Stars\, [v_{iteration \, a},\, v_{iteration \, aa}, \, v_{iteration \, a}]$ (three iterations)\\
|
|
754 |
$a \mid a \mid a\mid a $ & $\rightarrow$ & $\Stars\, [v_{iteration \, a},\, v_{iteration \, a} \,v_{iteration \, a}, \, v_{iteration \, a}]$ (four iterations)\\
|
538
|
755 |
& $\textit{etc}.$ &
|
|
756 |
\end{tabular}
|
|
757 |
\end{center}
|
|
758 |
\noindent
|
|
759 |
And for each iteration, there are still multiple ways to split
|
|
760 |
between the two $a^*$s.
|
|
761 |
It is not surprising there are exponentially many lexical values
|
|
762 |
that are distinct for the regex and string pair $r= (a^*\cdot a^*)^*$ and
|
|
763 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$.
|
|
764 |
A lexer to keep all the possible values will naturally
|
|
765 |
have an exponential runtime on ambiguous regular expressions.
|
|
766 |
With just $\inj$ and $\mkeps$, the lexing algorithm will keep track of all different values
|
|
767 |
of a match. This means Sulzmann and Lu's injection-based algorithm
|
539
|
768 |
exponential by nature.
|
|
769 |
Somehow one has to make sure which
|
|
770 |
lexical values are $\POSIX$ and need to be kept in a lexing algorithm.
|
538
|
771 |
|
|
772 |
|
|
773 |
For example, the above $r= (a^*\cdot a^*)^*$ and
|
|
774 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$ example has the POSIX value
|
|
775 |
$ \Stars\,[\Seq(Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}], Stars\,[])]$.
|
539
|
776 |
We want to keep this value only, and remove all the regular expression subparts
|
|
777 |
not corresponding to this value during lexing.
|
|
778 |
To do this, a two-phase algorithm with rectification is a bit too fragile.
|
|
779 |
Can we not create those intermediate values $v_1,\ldots v_n$,
|
|
780 |
and get the lexing information that should be already there while
|
|
781 |
doing derivatives in one pass, without a second injection phase?
|
|
782 |
In the meantime, can we make sure that simplifications
|
|
783 |
are easily handled without breaking the correctness of the algorithm?
|
538
|
784 |
|
539
|
785 |
Sulzmann and Lu solved this problem by
|
|
786 |
introducing additional information to the
|
|
787 |
regular expressions called \emph{bitcodes}.
|
538
|
788 |
|
|
789 |
|
|
790 |
|
|
791 |
|