<s>
Flex	B-General_Concept
(	O
fast	B-General_Concept
lexical	I-General_Concept
analyzer	I-General_Concept
generator	O
)	O
is	O
a	O
free	B-License
and	I-License
open-source	I-License
software	I-License
alternative	O
to	O
lex	B-General_Concept
.	O
</s>
<s>
It	O
is	O
a	O
computer	B-Application
program	I-Application
that	O
generates	O
lexical	B-Application
analyzers	I-Application
(	O
also	O
known	O
as	O
"	O
scanners	B-Application
"	O
or	O
"	O
lexers	B-Application
"	O
)	O
.	O
</s>
<s>
It	O
is	O
frequently	O
used	O
as	O
the	O
lex	B-General_Concept
implementation	O
together	O
with	O
Berkeley	B-Language
Yacc	I-Language
parser	B-Language
generator	I-Language
on	O
BSD-derived	O
operating	B-General_Concept
systems	I-General_Concept
(	O
as	O
both	O
lex	B-General_Concept
and	O
yacc	B-Application
are	O
part	O
of	O
POSIX	O
)	O
,	O
or	O
together	O
with	O
GNU	B-Application
bison	I-Application
(	O
a	O
version	O
of	O
yacc	B-Application
)	O
in	O
*	B-Application
BSD	I-Application
ports	I-Application
and	O
in	O
Linux	O
distributions	O
.	O
</s>
<s>
Unlike	O
Bison	B-Application
,	O
flex	B-General_Concept
is	O
not	O
part	O
of	O
the	B-Application
GNU	I-Application
Project	I-Application
and	O
is	O
not	O
released	O
under	O
the	O
GNU	B-License
General	I-License
Public	I-License
License	I-License
,	O
although	O
a	O
manual	O
for	O
Flex	B-General_Concept
was	O
produced	O
and	O
published	O
by	O
the	O
Free	O
Software	O
Foundation	O
.	O
</s>
<s>
Flex	B-General_Concept
was	O
written	O
in	O
C	B-Language
around	O
1987	O
by	O
Vern	O
Paxson	O
,	O
with	O
the	O
help	O
of	O
many	O
ideas	O
and	O
much	O
inspiration	O
from	O
Van	O
Jacobson	O
.	O
</s>
<s>
This	O
is	O
an	O
example	O
of	O
a	O
Flex	B-General_Concept
scanner	B-Application
for	O
the	O
instructional	O
programming	O
language	O
PL/0	B-Language
.	O
</s>
<s>
These	O
programs	O
perform	O
character	O
parsing	O
and	O
tokenizing	O
via	O
the	O
use	O
of	O
a	O
deterministic	B-General_Concept
finite	I-General_Concept
automaton	I-General_Concept
(	O
DFA	B-General_Concept
)	O
.	O
</s>
<s>
A	O
DFA	B-General_Concept
is	O
a	O
theoretical	O
machine	O
accepting	O
regular	B-General_Concept
languages	I-General_Concept
.	O
</s>
<s>
These	O
machines	O
are	O
a	O
subset	O
of	O
the	O
collection	O
of	O
Turing	B-Architecture
machines	I-Architecture
.	O
</s>
<s>
DFAs	B-General_Concept
are	O
equivalent	O
to	O
read-only	O
right	O
moving	O
Turing	B-Architecture
machines	I-Architecture
.	O
</s>
<s>
The	O
syntax	O
is	O
based	O
on	O
the	O
use	O
of	O
regular	B-Language
expressions	I-Language
.	O
</s>
<s>
See	O
also	O
nondeterministic	B-General_Concept
finite	I-General_Concept
automaton	I-General_Concept
.	O
</s>
<s>
A	O
Flex	B-General_Concept
lexical	I-General_Concept
analyzer	I-General_Concept
usually	O
has	O
time	O
complexity	O
in	O
the	O
length	O
of	O
the	O
input	O
.	O
</s>
<s>
This	O
constant	O
is	O
quite	O
low	O
:	O
GCC	B-Application
generates	O
12	O
instructions	O
for	O
the	O
DFA	B-General_Concept
match	O
loop	O
.	O
</s>
<s>
Note	O
that	O
the	O
constant	O
is	O
independent	O
of	O
the	O
length	O
of	O
the	O
token	O
,	O
the	O
length	O
of	O
the	O
regular	B-Language
expression	I-Language
and	O
the	O
size	O
of	O
the	O
DFA	B-General_Concept
.	O
</s>
<s>
However	O
,	O
using	O
the	O
REJECT	O
macro	O
in	O
a	O
scanner	B-Application
with	O
the	O
potential	O
to	O
match	O
extremely	O
long	O
tokens	O
can	O
cause	O
Flex	B-General_Concept
to	O
generate	O
a	O
scanner	B-Application
with	O
non-linear	O
performance	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
programmer	O
has	O
explicitly	O
told	O
Flex	B-General_Concept
to	O
"	O
go	O
back	O
and	O
try	O
again	O
"	O
after	O
it	O
has	O
already	O
matched	O
some	O
input	O
.	O
</s>
<s>
This	O
will	O
cause	O
the	O
DFA	B-General_Concept
to	O
backtrack	O
to	O
find	O
other	O
accept	O
states	O
.	O
</s>
<s>
The	O
REJECT	O
feature	O
is	O
not	O
enabled	O
by	O
default	O
,	O
and	O
because	O
of	O
its	O
performance	O
implications	O
its	O
use	O
is	O
discouraged	O
in	O
the	O
Flex	B-General_Concept
manual	O
.	O
</s>
<s>
By	O
default	O
the	O
scanner	B-Application
generated	O
by	O
Flex	B-General_Concept
is	O
not	O
reentrant	B-Operating_System
.	O
</s>
<s>
This	O
can	O
cause	O
serious	O
problems	O
for	O
programs	O
that	O
use	O
the	O
generated	O
scanner	B-Application
from	O
different	O
threads	O
.	O
</s>
<s>
To	O
overcome	O
this	O
issue	O
there	O
are	O
options	O
that	O
Flex	B-General_Concept
provides	O
in	O
order	O
to	O
achieve	O
reentrancy	B-Operating_System
.	O
</s>
<s>
A	O
detailed	O
description	O
of	O
these	O
options	O
can	O
be	O
found	O
in	O
the	O
Flex	B-General_Concept
manual	O
.	O
</s>
<s>
Normally	O
the	O
generated	O
scanner	B-Application
contains	O
references	O
to	O
the	O
unistd.h	B-Language
header	O
file	O
,	O
which	O
is	O
Unix	B-Operating_System
specific	O
.	O
</s>
<s>
To	O
avoid	O
generating	O
code	O
that	O
includes	O
unistd.h	B-Language
,	O
%option	O
nounistd	O
should	O
be	O
used	O
.	O
</s>
<s>
Another	O
issue	O
is	O
the	O
call	O
to	O
isatty	B-Error_Name
(	O
a	O
Unix	B-Operating_System
library	B-Library
function	I-Library
)	O
,	O
which	O
can	O
be	O
found	O
in	O
the	O
generated	O
code	O
.	O
</s>
<s>
The	O
%option	O
never-interactive	O
forces	O
flex	B-General_Concept
to	O
generate	O
code	O
that	O
does	O
not	O
use	O
isatty	B-Error_Name
.	O
</s>
<s>
Flex	B-General_Concept
can	O
only	O
generate	O
code	O
for	O
C	B-Language
and	O
C++	B-Language
.	O
</s>
<s>
To	O
use	O
the	O
scanner	B-Application
code	O
generated	O
by	O
flex	B-General_Concept
from	O
other	O
languages	O
a	O
language	B-Application
binding	I-Application
tool	O
such	O
as	O
SWIG	B-Application
can	O
be	O
used	O
.	O
</s>
<s>
Flex	B-General_Concept
is	O
limited	O
to	O
matching	O
1-byte	O
(	O
8-bit	O
)	O
binary	O
values	O
and	O
therefore	O
does	O
not	O
support	O
Unicode	O
.	O
</s>
<s>
RE/flex	B-General_Concept
and	O
other	O
alternatives	O
do	O
support	O
Unicode	O
matching	O
.	O
</s>
<s>
flex++	B-General_Concept
is	O
a	O
similar	O
lexical	O
scanner	B-Application
for	O
C++	B-Language
which	O
is	O
included	O
as	O
part	O
of	O
the	O
flex	B-General_Concept
package	O
.	O
</s>
<s>
The	O
generated	O
code	O
does	O
not	O
depend	O
on	O
any	O
runtime	B-Library
or	O
external	O
library	B-Library
except	O
for	O
a	O
memory	O
allocator	O
(	O
malloc	B-Language
or	O
a	O
user-supplied	O
alternative	O
)	O
unless	O
the	O
input	O
also	O
depends	O
on	O
it	O
.	O
</s>
<s>
This	O
can	O
be	O
useful	O
in	O
embedded	B-Architecture
and	O
similar	O
situations	O
where	O
traditional	O
operating	B-General_Concept
system	I-General_Concept
or	O
C	B-Language
runtime	I-Language
facilities	O
may	O
not	O
be	O
available	O
.	O
</s>
<s>
The	O
flex++	B-General_Concept
generated	O
C++	B-Language
scanner	B-Application
includes	O
the	O
header	O
file	O
FlexLexer.h	O
,	O
which	O
defines	O
the	O
interfaces	O
of	O
the	O
two	O
C++	B-Language
generated	O
classes	O
.	O
</s>
