-
" "
/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(...)
*
*
* .
* ,
* Lex-
* .
* ,
* ,
* .
*
* ,
* ,
* .
*/
LETTER [A-Z-a-z-_]
DIGIT [0-9]
IDENT {LETTER}({LETTER}|{DIGIT})*
INT {DIGIT}+
FIXED {INT}?.{INT}
WHISP [ 0*
%%
BEGIN Normal;
<Normal>"{" BEGIN IC1;
<IC1>[^}] ;
<IC1>"}" BEGIN Normal;
<Normal>"(*" BEGIN IC2;
<IC2>[^*]|[^)] ;
<IC2>>"*)" BEGIN Normal;
<Normal>'([^']|'')*' return( );
<Normal>"<>" return( _ );
<Normal>"=" return( );
<Normal>"<" return( );
<Normal>">" return( );
<Normal>">=" return(__);
<Normal>"<=" return(__);
<Normal>".." return( _ );
<Normal>"+" return( );
<Normal>"-" return( );
<Normal>":=" return( );
<Normal>"*" return( );
<Normal>"/" return( );
<Normal>mod return( t_mod );
24
<Normal>div return( t_div );
<Normal>and return( t_and );
<Normal>or return( t_or );
<Normal>not return( t_not );
<Normal>"(" return( lpar );
<Normal>")" return( rpar );
<Normal>"[" return( lbracket );
<Normal>"]" return( rbracket );
<Normal>"," return( comma );
<Normal>":" return( colon );
<Normal>"^" return( circumflex );
<Normal>";" return( semicolon );
<Normal>write return( Write );
<Normal>writeln return( Writeln );
<Normal>label return( t_label );
<Normal>program return( );
<Normal>const x( "" ) ;
<Normal>type x( "" ) ;
<Normal>var x( "" ) ;
<Normal>procedure x( "" ) ;
<Normal>function x( "" ) ;
<Normal>begin x( "" ) ;
<Normal>end{WHISP}. x( " " ) ;
<Normal>end x( "" ) ;
<Normal>array x( "" ) ;
<Normal>of x( "" ) ;
<Normal>record x( "" ) ;
<Normal>case x( "" ) ;
<Normal>in x( "" ) ;
<Normal>file x( "" ) ;
<Normal>for x( "" ) ;
<Normal>to x( "" ) ;
<Normal>downto x( " " ) ;
<Normal>do x( "" ) ;
<Normal>while x( "" ) ;
<Normal>repeat x( "" ) ;
<Normal>until x( "" ) ;
<Normal>set x( "" ) ;
<Normal>with x( "" );
<Normal>nil x( "nil" ) ;
<Normal>if x( "" ) ;
<Normal>then x( "" ) ;
<Normal>else x( "" ) ;
<Normal>{FIXED} x( "float" ) ;
<Normal>{INT} x( ".." ) ;
<Normal>{IDENT} x( "" ) ;
<Normal>[ 0] ;
%%
x( s )
char *s ;
{
25
printf("%-15.15s 177> %s <1770,
s, yytext ) ;
}
4. lex.yy.c
lex -
,
lex.yy.c. -
. - :
yylex()
,
;
yylook()
, -
Lex-
.
, -
-. :
input()
;
unput(c)
-
;
output(c)
c.
:
input -
fprintf( fout, "%s%d%s0,
"#define input() (((yytchar=yysptr>yysbuf
ctable['0],
"?(yylineno++,yytchar):yytchar)==EOF?0:yyt
unput -
26
#define unput(c){
yytchar = (c);
if( yytchar == '\n' ) yylineno--;
*yysptr++ = yytchar;
}
output -
#define output(c) putc(c,yyout)
,
.
ld -ll main,
. -
/usr/lib/libl.a
# include "stdio.h"
main(){
yylex();
exit(0);
}
5. yywrap()
yywrap
, -
. yywrap 1,
. ,
.
yywrap,
0, .
yywrap 1
.
Lex- ,
. -
- yywrap. ,
-
, -
yywrap. :
27
%START AA BB CC
/*
* ,
*
* -,
* ,
* ,
* main.
* ,
*
* ,
* .
*/
= [A-Z-a-z-_]
[0-9]
{}({}|{})*
int a1,a2,a3,b1,b2,c;
%%
{a1 = a2 = a3 = b1 = b2 = c = 0;}
^# BEGIN AA;
^[ \t]*main BEGIN BB;
^[ \t]*{} BEGIN CC;
\t ;
\n BEGIN 0;
<AA>define { a1++; }
<AA>include { a2++; }
<AA>ifdef { a3++; }
<BB>[^\,]*","[^\,]*")" { b1++; }
<BB>[^\,]*")" { b2++; }
<CC>":"/[ \t] { c++; }
%%
yywrap(){
if( b1 == 0 && b2 == 0 )
printf(" \
main.\n");
if( b1 >= 1 && b2 >= 1 ){
printf("\
main.\n");
} else {
if(b1 == 1 )
printf(" main\
28
.\n");
if( b2 == 1 )
printf(" main\
.\n");
}
printf(" : %d.\n",a2);
printf(" : %d.\n",a3);
printf(": %d.\n",a1);
printf(": %d.\n",c);
return(1);
}
return(1) yywrap , -
.
, return(0),
, , -
. ,
yywrap 1,
.
6. REJECT
lex ,
.
,
. ,
she he .
:
she s++;
he h++;
. |
\n ;
she he,
he, she, ,
she, .
.
REJECT " ".
, ,
. -
:
29
she { s++; REJECT; }
he { h++; REJECT; }
. |
\n ;
, .
REJECT , -
- ,
. ,
, -
, , the th, he.
, digram, :
%%
[a-z][a-z] {
digram[yytext[0]][yytext[1]]++;
REJECT;
}
\n ;
REJECT , -
, .
7. yyless yymore
yytext
, . ,
yytext -
. -
yytext -
. yymore.
:
yymore()
yytext,
.
yyless. :
yyless(n)
n , n
yytext.
.
yymore:
30
.
.
.
\"[^"]* {
if( yytext[yyleng - 1] == '\\'){
yymore();
}else{
/*
*
* ,
* .
*/
}
}
.
.
.
o, -
,
.
,
, , , -
\".
, "\"".
"\ ,
"\", yymore().
"\ ",
yytext : "\", .
yyless:
.
.
.
=-[A-Z-a-z-] {
printf("O (=-) .\n");
yyless(yyleng - 2);
/*
*
* "=-"
*/
}
.
.
.
"=-
31
" .
"=- " ( "-=" )
"= -"
,
"= -" .
.
, "yyless(yyleng - 2);" -
"-" , "="
yytext , .
,
"-", .
8. lex yacc
yacc
yylex(). lex.
, yacc y.tab.c .
y.tab.c yyparse, -
. yyparse -
yylex.
-
yylex
-
.
,
-
.
,
-
. -
, .
, ,
.
.
,
.
,
.
yylex -
-
, ,
32
, ,
.
, , -
Yacc-
, -
token:
%token < >
token, yacc
.
Yacc-:
%token IDENT CONST IF THEN GOTO
-
Yacc- -
, _ -
.
yacc
:
-
, -
;
- , , -
, 257.
, -
, yacc
y.tab.c :
#define <_> <_>
, yylex,
. , -
Yacc-, Lex-
.
y.tab.c lex.yy.c, -
:
- yacc -d -
#define y.tab.h.;
- #include
Lex-.
33
, , .
-
- , ,
( -
lex). ,
.
yylval. yylex -
, :
extern int yylval;
, _ ,
yylval. ,
yylval
.
, Yacc-
source.y Lex- source.l, -
.
:
- Lex- Yacc-
y.tab.h;
- Lex- Yacc-
y.tab.h.
.
makefile make, -
program:
34
program: y.tab.o lex.yy.o
cc y.tab.o lex.yy.o -ly -ll -o program
y.tab.o: y.tab.c
cc -c -O y.tab.c
lex.yy.o: lex.yy.c y.tab.h
cc -c -O lex.yy.c
y.tab.h:
y.tab.c: source.y
yacc -d source.y
lex.yy.c: source.l
lex -v source.l
clear:
rm -f yy.tab.? lex.yy.? program
source.l Yacc-,
. 52
, A z, -
, +, -,
*, /, % ( ), & ( ), | (
) . , ,
0, , - .
.
-
,
.
"=" ,
<>. -
, -
. integer.
35
%token DIGIT LETTER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%{
int base, regs[26];
%}
%%
list:
|
list stat '\n'
|list stat error '\n' { yyerrok; }
stat:
expr { printf( "%d\n",$1 ); }
|LETTER '=' expr { regs[$1]=$3; }
expr:
'(' expr ')' { $$=$2; }
|expr '+' expr { $$=$1+$3; }
|expr '-' expr { $$=$1-$3; }
|expr '*' expr { $$=$1*$3; }
|expr '/' expr { $$=$1/$3; }
|expr '%' expr { $$=$1%$3; }
|expr '&' expr { $$=$1&$3; }
|expr '|' expr { $$=$1|$3; }
|'-' expr %prec UMINUS { $$= -$2; }
| LETTER { $$=regs[$1]; }
| number;
number:
DIGIT { $$=$1;
base=10;
if($1==0) base=8; }
|number DIGIT { $$=base*$1+$2; }
source.l Lex-
:
36
%{
#include "y.tab.h"
extern int yylval;
%}
%%
^\n ;
[ \t]* ;
[A-Za-z] {
yylval = yytext[yyleng-1] - 'a';
return(LETTER);}
[0-9] {
yylval = yytext[yyleng-1] - '0';
return(DIGIT);}
. Makefile
:
program: y.tab.c lex.yy.c
cc -O y.tab.c -ly -ll -o program
y.tab.c: source.y
yacc source.y
lex.yy.c: source.l
lex -v source.l
clear:
rm -f y.tab.? lex.yy.? program
source.y source.l -
. Yacc- -
#include lex.yy.c, Lex-
#include "y.tab.h".
source.y :
37
%token DIGIT LETTER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%{
#include "lex.yy.c"
int base, regs[26];
%}
%%
list:
|
list stat '\n'
|list stat error '\n' { yyerrok; }
stat:
expr { printf( "%d\n",$1 ); }
|LETTER '=' expr { regs[$1]=$3; }
expr:
'(' expr ')' { $$=$2; }
|expr '+' expr { $$=$1+$3; }
|expr '-' expr { $$=$1-$3; }
|expr '*' expr { $$=$1*$3; }
|expr '/' expr { $$=$1/$3; }
|expr '%' expr { $$=$1%$3; }
|expr '&' expr { $$=$1&$3; }
|expr '|' expr { $$=$1|$3; }
|'-' expr %prec UMINUS { $$= -$2; }
| LETTER { $$=regs[$1]; }
| number;
number:
DIGIT { $$=$1;
base=10;
if($1==0) base=8; }
|number DIGIT { $$=base*$1+$2; }
source.l :
38
%{
extern int yylval;
%}
%%
^\n ;
[ \t]* ;
[A-Za-z] {
yylval = yytext[yyleng-1] - 'a';
return(LETTER);}
[0-9] {
yylval = yytext[yyleng-1] - '0';
return(DIGIT);}
9.
lex -
.
%R.
host- -
. ,
-. , lex ,
. ,
- lexshf. lexshf
( )
( ).
Lex-
goto ,
lex.yy.r.
, source.l
host-, -
:
% lex source.l
% rc lex.yy.r -llr
, 1,
, , yytex[yyleng] -
.
10. Lex
-t ,
lex.yy.c;
-v ;
-f , (
);
39
-n ( -
);
-d lex.
.
lex.yy.c
:
cc -d -DLEXDEBUG lex.yy.c
-
. -d, ,
lex.yy.c
cdeb.
40
. ......................................... 2
1. .......................................... 3
2. Lex- ............... 8
2.1. ............... 8
2.2. .................. 9
2.3. ............. 9
2.4. ..................................... 9
2.5. ................................ 10
2.6. {} ..................................... 11
2.7. <>. START BEGIN ...... 12
3. Lex- ........................... 15
3.1. Lex- ................ 15
3.2. ................................... 18
3.2.1. Lex- ............. 19
3.2.2. .............. 21
3.3. .................... 22
3.4. Lex- ....................... 22
3.5. Lex- ............................ 22
4. lex.yy.c .......................... 26
5. yywrap() .................................. 27
6. REJECT .................................... 29
7. yyless yymore ........................... 30
8. lex yacc ............... 32
9. ............................. 39
10. Lex ......................................... 39
41
Last-modified: Mon, 29 Jun 1998 14:01:55 GMT