<s>
In	O
the	O
automata	B-Application
theory	I-Application
,	O
a	O
tagged	B-General_Concept
deterministic	I-General_Concept
finite	I-General_Concept
automaton	I-General_Concept
(	O
TDFA	O
)	O
is	O
an	O
extension	O
of	O
deterministic	B-General_Concept
finite	I-General_Concept
automaton	I-General_Concept
(	O
DFA	B-General_Concept
)	O
.	O
</s>
<s>
In	O
addition	O
to	O
solving	O
the	O
recognition	O
problem	O
for	O
regular	B-General_Concept
languages	I-General_Concept
,	O
TDFA	O
is	O
also	O
capable	O
of	O
submatch	O
extraction	O
and	O
parsing	B-Language
.	O
</s>
<s>
While	O
canonical	O
DFA	B-General_Concept
can	O
find	O
out	O
if	O
a	O
string	O
belongs	O
to	O
the	O
language	O
defined	O
by	O
a	O
regular	B-Language
expression	I-Language
,	O
TDFA	O
can	O
also	O
extract	O
substrings	O
that	O
match	O
specific	O
subexpressions	O
.	O
</s>
<s>
More	O
generally	O
,	O
TDFA	O
can	O
identify	O
positions	O
in	O
the	O
input	O
string	O
that	O
match	O
tagged	O
positions	O
in	O
a	O
regular	B-Language
expression	I-Language
(	O
tags	O
are	O
meta-symbols	O
similar	O
to	O
capturing	O
parentheses	O
,	O
but	O
without	O
the	O
pairing	O
requirement	O
)	O
.	O
</s>
<s>
Laurikari	O
described	O
TDFA	O
construction	O
and	O
gave	O
a	O
proof	O
that	O
the	O
determinization	B-General_Concept
process	O
terminates	O
,	O
</s>
<s>
In	O
2007	O
Chris	O
Kuklewicz	O
implemented	O
TDFA	O
in	O
a	O
Haskell	B-Language
library	O
Regex-TDFA	O
with	O
POSIX	O
longest-match	O
semantics	O
.	O
</s>
<s>
Trafimovich	O
called	O
TDFA	O
variants	O
with	O
and	O
without	O
lookahead	O
TDFA(1 )	O
and	O
TDFA(0 )	O
by	O
analogy	O
with	O
LR	B-Application
parsers	I-Application
LR(1 )	O
and	O
LR(0 )	O
.	O
</s>
<s>
The	O
algorithm	O
was	O
implemented	O
in	O
the	O
open-source	O
lexer	B-Application
generator	I-Application
RE2C	B-Application
.	O
</s>
<s>
In	O
2020	O
Trafimovich	O
published	O
an	O
article	O
about	O
TDFA	O
implementation	O
in	O
RE2C	B-Application
.	O
</s>
<s>
The	O
paper	O
incorporated	O
their	O
past	O
research	O
and	O
presented	O
multi-pass	O
TDFA	O
that	O
are	O
better	O
suited	O
to	O
just-in-time	O
determinization	B-General_Concept
.	O
</s>
<s>
TDFA	O
have	O
the	O
same	O
basic	O
structure	O
as	O
ordinary	O
DFA	B-General_Concept
:	O
a	O
finite	O
set	O
of	O
states	B-Application
linked	O
by	O
transitions	O
.	O
</s>
<s>
The	O
values	O
may	O
be	O
scalar	O
offsets	O
,	O
or	O
offset	O
lists	O
for	O
tags	O
that	O
match	O
repeatedly	O
(	O
the	O
latter	O
can	O
be	O
represented	O
efficiently	O
using	O
a	O
trie	B-General_Concept
structure	O
)	O
.	O
</s>
<s>
There	O
is	O
no	O
one-to-one	O
mapping	O
between	O
tags	O
in	O
a	O
regular	B-Language
expression	I-Language
and	O
registers	O
in	O
a	O
TDFA	O
:	O
a	O
single	O
tag	O
may	O
need	O
many	O
registers	O
,	O
and	O
the	O
same	O
register	O
may	O
hold	O
values	O
of	O
different	O
tags	O
.	O
</s>
<s>
TDFA	O
has	O
four	O
states	B-Application
three	O
of	O
which	O
are	O
final	O
.	O
</s>
<s>
and	O
final	O
states	B-Application
have	O
operations	O
defined	O
by	O
the	O
function	O
(	O
marked	O
with	O
wide-tipped	O
arrow	O
)	O
.	O
</s>
<s>
Canonical	O
DFA	B-General_Concept
solve	O
the	O
recognition	O
problem	O
in	O
linear	O
time	O
.	O
</s>
<s>
The	O
same	O
holds	O
for	O
TDFA	O
,	O
since	O
the	O
number	O
of	O
registers	O
and	O
register	O
operations	O
is	O
fixed	O
and	O
depends	O
only	O
on	O
the	O
regular	B-Language
expression	I-Language
,	O
but	O
not	O
on	O
the	O
length	O
of	O
input	O
.	O
</s>
<s>
On	O
one	O
extreme	O
,	O
if	O
there	O
are	O
no	O
tags	O
,	O
a	O
TDFA	O
is	O
identical	O
to	O
a	O
canonical	O
DFA	B-General_Concept
.	O
</s>
<s>
On	O
the	O
other	O
extreme	O
,	O
if	O
every	O
subexpression	O
is	O
tagged	O
,	O
a	O
TDFA	O
effectively	O
performs	O
full	O
parsing	B-Language
and	O
has	O
many	O
operations	O
on	O
every	O
transition	O
.	O
</s>
<s>
In	O
practice	O
for	O
real-world	O
regular	B-Language
expressions	I-Language
with	O
a	O
few	O
submatch	O
groups	O
the	O
overhead	O
is	O
negligible	O
compared	O
to	O
matching	O
with	O
canonical	O
DFA	B-General_Concept
.	O
</s>
<s>
First	O
,	O
a	O
regular	B-Language
expression	I-Language
is	O
converted	O
to	O
a	O
tagged	O
nondeterministic	B-General_Concept
finite	I-General_Concept
automaton	I-General_Concept
(	O
TNFA	O
)	O
.	O
</s>
<s>
Second	O
,	O
a	O
TNFA	O
is	O
converted	O
to	O
a	O
TDFA	O
using	O
a	O
determinization	B-General_Concept
procedure	O
;	O
</s>
<s>
including	O
minimization	O
that	O
reduces	O
the	O
number	O
of	O
states	B-Application
.	O
</s>
<s>
This	O
section	O
explains	O
TDFA	O
construction	O
on	O
the	O
example	O
of	O
a	O
regular	B-Language
expression	I-Language
,	O
</s>
<s>
where	O
is	O
a	O
tag	O
and	O
are	O
alphabet	B-Application
symbols	O
.	O
</s>
<s>
TNFA	O
is	O
a	O
nondeterministic	B-General_Concept
finite	I-General_Concept
automaton	I-General_Concept
with	O
tagged	O
ε-transitions	O
.	O
</s>
<s>
although	O
similar	O
constructions	O
were	O
known	O
much	O
earlier	O
as	O
Mealy	B-General_Concept
machines	I-General_Concept
and	O
nondeterministic	O
finite-state	B-Architecture
transducers	I-Architecture
.	O
</s>
<s>
TNFA	O
construction	O
is	O
very	O
similar	O
to	O
Thompson	B-General_Concept
's	I-General_Concept
construction	I-General_Concept
:	O
</s>
<s>
it	O
mirrors	O
the	O
structure	O
of	O
a	O
regular	B-Language
expression	I-Language
.	O
</s>
<s>
Importantly	O
,	O
TNFA	O
preserves	O
ambiguity	O
in	O
a	O
regular	B-Language
expression	I-Language
:	O
</s>
<s>
then	O
TNFA	O
for	O
this	O
regular	B-Language
expression	I-Language
has	O
two	O
different	O
accepting	O
paths	O
for	O
this	O
string	O
.	O
</s>
<s>
Figure	O
1	O
shows	O
TNFA	O
for	O
the	O
example	O
regular	B-Language
expression	I-Language
.	O
</s>
<s>
transitions	O
on	O
alphabet	B-Application
symbols	O
(	O
dark	O
blue	O
)	O
,	O
</s>
<s>
In	O
order	O
to	O
understand	O
TNFA	O
determinization	B-General_Concept
,	O
it	O
helps	O
to	O
understand	O
TNFA	O
simulation	O
first	O
.	O
</s>
<s>
Recall	O
the	O
canonical	O
ε-NFA	O
simulation	O
:	O
it	O
constructs	O
a	O
subset	O
of	O
active	O
states	B-Application
as	O
an	O
ε-closure	O
of	O
the	O
start	O
state	O
,	O
</s>
<s>
unlike	O
canonical	O
NFA	B-General_Concept
simulation	O
,	O
TNFA	O
simulation	O
needs	O
to	O
distinguish	O
ambiguous	O
paths	O
,	O
</s>
<s>
Ambiguity	O
means	O
the	O
existence	O
of	O
multiple	O
different	O
parse	B-Language
trees	O
for	O
the	O
same	O
input	O
.	O
</s>
<s>
It	O
is	O
a	O
property	O
of	O
a	O
regular	B-Language
expression	I-Language
;	O
</s>
<s>
and	O
gets	O
resolved	O
during	O
TNFA	O
simulation	O
or	O
determinization	B-General_Concept
.	O
</s>
<s>
The	O
leftmost-greedy	O
policy	O
is	O
defined	O
in	O
terms	O
of	O
regular	B-Language
expression	I-Language
structure	O
;	O
</s>
<s>
The	O
POSIX	O
policy	O
,	O
on	O
the	O
other	O
hand	O
,	O
is	O
defined	O
in	O
terms	O
of	O
the	O
structure	O
of	O
parse	B-Language
results	O
;	O
</s>
<s>
on	O
disambiguation	O
,	O
since	O
it	O
happens	O
during	O
determinization	B-General_Concept
and	O
gets	O
built	O
into	O
TDFA	O
structure	O
.	O
</s>
<s>
it	O
allows	O
one	O
to	O
parse	B-Language
in	O
two	O
different	O
ways	O
:	O
</s>
<s>
TNFA	O
determinization	B-General_Concept
is	O
based	O
on	O
the	O
canonical	O
powerset	B-General_Concept
construction	I-General_Concept
algorithm	O
that	O
converts	O
an	O
NFA	B-General_Concept
to	O
a	O
DFA	B-General_Concept
.	O
</s>
<s>
The	O
algorithm	O
simulates	O
NFA	B-General_Concept
on	O
all	O
possible	O
strings	O
.	O
</s>
<s>
At	O
each	O
step	O
of	O
the	O
simulation	O
,	O
the	O
active	O
set	O
of	O
NFA	B-General_Concept
states	B-Application
forms	O
a	O
new	O
DFA	B-General_Concept
state	O
.	O
</s>
<s>
If	O
the	O
new	O
state	O
is	O
identical	O
to	O
an	O
existing	O
DFA	B-General_Concept
state	O
,	O
it	O
is	O
discarded	O
and	O
replaced	O
with	O
the	O
existing	O
one	O
,	O
and	O
the	O
current	O
branch	O
of	O
simulation	O
terminates	O
.	O
</s>
<s>
Otherwise	O
the	O
new	O
state	O
is	O
added	O
to	O
the	O
growing	O
set	O
of	O
DFA	B-General_Concept
states	B-Application
and	O
simulation	O
from	O
this	O
state	O
continues	O
.	O
</s>
<s>
Eventually	O
determinization	B-General_Concept
terminates	O
:	O
although	O
the	O
set	O
of	O
all	O
possible	O
strings	O
is	O
infinite	O
,	O
the	O
set	O
of	O
different	O
DFA	B-General_Concept
states	B-Application
is	O
finite	O
,	O
and	O
at	O
some	O
point	O
all	O
new	O
states	B-Application
become	O
identical	O
to	O
existing	O
ones	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
TDFA	O
naive	O
powerset	B-General_Concept
construction	I-General_Concept
faces	O
a	O
problem	O
:	O
</s>
<s>
TDFA	O
states	B-Application
contain	O
tag	O
information	O
,	O
which	O
changes	O
at	O
every	O
step	O
of	O
the	O
simulation	O
(	O
as	O
the	O
offsets	O
increase	O
)	O
.	O
</s>
<s>
This	O
prevents	O
TDFA	O
states	B-Application
from	O
mapping	O
:	O
a	O
pair	O
of	O
states	B-Application
that	O
contain	O
identical	O
TNFA	O
states	B-Application
but	O
different	O
tag	O
offsets	O
are	O
not	O
identical	O
and	O
cannot	O
be	O
mapped	O
.	O
</s>
<s>
As	O
a	O
result	O
,	O
simulation	O
continues	O
indefinitely	O
,	O
the	O
set	O
of	O
TDFA	O
states	B-Application
grows	O
,	O
and	O
determinization	B-General_Concept
does	O
not	O
terminate	O
.	O
</s>
<s>
instead	O
of	O
storing	O
immediate	O
tag	O
values	O
in	O
TDFA	O
states	B-Application
,	O
he	O
suggested	O
storing	O
them	O
indirectly	O
in	O
registers	O
.	O
</s>
<s>
and	O
take	O
them	O
into	O
account	O
when	O
mapping	O
TDFA	O
states	B-Application
.	O
</s>
<s>
Figure	O
3	O
shows	O
the	O
determinization	B-General_Concept
process	O
for	O
the	O
running	O
example	O
.	O
</s>
<s>
Fixed-tags	O
optimization	O
is	O
applied	O
at	O
the	O
regular	B-Language
expression	I-Language
level	O
(	O
before	O
TNFA	O
construction	O
)	O
.	O
</s>
<s>
TDFA	O
minimization	O
is	O
very	O
similar	O
to	O
DFA	B-General_Concept
minimization	I-General_Concept
,	O
except	O
for	O
one	O
additional	O
restriction	O
:	O
</s>
<s>
So	O
,	O
TDFA	O
states	B-Application
that	O
are	O
identical	O
,	O
but	O
have	O
different	O
register	O
actions	O
on	O
incoming	O
transitions	O
on	O
the	O
same	O
symbol	O
,	O
cannot	O
be	O
merged	O
.	O
</s>
<s>
All	O
the	O
usual	O
algorithms	O
for	O
DFA	B-General_Concept
minimization	I-General_Concept
can	O
be	O
adapted	O
to	O
TDFA	O
minimization	O
,	O
</s>
<s>
for	O
example	O
Moore	O
's	O
algorithm	O
is	O
used	O
in	O
the	O
RE2C	B-Application
lexer	B-Application
generator	I-Application
.	O
</s>
<s>
Figure	O
5	O
shows	O
an	O
optimized	O
TDFA	O
for	O
regular	B-Language
expression	I-Language
.	O
</s>
<s>
Note	O
that	O
it	O
is	O
in	O
fact	O
the	O
same	O
as	O
a	O
TDFA	O
for	O
a	O
semantically	O
equivalent	O
regular	B-Language
expression	I-Language
,	O
</s>
<s>
Ambiguity	O
is	O
resolved	O
during	O
determinization	B-General_Concept
,	O
and	O
the	O
unoptimized	O
TDFA	O
on	O
Figure	O
4	O
is	O
unambiguous	O
,	O
</s>
<s>
TDFA	O
with	O
registers	O
are	O
well	O
suited	O
for	O
ahead-of-time	B-Application
determinization	B-General_Concept
,	O
when	O
the	O
time	O
spent	O
on	O
TDFA	O
construction	O
is	O
not	O
included	O
in	O
the	O
runtime	O
(	O
e.g.	O
</s>
<s>
in	O
lexer	B-Application
generators	I-Application
)	O
.	O
</s>
<s>
But	O
for	O
just-in-time	O
determinization	B-General_Concept
(	O
e.g.	O
</s>
<s>
in	O
regular	B-Language
expression	I-Language
libraries	O
)	O
it	O
is	O
desirable	O
to	O
reduce	O
TDFA	O
construction	O
time	O
.	O
</s>
<s>
Another	O
concern	O
is	O
tag	O
density	O
in	O
a	O
regular	B-Language
expression	I-Language
:	O
</s>
<s>
But	O
for	O
heavily	O
tagged	O
regular	B-Language
expressions	I-Language
TDFA	O
with	O
registers	O
are	O
suboptimal	O
:	O
transitions	O
get	O
cluttered	O
with	O
operations	O
,	O
making	O
TDFA	O
execution	O
slow	O
.	O
</s>
<s>
states	B-Application
and	O
collect	O
submatch	O
information	O
.	O
</s>
<s>
In	O
order	O
to	O
trace	O
back	O
the	O
matching	O
TNFA	O
path	O
from	O
a	O
sequence	O
of	O
TDFA	O
states	B-Application
,	O
</s>
<s>
multi-pass	O
TDFA	O
use	O
backlinks	O
on	O
transitions	O
and	O
in	O
final	O
states	B-Application
.	O
</s>
<s>
and	O
the	O
second	O
component	O
is	O
a	O
sequence	O
of	O
tags	O
for	O
a	O
fragment	O
of	O
TNFA	O
path	O
between	O
TDFA	O
states	B-Application
.	O
</s>
<s>
(	O
compare	O
it	O
to	O
Figure	O
0	O
that	O
shows	O
TDFA	O
with	O
registers	O
for	O
the	O
same	O
regular	B-Language
expression	I-Language
)	O
:	O
</s>
<s>
The	O
forward	O
pass	O
collects	O
a	O
sequence	O
of	O
states	B-Application
0	O
,	O
1	O
,	O
1	O
,	O
2	O
.	O
</s>
<s>
are	O
very	O
similar	O
to	O
TDFA	O
,	O
except	O
that	O
they	O
have	O
register	O
operations	O
in	O
states	B-Application
,	O
not	O
on	O
transitions	O
.	O
</s>
<s>
are	O
more	O
distant	O
relatives	O
to	O
TDFA	O
,	O
better	O
suited	O
to	O
full	O
parsing	B-Language
than	O
submatch	O
extraction	O
.	O
</s>
<s>
DSST	O
states	B-Application
contain	O
path	O
trees	O
constructed	O
by	O
the	O
ε-closure	O
,	O
</s>
<s>
while	O
TDFA	O
states	B-Application
contain	O
similar	O
information	O
in	O
a	O
decomposed	O
form	O
(	O
register	O
versions	O
and	O
lookahead	O
tags	O
)	O
.	O
</s>
