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
|
564
|
7 |
% used for describing the lexing algorithm by Sulzmann and Lu,
|
|
8 |
%and then give the algorithm and its variant and discuss
|
532
|
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.
|
564
|
13 |
This is essentially a description in ``English"
|
|
14 |
of our formalisation in Isabelle/HOL.
|
|
15 |
We also give the definition of what $\POSIX$ lexing means,
|
|
16 |
followed by an algorithm by Sulzmanna and Lu\parencite{Sulzmann2014}
|
|
17 |
that produces the output conforming
|
|
18 |
to the $\POSIX$ standard.
|
|
19 |
It is also worth mentioning that
|
|
20 |
we choose to use the ML-style notation
|
|
21 |
for function applications, where
|
|
22 |
the parameters of a function is not enclosed
|
|
23 |
inside a pair of parentheses (e.g. $f \;x \;y$
|
|
24 |
instead of $f(x,\;y)$). This is mainly
|
|
25 |
to make the text visually more concise.
|
532
|
26 |
|
538
|
27 |
\section{Basic Concepts}
|
564
|
28 |
Usually, formal language theory starts with an alphabet
|
538
|
29 |
denoting a set of characters.
|
541
|
30 |
Here we just use the datatype of characters from Isabelle,
|
|
31 |
which roughly corresponds to the ASCII characters.
|
564
|
32 |
In what follows, we shall leave the information about the alphabet
|
541
|
33 |
implicit.
|
|
34 |
Then using the usual bracket notation for lists,
|
|
35 |
we can define strings made up of characters:
|
532
|
36 |
\begin{center}
|
|
37 |
\begin{tabular}{lcl}
|
541
|
38 |
$\textit{s}$ & $\dn$ & $[] \; |\; c :: s$
|
532
|
39 |
\end{tabular}
|
|
40 |
\end{center}
|
541
|
41 |
Where $c$ is a variable ranging over characters.
|
|
42 |
Strings can be concatenated to form longer strings in the same
|
564
|
43 |
way as we concatenate two lists, which we shall write as $s_1 @ s_2$.
|
541
|
44 |
We omit the precise
|
538
|
45 |
recursive definition here.
|
|
46 |
We overload this concatenation operator for two sets of strings:
|
532
|
47 |
\begin{center}
|
|
48 |
\begin{tabular}{lcl}
|
541
|
49 |
$A @ B $ & $\dn$ & $\{s_A @ s_B \mid s_A \in A \land s_B \in B \}$\\
|
532
|
50 |
\end{tabular}
|
|
51 |
\end{center}
|
538
|
52 |
We also call the above \emph{language concatenation}.
|
532
|
53 |
The power of a language is defined recursively, using the
|
|
54 |
concatenation operator $@$:
|
|
55 |
\begin{center}
|
|
56 |
\begin{tabular}{lcl}
|
|
57 |
$A^0 $ & $\dn$ & $\{ [] \}$\\
|
541
|
58 |
$A^{n+1}$ & $\dn$ & $A @ A^n$
|
532
|
59 |
\end{tabular}
|
|
60 |
\end{center}
|
564
|
61 |
The union of all powers of a language
|
|
62 |
can be used to define the Kleene star operator:
|
532
|
63 |
\begin{center}
|
|
64 |
\begin{tabular}{lcl}
|
536
|
65 |
$A*$ & $\dn$ & $\bigcup_{i \geq 0} A^i$ \\
|
532
|
66 |
\end{tabular}
|
|
67 |
\end{center}
|
|
68 |
|
538
|
69 |
\noindent
|
564
|
70 |
However, to obtain a more convenient induction principle
|
538
|
71 |
in Isabelle/HOL,
|
536
|
72 |
we instead define the Kleene star
|
532
|
73 |
as an inductive set:
|
538
|
74 |
|
532
|
75 |
\begin{center}
|
538
|
76 |
\begin{mathpar}
|
564
|
77 |
\inferrule{\mbox{}}{[] \in A*\\}
|
538
|
78 |
|
564
|
79 |
\inferrule{s_1 \in A \;\; s_2 \in A*}{s_1 @ s_2 \in A*}
|
538
|
80 |
\end{mathpar}
|
532
|
81 |
\end{center}
|
564
|
82 |
\noindent
|
541
|
83 |
We also define an operation of "chopping off" a character from
|
|
84 |
a language, which we call $\Der$, meaning \emph{Derivative} (for a language):
|
532
|
85 |
\begin{center}
|
|
86 |
\begin{tabular}{lcl}
|
|
87 |
$\textit{Der} \;c \;A$ & $\dn$ & $\{ s \mid c :: s \in A \}$\\
|
|
88 |
\end{tabular}
|
|
89 |
\end{center}
|
538
|
90 |
\noindent
|
541
|
91 |
This can be generalised to "chopping off" a string from all strings within set $A$,
|
|
92 |
namely:
|
532
|
93 |
\begin{center}
|
|
94 |
\begin{tabular}{lcl}
|
541
|
95 |
$\textit{Ders} \;s \;A$ & $\dn$ & $\{ s' \mid s@s' \in A \}$\\
|
532
|
96 |
\end{tabular}
|
|
97 |
\end{center}
|
538
|
98 |
\noindent
|
541
|
99 |
which is essentially the left quotient $A \backslash L$ of $A$ against
|
|
100 |
the singleton language with $L = \{w\}$
|
532
|
101 |
in formal language theory.
|
564
|
102 |
However, for the purposes here, the $\textit{Ders}$ definition with
|
541
|
103 |
a single string is sufficient.
|
532
|
104 |
|
577
|
105 |
The reason for defining derivatives
|
|
106 |
is that it provides a different approach
|
|
107 |
to test membership of a string in
|
|
108 |
a set of strings.
|
|
109 |
For example, to test whether the string
|
|
110 |
$bar$ is contained in the set $\{foo, bar, brak\}$, one takes derivative of the set with
|
|
111 |
respect to the string $bar$:
|
|
112 |
\begin{center}
|
|
113 |
\begin{tabular}{lclll}
|
|
114 |
$S = \{foo, bar, brak\}$ & $ \stackrel{\backslash b}{\rightarrow }$ &
|
|
115 |
$\{ar, rak\}$ &
|
|
116 |
$\stackrel{\backslash a}{\rightarrow}$ &
|
|
117 |
\\
|
|
118 |
$\{r \}$ & $\stackrel{\backslash r}{\rightarrow}$ & $\{[]\}$ &
|
|
119 |
$\stackrel{[] \in S \backslash bar}{\longrightarrow}$ & $bar \in S$\\
|
|
120 |
\end{tabular}
|
|
121 |
\end{center}
|
|
122 |
\noindent
|
|
123 |
and in the end test whether the set
|
|
124 |
has the empty string \footnote{ we use the infix notation $A\backslash c$
|
|
125 |
instead of $\Der \; c \; A$ for brevity, as it is clear we are operating
|
|
126 |
on languages rather than regular expressions }.
|
|
127 |
In general, if we have a language $S_{start}$,
|
|
128 |
then we can test whether $s$ is in $S_{start}$
|
|
129 |
by testing whether $[] \in S \backslash s$.
|
|
130 |
|
564
|
131 |
With the sequencing, Kleene star, and $\textit{Der}$ operator on languages,
|
532
|
132 |
we have a few properties of how the language derivative can be defined using
|
|
133 |
sub-languages.
|
577
|
134 |
For example, for the sequence operator, we have
|
|
135 |
something similar to the ``chain rule'' of the calculus derivative:
|
532
|
136 |
\begin{lemma}
|
536
|
137 |
\[
|
|
138 |
\Der \; c \; (A @ B) =
|
|
139 |
\begin{cases}
|
538
|
140 |
((\Der \; c \; A) \, @ \, B ) \cup (\Der \; c\; B) , & \text{if} \; [] \in A \\
|
|
141 |
(\Der \; c \; A) \, @ \, B, & \text{otherwise}
|
536
|
142 |
\end{cases}
|
|
143 |
\]
|
532
|
144 |
\end{lemma}
|
|
145 |
\noindent
|
|
146 |
This lemma states that if $A$ contains the empty string, $\Der$ can "pierce" through it
|
|
147 |
and get to $B$.
|
536
|
148 |
The language $A*$'s derivative can be described using the language derivative
|
532
|
149 |
of $A$:
|
|
150 |
\begin{lemma}
|
538
|
151 |
$\textit{Der} \;c \;(A*) = (\textit{Der}\; c A) @ (A*)$\\
|
532
|
152 |
\end{lemma}
|
|
153 |
\begin{proof}
|
564
|
154 |
There are too inclusions to prove:
|
532
|
155 |
\begin{itemize}
|
564
|
156 |
\item{$\subseteq$}:\\
|
532
|
157 |
The set
|
536
|
158 |
\[ \{s \mid c :: s \in A*\} \]
|
532
|
159 |
is enclosed in the set
|
564
|
160 |
\[ \{s_1 @ s_2 \mid s_1 \, s_2.\; s_1 \in \{s \mid c :: s \in A\} \land s_2 \in A* \} \]
|
532
|
161 |
because whenever you have a string starting with a character
|
538
|
162 |
in the language of a Kleene star $A*$,
|
|
163 |
then that character together with some sub-string
|
|
164 |
immediately after it will form the first iteration,
|
|
165 |
and the rest of the string will
|
536
|
166 |
be still in $A*$.
|
564
|
167 |
\item{$\supseteq$}:\\
|
532
|
168 |
Note that
|
538
|
169 |
\[ \Der \; c \; (A*) = \Der \; c \; (\{ [] \} \cup (A @ A*) ) \]
|
564
|
170 |
hold.
|
|
171 |
Also this holds:
|
536
|
172 |
\[ \Der \; c \; (\{ [] \} \cup (A @ A*) ) = \Der\; c \; (A @ A*) \]
|
564
|
173 |
where the $\textit{RHS}$ can be rewritten
|
|
174 |
as \[ (\Der \; c\; A) @ A* \cup (\Der \; c \; (A*)) \]
|
|
175 |
which of course contains $\Der \; c \; A @ A*$.
|
532
|
176 |
\end{itemize}
|
|
177 |
\end{proof}
|
538
|
178 |
|
|
179 |
\noindent
|
532
|
180 |
Before we define the $\textit{Der}$ and $\textit{Ders}$ counterpart
|
|
181 |
for regular languages, we need to first give definitions for regular expressions.
|
|
182 |
|
536
|
183 |
\subsection{Regular Expressions and Their Meaning}
|
564
|
184 |
The \emph{basic regular expressions} are defined inductively
|
532
|
185 |
by the following grammar:
|
|
186 |
\[ r ::= \ZERO \mid \ONE
|
|
187 |
\mid c
|
|
188 |
\mid r_1 \cdot r_2
|
|
189 |
\mid r_1 + r_2
|
|
190 |
\mid r^*
|
|
191 |
\]
|
538
|
192 |
\noindent
|
564
|
193 |
We call them basic because we will introduce
|
|
194 |
additional constructors in later chapters such as negation
|
538
|
195 |
and bounded repetitions.
|
564
|
196 |
We use $\ZERO$ for the regular expression that
|
|
197 |
matches no string, and $\ONE$ for the regular
|
|
198 |
expression that matches only the empty string\footnote{
|
|
199 |
some authors
|
|
200 |
also use $\phi$ and $\epsilon$ for $\ZERO$ and $\ONE$
|
|
201 |
but we prefer our notation}.
|
|
202 |
The sequence regular expression is written $r_1\cdot r_2$
|
|
203 |
and sometimes we omit the dot if it is clear which
|
|
204 |
regular expression is meant; the alternative
|
|
205 |
is written $r_1 + r_2$.
|
|
206 |
The \emph{language} or meaning of
|
|
207 |
a regular expression is defined recursively as
|
|
208 |
a set of strings:
|
532
|
209 |
%TODO: FILL in the other defs
|
|
210 |
\begin{center}
|
|
211 |
\begin{tabular}{lcl}
|
564
|
212 |
$L \; \ZERO$ & $\dn$ & $\phi$\\
|
|
213 |
$L \; \ONE$ & $\dn$ & $\{[]\}$\\
|
|
214 |
$L \; c$ & $\dn$ & $\{[c]\}$\\
|
|
215 |
$L \; r_1 + r_2$ & $\dn$ & $ L \; r_1 \cup L \; r_2$\\
|
|
216 |
$L \; r_1 \cdot r_2$ & $\dn$ & $ L \; r_1 @ L \; r_2$\\
|
|
217 |
$L \; r^*$ & $\dn$ & $ (L\;r)*$
|
532
|
218 |
\end{tabular}
|
|
219 |
\end{center}
|
536
|
220 |
\noindent
|
532
|
221 |
Now with semantic derivatives of a language and regular expressions and
|
|
222 |
their language interpretations in place, we are ready to define derivatives on regexes.
|
|
223 |
\subsection{Brzozowski Derivatives and a Regular Expression Matcher}
|
564
|
224 |
%Recall, the language derivative acts on a set of strings
|
|
225 |
%and essentially chops off a particular character from
|
|
226 |
%all strings in that set, Brzozowski defined a derivative operation on regular expressions
|
|
227 |
%so that after derivative $L(r\backslash c)$
|
|
228 |
%will look as if it was obtained by doing a language derivative on $L(r)$:
|
577
|
229 |
Recall that the semantic derivative acts on a
|
|
230 |
language (set of strings).
|
|
231 |
One can decide whether a string $s$ belongs
|
|
232 |
to a language $S$ by taking derivative with respect to
|
|
233 |
that string and then checking whether the empty
|
|
234 |
string is in the derivative:
|
|
235 |
\begin{center}
|
|
236 |
\parskip \baselineskip
|
|
237 |
\def\myupbracefill#1{\rotatebox{90}{\stretchto{\{}{#1}}}
|
|
238 |
\def\rlwd{.5pt}
|
|
239 |
\newcommand\notate[3]{%
|
|
240 |
\unskip\def\useanchorwidth{T}%
|
|
241 |
\setbox0=\hbox{#1}%
|
|
242 |
\def\stackalignment{c}\stackunder[-6pt]{%
|
|
243 |
\def\stackalignment{c}\stackunder[-1.5pt]{%
|
|
244 |
\stackunder[-2pt]{\strut #1}{\myupbracefill{\wd0}}}{%
|
|
245 |
\rule{\rlwd}{#2\baselineskip}}}{%
|
|
246 |
\strut\kern7pt$\hookrightarrow$\rlap{ \footnotesize#3}}\ignorespaces%
|
|
247 |
}
|
|
248 |
\Longstack{
|
|
249 |
\notate{$\{ \ldots ,\;$
|
|
250 |
\notate{s}{1}{$(c_1 :: s_1)$}
|
|
251 |
$, \; \ldots \}$
|
|
252 |
}{1}{$S_{start}$}
|
|
253 |
}
|
|
254 |
\Longstack{
|
|
255 |
$\stackrel{\backslash c_1}{\longrightarrow}$
|
|
256 |
}
|
|
257 |
\Longstack{
|
|
258 |
$\{ \ldots,\;$ \notate{$s_1$}{1}{$(c_2::s_2)$}
|
|
259 |
$,\; \ldots \}$
|
|
260 |
}
|
|
261 |
\Longstack{
|
|
262 |
$\stackrel{\backslash c_2}{\longrightarrow}$
|
|
263 |
}
|
|
264 |
\Longstack{
|
|
265 |
$\{ \ldots,\; s_2
|
|
266 |
,\; \ldots \}$
|
|
267 |
}
|
|
268 |
\Longstack{
|
|
269 |
$ \xdashrightarrow{\backslash c_3\ldots\ldots} $
|
|
270 |
}
|
|
271 |
\Longstack{
|
|
272 |
\notate{$\{\ldots, [], \ldots\}$}{1}{$S_{end} =
|
|
273 |
S_{start}\backslash s$}
|
|
274 |
}
|
|
275 |
\end{center}
|
|
276 |
\begin{center}
|
|
277 |
$s \in S_{start} \iff [] \in S_{end}$
|
|
278 |
\end{center}
|
|
279 |
\noindent
|
|
280 |
Brzozowski noticed that this operation
|
564
|
281 |
can be ``mirrored" on regular expressions which
|
|
282 |
he calls the derivative of a regular expression $r$
|
|
283 |
with respect to a character $c$, written
|
577
|
284 |
$r \backslash c$. This infix operator
|
|
285 |
takes an original regular expression $r$ as input
|
|
286 |
and a character as a right operand and
|
|
287 |
outputs a result, which is a new regular expression.
|
|
288 |
The derivative operation on regular expression
|
|
289 |
is defined such that the language of the derivative result
|
|
290 |
coincides with the language of the original
|
|
291 |
regular expression's language being taken the language
|
|
292 |
derivative with respect to the same character:
|
|
293 |
\begin{center}
|
|
294 |
\parskip \baselineskip
|
|
295 |
\def\myupbracefill#1{\rotatebox{90}{\stretchto{\{}{#1}}}
|
|
296 |
\def\rlwd{.5pt}
|
|
297 |
\newcommand\notate[3]{%
|
|
298 |
\unskip\def\useanchorwidth{T}%
|
|
299 |
\setbox0=\hbox{#1}%
|
|
300 |
\def\stackalignment{c}\stackunder[-6pt]{%
|
|
301 |
\def\stackalignment{c}\stackunder[-1.5pt]{%
|
|
302 |
\stackunder[-2pt]{\strut #1}{\myupbracefill{\wd0}}}{%
|
|
303 |
\rule{\rlwd}{#2\baselineskip}}}{%
|
|
304 |
\strut\kern8pt$\hookrightarrow$\rlap{ \footnotesize#3}}\ignorespaces%
|
|
305 |
}
|
|
306 |
\Longstack{
|
|
307 |
\notate{$r$}{1}{$L \; r = \{\ldots, \;c::s_1,
|
|
308 |
\;\ldots\}$}
|
|
309 |
}
|
|
310 |
\Longstack{
|
|
311 |
$\stackrel{\backslash c}{\longrightarrow}$
|
|
312 |
}
|
|
313 |
\Longstack{
|
|
314 |
\notate{$r\backslash c$}{2}{$L \; (r\backslash c)=
|
|
315 |
\{\ldots,\;s_1,\;\ldots\}$}
|
|
316 |
}
|
|
317 |
\end{center}
|
538
|
318 |
\begin{center}
|
532
|
319 |
|
|
320 |
\[
|
564
|
321 |
L(r \backslash c) = \Der \; c \; L(r)
|
532
|
322 |
\]
|
564
|
323 |
\end{center}
|
|
324 |
\noindent
|
577
|
325 |
where we do derivatives on the regular expression
|
|
326 |
$r$ and test membership of $s$ by checking
|
|
327 |
whether the empty string is in the language of
|
|
328 |
$r\backslash s$.
|
564
|
329 |
For example in the sequence case we have
|
|
330 |
\begin{center}
|
|
331 |
\begin{tabular}{lcl}
|
|
332 |
$\Der \; c \; (A @ B)$ & $\dn$ &
|
|
333 |
$ \textit{if} \;\, [] \in A \;
|
|
334 |
\textit{then} \;\, ((\Der \; c \; A) @ B ) \cup
|
|
335 |
\Der \; c\; B$\\
|
|
336 |
& & $\textit{else}\; (\Der \; c \; A) @ B$\\
|
|
337 |
\end{tabular}
|
|
338 |
\end{center}
|
|
339 |
\noindent
|
|
340 |
This can be translated to
|
|
341 |
regular expressions in the following
|
|
342 |
manner:
|
|
343 |
\begin{center}
|
|
344 |
\begin{tabular}{lcl}
|
|
345 |
$(r_1 \cdot r_2 ) \backslash c$ & $\dn$ & $\textit{if}\;\,([] \in L(r_1)) r_1 \backslash c \cdot r_2 + r_2 \backslash c$ \\
|
|
346 |
& & $\textit{else} \; (r_1 \backslash c) \cdot r_2$
|
|
347 |
\end{tabular}
|
|
348 |
\end{center}
|
532
|
349 |
|
|
350 |
\noindent
|
564
|
351 |
And similarly, the Kleene star's semantic derivative
|
|
352 |
can be expressed as
|
532
|
353 |
\[
|
564
|
354 |
\textit{Der} \;c \;(A*) \dn (\textit{Der}\; c A) @ (A*)
|
532
|
355 |
\]
|
564
|
356 |
which translates to
|
532
|
357 |
\[
|
564
|
358 |
(r^*) \backslash c \dn (r \backslash c)\cdot r^*.
|
532
|
359 |
\]
|
564
|
360 |
In the above definition of $(r_1\cdot r_2) \backslash c$,
|
|
361 |
the $\textit{if}$ clause's
|
|
362 |
boolean condition
|
|
363 |
$[] \in L(r_1)$ needs to be
|
|
364 |
somehow recursively computed.
|
|
365 |
We call such a function that checks
|
|
366 |
whether the empty string $[]$ is
|
|
367 |
in the language of a regular expression $\nullable$:
|
|
368 |
\begin{center}
|
|
369 |
\begin{tabular}{lcl}
|
|
370 |
$\nullable(\ZERO)$ & $\dn$ & $\mathit{false}$ \\
|
|
371 |
$\nullable(\ONE)$ & $\dn$ & $\mathit{true}$ \\
|
|
372 |
$\nullable(c)$ & $\dn$ & $\mathit{false}$ \\
|
|
373 |
$\nullable(r_1 + r_2)$ & $\dn$ & $\nullable(r_1) \vee \nullable(r_2)$ \\
|
|
374 |
$\nullable(r_1\cdot r_2)$ & $\dn$ & $\nullable(r_1) \wedge \nullable(r_2)$ \\
|
|
375 |
$\nullable(r^*)$ & $\dn$ & $\mathit{true}$ \\
|
|
376 |
\end{tabular}
|
|
377 |
\end{center}
|
|
378 |
\noindent
|
|
379 |
The $\ZERO$ regular expression
|
|
380 |
does not contain any string and
|
|
381 |
therefore is not \emph{nullable}.
|
|
382 |
$\ONE$ is \emph{nullable}
|
|
383 |
by definition.
|
|
384 |
The character regular expression $c$
|
|
385 |
corresponds to the singleton set $\{c\}$,
|
|
386 |
and therefore does not contain the empty string.
|
|
387 |
The alternative regular expression is nullable
|
|
388 |
if at least one of its children is nullable.
|
|
389 |
The sequence regular expression
|
|
390 |
would require both children to have the empty string
|
|
391 |
to compose an empty string, and the Kleene star
|
|
392 |
is always nullable because it naturally
|
|
393 |
contains the empty string.
|
|
394 |
|
|
395 |
The derivative function, written $r\backslash c$,
|
|
396 |
defines how a regular expression evolves into
|
|
397 |
a new one after all the string it contains is acted on:
|
|
398 |
if it starts with $c$, then the character is chopped of,
|
|
399 |
if not, that string is removed.
|
532
|
400 |
\begin{center}
|
|
401 |
\begin{tabular}{lcl}
|
|
402 |
$\ZERO \backslash c$ & $\dn$ & $\ZERO$\\
|
|
403 |
$\ONE \backslash c$ & $\dn$ & $\ZERO$\\
|
|
404 |
$d \backslash c$ & $\dn$ &
|
|
405 |
$\mathit{if} \;c = d\;\mathit{then}\;\ONE\;\mathit{else}\;\ZERO$\\
|
|
406 |
$(r_1 + r_2)\backslash c$ & $\dn$ & $r_1 \backslash c \,+\, r_2 \backslash c$\\
|
538
|
407 |
$(r_1 \cdot r_2)\backslash c$ & $\dn$ & $\mathit{if} \, [] \in L(r_1)$\\
|
532
|
408 |
& & $\mathit{then}\;(r_1\backslash c) \cdot r_2 \,+\, r_2\backslash c$\\
|
|
409 |
& & $\mathit{else}\;(r_1\backslash c) \cdot r_2$\\
|
|
410 |
$(r^*)\backslash c$ & $\dn$ & $(r\backslash c) \cdot r^*$\\
|
|
411 |
\end{tabular}
|
|
412 |
\end{center}
|
|
413 |
\noindent
|
564
|
414 |
The most involved cases are the sequence case
|
|
415 |
and the star case.
|
532
|
416 |
The sequence case says that if the first regular expression
|
564
|
417 |
contains an empty string, then the second component of the sequence
|
|
418 |
needs to be considered, as its derivative will contribute to the
|
|
419 |
result of this derivative.
|
|
420 |
The star regular expression $r^*$'s derivative
|
|
421 |
unwraps one iteration of $r$, turns it into $r\backslash c$,
|
|
422 |
and attaches the original $r^*$
|
|
423 |
after $r\backslash c$, so that
|
|
424 |
we can further unfold it as many times as needed.
|
|
425 |
We have the following correspondence between
|
|
426 |
derivatives on regular expressions and
|
|
427 |
derivatives on a set of strings:
|
|
428 |
\begin{lemma}\label{derDer}
|
532
|
429 |
$\textit{Der} \; c \; L(r) = L (r\backslash c)$
|
|
430 |
\end{lemma}
|
|
431 |
|
|
432 |
\noindent
|
|
433 |
The main property of the derivative operation
|
564
|
434 |
(that enables us to reason about the correctness of
|
|
435 |
derivative-based matching)
|
|
436 |
is
|
532
|
437 |
|
539
|
438 |
\begin{lemma}\label{derStepwise}
|
564
|
439 |
$c\!::\!s \in L(r)$ \textit{iff} $s \in L(r\backslash c)$.
|
539
|
440 |
\end{lemma}
|
532
|
441 |
|
|
442 |
\noindent
|
|
443 |
We can generalise the derivative operation shown above for single characters
|
|
444 |
to strings as follows:
|
|
445 |
|
|
446 |
\begin{center}
|
|
447 |
\begin{tabular}{lcl}
|
|
448 |
$r \backslash_s (c\!::\!s) $ & $\dn$ & $(r \backslash c) \backslash_s s$ \\
|
|
449 |
$r \backslash [\,] $ & $\dn$ & $r$
|
|
450 |
\end{tabular}
|
|
451 |
\end{center}
|
|
452 |
|
|
453 |
\noindent
|
564
|
454 |
When there is no ambiguity, we will
|
|
455 |
omit the subscript and use $\backslash$ instead
|
|
456 |
of $\backslash_r$ to denote
|
532
|
457 |
string derivatives for brevity.
|
539
|
458 |
Brzozowski's regular-expression matcher algorithm can then be described as:
|
532
|
459 |
|
|
460 |
\begin{definition}
|
564
|
461 |
$\textit{match}\;s\;r \;\dn\; \nullable \; (r\backslash s)$
|
532
|
462 |
\end{definition}
|
|
463 |
|
|
464 |
\noindent
|
|
465 |
Assuming the string is given as a sequence of characters, say $c_0c_1..c_n$,
|
|
466 |
this algorithm presented graphically is as follows:
|
|
467 |
|
|
468 |
\begin{equation}\label{graph:successive_ders}
|
|
469 |
\begin{tikzcd}
|
|
470 |
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}
|
|
471 |
\end{tikzcd}
|
|
472 |
\end{equation}
|
|
473 |
|
|
474 |
\noindent
|
539
|
475 |
It can be
|
|
476 |
relatively easily shown that this matcher is correct:
|
|
477 |
\begin{lemma}
|
564
|
478 |
$\textit{match} \; s\; r = \textit{true} \; \textit{iff} \; s \in L(r)$
|
539
|
479 |
\end{lemma}
|
|
480 |
\begin{proof}
|
564
|
481 |
By the stepwise property of derivatives (lemma \ref{derStepwise})
|
|
482 |
and lemma \ref{derDer}.
|
539
|
483 |
\end{proof}
|
|
484 |
\noindent
|
564
|
485 |
\begin{center}
|
|
486 |
\begin{figure}
|
539
|
487 |
\begin{tikzpicture}
|
|
488 |
\begin{axis}[
|
|
489 |
xlabel={$n$},
|
|
490 |
ylabel={time in secs},
|
|
491 |
ymode = log,
|
|
492 |
legend entries={Naive Matcher},
|
|
493 |
legend pos=north west,
|
|
494 |
legend cell align=left]
|
|
495 |
\addplot[red,mark=*, mark options={fill=white}] table {NaiveMatcher.data};
|
|
496 |
\end{axis}
|
|
497 |
\end{tikzpicture}
|
|
498 |
\caption{Matching $(a^*)^*b$ against $\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$}\label{NaiveMatcher}
|
|
499 |
\end{figure}
|
564
|
500 |
\end{center}
|
539
|
501 |
\noindent
|
564
|
502 |
If we implement the above algorithm naively, however,
|
|
503 |
the algorithm can be excruciatingly slow, as shown in
|
|
504 |
\ref{NaiveMatcher}.
|
|
505 |
Note that both axes are in logarithmic scale.
|
|
506 |
Around two dozens characters
|
|
507 |
would already explode the matcher on regular expression
|
|
508 |
$(a^*)^*b$.
|
|
509 |
For this, we need to introduce certain
|
539
|
510 |
rewrite rules for the intermediate results,
|
|
511 |
such as $r + r \rightarrow r$,
|
|
512 |
and make sure those rules do not change the
|
|
513 |
language of the regular expression.
|
564
|
514 |
One simpled-minded simplification function
|
|
515 |
that achieves these requirements is given below:
|
|
516 |
\begin{center}
|
|
517 |
\begin{tabular}{lcl}
|
|
518 |
$\simp \; r_1 \cdot r_2 $ & $ \dn$ &
|
|
519 |
$(\simp \; r_1, \simp \; r_2) \; \textit{match}$\\
|
|
520 |
& & $\quad \case \; (\ZERO, \_) \Rightarrow \ZERO$\\
|
|
521 |
& & $\quad \case \; (\_, \ZERO) \Rightarrow \ZERO$\\
|
|
522 |
& & $\quad \case \; (\ONE, r_2') \Rightarrow r_2'$\\
|
|
523 |
& & $\quad \case \; (r_1', \ONE) \Rightarrow r_1'$\\
|
|
524 |
& & $\quad \case \; (r_1', r_2') \Rightarrow r_1'\cdot r_2'$\\
|
|
525 |
$\simp \; r_1 + r_2$ & $\dn$ & $(\simp \; r_1, \simp \; r_2) \textit{match}$\\
|
|
526 |
& & $\quad \; \case \; (\ZERO, r_2') \Rightarrow r_2'$\\
|
|
527 |
& & $\quad \; \case \; (r_1', \ZERO) \Rightarrow r_1'$\\
|
|
528 |
& & $\quad \; \case \; (r_1', r_2') \Rightarrow r_1' + r_2'$\\
|
|
529 |
$\simp \; r$ & $\dn$ & $r$
|
|
530 |
|
|
531 |
\end{tabular}
|
|
532 |
\end{center}
|
|
533 |
If we repeatedly apply this simplification
|
|
534 |
function during the matching algorithm,
|
|
535 |
we have a matcher with simplification:
|
|
536 |
\begin{center}
|
|
537 |
\begin{tabular}{lcl}
|
|
538 |
$\derssimp \; [] \; r$ & $\dn$ & $r$\\
|
|
539 |
$\derssimp \; c :: cs \; r$ & $\dn$ & $\derssimp \; cs \; (\simp \; (r \backslash c))$\\
|
|
540 |
$\textit{matcher}_{simp}\; s \; r $ & $\dn$ & $\nullable \; (\derssimp \; s\;r)$
|
|
541 |
\end{tabular}
|
|
542 |
\end{center}
|
|
543 |
\begin{figure}
|
539
|
544 |
\begin{tikzpicture}
|
|
545 |
\begin{axis}[
|
|
546 |
xlabel={$n$},
|
|
547 |
ylabel={time in secs},
|
|
548 |
ymode = log,
|
|
549 |
xmode = log,
|
564
|
550 |
grid = both,
|
539
|
551 |
legend entries={Matcher With Simp},
|
|
552 |
legend pos=north west,
|
|
553 |
legend cell align=left]
|
|
554 |
\addplot[red,mark=*, mark options={fill=white}] table {BetterMatcher.data};
|
|
555 |
\end{axis}
|
564
|
556 |
\end{tikzpicture}
|
|
557 |
\caption{$(a^*)^*b$
|
|
558 |
against
|
|
559 |
$\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$ Using $\textit{matcher}_{simp}$}\label{BetterMatcher}
|
|
560 |
\end{figure}
|
|
561 |
\noindent
|
|
562 |
The running time of $\textit{ders}\_\textit{simp}$
|
|
563 |
on the same example of \ref{NaiveMatcher}
|
|
564 |
is now very tame in terms of the length of inputs,
|
|
565 |
as shown in \ref{BetterMatcher}.
|
539
|
566 |
|
538
|
567 |
Building derivatives and then testing the existence
|
539
|
568 |
of empty string in the resulting regular expression's language,
|
564
|
569 |
adding simplifications when necessary.
|
538
|
570 |
So far, so good. But what if we want to
|
|
571 |
do lexing instead of just getting a YES/NO answer?
|
564
|
572 |
Sulzmanna and Lu \cite{Sulzmann2014} first came up with a nice and
|
538
|
573 |
elegant (arguably as beautiful as the definition of the original derivative) solution for this.
|
|
574 |
|
539
|
575 |
\section{Values and the Lexing Algorithm by Sulzmann and Lu}
|
564
|
576 |
In this section, we present a two-phase regular expression lexing
|
|
577 |
algorithm.
|
|
578 |
The first phase takes successive derivatives with
|
|
579 |
respect to the input string,
|
|
580 |
and the second phase does the reverse, \emph{injecting} back
|
|
581 |
characters, in the meantime constructing a lexing result.
|
|
582 |
We will introduce the injection phase in detail slightly
|
|
583 |
later, but as a preliminary we have to first define
|
|
584 |
the datatype for lexing results,
|
|
585 |
called \emph{value} or
|
|
586 |
sometimes also \emph{lexical value}. Values and regular
|
538
|
587 |
expressions correspond to each other as illustrated in the following
|
|
588 |
table:
|
|
589 |
|
|
590 |
\begin{center}
|
|
591 |
\begin{tabular}{c@{\hspace{20mm}}c}
|
|
592 |
\begin{tabular}{@{}rrl@{}}
|
|
593 |
\multicolumn{3}{@{}l}{\textbf{Regular Expressions}}\medskip\\
|
|
594 |
$r$ & $::=$ & $\ZERO$\\
|
|
595 |
& $\mid$ & $\ONE$ \\
|
|
596 |
& $\mid$ & $c$ \\
|
|
597 |
& $\mid$ & $r_1 \cdot r_2$\\
|
|
598 |
& $\mid$ & $r_1 + r_2$ \\
|
|
599 |
\\
|
|
600 |
& $\mid$ & $r^*$ \\
|
|
601 |
\end{tabular}
|
|
602 |
&
|
|
603 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}}
|
|
604 |
\multicolumn{3}{@{}l}{\textbf{Values}}\medskip\\
|
|
605 |
$v$ & $::=$ & \\
|
|
606 |
& & $\Empty$ \\
|
|
607 |
& $\mid$ & $\Char(c)$ \\
|
|
608 |
& $\mid$ & $\Seq\,v_1\, v_2$\\
|
|
609 |
& $\mid$ & $\Left(v)$ \\
|
|
610 |
& $\mid$ & $\Right(v)$ \\
|
|
611 |
& $\mid$ & $\Stars\,[v_1,\ldots\,v_n]$ \\
|
|
612 |
\end{tabular}
|
|
613 |
\end{tabular}
|
|
614 |
\end{center}
|
|
615 |
\noindent
|
564
|
616 |
A value has an underlying string, which
|
|
617 |
can be calculated by the ``flatten" function $|\_|$:
|
|
618 |
\begin{center}
|
|
619 |
\begin{tabular}{lcl}
|
|
620 |
$|\Empty|$ & $\dn$ & $[]$\\
|
|
621 |
$|\Char \; c|$ & $ \dn$ & $ [c]$\\
|
|
622 |
$|\Seq(v_1, v_2)|$ & $ \dn$ & $ v_1| @ |v_2|$\\
|
|
623 |
$|\Left(v)|$ & $ \dn$ & $ |v|$\\
|
|
624 |
$|\Right(v)|$ & $ \dn$ & $ |v|$\\
|
|
625 |
$|\Stars([])|$ & $\dn$ & $[]$\\
|
|
626 |
$|\Stars(v::vs)|$ & $\dn$ & $ |v| @ |\Stars(vs)|$
|
|
627 |
\end{tabular}
|
|
628 |
\end{center}
|
|
629 |
Sulzmann and Lu used a binary predicate, written $\vdash v:r $,
|
|
630 |
to indicate that a value $v$ could be generated from a lexing algorithm
|
|
631 |
with input $r$. They call it the value inhabitation relation.
|
538
|
632 |
\begin{mathpar}
|
564
|
633 |
\inferrule{\mbox{}}{\vdash \Char(c) : \mathbf{c}} \hspace{2em}
|
|
634 |
|
|
635 |
\inferrule{\mbox{}}{\vdash \Empty : \ONE} \hspace{2em}
|
|
636 |
|
|
637 |
\inferrule{\vdash v_1 : r_1 \;\; \vdash v_2 : r_2 }{\vdash \Seq(v_1, v_2) : (r_1 \cdot r_2)}
|
|
638 |
|
|
639 |
\inferrule{\vdash v_1 : r_1}{\vdash \Left(v_1):r_1+r_2}
|
|
640 |
|
|
641 |
\inferrule{\vdash v_2 : r_2}{\vdash \Right(v_2):r_1 + r_2}
|
538
|
642 |
|
564
|
643 |
\inferrule{\forall v \in vs. \vdash v:r \land |v| \neq []}{\vdash \Stars(vs):r^*}
|
|
644 |
\end{mathpar}
|
|
645 |
\noindent
|
|
646 |
The condition $|v| \neq []$ in the premise of star's rule
|
|
647 |
is to make sure that for a given pair of regular
|
|
648 |
expression $r$ and string $s$, the number of values
|
|
649 |
satisfying $|v| = s$ and $\vdash v:r$ is finite.
|
|
650 |
Given the same string and regular expression, there can be
|
|
651 |
multiple values for it. For example, both
|
|
652 |
$\vdash \Seq(\Left \; ab)(\Right \; c):(ab+a)(bc+c)$ and
|
|
653 |
$\vdash \Seq(\Right\; a)(\Left \; bc ):(ab+a)(bc+c)$ hold
|
|
654 |
and the values both flatten to $abc$.
|
|
655 |
Lexers therefore have to disambiguate and choose only
|
|
656 |
one of the values to output. $\POSIX$ is one of the
|
|
657 |
disambiguation strategies that is widely adopted.
|
|
658 |
|
|
659 |
Ausaf and Urban\parencite{AusafDyckhoffUrban2016}
|
|
660 |
formalised the property
|
|
661 |
as a ternary relation.
|
|
662 |
The $\POSIX$ value $v$ for a regular expression
|
538
|
663 |
$r$ and string $s$, denoted as $(s, r) \rightarrow v$, can be specified
|
564
|
664 |
in the following set of rules\footnote{The names of the rules are used
|
|
665 |
as they were originally given in \cite{AusafDyckhoffUrban2016}}:
|
|
666 |
\noindent
|
|
667 |
\begin{figure}
|
|
668 |
\begin{mathpar}
|
|
669 |
\inferrule[P1]{\mbox{}}{([], \ONE) \rightarrow \Empty}
|
|
670 |
|
|
671 |
\inferrule[PC]{\mbox{}}{([c], c) \rightarrow \Char \; c}
|
|
672 |
|
|
673 |
\inferrule[P+L]{(s,r_1)\rightarrow v_1}{(s, r_1+r_2)\rightarrow \Left \; v_1}
|
|
674 |
|
|
675 |
\inferrule[P+R]{(s,r_2)\rightarrow v_2\\ s \notin L \; r_1}{(s, r_1+r_2)\rightarrow \Right \; v_2}
|
|
676 |
|
|
677 |
\inferrule[PS]{(s_1, v_1) \rightarrow r_1 \\ (s_2, v_2)\rightarrow r_2\\
|
|
678 |
\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land
|
|
679 |
s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2}{(s_1 @ s_2, r_1\cdot r_2) \rightarrow
|
|
680 |
\Seq \; v_1 \; v_2}
|
|
681 |
|
|
682 |
\inferrule[P{[]}]{\mbox{}}{([], r^*) \rightarrow \Stars([])}
|
|
683 |
|
|
684 |
\inferrule[P*]{(s_1, v) \rightarrow v \\ (s_2, r^*) \rightarrow \Stars \; vs \\
|
|
685 |
|v| \neq []\\ \nexists s_3 \; s_4. s_3 \neq [] \land s_3@s_4 = s_2 \land
|
|
686 |
s_1@s_3 \in L \; r \land s_4 \in L \; r^*}{(s_1@s_2, r^*)\rightarrow \Stars \;
|
|
687 |
(v::vs)}
|
|
688 |
\end{mathpar}
|
|
689 |
\caption{POSIX Lexing Rules}
|
|
690 |
\end{figure}
|
538
|
691 |
\noindent
|
564
|
692 |
The above $\POSIX$ rules follows the intuition described below:
|
538
|
693 |
\begin{itemize}
|
564
|
694 |
\item (Left Priority)\\
|
|
695 |
Match the leftmost regular expression when multiple options of matching
|
|
696 |
are available.
|
|
697 |
\item (Maximum munch)\\
|
|
698 |
Always match a subpart as much as possible before proceeding
|
|
699 |
to the next token.
|
538
|
700 |
\end{itemize}
|
564
|
701 |
\noindent
|
|
702 |
These disambiguation strategies can be
|
|
703 |
quite practical.
|
538
|
704 |
For instance, when lexing a code snippet
|
564
|
705 |
\[
|
|
706 |
\textit{iffoo} = 3
|
|
707 |
\]
|
|
708 |
using the regular expression (with
|
|
709 |
keyword and identifier having their
|
|
710 |
usualy definitions on any formal
|
|
711 |
language textbook, for instance
|
|
712 |
keyword is a nonempty string starting with letters
|
|
713 |
followed by alphanumeric characters or underscores):
|
|
714 |
\[
|
|
715 |
\textit{keyword} + \textit{identifier},
|
|
716 |
\]
|
|
717 |
we want $\textit{iffoo}$ to be recognized
|
|
718 |
as an identifier rather than a keyword (if)
|
|
719 |
followed by
|
|
720 |
an identifier (foo).
|
|
721 |
POSIX lexing achieves this.
|
|
722 |
|
|
723 |
We know that a $\POSIX$ value is also a normal underlying
|
|
724 |
value:
|
541
|
725 |
\begin{lemma}
|
|
726 |
$(r, s) \rightarrow v \implies \vdash v: r$
|
|
727 |
\end{lemma}
|
|
728 |
\noindent
|
538
|
729 |
The good property about a $\POSIX$ value is that
|
|
730 |
given the same regular expression $r$ and string $s$,
|
|
731 |
one can always uniquely determine the $\POSIX$ value for it:
|
|
732 |
\begin{lemma}
|
|
733 |
$\textit{if} \,(s, r) \rightarrow v_1 \land (s, r) \rightarrow v_2\quad \textit{then} \; v_1 = v_2$
|
|
734 |
\end{lemma}
|
539
|
735 |
\begin{proof}
|
564
|
736 |
By induction on $s$, $r$ and $v_1$. The inductive cases
|
|
737 |
are all the POSIX rules.
|
|
738 |
Probably the most cumbersome cases are
|
|
739 |
the sequence and star with non-empty iterations.
|
567
|
740 |
We shall give the details for proving the sequence case here.
|
539
|
741 |
|
567
|
742 |
When we have
|
|
743 |
\[
|
|
744 |
(s_1, r_1) \rightarrow v_1 \;\, and \;\,
|
|
745 |
(s_2, r_2) \rightarrow v_2 \;\, and \;\,\\
|
|
746 |
\nexists s_3 \; s_4. s_3 \neq [] \land s_3 @ s_4 = s_2 \land
|
|
747 |
s_1@ s_3 \in L \; r_1 \land s_4 \in L \; r_2
|
|
748 |
\]
|
|
749 |
we know that the last condition
|
|
750 |
excludes the possibility of a
|
|
751 |
string $s_1'$ longer than $s_1$ such that
|
|
752 |
\[
|
|
753 |
(s_1', r_1) \rightarrow v_1' \;\;
|
|
754 |
and\;\; (s_2', r_2) \rightarrow v_2'\;\; and \;\;s_1' @s_2' = s
|
|
755 |
\]
|
|
756 |
hold.
|
|
757 |
A shorter string $s_1''$ with $s_2''$ satisfying
|
|
758 |
\[
|
|
759 |
(s_1'', r_1) \rightarrow v_1''
|
|
760 |
\;\;and\;\; (s_2'', r_2) \rightarrow v_2'' \;\;and \;\;s_1'' @s_2'' = s
|
|
761 |
\]
|
|
762 |
cannot possibly form a $\POSIX$ value either, because
|
|
763 |
by definition there is a candidate
|
|
764 |
with longer initial string
|
|
765 |
$s_1$. Therefore, we know that the POSIX
|
|
766 |
value $\Seq \; a \; b$ for $r_1 \cdot r_2$ matching
|
|
767 |
$s$ must have the
|
|
768 |
property that
|
|
769 |
\[
|
|
770 |
|a| = s_1 \;\; and \;\; |b| = s_2.
|
|
771 |
\]
|
|
772 |
The goal is to prove that $a = v_1 $ and $b = v_2$.
|
|
773 |
If we have some other POSIX values $v_{10}$ and $v_{20}$ such that
|
|
774 |
$(s_1, r_1) \rightarrow v_{10}$ and $(s_2, r_2) \rightarrow v_{20}$ hold,
|
|
775 |
then by induction hypothesis $v_{10} = v_1$ and $v_{20}= v_2$,
|
|
776 |
which means this "other" $\POSIX$ value $\Seq(v_{10}, v_{20})$
|
539
|
777 |
is the same as $\Seq(v_1, v_2)$.
|
|
778 |
\end{proof}
|
567
|
779 |
\noindent
|
|
780 |
Now we know what a $\POSIX$ value is and why it is unique;
|
|
781 |
the problem is generating
|
|
782 |
such a value in a lexing algorithm using derivatives.
|
538
|
783 |
|
|
784 |
\subsection{Sulzmann and Lu's Injection-based Lexing Algorithm}
|
|
785 |
|
567
|
786 |
Sulzmann and Lu extended Brzozowski's
|
|
787 |
derivative-based matching
|
|
788 |
to a lexing algorithm by a second pass
|
|
789 |
after the initial phase of successive derivatives.
|
|
790 |
This second phase generates a POSIX value
|
|
791 |
if the regular expression matches the string.
|
538
|
792 |
Two functions are involved: $\inj$ and $\mkeps$.
|
567
|
793 |
The first one used is $\mkeps$, which constructs a POSIX value from the last
|
|
794 |
derivative $r_n$:
|
538
|
795 |
\begin{ceqn}
|
|
796 |
\begin{equation}\label{graph:mkeps}
|
|
797 |
\begin{tikzcd}
|
567
|
798 |
r_0 \arrow[r, "\backslash c_0"] & r_1 \arrow[r, "\backslash c_1"] & r_2 \arrow[r, dashed, "\ldots"] & r_n \arrow[d, "mkeps" description] \\
|
538
|
799 |
& & & v_n
|
|
800 |
\end{tikzcd}
|
|
801 |
\end{equation}
|
|
802 |
\end{ceqn}
|
567
|
803 |
\noindent
|
|
804 |
In the above diagram, again we assume that
|
|
805 |
the input string $s$ is made of $n$ characters
|
|
806 |
$c_0c_1 \ldots c_{n-1}$, and the input regular expression $r$
|
|
807 |
is given label $0$ and after each character $c_i$ is taken off
|
|
808 |
by the derivative operation the resulting derivative regular
|
|
809 |
expressioin is $r_{i+1}$.The last derivative operation
|
|
810 |
$\backslash c_{n-1}$ gives back $r_n$, which is transformed into
|
|
811 |
a value $v_n$ by $\mkeps$.
|
|
812 |
$v_n$ tells us how an empty string is matched by the (nullable)
|
|
813 |
regular expression $r_n$, in a $\POSIX$ way.
|
|
814 |
The definition of $\mkeps$ is
|
538
|
815 |
\begin{center}
|
|
816 |
\begin{tabular}{lcl}
|
564
|
817 |
$\mkeps \; \ONE$ & $\dn$ & $\Empty$ \\
|
567
|
818 |
$\mkeps \; (r_{1}+r_{2})$ & $\dn$
|
|
819 |
& $\textit{if}\; (\nullable \; r_{1}) \;\,
|
|
820 |
\textit{then}\;\, \Left \; (\mkeps \; r_{1})$\\
|
|
821 |
& & $\phantom{if}\; \textit{else}\;\, \Right \;(\mkeps \; r_{2})$\\
|
|
822 |
$\mkeps \; (r_1 \cdot r_2)$ & $\dn$ & $\Seq\;(\mkeps\;r_1)\;(\mkeps \; r_2)$\\
|
564
|
823 |
$\mkeps \; r^* $ & $\dn$ & $\Stars\;[]$
|
538
|
824 |
\end{tabular}
|
|
825 |
\end{center}
|
|
826 |
|
|
827 |
|
|
828 |
\noindent
|
567
|
829 |
We favour the left child $r_1$ of $r_1 + r_2$
|
|
830 |
to match an empty string if there is a choice.
|
538
|
831 |
When there is a star for us to match the empty string,
|
|
832 |
we give the $\Stars$ constructor an empty list, meaning
|
567
|
833 |
no iteration is taken.
|
538
|
834 |
The result of a call to $\mkeps$ on a $\nullable$ $r$ would
|
|
835 |
be a $\POSIX$ value corresponding to $r$:
|
567
|
836 |
\begin{lemma}\label{mePosix}
|
|
837 |
$\nullable\; r \implies (r, []) \rightarrow (\mkeps\; v)$
|
|
838 |
\end{lemma}
|
|
839 |
\begin{proof}
|
|
840 |
By induction on the shape of $r$.
|
|
841 |
\end{proof}
|
|
842 |
\noindent
|
|
843 |
After the $\mkeps$-call, we inject back the characters one by one
|
|
844 |
in reverse order as they were chopped off in the derivative phase.
|
|
845 |
The fucntion for this is called $\inj$. $\inj$ and $\backslash$
|
|
846 |
are not exactly reverse operations of one another, as $\inj$
|
|
847 |
operates on values instead of regular
|
|
848 |
expressions.
|
|
849 |
In the diagram below, $v_i$ stands for the (POSIX) value
|
|
850 |
for how the regular expression
|
|
851 |
$r_i$ matches the string $s_i$ consisting of the last $n-i$ characters
|
|
852 |
of $s$ (i.e. $s_i = c_i \ldots c_{n-1}$ ) from the previous lexical value $v_{i+1}$.
|
538
|
853 |
After injecting back $n$ characters, we get the lexical value for how $r_0$
|
|
854 |
matches $s$.
|
|
855 |
\begin{ceqn}
|
|
856 |
\begin{equation}\label{graph:inj}
|
|
857 |
\begin{tikzcd}
|
567
|
858 |
r_0 \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] \\
|
|
859 |
v_0 \arrow[u] & v_i \arrow[l, dashed] & v_{i+1} \arrow[l,"inj_{r_i} c_i"] & v_n \arrow[l, dashed]
|
538
|
860 |
\end{tikzcd}
|
|
861 |
\end{equation}
|
|
862 |
\end{ceqn}
|
|
863 |
\noindent
|
567
|
864 |
$\textit{inj}$ takes three arguments: a regular
|
|
865 |
expression ${r_{i}}$, before the character is chopped off,
|
|
866 |
a character ${c_{i}}$, the character we want to inject back and
|
|
867 |
the third argument $v_{i+1}$ the value we want to inject into.
|
568
|
868 |
The result of an application
|
|
869 |
$\inj \; r_i \; c_i \; v_{i+1}$ is a new value $v_i$ such that
|
|
870 |
\[
|
|
871 |
(s_i, r_i) \rightarrow v_i
|
|
872 |
\]
|
|
873 |
holds.
|
567
|
874 |
The definition of $\textit{inj}$ is as follows:
|
538
|
875 |
\begin{center}
|
568
|
876 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{5mm}}l}
|
|
877 |
$\textit{inj}\;(c)\;c\,Empty$ & $\dn$ & $\Char\,c$\\
|
|
878 |
$\textit{inj}\;(r_1 + r_2)\;c\; (\Left\; v)$ & $\dn$ & $\Left \; (\textit{inj}\; r_1 \; c\,v)$\\
|
|
879 |
$\textit{inj}\;(r_1 + r_2)\,c\; (\Right\;v)$ & $\dn$ & $\Right \; (\textit{inj}\;r_2\;c \; v)$\\
|
|
880 |
$\textit{inj}\;(r_1 \cdot r_2)\; c\;(\Seq \; v_1 \; v_2)$ & $\dn$ &
|
|
881 |
$\Seq \; (\textit{inj}\;r_1\;c\;v_1) \; v_2$\\
|
|
882 |
$\textit{inj}\;(r_1 \cdot r_2)\; c\;(\Left \; (\Seq \; v_1\;v_2) )$ &
|
|
883 |
$\dn$ & $\Seq \; (\textit{inj}\,r_1\,c\,v_1)\; v_2$\\
|
|
884 |
$\textit{inj}\;(r_1 \cdot r_2)\; c\; (\Right\; v)$ & $\dn$ & $\Seq\; (\textit{mkeps}\; r_1) \; (\textit{inj} \; r_2\;c\;v)$\\
|
|
885 |
$\textit{inj}\;(r^*)\; c \; (\Seq \; v\; (\Stars\;vs))$ & $\dn$ & $\Stars\;\,((\textit{inj}\;r\;c\;v)\,::\,vs)$\\
|
538
|
886 |
\end{tabular}
|
|
887 |
\end{center}
|
|
888 |
|
|
889 |
\noindent
|
568
|
890 |
The function does a recursion on
|
|
891 |
the shape of regular
|
|
892 |
expression $r_i$ and value $v_{i+1}$.
|
|
893 |
Intuitively, each clause analyses
|
|
894 |
how $r_i$ could have transformed when being
|
|
895 |
derived by $c$, identifying which subpart
|
|
896 |
of $v_{i+1}$ has the ``hole''
|
|
897 |
to inject the character back into.
|
|
898 |
Once the character is
|
|
899 |
injected back to that sub-value;
|
|
900 |
$\inj$ assembles all things together
|
|
901 |
to form a new value.
|
|
902 |
|
|
903 |
For instance, the last clause is an
|
|
904 |
injection into a sequence value $v_{i+1}$
|
|
905 |
whose second child
|
|
906 |
value is a star, and the shape of the
|
|
907 |
regular expression $r_i$ before injection
|
|
908 |
is a star.
|
|
909 |
We therefore know
|
|
910 |
the derivative
|
|
911 |
starts on a star and ends as a sequence:
|
|
912 |
\[
|
|
913 |
(r^*) \backslash c \longrightarrow r\backslash c \cdot r^*
|
|
914 |
\]
|
|
915 |
during which an iteration of the star
|
|
916 |
had just been unfolded, giving the below
|
|
917 |
value inhabitation relation:
|
|
918 |
\[
|
|
919 |
\vdash \Seq \; v \; (\Stars \; vs) : (r\backslash c) \cdot r^*.
|
|
920 |
\]
|
|
921 |
The value list $vs$ corresponds to
|
|
922 |
matched star iterations,
|
|
923 |
and the ``hole'' lies in $v$ because
|
|
924 |
\[
|
|
925 |
\vdash v: r\backslash c.
|
|
926 |
\]
|
|
927 |
Finally,
|
|
928 |
$\inj \; r \;c \; v$ is prepended
|
|
929 |
to the previous list of iterations, and then
|
|
930 |
wrapped under the $\Stars$
|
|
931 |
constructor, giving us $\Stars \; ((\inj \; r \; c \; v) ::vs)$.
|
538
|
932 |
|
568
|
933 |
Recall that lemma
|
|
934 |
\ref{mePosix} tells us
|
|
935 |
$\mkeps$ always selects the POSIX matching among
|
|
936 |
multiple values that flatten to the empty string.
|
|
937 |
Now $\inj$ preserves the POSIXness, provided
|
|
938 |
the value before injection is POSIX:
|
|
939 |
\begin{lemma}\label{injPosix}
|
|
940 |
If
|
|
941 |
\[
|
|
942 |
(r \backslash c, s) \rightarrow v
|
|
943 |
\]
|
|
944 |
then
|
|
945 |
\[
|
|
946 |
(r, c :: s) \rightarrow (\inj r \; c\; v).
|
|
947 |
\]
|
|
948 |
\end{lemma}
|
|
949 |
\begin{proof}
|
|
950 |
By induction on $r$.
|
|
951 |
The involved cases are sequence and star.
|
|
952 |
When $r = a \cdot b$, there could be
|
|
953 |
three cases for the value $v$ satisfying $\vdash v:a\backslash c$.
|
|
954 |
We give the reasoning why $\inj \; r \; c \; v$ is POSIX in each
|
|
955 |
case.
|
|
956 |
\begin{itemize}
|
|
957 |
\item
|
|
958 |
$v = \Seq \; v_a \; v_b$.\\
|
|
959 |
The ``not nullable'' clause of the $\inj$ function is taken:
|
|
960 |
\begin{center}
|
|
961 |
\begin{tabular}{lcl}
|
|
962 |
$\inj \; r \; c \; v$ & $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; (\Seq \; v_a \; v_b) $\\
|
|
963 |
& $=$ & $\Seq \; (\inj \;a \; c \; v_a) \; v_b$
|
|
964 |
\end{tabular}
|
|
965 |
\end{center}
|
|
966 |
We know that there exists a unique pair of
|
|
967 |
$s_a$ and $s_b$ satisfaying
|
|
968 |
$(a \backslash c, s_a) \rightarrow v_a$,
|
|
969 |
$(b , s_b) \rightarrow v_b$, and
|
|
970 |
$\nexists s_3 \; s_4. s_3 \neq [] \land s_a @ s_3 \in
|
|
971 |
L \; (a\backslash c) \land
|
|
972 |
s_4 \in L \; b$.
|
|
973 |
The last condition gives us
|
|
974 |
$\nexists s_3 \; s_4. s_3 \neq [] \land (c :: s_a )@ s_3 \in
|
|
975 |
L \; a \land
|
|
976 |
s_4 \in L \; b$.
|
|
977 |
By induction hypothesis, $(a, c::s_a) \rightarrow \inj \; a \; c \; v_a $ holds,
|
|
978 |
and this gives us
|
|
979 |
\[
|
|
980 |
(a\cdot b, (c::s_a)@s_b) \rightarrow \Seq \; (\inj \; a\;c \;v_a) \; v_b.
|
|
981 |
\]
|
|
982 |
\item
|
|
983 |
$v = \Left \; (\Seq \; v_a \; v_b)$\\
|
|
984 |
The argument is almost identical to the above case,
|
|
985 |
except that a different clause of $\inj$ is taken:
|
|
986 |
\begin{center}
|
|
987 |
\begin{tabular}{lcl}
|
|
988 |
$\inj \; r \; c \; v$ & $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; (\Left \; (\Seq \; v_a \; v_b)) $\\
|
|
989 |
& $=$ & $\Seq \; (\inj \;a \; c \; v_a) \; v_b$
|
|
990 |
\end{tabular}
|
|
991 |
\end{center}
|
|
992 |
With a similar reasoning,
|
538
|
993 |
|
568
|
994 |
\[
|
|
995 |
(a\cdot b, (c::s_a)@s_b) \rightarrow \Seq \; (\inj \; a\;c \;v_a) \; v_b.
|
|
996 |
\]
|
|
997 |
again holds.
|
|
998 |
\item
|
|
999 |
$v = \Right \; v_b$\\
|
|
1000 |
Again the injection result would be
|
|
1001 |
\begin{center}
|
|
1002 |
\begin{tabular}{lcl}
|
|
1003 |
$\inj \; r \; c \; v$ & $=$ & $ \inj \;\; (a \cdot b) \;\; c \;\; \Right \; (v_b) $\\
|
|
1004 |
& $=$ & $\Seq \; (\mkeps \; a) \; (\inj \;b \; c\; v_b)$
|
|
1005 |
\end{tabular}
|
|
1006 |
\end{center}
|
|
1007 |
We know that $a$ must be nullable,
|
|
1008 |
allowing us to call $\mkeps$ and get
|
|
1009 |
\[
|
|
1010 |
(a, []) \rightarrow \mkeps \; a.
|
|
1011 |
\]
|
|
1012 |
Also by inductive hypothesis
|
|
1013 |
\[
|
|
1014 |
(b, c::s) \rightarrow \inj\; b \; c \; v_b
|
|
1015 |
\]
|
|
1016 |
holds.
|
|
1017 |
In addition, as
|
|
1018 |
$\Right \;v_b$ instead of $\Left \ldots$ is
|
|
1019 |
the POSIX value for $v$, it must be the case
|
|
1020 |
that $s \notin L \;( (a\backslash c)\cdot b)$.
|
|
1021 |
This tells us that
|
|
1022 |
\[
|
|
1023 |
\nexists s_3 \; s_4.
|
|
1024 |
s_3 @s_4 = s \land s_3 \in L \; (a\backslash c)
|
|
1025 |
\land s_4 \in L \; b
|
|
1026 |
\]
|
|
1027 |
which translates to
|
|
1028 |
\[
|
|
1029 |
\nexists s_3 \; s_4. \; s_3 \neq [] \land
|
|
1030 |
s_3 @s_4 = c::s \land s_3 \in L \; a
|
|
1031 |
\land s_4 \in L \; b.
|
|
1032 |
\]
|
|
1033 |
(Which basically says there cannot be a longer
|
|
1034 |
initial split for $s$ other than the empty string.)
|
|
1035 |
Therefore we have $\Seq \; (\mkeps \; a) \;(\inj \;b \; c\; v_b)$
|
|
1036 |
as the POSIX value for $a\cdot b$.
|
|
1037 |
\end{itemize}
|
|
1038 |
The star case can be proven similarly.
|
|
1039 |
\end{proof}
|
|
1040 |
\noindent
|
|
1041 |
Putting all the functions $\inj$, $\mkeps$, $\backslash$ together
|
|
1042 |
by following the procedure outlined in the diagram \ref{graph:inj},
|
538
|
1043 |
and taking into consideration the possibility of a non-match,
|
568
|
1044 |
a lexer can be built with the following recursive definition:
|
538
|
1045 |
\begin{center}
|
539
|
1046 |
\begin{tabular}{lcl}
|
568
|
1047 |
$\lexer \; r \; [] $ & $=$ & $\textit{if} \; (\nullable \; r)\; \textit{then}\; \Some(\mkeps \; r) \; \textit{else} \; \None$\\
|
|
1048 |
$\lexer \; r \;c::s$ & $=$ & $\textit{case}\; (\lexer \; (r\backslash c) \; s) \;\textit{of}\; $\\
|
|
1049 |
& & $\quad \phantom{\mid}\; \None \implies \None$\\
|
|
1050 |
& & $\quad \mid \Some(v) \implies \Some(\inj \; r\; c\; v)$
|
538
|
1051 |
\end{tabular}
|
|
1052 |
\end{center}
|
568
|
1053 |
\noindent
|
|
1054 |
The central property of the $\lexer$ is that it gives the correct result by
|
|
1055 |
$\POSIX$ standards:
|
573
|
1056 |
\begin{theorem}\label{lexerCorrectness}
|
568
|
1057 |
The $\lexer$ based on derivatives and injections is correct:
|
|
1058 |
\begin{center}
|
|
1059 |
\begin{tabular}{lcl}
|
|
1060 |
$\lexer \; r \; s = \Some(v)$ & $ \Longleftrightarrow$ & $ (r, \; s) \rightarrow v$\\
|
|
1061 |
$\lexer \;r \; s = \None $ & $\Longleftrightarrow$ & $ \neg(\exists v. (r, s) \rightarrow v)$
|
|
1062 |
\end{tabular}
|
|
1063 |
\end{center}
|
|
1064 |
\end{theorem}
|
|
1065 |
\begin{proof}
|
|
1066 |
By induction on $s$. $r$ is allowed to be an arbitrary regular expression.
|
|
1067 |
The $[]$ case is proven by lemma \ref{mePosix}, and the inductive case
|
|
1068 |
by lemma \ref{injPosix}.
|
|
1069 |
\end{proof}
|
538
|
1070 |
\noindent
|
539
|
1071 |
As we did earlier in this chapter on the matcher, one can
|
|
1072 |
introduce simplification on the regex.
|
568
|
1073 |
However, now one needs to do a backward phase and make sure
|
539
|
1074 |
the values align with the regular expressions.
|
|
1075 |
Therefore one has to
|
538
|
1076 |
be careful not to break the correctness, as the injection
|
|
1077 |
function heavily relies on the structure of the regexes and values
|
|
1078 |
being correct and matching each other.
|
|
1079 |
It can be achieved by recording some extra rectification functions
|
|
1080 |
during the derivatives step, and applying these rectifications in
|
|
1081 |
each run during the injection phase.
|
568
|
1082 |
With extra care
|
|
1083 |
one can show that POSIXness will not be affected---although it is much harder
|
538
|
1084 |
to establish.
|
|
1085 |
Some initial results in this regard have been
|
|
1086 |
obtained in \cite{AusafDyckhoffUrban2016}.
|
|
1087 |
|
568
|
1088 |
However, with all the simplification rules allowed
|
|
1089 |
in an injection-based lexer, one could still end up in
|
|
1090 |
trouble, when cases that require more involved and aggressive
|
|
1091 |
simplifications arise.
|
|
1092 |
\section{A Case Requring More Aggressive Simplifications}
|
539
|
1093 |
For example, when starting with the regular
|
|
1094 |
expression $(a^* \cdot a^*)^*$ and building a few successive derivatives (around 10)
|
|
1095 |
w.r.t.~the character $a$, one obtains a derivative regular expression
|
|
1096 |
with more than 9000 nodes (when viewed as a tree)
|
|
1097 |
even with simplification.
|
|
1098 |
\begin{figure}
|
|
1099 |
\begin{tikzpicture}
|
|
1100 |
\begin{axis}[
|
|
1101 |
xlabel={$n$},
|
|
1102 |
ylabel={size},
|
|
1103 |
legend entries={Naive Matcher},
|
|
1104 |
legend pos=north west,
|
|
1105 |
legend cell align=left]
|
|
1106 |
\addplot[red,mark=*, mark options={fill=white}] table {BetterWaterloo.data};
|
|
1107 |
\end{axis}
|
|
1108 |
\end{tikzpicture}
|
|
1109 |
\caption{Size of $(a^*\cdot a^*)^*$ against $\protect\underbrace{aa\ldots a}_\text{n \textit{a}s}$}
|
|
1110 |
\end{figure}\label{fig:BetterWaterloo}
|
|
1111 |
|
568
|
1112 |
That is because Sulzmann and Lu's
|
|
1113 |
injection-based lexing algorithm keeps a lot of
|
541
|
1114 |
"useless" values that will not be used.
|
539
|
1115 |
These different ways of matching will grow exponentially with the string length.
|
568
|
1116 |
Take
|
|
1117 |
\[
|
|
1118 |
r= (a^*\cdot a^*)^* \quad and \quad
|
|
1119 |
s=\underbrace{aa\ldots a}_\text{n \textit{a}s}
|
|
1120 |
\]
|
|
1121 |
as an example.
|
|
1122 |
This is a highly ambiguous regular expression, with
|
|
1123 |
many ways to split up the string into multiple segments for
|
|
1124 |
different star iteratioins,
|
573
|
1125 |
and for each segment
|
|
1126 |
multiple ways of splitting between
|
568
|
1127 |
the two $a^*$ sub-expressions.
|
573
|
1128 |
When $n$ is equal to $1$, there are two lexical values for
|
|
1129 |
the match:
|
|
1130 |
\[
|
|
1131 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [])] \quad (value 1)
|
|
1132 |
\]
|
|
1133 |
and
|
|
1134 |
\[
|
|
1135 |
\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])] \quad (value 2)
|
|
1136 |
\]
|
|
1137 |
The derivative of $\derssimp \;s \; r$ is
|
|
1138 |
\[
|
|
1139 |
(a^*a^* + a^*)\cdot(a^*a^*)^*.
|
|
1140 |
\]
|
|
1141 |
The $a^*a^*$ and $a^*$ in the first child of the above sequence
|
|
1142 |
correspond to value 1 and value 2, respectively.
|
|
1143 |
When $n=2$, the number goes up to 7:
|
|
1144 |
\[
|
|
1145 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a, \Char \; a])\; (\Stars \; [])]
|
|
1146 |
\]
|
|
1147 |
,
|
|
1148 |
\[
|
|
1149 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [\Char \; a])]
|
|
1150 |
\]
|
|
1151 |
,
|
|
1152 |
\[
|
|
1153 |
\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a, \Char \; a])]
|
|
1154 |
\]
|
|
1155 |
,
|
|
1156 |
\[
|
|
1157 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; []), \Seq \; (\Stars \; [\Char\;a])\; (\Stars\; []) ]
|
|
1158 |
\]
|
|
1159 |
,
|
|
1160 |
\[
|
|
1161 |
\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; []),
|
|
1162 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])
|
|
1163 |
]
|
|
1164 |
\]
|
|
1165 |
,
|
|
1166 |
\[
|
|
1167 |
\Stars \; [
|
|
1168 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a]),
|
|
1169 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a])
|
|
1170 |
]
|
|
1171 |
\]
|
|
1172 |
and
|
|
1173 |
\[
|
|
1174 |
\Stars \; [
|
|
1175 |
\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a]),
|
|
1176 |
\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [])
|
|
1177 |
]
|
|
1178 |
\]
|
|
1179 |
And $\derssimp \; aa \; (a^*a^*)^*$ would be
|
|
1180 |
\[
|
|
1181 |
((a^*a^* + a^*)+a^*)\cdot(a^*a^*)^* +
|
|
1182 |
(a^*a^* + a^*)\cdot(a^*a^*)^*.
|
|
1183 |
\]
|
|
1184 |
which removes two out of the seven terms corresponding to the
|
|
1185 |
seven distinct lexical values.
|
|
1186 |
|
|
1187 |
It is not surprising that there are exponentially many
|
|
1188 |
distinct lexical values that cannot be eliminated by
|
|
1189 |
the simple-minded simplification of $\derssimp$.
|
|
1190 |
|
568
|
1191 |
A lexer without a good enough strategy to
|
|
1192 |
deduplicate will naturally
|
|
1193 |
have an exponential runtime on ambiguous regular expressions.
|
538
|
1194 |
|
573
|
1195 |
On the other hand, the
|
|
1196 |
$\POSIX$ value for $r= (a^*\cdot a^*)^*$ and
|
|
1197 |
$s=\underbrace{aa\ldots a}_\text{n \textit{a}s}$ is
|
|
1198 |
\[
|
|
1199 |
\Stars\,
|
|
1200 |
[\Seq \; (\Stars\,[\underbrace{\Char(a),\ldots,\Char(a)}_\text{n iterations}]), \Stars\,[]]
|
|
1201 |
\]
|
|
1202 |
and at any moment the subterms in a regular expression
|
|
1203 |
that will result in a POSIX value is only
|
|
1204 |
a minority among the many other terms,
|
|
1205 |
and one can remove ones that are absolutely not possible to
|
|
1206 |
be POSIX.
|
|
1207 |
In the above example,
|
|
1208 |
\[
|
|
1209 |
((a^*a^* + \underbrace{a^*}_\text{A})+\underbrace{a^*}_\text{duplicate of A})\cdot(a^*a^*)^* +
|
|
1210 |
\underbrace{(a^*a^* + a^*)\cdot(a^*a^*)^*}_\text{further simp removes this}.
|
|
1211 |
\]
|
|
1212 |
can be further simplified by
|
|
1213 |
removing the underlined term first,
|
|
1214 |
which would open up possibilities
|
|
1215 |
of further simplification that removes the
|
|
1216 |
underbraced part.
|
|
1217 |
The result would be
|
|
1218 |
\[
|
|
1219 |
(\underbrace{a^*a^*}_\text{term 1} + \underbrace{a^*}_\text{term 2})\cdot(a^*a^*)^*.
|
|
1220 |
\]
|
|
1221 |
with corresponding values
|
|
1222 |
\begin{center}
|
|
1223 |
\begin{tabular}{lr}
|
|
1224 |
$\Stars \; [\Seq \; (\Stars \; [\Char \; a, \Char \; a])\; (\Stars \; [])]$ & $(\text{term 1})$\\
|
|
1225 |
$\Stars \; [\Seq \; (\Stars \; [\Char \; a])\; (\Stars \; [\Char \; a])] $ & $(\text{term 2})$
|
|
1226 |
\end{tabular}
|
|
1227 |
\end{center}
|
|
1228 |
Other terms with an underlying value such as
|
|
1229 |
\[
|
|
1230 |
\Stars \; [\Seq \; (\Stars \; [])\; (\Stars \; [\Char \; a, \Char \; a])]
|
|
1231 |
\]
|
|
1232 |
is simply too hopeless to contribute a POSIX lexical value,
|
|
1233 |
and is therefore thrown away.
|
538
|
1234 |
|
573
|
1235 |
Ausaf and Dyckhoff and Urban \cite{AusafDyckhoffUrban2016}
|
|
1236 |
have come up with some simplification steps, however those steps
|
|
1237 |
are not yet sufficiently strong so that they achieve the above effects.
|
|
1238 |
And even with these relatively mild simplifications the proof
|
|
1239 |
is already quite a bit complicated than the theorem \ref{lexerCorrectness}.
|
|
1240 |
One would prove something like:
|
|
1241 |
\[
|
|
1242 |
\textit{If}\; (\textit{snd} \; (\textit{simp} \; r\backslash c), s) \rightarrow v \;\;
|
|
1243 |
\textit{then}\;\; (r, c::s) \rightarrow
|
|
1244 |
\inj\;\, r\, \;c \;\, ((\textit{fst} \; (\textit{simp} \; r \backslash c))\; v)
|
|
1245 |
\]
|
|
1246 |
instead of the simple lemma \ref{injPosix}, where now $\textit{simp}$
|
|
1247 |
not only has to return a simplified regular expression,
|
|
1248 |
but also what specific simplifications
|
|
1249 |
has been done as a function on values
|
|
1250 |
showing how one can transform the value
|
|
1251 |
underlying the simplified regular expression
|
|
1252 |
to the unsimplified one.
|
|
1253 |
|
|
1254 |
We therefore choose a slightly different approach to
|
|
1255 |
get better simplifications, which uses
|
|
1256 |
some augmented data structures compared to
|
|
1257 |
plain regular expressions.
|
|
1258 |
We call them \emph{annotated}
|
|
1259 |
regular expressions.
|
|
1260 |
With annotated regular expressions,
|
|
1261 |
we can avoid creating the intermediate values $v_1,\ldots v_n$ and a
|
|
1262 |
second phase altogether.
|
|
1263 |
In the meantime, we can also ensure that simplifications
|
|
1264 |
are easily handled without breaking the correctness of the algorithm.
|
|
1265 |
We introduce this new datatype and the
|
|
1266 |
corresponding algorithm in the next chapter.
|
538
|
1267 |
|
|
1268 |
|
|
1269 |
|
|
1270 |
|