lex
-
" "
/P 2.1
lex
1988
lex, -
. -
lex .
1.
lex - . -
-
. , -
()
. , (
) .
. -
.
, -
,
..
,
- .
.
-
- , -
(-
, ..).
. , -
- :
, , , ..
-
, . ,
- , -
()
. - ,
,
, .
,
-
, . - -
-
. -
,
.
, -
, .
, :
make
makefile
3
:
...makefile...
make, makefile.
- -
.
make
, "f",
, makefile.
.
, -
.
...
. .
source make.f.ile file compiler
. .
...
<=== ===>
-
.
,
(+?[0-9])+|(-?[0-9])+
,
(+ -), .
:
(+?[0-9.])+|(-?[0-9.])+
,
,
, - ,
, -
-
.
lex
. lex - -
. lex -
- host-
( "" ). , Lex-
"" lex, Lex-, ,
- .
4
lex -
( ).
host- .
host-
.
/usr/lib/lex - ncform,
Lex- -
. -
, -
, ,
,
..
lex Lex- ncform.
lex.yy.c,
-, -
.
Lex- :
%%
%%
,
. Lex-
:
%%
.
Lex- .
,
.
:
_
, ,
lex .
, . -
-
,
.
,
, , ,
-, -
.
5
Lex-:
%%
[jJ][aA][nN][uU][aA][rR][yY] {
printf(""); }
[fF][eE][bB][rR][uU][aA][rR][yY] {
printf(""); }
[mM][aA][rR][cC][hH] {
printf(""); }
[aA][pP][rR][iI][lL] {
printf(""); }
[mM][aA][yY] {
printf(""); }
[jJ][uU][nN][eE] {
printf(""); }
[jJ][uU][lL][yY] {
printf(""); }
[aA][uU][gG][uU][sS][tT] {
printf(""); }
[sS][eE][pP][tT][eE][mM][bB][eE][rR] {
printf(""); }
[oO][cC][tT][oO][bB][eE][rR] {
printf(""); }
[nN][oO][vV][eE][mM][bB][eE][rR] {
printf(""); }
[dD][eE][cC][eE][mM][bB][eE][rR] {
printf(""); }
[mM][oO][nN][dD][aA][yY] {
printf("");}
[tT][uU][eE][sS][dD][aA][yY] {
printf(""); }
[wW][eE][dD][nN][eE][sS][dD][aA][yY] {
printf(""); }
[tT][hH][uU][rR][sS][dD][aA][yY] {
printf(""); }
[fF][rR][iI][dD][aA][yY] {
printf(""); }
[sS][aA][tT][uU][rR][dD][aA][yY] {
printf(""); }
[sS][uU][nN][dD][aA][yY] {
printf("");}
, -
.
(
). ,
, -
lex.
Lex- -
.
, , -
.
6
(
), .
,
, . -
,
.
,
19. -
,
. "May" ()
"[mM][aA][yY]". -
"May",
"".
"May", .
, Lex- -
,
.
, Lex- source.l,
, , -
:
% lex source.l
% cc -O lex.yy.c -ll -o program
%
lex , ,
lex.yy.c - - - .
-, program.
Program ,
. -
:
% program
May
MONDAY
MoNdaY
CNTRL/C
%
-ll /usr/lib/libl.a -
lex.
, ,
7
,
main. ,
(, ), -
, main
,
main -
lex.
,
lex.yy.c, -t :
% lex -t source.l >> file
file.
2. Lex-
.
,
(, ..) -.
.
.
2.1.
.
.
- . -
:
"abc"
abc
.
. ,
"\n";
\__
( );
\n ;
\t ;
\b ;
8
,
, -
. ,
lex -
.
2.2.
-,
:
\ ^ ? * + | $ / %
[] {} () <<>>
-
. -
, ,
\ .
:
abc+ - "+" - ;
abc\+ - "+";
abc"+" - "+".
2.3.
,
.
[abc]
"a", "b", "c";
- -
:
[A-z]
;
[-]
;
[+-0-9]
"+" "-".
2.4.
-
, -
* +.
9
* ( 0) -
. :
x* "x";
abc* "abc";
[A-z]*
;
[A-Z-a-z-_0-9]*
, -
.
+ . :
x+ "x";
[0-9]+
;
abc+ abc;
[A-z]+
.
2.5.
:
/ | ? $ ^
.
/:
ab/cd
"ab" ,
"cd".
e |:
ab|cd
"ab", "cd".
e ?:
x? "x".
_?[A-Za-z]*
, -
.
10
-?[0-9]+
-
.
$:
x$ "x",
. "\n"!
abc$ "abc",
.
^:
^x "x",
;
^abc "abc", -
.
[^A-Z]*
, .
^ [],
.
^
!
2.6. {}
{} :
x{n,m} n m , m > n. n m
x, , x{2,7} - 2 7
x.
{} {} -
Lex-
.
:
[A-Z-a-z-_]
[0-9]
{}({}|{})*
%%
{} printf("\n%s",yytext);
lex , -
"" .
-.
{}
{}({}|{})*, [A-Z-a-z-_]([A-Z-
a-z-_]|[0-9])*.
11
yytext -
lex.yy.c, lex. yytext
, -
- . -
Lex-.
printf
.
".|\n ;" ,
( ) ,
{}.
2.7. <<>>. START BEGIN
Lex-
. .
, -
.
Lex-
, -
. START
Lex-, BEGIN
, .
:
_
:
<<_>>_
:
, -
!
:
12
%START COMMENT
_ "/*"
_ "*/"
%%
{_} { ECHO;
BEGIN COMMENT;};
[\t\n]* ;
<COMMENT>[^*]* ECHO;
<COMMENT>[^/] ECHO;
<COMMENT>{_} {
ECHO;
printf("0);
BEGIN 0;};
lex , -
-
. START, -
%. START
: Start, S, s .
START COMMENT.
"<COMMENT>x" - x,
COMMENT.
O "BEGIN COMMENT;"
COMMENT (
Lex-). -
-
, -
"<COMMENT>". ,
<COMMENT>[^*]* ECHO;
,
("/*").
( ) , "*". -
"BEGIN 0;" .
Lex-
. , Lex-
%START AA BB CC DD
, -
.
, BEGIN.
13
,
"<<>>", . -
, .
.
, ,
:
<<1,2,3>>x
- !
:
%START AA BB CC
[A-Z-a-z-_]
[0-9]
{}({}|{})*
%%
^# BEGIN AA;
^[ \t]*main BEGIN BB;
^[ \t]*{} BEGIN CC;
\t ;
\n BEGIN 0;
<AA>define printf(".\n");
<AA>include printf(".\n");
<AA>ifdef {
printf(" .\n"); }
<BB>[^\,]*","[^\,]*")" {
printf("main .\n"); }
<BB>[^\,]*")" {
printf("main .\n"); }
<CC>":"/[ \t] printf(".\n");
. -
,
. Lex- ,
.
lex -
, - -
-, main, -
, , .
,
.
14
3. Lex-
Lex- e,
.
.
, ,
Lex-. ,
, ,
lex.yy.c -
lex.
3.1. Lex-
, lex,
%%. ,
%{ %} , -
lex. Lex-
:
,
,
,
,
host-,
,
host-.
:
%START 1 2 ...
,
Lex-.
:
. :
[A-Z-a-z-_]
DIGIT [0-9]
{}({}|{DIGIT})*
- , ,
. -
( ), ,
( ).
15
-
:
- " ";
- :
%{
%}
, , ,
.
, -
, -
lex.yy.c
:
%T
_ _
.........
_ _
%T
lex.yy.c -
lex
input, output, unput. , lex yytext
,
. -
,
, .
( ) .
,
, ,
, .
0 , ,
.
:
16
%T
1 Aa
2 Bb
3 Cc
.
.
.
26 Zz
27
28 +
29 -
30 0
31 1
.
.
.
39 9
%T
-
1-26, 27, "+" "-"
28 29, - 30-39.
:
%x
- ;
x - :
p - ;
n - ;
e - ;
a - ;
k - ;
o - .
lex , .
-
, lex
. -
(
) ,
. , -
,
.
, -
:
17
p - 1500
n - 300
e - 600
a - 1500
k - 1000
o - 1500
,
, -v, :
% lex -v source.l
33/600 (%e)
97/1500 (%p)
17/300 (%n)
2551
18/1000 (%k)
41/1500 (%a)
68/1500 (%o)
%
, lex -v.
"/"
, "/" -
.
host- .
3.2.
, %% Lex-
%%, ,
.
. , -
, yylex lex.yy.c,
-
. :
%{
%}
:
%%
%{
#include file.h
%}
.
.
.
18
"#include file.h"
yylex().
-
() .
, "-
" . ,
BEGIN.
:
:
<>
<_>
_ :
1,2,...
:
BEGIN ;
,
.
:
BEGIN 0;
. Lex- -
,
. "BEGIN ;"
, . -
"BEGIN 0;" -
, .
, -
BEGIN ,
,
.
3.2.1. Lex-
lex, -
, "BEGIN ;", .
-
, - (
19
-
), .
,
(
, ), -
,
-.
, ,
.
, -
Lex-.
Lex- ,
,
. , -
.
, ,
. , -
, . -
, - ,
.
,
.
, :
[ 0 ;
() ,
. ,
e
";" - ,
.
-
.
"|", , -
, :
" " |
|
;
, , .
, -
,
, lex.
yytext . :
20
[A-Z]+ printf("%s",yytext);
,
printf, -
.
,
:
[A-Z]+ ECHO;
. lex.yy.c ECHO -
:
#define ECHO fprintf(yyout, "%s",yytext);
, yyleng,
. :
[A-Z]+ printf("%c",yytext[yyleng-1]);
, -
[A-Z]+.
:
[A-Z]+ {_++;_ += yyleng;}
.
3.2.2.
Lex-
,
.
-
BEGIN.
,
, , -
: ?
()
Lex- ,
, , .
, , lex
:
- ,
-
;
21
-
,
,
Lex-.
:
.
.
.
[][][] ECHO;
[--]+ ECHO;
.
.
.
"" , , -
, ,
(3 ).
, , "", 3
, 7 -
, , -
, -
.
3.3.
, %%,
.
lex.yy.c - -
. lex.yy.c
.
, , -
.
3.4. Lex-
Lex-
. -
host-. , Lex-
.
-
.
, .
host-
.
3.5. Lex-
1.
22
%Start KOMMENT
/*
*
*
* -.
* ,
*
* !
*/
_ "/*"
_ "*/"
%%
{_} { ECHO;
BEGIN KOMMENT;}
[0* ;
<KOMMENT>[^*]* ECHO;
<KOMMENT>[^/] ECHO;
<KOMMENT>{_} {
ECHO;
printf("0);
/*
*
*
* Lex-.
* ,
* ,
* .
*/
BEGIN 0;}
%%
/*
*
* .
*/
2.
23
%Start IC1 IC2 Normal
/*
*
* Lex-,
*
* .
* return(...)
*
*