* . * , * 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>&gt;"*)" 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