
			Lex --  ᪨ ஢
     
                         M. E. Lesk and E. Schmidt
                                     
                             Bell Laboratories
                       Murray Hill, New Jersey 07974

ॢ Copyright (C) Alexey Mahotkin 1995, 1998

$Id: lex-rus.txt,v 1.11 1998/12/18 22:12:10 alexm Exp $                       
     


   ணࠬ, 祩 ⮪ ࠢ ࠢ ॣ묨
ࠦﬨ, 騬  室 ⮪.   ᯮᮡ  
࠭ଠ権 ⨯ ।-ਯ   ᥣ஢   ⮢
 楤 ࠧ.

室 ⥪  --  ⠡ ॣ ࠦ  ᮮ⢥
ࠣ⮢ ணࠬ.  ࠭᫨  ணࠬ,  ⠥ 室
⮪,    室 ⮪  ࠧ 室   ப, 
ᮮ⢥騥  ࠦ.  ᯮ  ப 믮
ᮮ⢥騩 ࠣ ணࠬ. ᯮ ࠦ ந 
ନ஢  ⮬⮬, ᣥ஢ ᮬ. ࠣ
ணࠬ, ᠭ 짮⥫, ᯮ  浪,  ஬
ᮮ⢥騥 ॣ ࠦ   室 ⮪.

ணࠬ ᪮ , ᠭ   , ਭ
᫥ ᯥ䨪樨  롨 ᠬ  ᮮ⢥⢨,  
 窥 . ᫨ 室, ந ⥫ ᬮ ।,
 室 ⮪ ࠭   ⥪饣 ࠧ, ⠪   짮⥫
 ᢮  樨 .

  ஢   C  Ratfor, 몥,  
⮬᪨ ࠭᫨  ७ᨬ ࠭.  㯥  PDP-11 UNIX,
Honeywell GCOS  ⥬ IBM OS.  㪮⢮, , 㦤 ⮫쪮
 ஢  C  ⥬ UNIX, ⢥ ଠ   UNIX
Version 7.  ࠧࠡ⠭  饭 ⢨  Yacc,  ,  
   ⮩ ⥬  ஢.

     21  1975

		 --  ᪨ ஢

     
     
                         M. E. Lesk and E. Schmidt
                                     
                             Bell Laboratories
                       Murray Hill, New Jersey 07974
     

1. 

 --   ணࠬ, ࠧࠡ⠭  ᪮ ࠡ⪨
ᨬ 室 ப.  ਭ ᮪஢ ஡-
ਥ஢ ᯥ䨪  ࠢ ப ᨬ  ந ணࠬ
 몥 饣 祭,  ᯮ ॣ ࠦ. 
ࠦ 㪠뢠 짮⥫  室 ᯥ䨪樨,  . ,
ᠭ ᮬ, ᯮ  ࠦ  室 ⮪  ࠧ 
 ப, ᮮ⢥騥 ࠦ. H ࠭  ப ᯮ
ᥪ樨 ணࠬ, ᠭ 짮⥫. 室 䠩  뢠
ॣ ࠦ  ࠣ ணࠬ.    ࠦ 
室 ⮪ ணࠬ, ᠭ ᮬ, ᯮ ᮮ⢥騩
ࠣ.

짮⥫  ࠢ ࠦ ᢮ , ॡ㥬  
믮  , ,  , ᠭ 㣨 ࠬ.
ணࠬ, ᯮ ࠦ, ᣥ஢  몥 饣 祭,
ᯮ㥬  ࠣ ணࠬ 짮⥫.  ࠧ, 
ᯥ稢 ᮪஢  ࠢ ப ࠦ,
࠭ ᢮ 짮⥫  ⢨ ⠪,   㣮.  
  ⠢ 짮⥫, 饣 ᯮ짮  樨
ப   室 ,  ணࠬ ࠡ⪨  ⮬  
ᠬ,  室饬, 몥 ࠡ⪨ ப.

 --  祭  ணࠬ஢,  ᪮॥ ,
।⠢騩 ᮡ  ᢮⢮ 몠, ஥    
 ࠧ 몠 ணࠬ஢, 뢠 " 모". 筮 ⠪
,  모 ணࠬ஢  ந , ᯮ塞  
ࠧ ୮ 㤮,      ࠧ 몠. 
  ᯮ  室 , ᣥ஢ ᮬ  
ࠣ⮢ ணࠬ,  짮⥫. ⨬ ⥪
६ 믮  ࠧࠧ  몮 ⠢  .
  ᯮᮡ   ࠧ 㦥  ࠧ 짮⥫.
  ⭮ 砥  室  ᫮ -- 樨 
饣 㤮, ᭮ 몠 ணࠬ஢,
ᮮ⢥饣 ,  짮⥫  ᮡ ᫮ . 
 騩  ⢥ ন 몮  C, 
 諮 ⢮ ॠ  Fortran (  Ratfor).  lex 
  UNIX, GCOS  OS/370,  , ᣥ஢ , 
ᯮ짮 ,   ᮮ⢥騩 . 

Lex ॢ頥 짮⥫᪨ ࠦ  ⢨ (뢠 
室 ⥪⮬)    ᭮ 몥 ணࠬ஢; ᣥ஢
ணࠬ 뢠 yylex.  ணࠬ  ࠧ祭 ࠦ 
 室 ⮪  믮 ᮮ⢥ ⢨   
ࠦ. . . 1.

          Ŀ    Ŀ    Ŀ
           Source ===> Lex ===>yylex 
                  
          Ŀ    Ŀ    Ŀ
           Input  ===>yylex===>Output
                  
                         Figure 1

 ⢥ ਢ쭮 ਬ, ।⠢ ᥡ ணࠬ, 㤠
 室 ⮪  ஡   ⠡樨   ப.
          
               %%
               [ \t]+$ ;

 -- ,  ॡ.  ணࠬ ⮨  ࠧ⥫ %%,
⬥饣 砫 ࠢ,   ࠢ.  ࠢ ᮤন
ॣ୮ ࠦ, ஬ ᮮ⢥    
ᨬ ஡  ⠡樨 (砥  \t  ⠡쭮,
 ᮮ⢥⢨  ᮣ襭ﬨ 몠 C) 筮   ப. 
᪮   ᨬ, 騩  ஡  ⠡樨;
+ 砥 "  ...";  $ 砥 " ப",   
QED.    ⢨  㪠,  ணࠬ, ᣥ஢
lex', 㤥 ஢  ᨬ.  ⠫쭮 㤥 ᪮஢. 
⮡   ⠢訥 ப ஡  ⠡権  
⢥ ஡,    ࠢ:
    
               %%
               [ \t]+$ ;
               [ \t]+ printf(" ");

 ⮬, ᣥ஢  ⮣ 室 ⥪, 㤥
᪠஢ 室 ⮪  ᪠ ࠧ  ࠢ, , 
室    ப  ஡   ⠡樨 ᨬ 
ப  믮 ᮮ⢥饥 ⢨. ࢮ ࠢ ᮮ⢥
 ப  ஡   ⠡樨   ப,  ஬ --
 ⠢訥 ⠪ ப.

Lex  ᯮ짮 ᠬ  ᥡ   ८ࠧ, 
    ᡮ ⨪  ᪮ ஢. Lex ⠪
 ᯮ짮 ᮢ⭮  ஬ ஢  믮
䠧 ᪮ ; ᮡ  ⢨  Lex 
 Yacc'. ணࠬ  Lex ᯮ ⮫쪮 ॣ ࠦ;
Yacc ᮧ , ᯮ騥 让  ⥪
ࠬ⨪,  ॡ ᪮   ᯮ ⮪
 室 ⮪.  ࠧ, 㤮   Lex  Yacc. 
 ᯮ짮  ⢥ ९  , Lex ᯮ
 ࠧ 室 ⮪,    稢訥 窨. 
⮪ ࠢ   砥 (ਬ,    ࢮ
 )   . 2.  ணࠬ, ᣥ஢
Lex',     ணࠬ, ᠭ   
  㣨 ஢. 

                 Ŀ         Ŀ
                 ᪨          ࠬ᪨ 
                    ࠢ               ࠢ    
                          
                                               
                  Ŀ            Ŀ
                      Lex                  Yacc    
                              
                    Ŀ              Ŀ
   室 ⮪ ==> yylex =============> yyparse ==> ࠭
                                      室 ⮪
                                 Figure 2

짮⥫ Yacc' ,   yylex --   , ஥ Yacc
  ⢥  ᢮ ᪮ , ⠪ 롮
砥 ⢨.

   ॣ ࠦ  室 ⥪ ନ᪨
 ⮬.  ⮬ ,   , ⮡
 .  १  ࠢ 砥  . 
 ᭮, ६, ॡ㥬 ணࠬ  Lex  ᯮ  
ࠧ 室 ⮪, ய樮쭮  . ⢮ ࠢ 
 ᫮  뢠 ﭨ  ᪮, ᪫祭 ࠢ, 
ॡ  । (forward context),  ॡ ⨬ 
⢠ ᪠஢.  㢥祭 ⢠  ᫮ ࠢ
㢥稢 ࠧ 筮 ⮬, , ᮮ⢥⢥, ࠧ 
ணࠬ, 㥬 Lex'.

 ணࠬ, ᣥ஢ Lex', ࠣ , ᠭ 짮⥫,
(騥 ⢨,  ᫥ 믮  宦
ᮮ⢥饣 ॣ୮ ࠦ), ᮡ   
switch.  ⮬ ࠢ ⮪ 믮 ணࠬ
짮⥫     ६ 
⥫   楤, ᮤঠ  ⢨,  
 ᢮ ணࠬ  ⮩ 楤  ⢨ﬨ.

Lex  ࠭祭 室 ,    ஢
᭮ 뢠 ।   ᨬ. ਬ, ᫨  
ࠢ --   ab,  ஥ -- abcdefg,  室 ⮪ ᮤন
abcdefh,  Lex 㦨 ab  ⠢ 㪠⥫  室 ⮪ 
  । cd.    業, 祬 ࠡ⪠  
 몮.


2. 室 ⥪  Lex

騩 ଠ 室 ⥪  Lex ⠪:
          
               {।}
               %%
               {ࠢ}
               %%
               {짮⥫᪨ ணࠬ}

 ।  짮⥫᪨ ணࠬ  ய᪠.
  "%%" 易⥫쭠,  ࢠ ॡ, ⮡ 
⬥ 砫 ࠢ. 쭠 ணࠬ  Lex 룫廊 

		%%

(  ।,   ࠢ)  ࠭᫨  ணࠬ,
  室 ⮪  室  .

 奬 ணࠬ  Lex,  , {ࠢ} ।⠢ ᮡ 
짮⥫᪨ 襭  ᫥⥫쭮 믮 ணࠬ; 
।⠢ ᮡ ⠡,    ᮤন ॣ
ࠦ (. ᥪ 3),  ࠢ  ᮤন ⢨, ࠣ
ணࠬ,  믮,   室 ⮪ ᯮ
ᮮ⢥騥 ࠦ.  ࠧ, ࠢ  룫拉 ⠪:

               integer   printf("㦥 祢 ᫮ INT");

 ࠢ   室 ⮪ ப "integer"  ⠥
ᮮ饭 "㦥 祢 ᫮ INT"  㦥.  ⮬ ਬ
 楤 몮  C,    ப ᯮ
筠 㭪 printf.  ࠦ 㪠뢠  ஡
 ᨬ ⠡樨. ᫨ ⢨ --  ⢥ ࠦ  C,
      ࠢ  ப; ᫨   -- 
⠢ ࠦ,      ப,   ᫥
  窨.  ⢥    ਬ ।,
  室  ஥ ⢮ ᫮  ⠭᪮ 
ਪ᪮ ந襭. ࠢ  몥 Lex ⠪:

               colour    printf("color");
               mechanise printf("mechanize");
               petrol    printf("gas");

 ࠢ 筮, ⮬  ᫮ "petroleum" ॢ 
᫮ "gaseum".  ࠢ  ⨬, ᪠ .

			3.  ࠦ Lex'

। "ॣ୮ ࠦ"  ⮬,  ᯮ  
QED5. ୮ ࠦ  ⢮ ப,  
ᮮ⢥.  ᮤন ⥪⮢ ᨬ (ᮮ⢥騥 ᨬ
 ࠢ ப)  ᨬ ஢ (  ७,
ਠ 롮  㣨 ).   㪢 䠢 ᥣ
 ⥪⮢묨 ᨬ; ⠪ ࠧ, ॣ୮ ࠦ
"integer" ᮮ⢥ ப "integer",     , 
ࠦ "a57D"  ப "a57D".

.  ஢ -- 
          
               " \ [ ] ^ - ? . * + | ( ) $ / { } % < >

᫨  ᯮ  ⥪⮢ ᨬ,    ࠭஢.
 ஢ (") 㪠뢠,  ,  室  ன
 祪   ⥪⮢묨 ᨬ.  ࠧ, 

                    xyz"++"
ᮮ⢥ ப "xyz++". ,    ப   
祭  窨.  ⠪,   易⥫쭮,   
窨  ⥪⮢ ᨬ; ࠦ

                    "xyz++"

筮 ।饬.  ࠧ,   窨  
-䠢⭮-஢ ᨬ,  ᯮ  ⢥ ⥪⮢
ᨬ, 짮⥫   室  ᨬ
஢,  客    ᨬ ஢  
饬.     

  ⠪   ॢ饭  ⥪⮢ ᨬ, ᫨
⠢ ।  "\", ਬ,

		xyz\+\+

  ,  ⠡쭮,  襯ਢ ࠦ. 
㣮 ᯮ짮 祪 -- ⠢  ॣ୮ ࠦ ஡;
筮,  뫮 ᭥ , ஡  ⠡樨  ࠢ.
஡,  室騥  [] (. )   祭  
窨.  ᯮ  ᯥ樠 ᨬ 몠 C:
"\n" --   ப, "\t" --  ᨬ ⠡樨, \b --  backspace.
⮡ ᯮ짮 ᠬ ᨬ "\",  "\\".   ᨬ  
ப   ᯮ짮  ॣ ࠦ, ᫥ 
"\n"; ࠭஢ ᨬ ⠡樨  backspace 易⥫쭮.  ᨬ,
஬ ஡, ⠡樨, ᨬ  ப  襯ਢ ᯨ᪠
஢, ᥣ  ⥪⮢묨 ᨬ.

 ᨬ.

 ᨬ     ᯮ짮  ஢ []. 
 [abc] ᮮ⢥ ⢥ ᨬ,   
"a", "b"  "c".   ᪮ 設⢮ ஢ 
 ⠥ ⥪⮢묨 ᨬ. 쪮  ᨬ ࠭ 
஢: "\", "-"  "^".  "-" 砥 . ਬ,

		[a-z0-9<>_]
砥  ᨬ, 騩   㪢, ,  ᪮ 
ᨬ ઠ.       浪. ᯮ짮
"-"  ன ᨬ,      ய묨  묨
㪢,  ࠬ,   ॠ樨  ਢ  뤠 ।०饣
ᮮ饭. (ਬ, [0-z]  ASCII ᮤন ࠧ  ᨬ, 祬
 EBCDIC). ᫨   ᨬ 室  ᨬ "-",  
  砫   ᯨ᪠; ⠪ ࠧ,

		[-+0-9]
ᮮ⢥ ࠬ   ""  "".

  ᨬ  "^"    ᨬ ᫥
 ᪮;  砥,  १ ப   
"஢".  ࠧ,

		[^abc]

ᮮ⢥ ᥬ ᨬ, ஬ "a", "b"  "c",   
ᯥ樠  ஫ ᨬ; ,

		[^a-zA-z]

  ᨬ,  騩 㪢.  "\" ᯥ稢
筮 ࠭஢   ᪮.


ந ᨬ.  "."  ᮬ  ᨬ 
᪫祭 ᨬ ॢ ப.  ⠪,   ⠡쭮,
ᯮ짮  쬨  ᨬ:

		[\40-\176]
ᮮ⢥ ᥬ ⠥ ᨬ   ASCII,  쬨筮 
40 (஡)  쬨筮 176 (⨫줠). 


易⥫ ࠦ.  "?" 砥 易⥫
 ࠦ.  ࠧ,

		ab?c

ᮮ⢥ "ac"  "abc".


騥 ࠦ. ७ ᮢ  ᯮ짮
஢ "*"  "+". 

		a*

।⠢ ᮡ  ⢮,  , ᫥⥫
ᨬ "a"; ⮣ 

		a+

ᮮ⢥    ஢ "a". ਬ, 

		[a-z]+

ᮮ⢥ ᥬ ப, 騬   㪢, 

		[A-Za-z][A-Za-z0-9]*

砥  䠢⭮-஢ ப   ᨬ, 騬
㪢.  ⨯筮 ࠦ  । 䨪஢ 
몠 ணࠬ஢.

ୠ⨢  㯯஢.  "|" 砥 ୠ⨢:

		(ab|cd)

ᮢ  "ab"   "cd". ,  ᪮ ᯮ 
㯯஢,   ᠬ 孥 ஢  易⥫; ਬ,
筮 뫮  

		ab|cd

  ᯮ짮  ⠢  ᫮ ࠦ: 

		(ab|cd+)?(ef)*

ᮮ⢥ ⠪ ப,  "abefef", "efefef", "cdef"  "cddd",  
"abc", "abcd"  "abcdef".

⢨⥫쭮  ⥪. Lex ᯮ 讥 ⢮ 
⥪.  ᠬ    ⮣ -- "^"  "$". ᫨ 
ᨬ ࠦ  "^",  ࠦ 㤥 ᮮ⢥⢮ ப
⮫쪮 ᫨  砥  砫 ப (᫥ ᨬ ॢ ப 
  砫 室 ⮪).  ᯮ짮  䫨  㣨
祭 "^", ஢ ᮢ ᨬ, ᯮ騬 ⮫쪮 
 ஢ "[]". ᫨ ᠬ ᫥ ᨬ ࠦ -- "$", 
ࠦ ᮮ⢥ ப ⮫쪮 ᫨  砥   ப,
      ᫥ ᨬ  ப, "\n". 
᫥  -- ᯥ樠 砩  "/", 饣 
ᮯ騩 ⥪. ࠦ

		ab/cd

ᮮ⢥ ப "ab", 祬    ᫥ "cd".  ࠧ,

		ab$

   ᠬ,   

		ab/\n


⥪ ᫥ ࠡ뢠 Lex'   ⮢ ᫮,  
  ᥪ樨 10. ᫨ ࠢ  믮 ⮫쪮 
Lex 室  ⮢ ᫮ "x", ࠢ  뢠  
"<x>"  砫, ᯮ  "<>". ᫨   ﭨ 
" 砫 ப" ﭨ "ONE",   "^" ࠢᨫ

		<ONE>

⮢ ᫮ ஡  .

७  ।.  "{}"  ७ (᫨  
ᮤঠ )  ம। (᫨   ᮤঠ ). 
ਬ,

		{digit} 

ᮮ⢥ ࠭ । ப   "digit"  砥
 ᠬ ப. ।   ࢮ  室 䠩  
Lex', । ࠢ. ,

		a{1,5}

砥 "     ``a''". 

, ப, 稭  "%",  ࠧ⥫ ᥣ⮢
室   Lex.



4. ⢨ Lex'.

 㦥 ࠦ, ᠭ 㪠 ᯮᮡ, Lex 
믮 ᮮ⢥饥 ⢨.  ⮩  뢠 
 Lex', 騥  ᠭ ⢨. ,  
 ⠪ ⢨  㬮砭, 饥  ஢ 
室 ⮪  室.  ⢨ ⢫  ᥬ ப,
   㪠 .  ࠧ, 짮⥫ Lex', 饬
ࠡ  室 ⮪, ᫥ ᯥ ࠢ,  ⠪ 
 ᮮ⢥ .  Lex ᯮ    Yacc' -- 
筠 .  ,  ⢨ --  ,  ந室
 ஢ 室 ⮪  室; ⠪ ࠧ,  饬, 
ࠢ, ஥   ⠪ ஢,  ய. 
筮 ⠪   ᨬ  室 ⮪,   ᮢ
   ࠢ, ᪮॥ ᥣ 㤥 ⠭  室 ⮪, 
ਢ     ࠢ.

 ⮥,   ᤥ --  ஢ 室 . 
  ᤥ, 㪠 ⮩  몠 C:

		;

 ⢥ ⢨.  砥 ࠢ

		[ \t\n] ;

஥ ਢ  ஢ ⮣ ࠭⢠ (஡, 
ᨬ ⠡樨   ப).

   ᯮᮡ  ᠭ ⢨  ᨬ
"|",  㪠뢠,  ⢨   ࠢ 筮 ⠪ ,
   ᫥饣. ।騩 ਬ   ᠭ 

		" "  |
                "\t" |
                "\n" ;

 筮 ⥬  १⮬,   㣮 ⨫. 窨  "\n" 
"\t"  ॡ.

  ᫮ ⢨ 짮⥫  ॡ ,  
 ⥪ ᮢ  ⥬   ॣ ࠦ, ᪠,
"[a-z]+". Lex ࠭ ᮢ訩 ⥪  ᨢ ᨬ yytext. 
ࠧ, ⮡   ,  ࠢ ⨯

		[a-z]+	printf("%s", yytext);

㭪 몠 C printf ਭ  ⢥ 㬥⮢ ଠ ப
 ,  ᫥ .  ⮬ 砥 ଠ⭠ ப --
 " ப" (ᨬ "%" 砥 ८ࠧ , 
ᨬ "s" 砥 ப ⨯). 묨    ᨬ
 yytext.  ࠧ,  ࠢ  頥 ᮢ ப 
⠭ 뢮.  ⢨ ⮫ ࠭,   
⪮ ᠭ  ECHO:

		[a-z]+	ECHO;

砥   ᠬ,   襯ਢ ਬ.   ⢨  
㬮砭 --     ᨬ,   ,
祬   ࠢ, ஥ ᮢ  ⢨  㬮砭? 
 ࠢ  ॡ, ⮡  ᮢ  -
㣨 ࠢ, ⥫쭮   砥. ਬ, ᫨  
ࠢ, ஥ ᮮ⢥ ப "read",  筮  ⠪ 㤥
ᮮ⢥⢮ ⮩ ப, 饩  ப "bread"  
"readjust". ⮡  ⮣, ॡ ࠢ  "[a-z]+". 
 㤥 ਢ 쭥襥 ᭥ ⮬.

 㤮 뢠 ,  ᮤন   ᮢ襩 ப. 
 ⮣ lex ᯥ稢 稪 yyleng, ᮤঠ騩 ⢮ 
ᮢ ᨬ. ⮡   ᫮ ᫮, ⠪  ⢮
ᨬ, ⨢  室 ⮪, 짮⥫  :

		[a-zA-Z]+	{ words++; chars += yyleng; }

 ࠢ  ⢮ ᨬ  㦥 ᫮. 
᫥ ᨬ  ᮢ襩 ப   祭   
ࠦ yytext[yyleng - 1].

 -  ⢨  ,  ࠢ ᯮ 
 ᫥⥫쭮 ᨬ.  ⮬ 砥  
 ᯥ樠 楤. -,  맢 yymore(), ⮡
㪠,  ᫥饥 ᯮ ࠦ  室 ⮪ 
     ⥪饣. 筮  ᫥饥 ࠦ
뢠  ⥪饣 ᮤন yytext. -, 
맢 yyless(n), ⮡ 㪠,    ᨬ  ᮢ襣
ࠦ  ॡ אַ ᥩ. 㬥 n  ⢮ ᨬ,
 ᫥ ࠭  yytext. ⠫ ᨬ  ⭮
 室 ⮪  ᫥饣 ⥭.  ᯥ稢  
।, 宦   "/",  ᪮쪮 㣮.

ਬ: ।⠢ ,  ஬ ப ।   ᨬ
 窠,  ⮡   ப ᠬ ᨬ 窨,  室
࠭஢ ᨬ "\". ୮ ࠦ, ᮮ⢥饥 ⮬
।, 룫廊 ᫨誮 ᫮, ⮬ ।⥫쭮 㤥 

     
               \"[^"]*   {
                              if (yytext[yyleng-1]  == '\\') yymore();
                              else ... /* 筠 ࠡ⪠ */
                         }

 ࠡ⠥ ⠪: ⨢ ப ``"abc\"def"'', ᭠砫 롥 
ᨬ ``"abc\''; ⥬ 맮 yymore() ਢ  ⮬,  ᫥
 ப, ``"def'', 㤥     . ,  
⥫쭠 窠,  ப,   ࠡ⠭  
, 祭  "ଠ쭠 ࠡ⪠".

㭪 yyless()  ᯮ짮  ୮ ࠡ⪨ ⥪ 
ࠧ ᫮. ।⠢ ᥡ   몥 C ஡
ᯮ 筮   "=-a" (ਬ. ॢ.: ஡
  ). ।,  ᫥    
饩 "=- a",  뤠 ।०饥 ᮮ饭. ࠢ 
룫拉 ⠪:

          =-[a-zA-Z] {
                         printf(" (=-) ᠭ 筮\n");
                         yyless(yyleng-1);
                         ... ⢨  =- ...
                    }

 뤠 ᮮ饭, 頥 㪢, ᫥  ஬,  
⭮  室 ⮪,  ࠡ⠥  ⨬ ஬   "=-". 
 ⠪ ࠡ뢠    "= -a".  ⮣  
 ᨬ    㪢 ⭮  室 ⮪:
     
          =-[a-zA-Z] {
                         printf(" (=-) ᠭ 筮\n");
                         yyless(yyleng-2);
                         ... ⢨  = ...
                    }

 ਢ  ୠ⨢ 樨. ,  ࠦ 
  砥    ᠭ 

		=-/[A-Za-z]

 ࢮ 砥,                      
               
		=/-[A-Za-z]
     
 ஬.  ⮬ 砥   ࠢ  ॡ. 易⥫쭮
ᯮ 楫 䨪, ⮡ 㢨 筮. ,
  "=-", ਢ  ⮬,   訬 ࠢ 
 

		=-/[^ \t\n]

   ⨬ 楤ࠬ lex ⠪ ᯥ稢   楤ࠬ
-뢮,   ᯮ. :

1) input(),  ᫥騩 ᨬ  室 ⮪;
2) output(c), 뢠 ᨬ c  室 ⮪; 
3) unput(c), 騩 ᨬ c ⭮  室 ⮪  쭥襣
୮ ⥭ 楤ன input().

 㬮砭  楤 ᠭ  ம।,  
짮⥫  ८।  ᢮ ᮡ⢥묨 ਠ⠬. 
 楤 । ⭮襭  譨 䠩  ७
ᨬ,    ᮢ⨬   㣮.    
८।, ਬ, ⮡ /뢮 ந, ᯮ 㣨
ணࠬ   ⪨ ; , ⮡ ᯮ㥬 
ᨬ     楤; 㫥 ᨬ, 饭
楤ன input(), 砥  䠩. ⭮襭  input  
unput   ࠭,   㤥 ࠡ  ।. 
Lex ᯮ  । ⮫쪮 ᫨  ⢨⥫쭮 ॡ,
  ࠢ, 稢饥  "+", "*", "?"  "$", 
ᮤঠ饥 "/" ࠧ㬥 ᯮ짮  ।.  ⠪
室, ⮡ ࠡ ࠦ, 饥 䨪ᮬ 㣮
ࠦ.  㦤  ᨬ, ᯮ㥬 lex'.
⠭⭠ ⥪ lex'  ࠭祭   । 
100 ᨬ.

  筠 楤,   ॡ ८।,
 yywrap(),  뢠  ⨦  䠩. ᫨ yywrap()
頥 1, lex 稢 ࠡ 室 ⮪. , , 
ॡ த ࠡ 室 ⮪, ⥯ 㦥  
 㣮 筨.  ⮬ 砥 짮⥫ ८। yywrap, 
 뢠   筨  頥 0.  㪠뢠 lex',
 ॡ த ࠡ.  㬮砭 yywrap ᥣ
頥 1. 

 楤 ⠪ ।⠢ 㤮   뤠 ⠡, 
᢮  ⮬    ணࠬ. ,  
 筮 ࠢ, ᯮ饥  䠩; ⢥ ,
  ।  䠩 --  yywrap().  ⢨⥫쭮, 
᫨ ⮫쪮   짮⥫᪠  楤 input(), 
 ࠡ 䠩 ᮤঠ騩 㫥 ᨬ, , ⮬  
  ⠪ ᨬ,  ୥ 楤 input(), 㤥 
殬 䠩. 


5.  ࠢ

Lex ᯮᮡ ࠡ뢠  ᯥ䨪樨.    
 室 ⮪ ᮢ  ᪮쪨 ࠦﬨ, lex 롨ࠥ 
   ᫥騬 ࠢ:

1) ᠬ  ᮢ ।⥫쭮;

2)  ࠢ,  ᮮ⢥ ᫥⥫쭮  , 
롨ࠥ ࢮ. 

 ࠧ, ।,   ࠢ:
          
		integer   ⢨  祢 ᫮ ...;
		[a-z]+    ⢨  䨪 ...;

ࠢ    ⠪ 浪. ᫨  室 ⮪ 
᫥⥫쭮 ``integers'',   ⠭ 䨪஬, ⮬ 
 ࠢ "[a-z]+" ᮮ⢥ 쬨 ᨬ, ⮣  ࠢ
"integer" --  ᥬ. ᫨   室 ⮪  ``integer'',
  ࠢ ᮮ⢥ ᥬ ᨬ,  롨ࠥ ࠢ 
祢 ᫮, ஥ 뫮  .   ⪨
᫥⥫쭮, ⠪  ``int''  ᮢ  ࠦ "integer" 
  ஢  䨪.

ਭ樯,  ஬ ।⥭ ⤠ ᠬ  ᮢ,
ਢ  ⮬,  ࠦ ⨯ ".*" ⠭ . ਬ, 
"'.*'"  ⫨ ᯮᮡ ᯮ ப   窠.
  ᠬ   -- ।  室 ⮪   
,  ᪠ ᠬ ᫥ ୮ 窨. ਬ, ᫨ 
室 ⮪ 룫廊 

	'first' quoted string here, 'second' here

 襯ਢ ࠦ 㤥 ᮮ⢥⢮ ᫥⥫쭮

	'first' quoted string here, 'second'

, ᪮॥ ᥣ,  ,   .  訬 ࠢ 
 "'[^'\n]*'", ஥   砥 ⠭  "'first'". 
᫥⢨  訡  ⥬ 䠪⮬,   "." 
 ᮮ⢥ ᨬ  ப. ⮬ ࠦ ⨯ ".*"
  । ⥪饩 ப.  ⠩ ࠢ  ⨬
⥫⢮, ᯮ ࠦ ⨯ "[.\n]+", ⮬  
ணࠬ, ᣥ஢ lex', ⠥   室
䠩, ਢ  ९ ७ ஢.

,  lex 筮 ࠧ 室 ⮪,     
宦  ࠦ.  砥,   ᨬ ᯮ
஢  ࠧ. ।, ਬ,    뢠, ᪮쪮
ࠧ  室 ⥪  ᫥⥫쭮 ``she''  ``he''. 
묨 ࠢ lex'  ⮣   

		she	s++;
		he	h++;
                \n	|
                .	;

  ᫥ ࠢ  , ஬ "he"  "she". ᯮ,
 "."  ᮮ⢥ ᨬ  ப.   "she" 砥
 ᥡ "he",  lex 筮  㤥 ᯮ ᫥⥫쭮 "he",
  "she", ⠪   ᫥⥫쭮 "she"
ࠡ⠭,   ᨬ     ࠡ.

 짮⥫ ॡ ८।  . ⢨
"REJECT" 砥 "३  ᫥饩 ୠ⨢."  ⢨
ਢ  ⮬,  믮 ᫥饥 ᫥ ⥪饣 ࠢ.
  室 ⮪ ᮮ⢥騬 ࠧ . ।,
 짮⥫ ⢨⥫쭮  뢠 ᫥⥫쭮
"he"  "she":

		she	{ s++; REJECT; }
		he	{ h++; REJECT; }
		\n	|
                .	;

 ࠢ ।⠢ ᮡ   ਠ⮢  .
᫥ 뢠  ࠦ  뢠; ᫨ ॡ
 㤥 ⠪ ⠭ 㣮 ࠦ.  ⮬ ਬ, 筮,
짮⥫  ,  "she" 砥  ᥡ "he",  
,    "REJECT"  ⢥ ⢨  "he";  㣨
, , ࠭  ᪠,  ᨬ  ⢮
  . 

।⠢  ࠢ:

		a[bc]+	{ ... ; REJECT; }
		a[cd]+	{ ... ; REJECT; }

᫨  室 ⮪ 砥 "ab",  ந室 ᮢ ⮫쪮
  ࠢ,   砥  "ad" -- ⮫쪮  . 室 ⮪
"accb" ᮮ⢥ ࢮ ࠢ ᥬ  ᨬ,  ஬
ࠢ -- ६ ᨬ.  , 室 ⮪ "accd" ᮢ
 ᨬ   ࠢ,    ࠢ -- 
६ ᨬ. 

, REJECT ,  楫 lex'   ࠧ 室
⮪,  ।  ਠ⮢ - ࠦ  室
⮪,   ਠ  ४뢠    㣠.
।,  室 ந ⠡ ࠬ (  
ᨬ (ਬ. ॢ.)) 室 ⮪. 筮 ࠬ ४뢠,
  ᫮ "the" ⮨  ࠬ "th"  "he". ।, 
⠡ ࠭  㬥୮ ᨢ digram, ᮮ⢥騬 室
⥪⮬ 

               %%
               [a-z][a-z] {digram[yytext[0]][yytext[1]]++; REJECT;}
               \n ;

 REJECT 室  ⮣, ⮡ ࠡ  , 稭騥
  ᨬ,     ஬ ᨬ.                


6. ଠ 室 䠩  lex.

ᯮ ଠ 室 䠩  lex:

     
               {।}
               %%
               {ࠢ}
               %%
               {짮⥫᪨ 楤}

   㦤 ⮫쪮 ࠢ. 짮⥫ ॡ 
⥫ , ਬ, । ६,
ᯮ㥬  ணࠬ.  ।  室
 ᥪ ।   ᥪ樨 ࠢ. 

ᯮ,  lex ॢ頥 ࠢ  ணࠬ.  室 ,
 墠祭 lex',     ᣥ஢ 
ணࠬ.   ⨯ ⠪ 室 :

1)  ப,    ࠢ  ⢨,
稭  ஡  ᨬ ⠡樨,  
ᣥ஢ ணࠬ.  , ।騩 ࢮ
ࠧ⥫ "%%" 㤥 室   㭪樨  祭
ணࠬ. ᫨     ᫥ ࢮ "%%", 
  뢠  ᮮ⢥饬   権  
㭪樨, ᠭ lex'  ᮤঠ饩 ⢨.   
 룫拉  ࠣ ணࠬ   室 ࠭
ࢮ ࠢ. 

 䥪⮬ ⮣  ,  ப, 稭騥 
⮩ ப  ᨬ ⠡樨,  ᮤঠ騥 ਨ,
  ᣥ஢ ணࠬ.   ᯮ짮  
祭 ਥ    lex', ⠪   ᣥ஢ 
ணࠬ. ਨ  룫拉  ਨ 楫 몠. 

2) ,  室  ப, ᮤঠ騬 ⮫쪮 ࠧ⥫
"%{"  "%}",   室 ⥪. ⥫ 㤠. 
  ⥪ ⨯ ४⨢ ९,  
稭  ࢮ ,  ⥪,   룫廊  ணࠬ.

3) , 室饥 ᫥ 쥣 ࠧ⥫ "%%", ᨬ 
ଠ   祣    뫮,   室 ⥪.

।  ।  ࠧ⥫ "%%".  ப  ⮩
ᥪ樨,  室騥  "%{"  "%}"  稭騥  ࢮ ,
 ।ﬨ ப ⠭. ଠ  :

	<>	<⠭>

 砥,  <> 樨  <⠭>.   ⠭
  ࠧ    ࠭⢮,   
稭  㪢. ⠭ ந室  ᯮ짮  ࠢ
ᨭ⠪ "{}". ਬ, ᯮ "{D}"    "{E}"  
㪠⥫ ᯮ, ਬ,  ⠪ 뢠 ࠢ  
ᯮ ᥫ:

	D [0-9]
        E [DEde][-+]?{D}+
        %%
        {D}+			printf("楫");
        {D}+"."{D}*({E})?	|
        {D}*"."{D}+({E})?	|
        {D}+{E}			printf("⢥");

     ࠢ  ⢥ ᥫ:  
ॡ    ᮤঠ 易⥫쭮  ᯮ, 
ࢮ ॡ  ࠩ    । 筮 窮,
⮣  ஥ ॡ  ࠩ    ᫥ 筮
窨. ⮡ ४⭮ ࠡ ஡,   裡  
ࠦﬨ ⨯ "35.EQ.I" 몠 ࠭,   ᮤঠ ⢥
᫠, ॡ ⥫쭮 ⥪⭮-⢨⥫쭮 ࠢ ⨯

	[0-9]+/"."EQ		printf("楫");

 ।  ⠪  稥 , ⠪  
롮 ᭮ 몠, ⠡  ᨬ, ᯨ᮪ ⮢ ᫮,
 ன ࠧ஢ ᨢ, ᯮ㥬 lex', ᫨ ணࠬ
쭮 .    㦤   
"騩  ଠ 室 䠩".


			7. ᯮ짮

 樨 ணࠬ  lex' 室 ந  蠣.
-, 室  lex   ॢ饭  ணࠬ  
몥 饣 祭. ⥬  ணࠬ   ᪮஢ 
᫨, 筮   ⥪ ணࠬ lex'. ஢
ணࠬ 室  䠩   ``lex.yy.c''. ⥪ -뢮 
।  ⠭⭠ ⥪ 몠 C [6].

ணࠬ  C, ᣥ஢ Lex'  OS/370, ᫥ ⫨砥 
 , ⮬  ⠬譨  ᫠, 祬  
UNIX  GCOS. ணࠬ, ᣥ஢  GCOS  UNIX, .

UNIX. ⥪ 砥   䫠 騪 ``-ll''. 
ࠧ, ᮮ⢥饩 ᫥⥫쭮  :

lex source
cc lex.yy.c -ll

 ணࠬ,  筮, 頥  䠩 ``a.out'' 
᫥饣 믮.   ᠭ ᯮ짮 Lex' ᮢ⭮  
Yacc'. ᬮ  ,   楤 /뢮  Lex'
ᯮ ⠭ ⥪ C, ᠬ ⮬   易  ,
 ᫨  ।⠢ ᮡ⢥ ᨨ input(), output()  unput(), 
   ⥪.


8. Lex  Yacc

᫨   ᯮ짮 Lex ᮢ⭮  Yacc',  ,  Lex
ᮧ ணࠬ   yylex(),    ࠧ  , ஥ ॡ
Yacc  ⢥ ᪮ . 筮 ⠭⭠ ணࠬ
main()  ⥪ Lex' 뢠  楤,  ᫨ Yacc ⠪
㦥,  ᯮ   ணࠬ main(),  ᠬ Yacc 㤥
뢠 yylex().  ⮬ 砥  ࠢ Lex'  稢
 
		return(token);
  ⢥ १ 祭 ᮮ⢥饣 ⮪. 
 ᯮᮡ     ⮪ Yacc' -- ᪮஢ 
室 䠩 Lex'   室 䠩 Yacc', ⨢ ப 

		#include "lex.yy.c"

 ᫥ ᥪ樨 室 䠩 Yacc'. ।,  ࠬ⨪
뢠 ``good'',  ᪨ ࠢ   -- ``better''. 
᫥⥫쭮  樮 ⥬ UNIX ⠪:
     
               yacc good
               lex better
               cc y.tab.c -ly -ll

⥪ Yacc' (-ly)   ᫨ ࠭, 祬 ⥪
Lex', ⮡ ࠡ⠫ ࠢ쭠 ணࠬ main(),  맮
, ᣥ஢ Yacc'.  ணࠬ Yacc'  Lex' 
 ஢   浪. 


9. ਬ.

 ⢥ ਢ쭮 ஡ ।⠢ ᥡ  ஢
室 䠩   ᫠ 3   ⥫쭮 ,
饬  7.  ਬ ணࠬ  Lex:
          
               %%
                         int k;
               [0-9]+    {
                              k = atoi(yytext);
                              if (k%7 == 0) printf("%d", k+3);
                              else printf("%d",k);
                         }

ࠢ ``[0-9]+'' ᯮ ப ; atoi()   
 ଠ  ࠭ १  k.  ``%'' (⮪)
ᯮ  ஢ન,   k  7; ᫨ ,  k 㢥稢
   뢠  室 䠩.  ࠧ,  ⠪
ணࠬ 㤥  ⠪ ப  室 ⮪,  ``49.63''
 ``X7''.  ⮣,  㢥稢 ᮫⭮ 祭 
⥫ ᥫ,   7. ⮡  ⮣,  
᪮쪮 ࠢ, ਬ:
     
               %%
                         int k;
               -?[0-9]+  {
                              k = atoi(yytext);
                              printf("%d", k%7 == 0 ? k+3 : k);
                         }
               -?[0-9.]+ ECHO;
               [A-Za-z][A-Za-z0-9]+ ECHO;

஢ ப, ᮤঠ騥 "."  稭騥  㪢  ⮡࠭
᫥  ࠢ    .  if-else 
  ᫮ ࠦ 몠 C, ⮡ ᡥ ࠭⢮, 
``a?b:c'' 砥: ``if a then b else c''. 

 ⢠ ਬ ᡮ ⨪,  ணࠬ, ⠢
⮣ࠬ  ᫮,  ᫮ ।  ப 㪢:
          
                         int lengs[100];
               %%
               [a-z]+    lengs[yyleng]++;
               .    |
               \n   ;
               %%
               yywrap()
               {
                    int i;
                    printf("Length No. words\n");
                    for(i=0; i<100; i++)
                         if (lengs[i] > 0)
                               printf("%5d%10d\n",i,lengs[i]);
                         return(1);
               }

 ணࠬ  ⮣ࠬ,  ந  ⮬ 
뢮. ⠢ 室 䠩,  ⠥ ⮣ࠬ. ᫥
, return(1), 砥,  Lex  ந ୮ ⥭. 
᫨ yywrap() 頥 ,  砥,  㯭 ⥫
  ணࠬ  த ⥭  ࠡ. ᫨ 
yywrap() ⠪,     ୥ 1,  ணࠬ  
᪮ 横.

 ⢥ ਬ , ᬮਬ  ࠣ ணࠬ, ᠭ
N. L. Schryer',    Fortran  ᫠ 
筮  Fortran  ᫠ ୮ 筮.   Fortran 
ࠧ砥 ய   㪢,  ணࠬ 稭  ᠭ
 ᮢ,   ᥡ  ਠ  㪢:
          
               a [aA]
               b [bB]
               c [cC]
               ...
               z [zZ]

⥫  ࠧ砥 ஡:               
     
               W [ \t]*
     
ࢮ ࠢ  "double precision"  "real",  "DOUBLE
PRECISION"  "REAL". 
     
               {d}{o}{u}{b}{l}{e}{W}{p}{r}{e}{c}{i}{s}{i}{o}{n} {     
                    printf(yytext[0]=='d'? "real" : "REAL");
               }

 ணࠬ ।ਭ ᯥ樠   ࠭ 室
ॣ 㪢.  롮 㦭 ਠ ᯮ ᫮ . 
饥 ࠢ  "continuation card indications", ⮡ 
⠭  ⠭⠬:

               ^"     "[^ 0]      ECHO;

 ॣ ࠦ ஡ 㦭   窨.  砩
  " 砫 ப  ஡, ⥬ , 
㣮, ஬ ஡   ".     ࠧ
祭 ᨬ ``^''.  ᫥ ᪮쪮 ࠢ  ८ࠧ
⠭  筮   ⠭  饩 窮.
               
               [0-9]+{W}{d}{W}[+-]?{W}[0-9]+ |
               [0-9]+{W}"."{W}{d}{W}[+-]?{W}[0-9]+ |
               "."{W}[0-9]+{W}{d}{W}[+-]?{W}[0-9]+ {
                    /* ८ࠧ ⠭ */
                    for(p=yytext; *p != 0; p++) {
                         if (*p == 'd' || *p == 'D') *p += 'e'- 'd';
                          ECHO;
                    }

᫥ ⮣,  ᯮ ⠭  饩 窮, 
ᬠਢ  ᪠ 㪢 ``d''  ``D''. ⥬  ⮩ 㪢
ਡ 祭 "'e'-'d'", ⮡ ८ࠧ  , ᮮ⢥⢥, 
``e''  ``E''. ஢ ⠭, ⥯ 㦥 ୮
筮, 뢠  室 ⮪. ⥬ ᫥  ࠢ,
㡨    㭪権  砫 㪢 ``d''. ᯮ
ᨢ yytext,  ࠭ ⢥ ⢨   ⠪
㭪樨 ( ਢ ⮫쪮 ᮪ 쭮 讣 ᯨ᪠).
                    
                    {d}{s}{i}{n} |
                    {d}{c}{o}{s} |
                    {d}{s}{q}{r}{t} |
                    {d}{a}{t}{a}{n} |
                    ...
                    {d}{f}{l}{o}{a}{t} printf("%s",yytext+1);

 㣮 ᯨ᪥  砫쭠 㪢 ``d''    
砫 ``a'': 

                    {d}{l}{o}{g} |
                    {d}{l}{o}{g}10 |
                    {d}{m}{i}{n}1 |
                    {d}{m}{a}{x}1  {
                         yytext[0] =+ 'a' - 'd'; ECHO;
                    }

   楤 砫쭠 ``d''   ``r'':                    
                    
                    {d}1{m}{a}{c}{h} yytext[0] += 'r' - 'd';
     
⮡  訡筮 ᮢ ⠪ ,  ``dsinx'',  ⠪
蠡,  ``dsin'', ⥫ ࠢ ᮡ    
᫮  䨪,  ⠪    稥 楫訥
ᨬ:
          
               [A-Za-z][A-Za-z0-9]* |
               [0-9]+ |
               \n   |
               .    ECHO;

,   ணࠬ  ;    뢠 ஡ 
஡   ᯮ짮 祢 ᫮  ⢥
䨪஢.
          
          

                       10. Left Context Sensitivity.

          Sometimes it  is desirable  to have  several sets of lexical
     rules to be applied at different times in the input. For example,
     a compiler preprocessor might distinguish preprocessor statements
     and analyze  them  differently  from  ordinary  statements.  This
     requires sensitivity to prior context, and there are several ways
     of handling  such problems.  The ^  operator, for  example, is  a
     prior context  operator, recognizing  immediately preceding  left
     context just as $ recognizes immediately following right context.
     Adjacent left  context could  be extended,  to produce a facility
     similar to that for adjacent right context, but it is unlikely to
     be as useful, since often the relevant left context appeared some
     time earlier, such as at the beginning of a line.
     
          This section describes three means of dealing with different
     environments: a simple use of flags, when only a few rules change
     from one  environment to  another, the use of start conditions on
     rules, and  the possibility  of making multiple lexical analyzers
     all run  together. In  each case, there are rules which recognize
     the need  to change  the environment in which the following input
     text is  analyzed, and  set some parameter to reflect the change.
     This may  be a  flag explicitly tested by the user's action code;
     such a  flag is  the simplest  way of  dealing with  the problem,
     since Lex  is not  involved at  all. It  may be  more convenient,
     however, to  have Lex remember the flags as initial conditions on
     the rules.  Any rule may be associated with a start condition. It
     will only  be recognized when Lex is in that start condition. The
     current start  condition may  be changed at any time. Finally, if
     the sets  of  rules  for  the  different  environments  are  very
     dissimilar, clarity  may be  best  achieved  by  writing  several
     distinct lexical  analyzers, and switching from one to another as
     desired.
     
          Consider the  following  problem:  copy  the  input  to  the
     output, changing  the word  magic to  first on  every line  which
     began with  the letter  a, changing magic to second on every line
     which began  with the  letter b,  and changing  magic to third on
     every line which began with the letter c. All other words and all
     other lines are left unchanged.
     
          These rules  are so  simple that  the easiest way to do this
     job is with a flag:
          
                    int flag;
               %%
               ^a   {flag = 'a'; ECHO;}
               ^b   {flag = 'b'; ECHO;}
               ^c   {flag = 'c'; ECHO;}
               \n   {flag = 0 ; ECHO;}
               magic     { switch (flag) {
                           case 'a':
                              printf("first"); break;
                           case 'b':
                              printf("second"); break;
                           case 'c':
                              printf("third"); break;
                           default: ECHO; break;
                           }
                         }
          
     should be adequate.
     
          To handle the same problem with start conditions, each start
     condition must  be introduced  to Lex  in the definitions section
     with a line reading
          
               %start name1 name2 ...
          
     where the  conditions may  be named  in any order. The word start
     may be abbreviated to s or S. The conditions may be referenced at
     the head  of a  rule with the <> brackets: <name1>expression is a
     rule which  is only recognized when Lex is in the start condition
     name1. To enter a start condition, execute the action statement
     
               BEGIN name1;
     
     which changes  the start condition to name1. To resume the normal
     state,
     
               BEGIN 0;
     
     resets the  initial condition of the Lex automaton interpreter. A
     rule may be active in several start conditions:
     
               <name1,name2,name3>
     
     is a  legal prefix.  Any rule  not beginning  with the  <> prefix
     operator is always active.
     
          The same example as before can be written:
          
               %START AA BB CC
               %%
               ^a             {ECHO; BEGIN AA;}
               ^b             {ECHO; BEGIN BB;}
               ^c             {ECHO; BEGIN CC;}
               \n             {ECHO; BEGIN 0;}
               <AA>magic      printf("first");
               <BB>magic      printf("second");
               <CC>magic      printf("third");

     where the  logic is exactly the same as in the previous method of
     handling the  problem, but  Lex does  the work  rather  than  the
     user's code.
     
     

                            11. Character Set.

          The programs  generated by  Lex handle  character  I/O  only
     through the routines input, output, and unput. Thus the character
     representation provided  in these routines is accepted by Lex and
     employed to return values in yytext. For internal use a character
     is represented  as a small integer which, if the standard library
     is used,  has a  value equal  to the  integer value  of  the  bit
     pattern  representing   the  character   on  the  host  computer.
     Normally, the  letter a  is represented  as the  same form as the
     character constant  'a'. If  this interpretation  is changed,  by
     providing I/O  routines which  translate the characters, Lex must
     be told  about it, by giving a translation table. This table must
     be in  the definitions  section, and  must be  bracketed by lines
     containing only "%T". The table contains lines of the form
               
                {integer} {character string}
          
     which indicate the value associated with each character. Thus the
     next example
     
               %T
               1     Aa
               2     Bb
               ...
               26    Zz
               27    \n
               28    +
               29    -
               30    0
               31    1
               ...
               39    9
               %T
          Sample character table.
     
     maps the  lower and upper case letters together into the integers
     1 through  26, newline  into 27,  + and - into 28 and 29, and the
     digits into  30 through  39. Note  the escape  for newline.  If a
     table is  supplied, every  character that  is to appear either in
     the rules or in any valid input must be included in the table. No
     character may  be assigned  the number 0, and no character may be
     assigned a  bigger number than the size of the hardware character
     set.
     

                       12. Summary of Source Format.

          The general form of a Lex source file is:
          
                    {definitions}
                    %%
                    {rules}
                    %%
                    {user subroutines}
          
          The definitions section contains a combination of
          
     1)   Definitions, in the form "name space translation".
     
     2)   Included code, in the form "space code".
     
     3)   Included code, in the form
                    %{
                    code
                    %}
     
     4)   Start conditions, given in the form
                    %S name1 name2 ...
     
     5)   Character set tables, in the form
                    %T number space character-string
                    ...
                    %T
     
     6)   Changes to internal array sizes, in the form
                    %x   nnn
          where nnn  is a  decimal integer  representing an array size
          and x selects the parameter as follows:
               Letter         Parameter
               p              positions
               n              states
               e              tree nodes
               a              transitions
               k              packed character classes
               o              output array size
               
          Lines in the rules section have the form "expression action"
     where the  action may  be continued  on succeeding lines by using
     braces to delimit it.
     
          Regular expressions in Lex use the following operators:
     
     x         the character "x"
     "x"       an "x", even if x is an operator.
     \x        an "x", even if x is an operator.
     [xy]      the character x or y.
     [x-z]     the characters x, y or z.
     [^x]      any character but x.
     .         any character but newline.
     ^x        an x at the beginning of a line.
     <y>x      an x when Lex is in start condition y.
     x$        an x at the end of a line.
     x?        an optional x.
     x*        0,1,2, ... instances of x.
     x+        1,2,3, ... instances of x.
     x|y       an x or a y.
     (x)       an x.
     x/y       an x but only if followed by y.
     {xx}      the translation of xx from the definitions section.
     x{m,n}    m through n occurrences of x
     
     

                           13. Caveats and Bugs.

          There are pathological expressions which produce exponential
     growth of  the tables  when converted  to deterministic machines;
     fortunately, they are rare.
     
          REJECT does  not rescan  the input; instead it remembers the
     results of  the previous  scan. This  means that  if a  rule with
     trailing context is found, and REJECT executed, the user must not
     have used  unput to  change the  characters forthcoming  from the
     input stream.  This is the only restriction on the user's ability
     to manipulate the not-yet-processed input.
     
     

                           14. Acknowledgments.

          As should  be obvious  from the above, the outside of Lex is
     patterned on  Yacc  and  the  inside  on  Aho's  string  matching
     routines. Therefore,  both S. C. Johnson and A. V. Aho are really
     originators of  much of  Lex, as  well as  debuggers of  it. Many
     thanks are due to both.
     
          The code  of  the  current  version  of  Lex  was  designed,
     written, and debugged by Eric Schmidt.
     
     

                              15. References.

     1.   B. W.  Kernighan  and  D.  M.  Ritchie,  The  C  Programming
          Language, Prentice-Hall, N. J. (1978).
     
     2.   B. W.  Kernighan, Ratfor:  A  Preprocessor  for  a  Rational
          Fortran, Software  - Practice and Experience, 5, pp. 395-496
          (1975).
     
     3.   S.  C.   Johnson,  Yacc:   Yet  Another  Compiler  Compiler,
          Computing  Science  Technical  Report  No.  32,  1975,  Bell
          Laboratories, Murray Hill, NJ 07974.
     
     4.   A. V.  Aho and M. J. Corasick, Efficient String Matching: An
          Aid to Bibliographic Search, Comm. ACM _18, 333-340 (1975).
     
     5.   B. W.  Kernighan, D. M. Ritchie and K. L. Thompson, QED Text
          Editor, Computing Science Technical Report No. 5, 1972, Bell
          Laboratories, Murray Hill, NJ 07974.
     
     6.   D. M.  Ritchie, private  communication. See also M. E. Lesk,
          The Portable  C Library,  Computing Science Technical Report
          No. 31, Bell Laboratories, Murray Hill, NJ 07974.
     
