Ulya Trofimovich [Wed, 27 Feb 2019 10:50:07 +0000 (10:50 +0000)]
libre2c: use a more efficient algorithm for computing POSIX precedence matrix.
The old alrorithm was straightforward: it computed precedence for each
pair of core closure items. This has worst-case cubic complexity in the
size of TNFA, because closure can have O(N) states and tag history length
can be also O(N). One example is ((a?){1,1000})*.
The new algorithm avoids that by computing parecedence for all pairs of
core items in one traversal of the shortest path tree constructed by
closure. It avoids traversing deadend branches (those that were cancelled
by search because a better path was found) by marking used nodes of the
tree from each leaf down to the root. The new algorithm has worst-case
quadratic behaviour. It can be slightly slower for simple cases when the
number of core items is low and histories are short.
Ulya Trofimovich [Wed, 20 Feb 2019 17:00:47 +0000 (17:00 +0000)]
libre2c: found pathological case for constant-memory POSIX algorithms.
The regexp is ((a?){1,200})*, and the input string is just "a".
Takes quibic time in the size of counter. This is caused by quadratic-
time computation of precedence matrix on each step (the number of TNFA
states in the closure approaches TNFA size), multiplied by the length
of compared histores (which also approaches TNFA size). Trie-based
algorithms are not affected, but they consume memory proportional to
the length of the input string, and so are also not practical.
Ulya Trofimovich [Tue, 19 Feb 2019 21:51:43 +0000 (21:51 +0000)]
No need to fully unwind tag histories when calculating POSIX precedence.
Histories are stored in a trie. Each history is represented by an index
to its tail in the trie. In order to compare two histories, we can simply
unwind both indices, step by step, until they coincide or root index is
reached.
Ulya Trofimovich [Mon, 18 Feb 2019 16:42:07 +0000 (16:42 +0000)]
libre2c: compare histories in a trie without reconstructing them.
Previously we used to unwing each history, store it as a linear sequence
in a separate buffer, and then iterate buffers from the beginning of
histories to fing the fork point.
This reconstruction step can be avoided, because we can find fork by
tracing both histories together from tail to root, element by element,
until their indices coincide (this is the fork point. Someone called
thos "two-fingers algorithm".
Ulya Trofimovich [Mon, 18 Feb 2019 10:52:26 +0000 (10:52 +0000)]
Correctly set values on main diagonal of POSIX precedence matrix (configuration compared to itself).
It doesn't show any difference on the tests, probably because previously
these values were set to zero, which unpacks to symmetrical (though
incorrect) values, and thus doesn't affect comparison.
Ulya Trofimovich [Thu, 14 Feb 2019 12:52:50 +0000 (12:52 +0000)]
Assign indices to core TNFA states (those that are final or have transitions on symbols).
During epsilon closure constructions, many operations only need to be
performed for core states. This allows to save some memory, e.g.
precedence matrix can be indexed by core states.
libre2c: speedup of POSIX NFA matcher by lazy computation and caching of precedence.
A lot of the match time was taken by eagerly computing precedence values
on each step for each pair of closure states. Most of these values are not
needed: RE may be unambigous; or the given input string may be unambigous,
even if there is ambiguity, it may take only a few comparisons to resolve.
All the rest is wasted effort.
We can avoid it by delaying precedence computation until necessary, and
then unwinding all the steps backwards, computing precedence for each step
and caching the computed values (so that the same pair of histories is not
compared twice). It is still the same incremental comparison as with
precedence matrices: we compare step by step, going from the fork frame to
the join frame. The result of comparison on each step is folded to a triple
of numbers and recorded in cache. It is important that we do record each
step, not just the first step, because the next pair of ambiguous histories
may unwind to the same pair of prefixes that was compared before.
For all this to work, it is necessary that we can store all history until
the very end, because at any step we might need to unwind an arbitrary
number of steps back. We also need to address individual subhistories
efficiently in order to use them as keys in the cache. All this is achieved
by storing history in the form of a trie and addressing individual
histories by indices in the trie. We also use trie to compute the resulting
tag values (instead of storing tags in registers at each step).
Differentiate between "POSIX syntax" and "POSIX semantics" options.
This is needed because we may want to use POSIX syntax with leftmost
greedy disambiguation. In that case, we still need parentheses-as-tags
attitude, but we don't want all the overhead and heuristics to spped
up POSIX closure.
libre2c_posix: added test for leftmost TNFA matcher.
Ulya Trofimovich [Sun, 27 Jan 2019 10:23:23 +0000 (10:23 +0000)]
libre2c_posix: extended regex_t structure to hold more submatch data.
Added fields:
- re_nsub: total number of submatch groups, required by POSIX standard
- pmatch: buffer for submatch results, usually supplied by the user to
regexec(), but we allow to do the allocation and storage in regex_t.
This is convenient for users that have hard time managing memory, e.g.
java bindings to libre2c_posix.
- regs: buffer for internal use by regexec(), strored in regex_t to
avoid repeated memory allocation on each call to regexec() with the
same regex.
Ulya Trofimovich [Sun, 27 Jan 2019 10:06:22 +0000 (10:06 +0000)]
Adjusted build system to correctly build DLLs for windows.
Adjustments:
- configure.ac: pass win32-dll option to LT_INIT
- Makefile_libre2c_posix.am: use -no-undefined in LDFLAGS
- use slibtool: https://github.com/midipix-project/slibtool for windows
builds The problem with libtool is that it doesn't allow to link libstdc++
and libgcc statically, which is necessary to build portable DLLs with
Mingw. Libtool adds -nostdlib option to LDFLAGS and links some predefined
objects that pull in dependency on dynamic libstdc++ and libgcc, even in
the presence of -static-libstdc++ -static-libgcc.
Ulya Trofimovich [Thu, 17 Jan 2019 22:52:04 +0000 (22:52 +0000)]
Fixed operator precedence with --flex-syntax option.
Operator precedence was broken because re2c tried to parse whole strings
of characters at once instead of parsing one character at a time (in
much the same way as it would parse properly quotes string literals in
the original re2c format). This caused ab* being parsed as (ab)*, which
is clearly wrong (should be a(b)*).
This fixes bug #242:
"Operator precedence with --flex-syntax is broken."
Ulya Trofimovich [Sat, 12 Jan 2019 20:32:15 +0000 (20:32 +0000)]
libre2c_posix: use C-array initializers instead of variadic functions for offset lists.
Variadic functions cause subtle toolchain-specific errors because integer
literals used to initialize offsets may have different type than the type
expected by variadic function (e.g. int vs long).