<s>
Lex	B-General_Concept
is	O
a	O
computer	B-Application
program	I-Application
that	O
generates	O
lexical	B-Application
analyzers	I-Application
(	O
"	O
scanners	B-Application
"	O
or	O
"	O
lexers	B-Application
"	O
)	O
.	O
</s>
<s>
Lex	B-General_Concept
is	O
commonly	O
used	O
with	O
the	O
yacc	B-Application
parser	B-Language
generator	I-Language
.	O
</s>
<s>
Lex	B-General_Concept
,	O
originally	O
written	O
by	O
Mike	O
Lesk	O
and	O
Eric	O
Schmidt	O
and	O
described	O
in	O
1975	O
,	O
is	O
the	O
standard	O
lexical	B-Application
analyzer	I-Application
generator	O
on	O
many	O
Unix	B-Application
systems	I-Application
,	O
and	O
an	O
equivalent	O
tool	O
is	O
specified	O
as	O
part	O
of	O
the	O
POSIX	O
standard	O
.	O
</s>
<s>
Lex	B-General_Concept
reads	O
an	O
input	B-Architecture
stream	I-Architecture
specifying	O
the	O
lexical	B-Application
analyzer	I-Application
and	O
writes	O
source	O
code	O
which	O
implements	O
the	O
lexical	B-Application
analyzer	I-Application
in	O
the	O
C	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
In	O
addition	O
to	O
C	B-Language
,	O
some	O
old	O
versions	O
of	O
Lex	B-General_Concept
could	O
generate	O
a	O
lexer	B-Application
in	O
Ratfor	B-Language
.	O
</s>
<s>
Although	O
originally	O
distributed	O
as	O
proprietary	O
software	O
,	O
some	O
versions	O
of	O
Lex	B-General_Concept
are	O
now	O
open-source	B-Application
.	O
</s>
<s>
Open-source	B-Application
versions	O
of	O
Lex	B-General_Concept
,	O
based	O
on	O
the	O
original	O
proprietary	O
code	O
,	O
are	O
now	O
distributed	O
with	O
open-source	B-Application
operating	O
systems	O
such	O
as	O
OpenSolaris	B-Operating_System
and	O
Plan	B-Operating_System
9	I-Operating_System
from	I-Operating_System
Bell	I-Operating_System
Labs	I-Operating_System
.	O
</s>
<s>
One	O
popular	O
open-source	B-Application
version	O
of	O
Lex	B-General_Concept
,	O
called	O
flex	B-General_Concept
,	O
or	O
the	O
"	O
fast	B-General_Concept
lexical	I-General_Concept
analyzer	I-General_Concept
"	O
,	O
is	O
not	O
derived	O
from	O
proprietary	O
coding	O
.	O
</s>
<s>
The	O
structure	O
of	O
a	O
Lex	B-General_Concept
file	O
is	O
intentionally	O
similar	O
to	O
that	O
of	O
a	O
yacc	B-Application
file	O
:	O
files	O
are	O
divided	O
into	O
three	O
sections	O
,	O
separated	O
by	O
lines	O
that	O
contain	O
only	O
two	O
percent	O
signs	O
,	O
as	O
follows	O
:	O
</s>
<s>
The	O
definitions	O
section	O
defines	O
macros	O
and	O
imports	O
header	B-Language
files	I-Language
written	O
in	O
C	B-Language
.	O
It	O
is	O
also	O
possible	O
to	O
write	O
any	O
C	B-Language
code	O
here	O
,	O
which	O
will	O
be	O
copied	O
verbatim	O
into	O
the	O
generated	O
source	O
file	O
.	O
</s>
<s>
The	O
rules	O
section	O
associates	O
regular	B-Language
expression	I-Language
patterns	O
with	O
C	B-Language
statements	O
.	O
</s>
<s>
When	O
the	O
lexer	B-Application
sees	O
text	O
in	O
the	O
input	O
matching	O
a	O
given	O
pattern	O
,	O
it	O
will	O
execute	O
the	O
associated	O
C	B-Language
code	O
.	O
</s>
<s>
The	O
C	B-Language
code	O
section	O
contains	O
C	B-Language
statements	O
and	O
functions	O
that	O
are	O
copied	O
verbatim	O
to	O
the	O
generated	O
source	O
file	O
.	O
</s>
<s>
In	O
large	O
programs	O
it	O
is	O
more	O
convenient	O
to	O
place	O
this	O
code	O
in	O
a	O
separate	O
file	O
linked	O
in	O
at	O
compile	B-Language
time	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
example	O
Lex	B-General_Concept
file	O
for	O
the	O
flex	B-General_Concept
version	O
of	O
Lex	B-General_Concept
.	O
</s>
<s>
If	O
this	O
input	O
is	O
given	O
to	O
flex	B-General_Concept
,	O
it	O
will	O
be	O
converted	O
into	O
a	O
C	B-Language
file	O
,	O
.	O
</s>
<s>
This	O
can	O
be	O
compiled	B-Language
into	O
an	O
executable	O
which	O
matches	O
and	O
outputs	O
strings	O
of	O
integers	O
.	O
</s>
<s>
Lex	B-General_Concept
and	O
parser	B-Language
generators	I-Language
,	O
such	O
as	O
Yacc	B-Application
or	O
Bison	B-Application
,	O
are	O
commonly	O
used	O
together	O
.	O
</s>
<s>
Parser	B-Language
generators	I-Language
use	O
a	O
formal	O
grammar	O
to	O
parse	O
an	O
input	B-Architecture
stream	I-Architecture
,	O
something	O
which	O
Lex	B-General_Concept
cannot	O
do	O
using	O
simple	O
regular	B-Language
expressions	I-Language
,	O
as	O
Lex	B-General_Concept
is	O
limited	O
to	O
simple	O
finite	B-Architecture
state	I-Architecture
automata	I-Architecture
.	O
</s>
<s>
It	O
is	O
typically	O
preferable	O
to	O
have	O
a	O
parser	O
,	O
one	O
generated	O
by	O
Yacc	B-Application
for	O
instance	O
,	O
accept	O
a	O
stream	B-Architecture
of	O
tokens	O
(	O
a	O
"	O
token-stream	O
"	O
)	O
as	O
input	O
,	O
rather	O
than	O
having	O
to	O
process	O
a	O
stream	B-Architecture
of	O
characters	O
(	O
a	O
"	O
character-stream	O
"	O
)	O
directly	O
.	O
</s>
<s>
Lex	B-General_Concept
is	O
often	O
used	O
to	O
produce	O
such	O
a	O
token-stream	O
.	O
</s>
<s>
Scannerless	B-Application
parsing	I-Application
refers	O
to	O
parsing	O
the	O
input	O
character-stream	O
directly	O
,	O
without	O
a	O
distinct	O
lexer	B-Application
.	O
</s>
<s>
make	B-Application
is	O
a	O
utility	O
that	O
can	O
be	O
used	O
to	O
maintain	O
programs	O
involving	O
Lex	B-General_Concept
.	O
</s>
<s>
Make	B-Application
assumes	O
that	O
a	O
file	O
that	O
has	O
an	O
extension	O
of	O
.l	B-General_Concept
is	O
a	O
Lex	B-General_Concept
source	O
file	O
.	O
</s>
<s>
The	O
make	B-Application
internal	O
macro	O
LFLAGS	O
can	O
be	O
used	O
to	O
specify	O
Lex	B-General_Concept
options	O
to	O
be	O
invoked	O
automatically	O
by	O
make	B-Application
.	O
</s>
