532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
\chapter{A Better Bound and Other Extensions} % Main chapter title
|
|
4 |
|
|
5 |
\label{Cubic} %In Chapter 5\ref{Chapter5} we discuss stronger simplifications to improve the finite bound
|
|
6 |
%in Chapter 4 to a polynomial one, and demonstrate how one can extend the
|
|
7 |
%algorithm to include constructs such as bounded repetitions and negations.
|
|
8 |
|
|
9 |
%----------------------------------------------------------------------------------------
|
|
10 |
% SECTION strongsimp
|
|
11 |
%----------------------------------------------------------------------------------------
|
|
12 |
\section{A Stronger Version of Simplification}
|
|
13 |
%TODO: search for isabelle proofs of algorithms that check equivalence
|
533
|
14 |
In our bit-coded lexing algorithm, with or without simplification,
|
|
15 |
two alternative (distinct) sub-matches for the (sub-)string and (sub-)regex pair
|
532
|
16 |
are always expressed in the "derivative regular expression" as two
|
|
17 |
disjoint alternative terms at the current (sub-)regex level. The fact that they
|
|
18 |
are parallel tells us when we retrieve the information from this (sub-)regex
|
|
19 |
there will always be a choice of which alternative term to take.
|
533
|
20 |
As an example, the regular expression $aa \cdot a^*+ a \cdot a^*$ (omitting bit-codes)
|
|
21 |
expresses two possibilities it will match future input.
|
|
22 |
It will either match 2 $a$'s, then 0 or more $a$'s, in other words, at least 2 more $a$'s
|
|
23 |
\begin{figure}\label{string_3parts1}
|
|
24 |
\begin{center}
|
|
25 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
26 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=3]
|
|
27 |
{Consumed Input
|
|
28 |
\nodepart{two} Expects $aa$
|
|
29 |
\nodepart{three} Then expects $a^*$};
|
|
30 |
|
|
31 |
\end{tikzpicture}
|
|
32 |
\end{center}
|
|
33 |
\end{figure}
|
|
34 |
\noindent
|
|
35 |
Or it will match at least 1 more $a$:
|
|
36 |
\begin{figure}\label{string_3parts2}
|
|
37 |
\begin{center}
|
|
38 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
39 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=3]
|
|
40 |
{Consumed
|
|
41 |
\nodepart{two} Expects $a$
|
|
42 |
\nodepart{three} Then expects $a^*$};
|
|
43 |
|
|
44 |
\end{tikzpicture}
|
|
45 |
\end{center}
|
|
46 |
\end{figure}
|
|
47 |
If these two possibilities are identical, we can eliminate
|
|
48 |
the second one as we know the second one corresponds to
|
|
49 |
a match that is not $\POSIX$.
|
|
50 |
|
|
51 |
|
|
52 |
If two identical regexes
|
|
53 |
happen to be grouped into different sequences, one cannot use
|
|
54 |
the $a + a \rightsquigarrow a$ rule to eliminate them, even if they
|
|
55 |
are "parallel" to each other:
|
|
56 |
\[
|
|
57 |
(a+b) \cdot r_1 + (a+c) \cdot r_2 \rightsquigarrow (a+b) \cdot r_1 + (c) \cdot r_2
|
|
58 |
\]
|
|
59 |
and that's because they are followed by possibly
|
|
60 |
different "suffixes" $r_1$ and $r_2$, and if
|
|
61 |
they do turn out to be different then doing
|
|
62 |
\[
|
|
63 |
(a+b) \cdot r_1 + (a+c) \cdot r_2 \rightsquigarrow (a+b) \cdot r_1 + (c) \cdot r_2
|
|
64 |
\]
|
|
65 |
might cause a possible match where the string is in $L(a \cdot r_2)$ to be lost.
|
532
|
66 |
|
|
67 |
Here is an example for this.
|
|
68 |
Assume we have the derivative regex
|
533
|
69 |
\[( r_1 + r_2 + r_3)\cdot r+
|
|
70 |
( r_1 + r_5 + r_6)\cdot( r_1 + r_2 + r_3)^*
|
532
|
71 |
\]
|
|
72 |
|
533
|
73 |
occurring in an intermediate step in our bit-coded lexing algorithm.
|
|
74 |
|
532
|
75 |
In this expression, there will be 6 "parallel" terms if we break up regexes
|
|
76 |
of shape $(a+b)\cdot c$ using the distributivity law (into $ac + bc$).
|
|
77 |
\begin{align}
|
|
78 |
(\nonumber \\
|
533
|
79 |
r_1 + & \label{term:1} \\
|
|
80 |
r_2 + & \label{term:2} \\
|
|
81 |
r_3 & \label{term:3} \\
|
|
82 |
& )\cdot r\; + \; (\nonumber \\
|
|
83 |
r_1 + & \label{term:4} \\
|
|
84 |
r_5 + & \label{term:5} \\
|
|
85 |
r_6 \label{term:6}\\
|
|
86 |
& )\cdot r\nonumber
|
532
|
87 |
\end{align}
|
|
88 |
|
|
89 |
They have been labelled, and each label denotes
|
|
90 |
one term, for example, \ref{term:1} denotes
|
|
91 |
\[
|
533
|
92 |
r_1 \cdot r
|
532
|
93 |
\]
|
|
94 |
\noindent
|
|
95 |
and \ref{term:3} denotes
|
|
96 |
\[
|
533
|
97 |
r_3\cdot r.
|
532
|
98 |
\]
|
533
|
99 |
From a lexer's point of view, \ref{term:4} will never
|
|
100 |
be picked up in later phases of matching because there
|
|
101 |
is a term \ref{term:1} giving identical matching information.
|
|
102 |
The first term \ref{term:1} will match a string in $L(r_1 \cdot r)$,
|
|
103 |
and so on for term \ref{term:2}, \ref{term:3}, etc.
|
|
104 |
|
532
|
105 |
\mybox{previous input $\ldots$}\mybox{$aaa$ }\mybox{rest of input $\ldots$}
|
533
|
106 |
\begin{center}\label{string_2parts}
|
|
107 |
|
|
108 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
109 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2]
|
|
110 |
{$r_{x1}$
|
|
111 |
\nodepart{two} $r_1\cdot r$ };
|
|
112 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
113 |
\end{tikzpicture}
|
|
114 |
|
|
115 |
\end{center}
|
|
116 |
For term 1 \ref{term:1}, whatever was before the current
|
|
117 |
input position was fully matched and the regex corresponding
|
|
118 |
to it reduced to $\ONE$,
|
|
119 |
and in the next input token, it will start on $r_1\cdot r$.
|
|
120 |
The resulting value will be something of a similar configuration:
|
|
121 |
\begin{center}\label{value_2parts}
|
|
122 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
123 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
124 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2]
|
|
125 |
{$v_{x1}$
|
|
126 |
\nodepart{two} $v_{r_1\cdot r}$ };
|
|
127 |
\end{tikzpicture}
|
|
128 |
\end{center}
|
|
129 |
For term 2 \ref{term:2} we have a similar value partition:
|
|
130 |
\begin{center}\label{value_2parts2}
|
|
131 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
132 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
133 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2]
|
|
134 |
{$v_{x2}$
|
|
135 |
\nodepart{two} $v_{r_2\cdot r}$ };
|
|
136 |
\end{tikzpicture}
|
|
137 |
\end{center}
|
|
138 |
\noindent
|
|
139 |
and so on.
|
|
140 |
We note that for term 4 \ref{term:4} its result value
|
|
141 |
after any position beyond the current one will always
|
|
142 |
be the same:
|
|
143 |
\begin{center}\label{value_2parts4}
|
|
144 |
%\caption{term 1 \ref{term:1}'s matching configuration}
|
|
145 |
\begin{tikzpicture}[every node/.append style={draw, rounded corners, inner sep=10pt}]
|
|
146 |
\node [rectangle split, rectangle split horizontal, rectangle split parts=2]
|
|
147 |
{$v_{x4}$
|
|
148 |
\nodepart{two} $v_{r_1\cdot r}$ };
|
|
149 |
\end{tikzpicture}
|
|
150 |
\end{center}
|
|
151 |
And $\POSIX$ rules says that we can eliminate at least one of them.
|
|
152 |
Our algorithm always puts the regex with the longest initial sub-match at the left of the
|
|
153 |
alternatives, so we safely throw away \ref{term:4}.
|
|
154 |
The fact that term 1 and 4 are not immediately in the same alternative
|
|
155 |
expression does not prevent them from being two duplicate matches at
|
|
156 |
the current point of view.
|
|
157 |
To implement this idea into an algorithm, we define a "pruning function"
|
|
158 |
$\textit{prune}$, that takes away parts of all terms in $r$ that belongs to
|
|
159 |
$\textit{acc}$, which acts as an element
|
|
160 |
is a stronger version of $\textit{distinctBy}$.
|
|
161 |
Here is a concise version of it (in the style of Scala):
|
|
162 |
\begin{verbatim}
|
|
163 |
def distinctByPlus(rs: List[ARexp], acc: Set[Rexp] = Set()) :
|
|
164 |
List[ARexp] = rs match {
|
|
165 |
case Nil => Nil
|
|
166 |
case r :: rs =>
|
|
167 |
if(acc.contains(erase(r)))
|
|
168 |
distinctByPlus(rs, acc)
|
|
169 |
else
|
|
170 |
prune(r, acc) :: distinctByPlus(rs, prune(r, acc) +: acc)
|
|
171 |
}
|
|
172 |
|
|
173 |
\end{verbatim}
|
|
174 |
But for the function $\textit{prune}$ there is a difficulty:
|
|
175 |
how could one define the $L$ function in a "computable" way,
|
|
176 |
so that they generate a (lazy infinite) set of strings in $L(r)$.
|
|
177 |
We also need a function that tests whether $L(r_1) \subseteq L(r_2)$
|
|
178 |
is true.
|
|
179 |
For the moment we cut corners and do not define these two functions
|
|
180 |
as they are not used by Antimirov (and will probably not contribute
|
|
181 |
to a bound better than Antimirov's cubic bound).
|
|
182 |
Rather, we do not try to eliminate in every instance of regular expressions
|
|
183 |
that have "language duplicates", but only eliminate the "exact duplicates".
|
|
184 |
For this we need to break up terms $(a+b)\cdot c$ into two
|
|
185 |
terms $a\cdot c$ and $b\cdot c$ before we add them to the accumulator:
|
|
186 |
\begin{verbatim}
|
|
187 |
def distinctWith(rs: List[ARexp],
|
|
188 |
pruneFunction: (ARexp, Set[Rexp]) => ARexp,
|
|
189 |
acc: Set[Rexp] = Set()) : List[ARexp] =
|
|
190 |
rs match{
|
|
191 |
case Nil => Nil
|
|
192 |
case r :: rs =>
|
|
193 |
if(acc(erase(r)))
|
|
194 |
distinctWith(rs, pruneFunction, acc)
|
|
195 |
else {
|
|
196 |
val pruned_r = pruneFunction(r, acc)
|
|
197 |
pruned_r ::
|
|
198 |
distinctWith(rs,
|
|
199 |
pruneFunction,
|
|
200 |
turnIntoTerms(erase(pruned_r)) ++: acc
|
|
201 |
)
|
|
202 |
}
|
|
203 |
}
|
|
204 |
\end{verbatim}
|
|
205 |
\noindent
|
|
206 |
This process is done by the function $\textit{turnIntoTerms}$:
|
|
207 |
\begin{verbatim}
|
|
208 |
def turnIntoTerms(r: Rexp): List[Rexp] = r match {
|
|
209 |
case SEQ(r1, r2) => if(isOne(r1))
|
|
210 |
turnIntoTerms(r2)
|
|
211 |
else
|
|
212 |
turnIntoTerms(r1).map(r11 => SEQ(r11, r2))
|
|
213 |
case ALTS(r1, r2) => turnIntoTerms(r1) ::: turnIntoTerms(r2)
|
|
214 |
case ZERO => Nil
|
|
215 |
case _ => r :: Nil
|
|
216 |
}
|
|
217 |
\end{verbatim}
|
|
218 |
\noindent
|
|
219 |
The pruning function can be defined recursively:
|
|
220 |
\begin{verbatim}
|
|
221 |
def prune7(r: ARexp, acc: Set[Rexp]) : ARexp = r match{
|
|
222 |
case AALTS(bs, rs) => rs.map(r => prune7(r, acc)).filter(_ != ZERO) match
|
|
223 |
{
|
|
224 |
case Nil => AZERO
|
|
225 |
case r::Nil => fuse(bs, r)
|
|
226 |
case rs1 => AALTS(bs, rs1)
|
|
227 |
}
|
|
228 |
case ASEQ(bs, r1, r2) => prune7(r1, acc.map(r => removeSeqTail(r, erase(r2)))) match {
|
|
229 |
case AZERO => AZERO
|
|
230 |
case r1p if(isOne(erase(r1p))) => fuse(bs ++ mkepsBC(r1p), r2)
|
|
231 |
case r1p => ASEQ(bs, r1p, r2)
|
|
232 |
}
|
|
233 |
case r => if(acc(erase(r))) AZERO else r
|
|
234 |
}
|
|
235 |
\end{verbatim}
|
|
236 |
|
|
237 |
\begin{figure}
|
|
238 |
\centering
|
|
239 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
240 |
\begin{tikzpicture}
|
|
241 |
\begin{axis}[
|
|
242 |
xlabel={$n$},
|
|
243 |
x label style={at={(1.05,-0.05)}},
|
|
244 |
ylabel={size},
|
|
245 |
enlargelimits=false,
|
|
246 |
xtick={0,5,...,30},
|
|
247 |
xmax=30,
|
|
248 |
ymax=800,
|
|
249 |
ytick={0,200,...,800},
|
|
250 |
scaled ticks=false,
|
|
251 |
axis lines=left,
|
|
252 |
width=5cm,
|
|
253 |
height=4cm,
|
|
254 |
legend entries={$bsimpStrong$ size growth},
|
|
255 |
legend pos=north west,
|
|
256 |
legend cell align=left]
|
|
257 |
\addplot[red,mark=*, mark options={fill=white}] table {strongSimpCurve.data};
|
|
258 |
\end{axis}
|
|
259 |
\end{tikzpicture}
|
|
260 |
&
|
|
261 |
\begin{tikzpicture}
|
|
262 |
\begin{axis}[
|
|
263 |
xlabel={$n$},
|
|
264 |
x label style={at={(1.05,-0.05)}},
|
|
265 |
ylabel={size},
|
|
266 |
enlargelimits=false,
|
|
267 |
xtick={0,5,...,30},
|
|
268 |
xmax=30,
|
|
269 |
ymax=42000,
|
|
270 |
ytick={0,10000,...,40000},
|
|
271 |
scaled ticks=true,
|
|
272 |
axis lines=left,
|
|
273 |
width=5cm,
|
|
274 |
height=4cm,
|
|
275 |
legend entries={$bsimp$ size growth},
|
|
276 |
legend pos=north west,
|
|
277 |
legend cell align=left]
|
|
278 |
\addplot[blue,mark=*, mark options={fill=white}] table {bsimpExponential.data};
|
|
279 |
\end{axis}
|
|
280 |
\end{tikzpicture}\\
|
|
281 |
\multicolumn{2}{c}{Graphs: Runtime for matching $((a^* + (aa)^* + \ldots + (aaaaa)^* )^*)^*$ with strings
|
|
282 |
of the form $\underbrace{aa..a}_{n}$.}
|
|
283 |
\end{tabular}
|
|
284 |
\caption{aaaaaStarStar} \label{fig:aaaaaStarStar}
|
|
285 |
\end{figure}
|
532
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
%----------------------------------------------------------------------------------------
|
|
290 |
% SECTION 1
|
|
291 |
%----------------------------------------------------------------------------------------
|
|
292 |
|
|
293 |
\section{Adding Support for the Negation Construct, and its Correctness Proof}
|
|
294 |
We now add support for the negation regular expression:
|
|
295 |
\[ r ::= \ZERO \mid \ONE
|
|
296 |
\mid c
|
|
297 |
\mid r_1 \cdot r_2
|
|
298 |
\mid r_1 + r_2
|
|
299 |
\mid r^*
|
|
300 |
\mid \sim r
|
|
301 |
\]
|
|
302 |
The $\textit{nullable}$ function's clause for it would be
|
|
303 |
\[
|
|
304 |
\textit{nullable}(~r) = \neg \nullable(r)
|
|
305 |
\]
|
|
306 |
The derivative would be
|
|
307 |
\[
|
|
308 |
~r \backslash c = ~ (r \backslash c)
|
|
309 |
\]
|
|
310 |
|
|
311 |
The most tricky part of lexing for the $~r$ regex
|
|
312 |
is creating a value for it.
|
|
313 |
For other regular expressions, the value aligns with the
|
|
314 |
structure of the regex:
|
|
315 |
\[
|
|
316 |
\vdash \Seq(\Char(a), \Char(b)) : a \cdot b
|
|
317 |
\]
|
|
318 |
But for the $~r$ regex, $s$ is a member of it if and only if
|
|
319 |
$s$ does not belong to $L(r)$.
|
|
320 |
That means when there
|
|
321 |
is a match for the not regex, it is not possible to generate how the string $s$ matched
|
|
322 |
with $r$.
|
|
323 |
What we can do is preserve the information of how $s$ was not matched by $r$,
|
|
324 |
and there are a number of options to do this.
|
|
325 |
|
|
326 |
We could give a partial value when there is a partial match for the regex inside
|
|
327 |
the $\mathbf{not}$ construct.
|
|
328 |
For example, the string $ab$ is not in the language of $(a\cdot b) \cdot c$,
|
|
329 |
A value for it could be
|
|
330 |
\[
|
|
331 |
\vdash \textit{Not}(\Seq(\Char(a), \Char(b))) : ~((a \cdot b ) \cdot c)
|
|
332 |
\]
|
|
333 |
The above example demonstrates what value to construct
|
|
334 |
when the string $s$ is at most a real prefix
|
|
335 |
of the strings in $L(r)$. When $s$ instead is not a prefix of any strings
|
|
336 |
in $L(r)$, it becomes unclear what to return as a value inside the $\textit{Not}$
|
|
337 |
constructor.
|
|
338 |
|
|
339 |
Another option would be to either store the string $s$ that resulted in
|
|
340 |
a mis-match for $r$ or a dummy value as a placeholder:
|
|
341 |
\[
|
533
|
342 |
\vdash \textit{Not}(abcd) : ~( r_1 )
|
532
|
343 |
\]
|
|
344 |
or
|
|
345 |
\[
|
533
|
346 |
\vdash \textit{Not}(\textit{Dummy}) : ~( r_1 )
|
532
|
347 |
\]
|
|
348 |
We choose to implement this as it is most straightforward:
|
|
349 |
\[
|
|
350 |
\mkeps(~(r)) = \textit{if}(\nullable(r)) \; \textit{Error} \; \textit{else} \; \textit{Not}(\textit{Dummy})
|
|
351 |
\]
|
|
352 |
|
|
353 |
%----------------------------------------------------------------------------------------
|
|
354 |
% SECTION 2
|
|
355 |
%----------------------------------------------------------------------------------------
|
|
356 |
|
|
357 |
\section{Bounded Repetitions}
|
|
358 |
|
|
359 |
|