home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / FLEX237.ZIP / FLEXDOC.MAN < prev    next >
Encoding:
Text File  |  1991-04-02  |  80.8 KB  |  2,241 lines

  1.  
  2.  
  3. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  4.  
  5.  
  6. NAME
  7.      flex - fast lexical analyzer generator
  8.  
  9. SYNOPSIS
  10.      flex [-bcdfinpstvFILT8 -C[efmF] -Sskeleton] [_f_i_l_e_n_a_m_e ...]
  11.  
  12. DESCRIPTION
  13.      _f_l_e_x is a tool for generating _s_c_a_n_n_e_r_s: programs which
  14.      recognized lexical patterns in text.  _f_l_e_x reads the given
  15.      input files, or its standard input if no file names are
  16.      given, for a description of a scanner to generate.  The
  17.      description is in the form of pairs of regular expressions
  18.      and C code, called _r_u_l_e_s. _f_l_e_x generates as output a C
  19.      source file, lex.yy.c, which defines a routine yylex(). This
  20.      file is compiled and linked with the -lfl library to produce
  21.      an executable.  When the executable is run, it analyzes its
  22.      input for occurrences of the regular expressions.  Whenever
  23.      it finds one, it executes the corresponding C code.
  24.  
  25. SOME SIMPLE EXAMPLES
  26.      First some simple examples to get the flavor of how one uses
  27.      _f_l_e_x. The following _f_l_e_x input specifies a scanner which
  28.      whenever it encounters the string "username" will replace it
  29.      with the user's login name:
  30.  
  31.          %%
  32.          username    printf( "%s", getlogin() );
  33.  
  34.      By default, any text not matched by a _f_l_e_x scanner is copied
  35.      to the output, so the net effect of this scanner is to copy
  36.      its input file to its output with each occurrence of "user-
  37.      name" expanded.  In this input, there is just one rule.
  38.      "username" is the _p_a_t_t_e_r_n and the "printf" is the _a_c_t_i_o_n.
  39.      The "%%" marks the beginning of the rules.
  40.  
  41.      Here's another simple example:
  42.  
  43.              int num_lines = 0, num_chars = 0;
  44.  
  45.          %%
  46.          \n    ++num_lines; ++num_chars;
  47.          .     ++num_chars;
  48.  
  49.          %%
  50.          main()
  51.              {
  52.              yylex();
  53.              printf( "# of lines = %d, # of chars = %d\n",
  54.                      num_lines, num_chars );
  55.              }
  56.  
  57.      This scanner counts the number of characters and the number
  58.      of lines in its input (it produces no output other than the
  59.  
  60.  
  61. Printed 4/3/91             26 May 1990                          1
  62.  
  63.  
  64.  
  65.  
  66. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  67.  
  68.  
  69.      final report on the counts).  The first line declares two
  70.      globals, "num_lines" and "num_chars", which are accessible
  71.      both inside yylex() and in the main() routine declared after
  72.      the second "%%".  There are two rules, one which matches a
  73.      newline ("\n") and increments both the line count and the
  74.      character count, and one which matches any character other
  75.      than a newline (indicated by the "." regular expression).
  76.  
  77.      A somewhat more complicated example:
  78.  
  79.          /* scanner for a toy Pascal-like language */
  80.  
  81.          %{
  82.          /* need this for the call to atof() below */
  83.          #include <math.h>
  84.          %}
  85.  
  86.          DIGIT    [0-9]
  87.          ID       [a-z][a-z0-9]*
  88.  
  89.          %%
  90.  
  91.          {DIGIT}+    {
  92.                      printf( "An integer: %s (%d)\n", yytext,
  93.                              atoi( yytext ) );
  94.                      }
  95.  
  96.          {DIGIT}+"."{DIGIT}*        {
  97.                      printf( "A float: %s (%g)\n", yytext,
  98.                              atof( yytext ) );
  99.                      }
  100.  
  101.          if|then|begin|end|procedure|function        {
  102.                      printf( "A keyword: %s\n", yytext );
  103.                      }
  104.  
  105.          {ID}        printf( "An identifier: %s\n", yytext );
  106.  
  107.          "+"|"-"|"*"|"/"   printf( "An operator: %s\n", yytext );
  108.  
  109.          "{"[^}\n]*"}"     /* eat up one-line comments */
  110.  
  111.          [ \t\n]+          /* eat up whitespace */
  112.  
  113.          .           printf( "Unrecognized character: %s\n", yytext );
  114.  
  115.          %%
  116.  
  117.          main( argc, argv )
  118.          int argc;
  119.          char **argv;
  120.              {
  121.              ++argv, --argc;  /* skip over program name */
  122.  
  123.  
  124. Printed 4/3/91             26 May 1990                          2
  125.  
  126.  
  127.  
  128.  
  129. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  130.  
  131.  
  132.              if ( argc > 0 )
  133.                      yyin = fopen( argv[0], "r" );
  134.              else
  135.                      yyin = stdin;
  136.  
  137.              yylex();
  138.              }
  139.  
  140.      This is the beginnings of a simple scanner for a language
  141.      like Pascal.  It identifies different types of _t_o_k_e_n_s and
  142.      reports on what it has seen.
  143.  
  144.      The details of this example will be explained in the follow-
  145.      ing sections.
  146.  
  147. FORMAT OF THE INPUT FILE
  148.      The _f_l_e_x input file consists of three sections, separated by
  149.      a line with just %% in it:
  150.  
  151.          definitions
  152.          %%
  153.          rules
  154.          %%
  155.          user code
  156.  
  157.      The _d_e_f_i_n_i_t_i_o_n_s section contains declarations of simple _n_a_m_e
  158.      definitions to simplify the scanner specification, and
  159.      declarations of _s_t_a_r_t _c_o_n_d_i_t_i_o_n_s, which are explained in a
  160.      later section.
  161.  
  162.      Name definitions have the form:
  163.  
  164.          name definition
  165.  
  166.      The "name" is a word beginning with a letter or an under-
  167.      score ('_') followed by zero or more letters, digits, '_',
  168.      or '-' (dash).  The definition is taken to begin at the
  169.      first non-white-space character following the name and con-
  170.      tinuing to the end of the line.  The definition can subse-
  171.      quently be referred to using "{name}", which will expand to
  172.      "(definition)".  For example,
  173.  
  174.          DIGIT    [0-9]
  175.          ID       [a-z][a-z0-9]*
  176.  
  177.      defines "DIGIT" to be a regular expression which matches a
  178.      single digit, and "ID" to be a regular expression which
  179.      matches a letter followed by zero-or-more letters-or-digits.
  180.      A subsequent reference to
  181.  
  182.          {DIGIT}+"."{DIGIT}*
  183.  
  184.      is identical to
  185.  
  186.  
  187. Printed 4/3/91             26 May 1990                          3
  188.  
  189.  
  190.  
  191.  
  192. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  193.  
  194.  
  195.          ([0-9])+"."([0-9])*
  196.  
  197.      and matches one-or-more digits followed by a '.' followed by
  198.      zero-or-more digits.
  199.  
  200.      The _r_u_l_e_s section of the _f_l_e_x input contains a series of
  201.      rules of the form:
  202.  
  203.          pattern   action
  204.  
  205.      where the pattern must be unindented and the action must
  206.      begin on the same line.
  207.  
  208.      See below for a further description of patterns and actions.
  209.  
  210.      Finally, the user code section is simply copied to lex.yy.c
  211.      verbatim.  It is used for companion routines which call or
  212.      are called by the scanner.  The presence of this section is
  213.      optional; if it is missing, the second %% in the input file
  214.      may be skipped, too.
  215.  
  216.      In the definitions and rules sections, any _i_n_d_e_n_t_e_d text or
  217.      text enclosed in %{ and %} is copied verbatim to the output
  218.      (with the %{}'s removed).  The %{}'s must appear unindented
  219.      on lines by themselves.
  220.  
  221.      In the rules section, any indented or %{} text appearing
  222.      before the first rule may be used to declare variables which
  223.      are local to the scanning routine and (after the declara-
  224.      tions) code which is to be executed whenever the scanning
  225.      routine is entered.  Other indented or %{} text in the rule
  226.      section is still copied to the output, but its meaning is
  227.      not well-defined and it may well cause compile-time errors
  228.      (this feature is present for _P_O_S_I_X compliance; see below for
  229.      other such features).
  230.  
  231.      In the definitions section, an unindented comment (i.e., a
  232.      line beginning with "/*") is also copied verbatim to the
  233.      output up to the next "*/".  Also, any line in the defini-
  234.      tions section beginning with '#' is ignored, though this
  235.      style of comment is deprecated and may go away in the
  236.      future.
  237.  
  238. PATTERNS
  239.      The patterns in the input are written using an extended set
  240.      of regular expressions.  These are:
  241.  
  242.          x          match the character 'x'
  243.          .          any character except newline
  244.          [xyz]      a "character class"; in this case, the pattern
  245.                       matches either an 'x', a 'y', or a 'z'
  246.          [abj-oZ]   a "character class" with a range in it; matches
  247.                       an 'a', a 'b', any letter from 'j' through 'o',
  248.  
  249.  
  250. Printed 4/3/91             26 May 1990                          4
  251.  
  252.  
  253.  
  254.  
  255. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  256.  
  257.  
  258.                       or a 'Z'
  259.          [^A-Z]     a "negated character class", i.e., any character
  260.                       but those in the class.  In this case, any
  261.                       character EXCEPT an uppercase letter.
  262.          [^A-Z\n]   any character EXCEPT an uppercase letter or
  263.                       a newline
  264.          r*         zero or more r's, where r is any regular expression
  265.          r+         one or more r's
  266.          r?         zero or one r's (that is, "an optional r")
  267.          r{2,5}     anywhere from two to five r's
  268.          r{2,}      two or more r's
  269.          r{4}       exactly 4 r's
  270.          {name}     the expansion of the "name" definition
  271.                     (see above)
  272.          "[xyz]\"foo"
  273.                     the literal string: [xyz]"foo
  274.          \X         if X is an 'a', 'b', 'f', 'n', 'r', 't', or 'v',
  275.                       then the ANSI-C interpretation of \x.
  276.                       Otherwise, a literal 'X' (used to escape
  277.                       operators such as '*')
  278.          \123       the character with octal value 123
  279.          \x2a       the character with hexadecimal value 2a
  280.          (r)        match an r; parentheses are used to override
  281.                       precedence (see below)
  282.  
  283.  
  284.          rs         the regular expression r followed by the
  285.                       regular expression s; called "concatenation"
  286.  
  287.  
  288.          r|s        either an r or an s
  289.  
  290.  
  291.          r/s        an r but only if it is followed by an s.  The
  292.                       s is not part of the matched text.  This type
  293.                       of pattern is called as "trailing context".
  294.          ^r         an r, but only at the beginning of a line
  295.          r$         an r, but only at the end of a line.  Equivalent
  296.                       to "r/\n".
  297.  
  298.  
  299.          <s>r       an r, but only in start condition s (see
  300.                     below for discussion of start conditions)
  301.          <s1,s2,s3>r
  302.                     same, but in any of start conditions s1,
  303.                     s2, or s3
  304.  
  305.  
  306.          <<EOF>>    an end-of-file
  307.          <s1,s2><<EOF>>
  308.                     an end-of-file when in start condition s1 or s2
  309.  
  310.      The regular expressions listed above are grouped according
  311.  
  312.  
  313. Printed 4/3/91             26 May 1990                          5
  314.  
  315.  
  316.  
  317.  
  318. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  319.  
  320.  
  321.      to precedence, from highest precedence at the top to lowest
  322.      at the bottom.  Those grouped together have equal pre-
  323.      cedence.  For example,
  324.  
  325.          foo|bar*
  326.  
  327.      is the same as
  328.  
  329.          (foo)|(ba(r*))
  330.  
  331.      since the '*' operator has higher precedence than concatena-
  332.      tion, and concatenation higher than alternation ('|').  This
  333.      pattern therefore matches _e_i_t_h_e_r the string "foo" _o_r the
  334.      string "ba" followed by zero-or-more r's.  To match "foo" or
  335.      zero-or-more "bar"'s, use:
  336.  
  337.          foo|(bar)*
  338.  
  339.      and to match zero-or-more "foo"'s-or-"bar"'s:
  340.  
  341.          (foo|bar)*
  342.  
  343.  
  344.      Some notes on patterns:
  345.  
  346.      -    A negated character class such as the example "[^A-Z]"
  347.           above _w_i_l_l _m_a_t_c_h _a _n_e_w_l_i_n_e unless "\n" (or an
  348.           equivalent escape sequence) is one of the characters
  349.           explicitly present in the negated character class
  350.           (e.g., "[^A-Z\n]").  This is unlike how many other reg-
  351.           ular expression tools treat negated character classes,
  352.           but unfortunately the inconsistency is historically
  353.           entrenched.  Matching newlines means that a pattern
  354.           like [^"]* can match an entire input (overflowing the
  355.           scanner's input buffer) unless there's another quote in
  356.           the input.
  357.  
  358.      -    A rule can have at most one instance of trailing con-
  359.           text (the '/' operator or the '$' operator).  The start
  360.           condition, '^', and "<<EOF>>" patterns can only occur
  361.           at the beginning of a pattern, and, as well as with '/'
  362.           and '$', cannot be grouped inside parentheses.  A '^'
  363.           which does not occur at the beginning of a rule or a
  364.           '$' which does not occur at the end of a rule loses its
  365.           special properties and is treated as a normal charac-
  366.           ter.
  367.  
  368.           The following are illegal:
  369.  
  370.               foo/bar$
  371.               <sc1>foo<sc2>bar
  372.  
  373.           Note that the first of these, can be written
  374.  
  375.  
  376. Printed 4/3/91             26 May 1990                          6
  377.  
  378.  
  379.  
  380.  
  381. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  382.  
  383.  
  384.           "foo/bar\n".
  385.  
  386.           The following will result in '$' or '^' being treated
  387.           as a normal character:
  388.  
  389.               foo|(bar$)
  390.               foo|^bar
  391.  
  392.           If what's wanted is a "foo" or a bar-followed-by-a-
  393.           newline, the following could be used (the special '|'
  394.           action is explained below):
  395.  
  396.               foo      |
  397.               bar$     /* action goes here */
  398.  
  399.           A similar trick will work for matching a foo or a bar-
  400.           at-the-beginning-of-a-line.
  401.  
  402. HOW THE INPUT IS MATCHED
  403.      When the generated scanner is run, it analyzes its input
  404.      looking for strings which match any of its patterns.  If it
  405.      finds more than one match, it takes the one matching the
  406.      most text (for trailing context rules, this includes the
  407.      length of the trailing part, even though it will then be
  408.      returned to the input).  If it finds two or more matches of
  409.      the same length, the rule listed first in the _f_l_e_x input
  410.      file is chosen.
  411.  
  412.      Once the match is determined, the text corresponding to the
  413.      match (called the _t_o_k_e_n) is made available in the global
  414.      character pointer yytext, and its length in the global
  415.      integer yyleng. The _a_c_t_i_o_n corresponding to the matched pat-
  416.      tern is then executed (a more detailed description of
  417.      actions follows), and then the remaining input is scanned
  418.      for another match.
  419.  
  420.      If no match is found, then the _d_e_f_a_u_l_t _r_u_l_e is executed: the
  421.      next character in the input is considered matched and copied
  422.      to the standard output.  Thus, the simplest legal _f_l_e_x input
  423.      is:
  424.  
  425.          %%
  426.  
  427.      which generates a scanner that simply copies its input (one
  428.      character at a time) to its output.
  429.  
  430. ACTIONS
  431.      Each pattern in a rule has a corresponding action, which can
  432.      be any arbitrary C statement.  The pattern ends at the first
  433.      non-escaped whitespace character; the remainder of the line
  434.      is its action.  If the action is empty, then when the pat-
  435.      tern is matched the input token is simply discarded.  For
  436.      example, here is the specification for a program which
  437.  
  438.  
  439. Printed 4/3/91             26 May 1990                          7
  440.  
  441.  
  442.  
  443.  
  444. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  445.  
  446.  
  447.      deletes all occurrences of "zap me" from its input:
  448.  
  449.          %%
  450.          "zap me"
  451.  
  452.      (It will copy all other characters in the input to the out-
  453.      put since they will be matched by the default rule.)
  454.  
  455.      Here is a program which compresses multiple blanks and tabs
  456.      down to a single blank, and throws away whitespace found at
  457.      the end of a line:
  458.  
  459.          %%
  460.          [ \t]+        putchar( ' ' );
  461.          [ \t]+$       /* ignore this token */
  462.  
  463.  
  464.      If the action contains a '{', then the action spans till the
  465.      balancing '}' is found, and the action may cross multiple
  466.      lines.  _f_l_e_x knows about C strings and comments and won't be
  467.      fooled by braces found within them, but also allows actions
  468.      to begin with %{ and will consider the action to be all the
  469.      text up to the next %} (regardless of ordinary braces inside
  470.      the action).
  471.  
  472.      An action consisting solely of a vertical bar ('|') means
  473.      "same as the action for the next rule."  See below for an
  474.      illustration.
  475.  
  476.      Actions can include arbitrary C code, including return
  477.      statements to return a value to whatever routine called
  478.      yylex(). Each time yylex() is called it continues processing
  479.      tokens from where it last left off until it either reaches
  480.      the end of the file or executes a return.  Once it reaches
  481.      an end-of-file, however, then any subsequent call to yylex()
  482.      will simply immediately return, unless yyrestart() is first
  483.      called (see below).
  484.  
  485.      Actions are not allowed to modify yytext or yyleng.
  486.  
  487.      There are a number of special directives which can be
  488.      included within an action:
  489.  
  490.      -    ECHO copies yytext to the scanner's output.
  491.  
  492.      -    BEGIN followed by the name of a start condition places
  493.           the scanner in the corresponding start condition (see
  494.           below).
  495.  
  496.      -    REJECT directs the scanner to proceed on to the "second
  497.           best" rule which matched the input (or a prefix of the
  498.           input).  The rule is chosen as described above in "How
  499.           the Input is Matched", and yytext and yyleng set up
  500.  
  501.  
  502. Printed 4/3/91             26 May 1990                          8
  503.  
  504.  
  505.  
  506.  
  507. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  508.  
  509.  
  510.           appropriately.  It may either be one which matched as
  511.           much text as the originally chosen rule but came later
  512.           in the _f_l_e_x input file, or one which matched less text.
  513.           For example, the following will both count the words in
  514.           the input and call the routine special() whenever
  515.           "frob" is seen:
  516.  
  517.                       int word_count = 0;
  518.               %%
  519.  
  520.               frob        special(); REJECT;
  521.               [^ \t\n]+   ++word_count;
  522.  
  523.           Without the REJECT, any "frob"'s in the input would not
  524.           be counted as words, since the scanner normally exe-
  525.           cutes only one action per token.  Multiple REJECT's are
  526.           allowed, each one finding the next best choice to the
  527.           currently active rule.  For example, when the following
  528.           scanner scans the token "abcd", it will write "abcdab-
  529.           caba" to the output:
  530.  
  531.               %%
  532.               a        |
  533.               ab       |
  534.               abc      |
  535.               abcd     ECHO; REJECT;
  536.               .|\n     /* eat up any unmatched character */
  537.  
  538.           (The first three rules share the fourth's action since
  539.           they use the special '|' action.) REJECT is a particu-
  540.           larly expensive feature in terms scanner performance;
  541.           if it is used in _a_n_y of the scanner's actions it will
  542.           slow down _a_l_l of the scanner's matching.  Furthermore,
  543.           REJECT cannot be used with the -_f or -_F options (see
  544.           below).
  545.  
  546.           Note also that unlike the other special actions, REJECT
  547.           is a _b_r_a_n_c_h; code immediately following it in the
  548.           action will _n_o_t be executed.
  549.  
  550.      -    yymore() tells the scanner that the next time it
  551.           matches a rule, the corresponding token should be
  552.           _a_p_p_e_n_d_e_d onto the current value of yytext rather than
  553.           replacing it.  For example, given the input "mega-
  554.           kludge" the following will write "mega-mega-kludge" to
  555.           the output:
  556.  
  557.               %%
  558.               mega-    ECHO; yymore();
  559.               kludge   ECHO;
  560.  
  561.           First "mega-" is matched and echoed to the output.
  562.           Then "kludge" is matched, but the previous "mega-" is
  563.  
  564.  
  565. Printed 4/3/91             26 May 1990                          9
  566.  
  567.  
  568.  
  569.  
  570. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  571.  
  572.  
  573.           still hanging around at the beginning of yytext so the
  574.           ECHO for the "kludge" rule will actually write "mega-
  575.           kludge".  The presence of yymore() in the scanner's
  576.           action entails a minor performance penalty in the
  577.           scanner's matching speed.
  578.  
  579.      -    yyless(n) returns all but the first _n characters of the
  580.           current token back to the input stream, where they will
  581.           be rescanned when the scanner looks for the next match.
  582.           yytext and yyleng are adjusted appropriately (e.g.,
  583.           yyleng will now be equal to _n ).  For example, on the
  584.           input "foobar" the following will write out "foobar-
  585.           bar":
  586.  
  587.               %%
  588.               foobar    ECHO; yyless(3);
  589.               [a-z]+    ECHO;
  590.  
  591.           An argument of 0 to yyless will cause the entire
  592.           current input string to be scanned again.  Unless
  593.           you've changed how the scanner will subsequently pro-
  594.           cess its input (using BEGIN, for example), this will
  595.           result in an endless loop.
  596.  
  597.      -    unput(c) puts the character _c back onto the input
  598.           stream.  It will be the next character scanned.  The
  599.           following action will take the current token and cause
  600.           it to be rescanned enclosed in parentheses.
  601.  
  602.               {
  603.               int i;
  604.               unput( ')' );
  605.               for ( i = yyleng - 1; i >= 0; --i )
  606.                   unput( yytext[i] );
  607.               unput( '(' );
  608.               }
  609.  
  610.           Note that since each unput() puts the given character
  611.           back at the _b_e_g_i_n_n_i_n_g of the input stream, pushing back
  612.           strings must be done back-to-front.
  613.  
  614.      -    input() reads the next character from the input stream.
  615.           For example, the following is one way to eat up C com-
  616.           ments:
  617.  
  618.               %%
  619.               "/*"        {
  620.                           register int c;
  621.  
  622.                           for ( ; ; )
  623.                               {
  624.                               while ( (c = input()) != '*' &&
  625.                                       c != EOF )
  626.  
  627.  
  628. Printed 4/3/91             26 May 1990                         10
  629.  
  630.  
  631.  
  632.  
  633. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  634.  
  635.  
  636.                                   ;    /* eat up text of comment */
  637.  
  638.                               if ( c == '*' )
  639.                                   {
  640.                                   while ( (c = input()) == '*' )
  641.                                       ;
  642.                                   if ( c == '/' )
  643.                                       break;    /* found the end */
  644.                                   }
  645.  
  646.                               if ( c == EOF )
  647.                                   {
  648.                                   error( "EOF in comment" );
  649.                                   break;
  650.                                   }
  651.                               }
  652.                           }
  653.  
  654.           (Note that if the scanner is compiled using C++, then
  655.           input() is instead referred to as yyinput(), in order
  656.           to avoid a name clash with the C++ stream by the name
  657.           of _i_n_p_u_t.)
  658.  
  659.      -    yyterminate() can be used in lieu of a return statement
  660.           in an action.  It terminates the scanner and returns a
  661.           0 to the scanner's caller, indicating "all done".  Sub-
  662.           sequent calls to the scanner will immediately return
  663.           unless preceded by a call to yyrestart() (see below).
  664.           By default, yyterminate() is also called when an end-
  665.           of-file is encountered.  It is a macro and may be rede-
  666.           fined.
  667.  
  668. THE GENERATED SCANNER
  669.      The output of _f_l_e_x is the file lex.yy.c, which contains the
  670.      scanning routine yylex(), a number of tables used by it for
  671.      matching tokens, and a number of auxiliary routines and mac-
  672.      ros.  By default, yylex() is declared as follows:
  673.  
  674.          int yylex()
  675.              {
  676.              ... various definitions and the actions in here ...
  677.              }
  678.  
  679.      (If your environment supports function prototypes, then it
  680.      will be "int yylex( void )".)  This definition may be
  681.      changed by redefining the "YY_DECL" macro.  For example, you
  682.      could use:
  683.  
  684.          #undef YY_DECL
  685.          #define YY_DECL float lexscan( a, b ) float a, b;
  686.  
  687.      to give the scanning routine the name _l_e_x_s_c_a_n, returning a
  688.      float, and taking two floats as arguments.  Note that if you
  689.  
  690.  
  691. Printed 4/3/91             26 May 1990                         11
  692.  
  693.  
  694.  
  695.  
  696. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  697.  
  698.  
  699.      give arguments to the scanning routine using a K&R-
  700.      style/non-prototyped function declaration, you must ter-
  701.      minate the definition with a semi-colon (;).
  702.  
  703.      Whenever yylex() is called, it scans tokens from the global
  704.      input file _y_y_i_n (which defaults to stdin).  It continues
  705.      until it either reaches an end-of-file (at which point it
  706.      returns the value 0) or one of its actions executes a _r_e_t_u_r_n
  707.      statement.  In the former case, when called again the
  708.      scanner will immediately return unless yyrestart() is called
  709.      to point _y_y_i_n at the new input file.  ( yyrestart() takes
  710.      one argument, a FILE * pointer.) In the latter case (i.e.,
  711.      when an action executes a return), the scanner may then be
  712.      called again and it will resume scanning where it left off.
  713.  
  714.      By default (and for purposes of efficiency), the scanner
  715.      uses block-reads rather than simple _g_e_t_c() calls to read
  716.      characters from _y_y_i_n. The nature of how it gets its input
  717.      can be controlled by redefining the YY_INPUT macro.
  718.      YY_INPUT's calling sequence is
  719.      "YY_INPUT(buf,result,max_size)".  Its action is to place up
  720.      to _m_a_x__s_i_z_e characters in the character array _b_u_f and return
  721.      in the integer variable _r_e_s_u_l_t either the number of charac-
  722.      ters read or the constant YY_NULL (0 on Unix systems) to
  723.      indicate EOF.  The default YY_INPUT reads from the global
  724.      file-pointer "yyin".
  725.  
  726.      A sample redefinition of YY_INPUT (in the definitions sec-
  727.      tion of the input file):
  728.  
  729.          %{
  730.          #undef YY_INPUT
  731.          #define YY_INPUT(buf,result,max_size) \
  732.              { \
  733.              int c = getchar(); \
  734.              result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
  735.              }
  736.          %}
  737.  
  738.      This definition will change the input processing to occur
  739.      one character at a time.
  740.  
  741.      You also can add in things like keeping track of the input
  742.      line number this way; but don't expect your scanner to go
  743.      very fast.
  744.  
  745.      When the scanner receives an end-of-file indication from
  746.      YY_INPUT, it then checks the yywrap() function.  If yywrap()
  747.      returns false (zero), then it is assumed that the function
  748.      has gone ahead and set up _y_y_i_n to point to another input
  749.      file, and scanning continues.  If it returns true (non-
  750.      zero), then the scanner terminates, returning 0 to its
  751.      caller.
  752.  
  753.  
  754. Printed 4/3/91             26 May 1990                         12
  755.  
  756.  
  757.  
  758.  
  759. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  760.  
  761.  
  762.      The default yywrap() always returns 1.  Presently, to rede-
  763.      fine it you must first "#undef yywrap", as it is currently
  764.      implemented as a macro.  As indicated by the hedging in the
  765.      previous sentence, it may be changed to a true function in
  766.      the near future.
  767.  
  768.      The scanner writes its ECHO output to the _y_y_o_u_t global
  769.      (default, stdout), which may be redefined by the user simply
  770.      by assigning it to some other FILE pointer.
  771.  
  772. START CONDITIONS
  773.      _f_l_e_x provides a mechanism for conditionally activating
  774.      rules.  Any rule whose pattern is prefixed with "<sc>" will
  775.      only be active when the scanner is in the start condition
  776.      named "sc".  For example,
  777.  
  778.          <STRING>[^"]*        { /* eat up the string body ... */
  779.                      ...
  780.                      }
  781.  
  782.      will be active only when the scanner is in the "STRING"
  783.      start condition, and
  784.  
  785.          <INITIAL,STRING,QUOTE>\.        { /* handle an escape ... */
  786.                      ...
  787.                      }
  788.  
  789.      will be active only when the current start condition is
  790.      either "INITIAL", "STRING", or "QUOTE".
  791.  
  792.      Start conditions are declared in the definitions (first)
  793.      section of the input using unindented lines beginning with
  794.      either %s or %x followed by a list of names.  The former
  795.      declares _i_n_c_l_u_s_i_v_e start conditions, the latter _e_x_c_l_u_s_i_v_e
  796.      start conditions.  A start condition is activated using the
  797.      BEGIN action.  Until the next BEGIN action is executed,
  798.      rules with the given start condition will be active and
  799.      rules with other start conditions will be inactive.  If the
  800.      start condition is _i_n_c_l_u_s_i_v_e, then rules with no start con-
  801.      ditions at all will also be active.  If it is _e_x_c_l_u_s_i_v_e,
  802.      then _o_n_l_y rules qualified with the start condition will be
  803.      active.  A set of rules contingent on the same exclusive
  804.      start condition describe a scanner which is independent of
  805.      any of the other rules in the _f_l_e_x input.  Because of this,
  806.      exclusive start conditions make it easy to specify "mini-
  807.      scanners" which scan portions of the input that are syntac-
  808.      tically different from the rest (e.g., comments).
  809.  
  810.      If the distinction between inclusive and exclusive start
  811.      conditions is still a little vague, here's a simple example
  812.      illustrating the connection between the two.  The set of
  813.      rules:
  814.  
  815.  
  816.  
  817. Printed 4/3/91             26 May 1990                         13
  818.  
  819.  
  820.  
  821.  
  822. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  823.  
  824.  
  825.          %s example
  826.          %%
  827.          <example>foo           /* do something */
  828.  
  829.      is equivalent to
  830.  
  831.          %x example
  832.          %%
  833.          <INITIAL,example>foo   /* do something */
  834.  
  835.  
  836.      The default rule (to ECHO any unmatched character) remains
  837.      active in start conditions.
  838.  
  839.      BEGIN(0) returns to the original state where only the rules
  840.      with no start conditions are active.  This state can also be
  841.      referred to as the start-condition "INITIAL", so
  842.      BEGIN(INITIAL) is equivalent to BEGIN(0). (The parentheses
  843.      around the start condition name are not required but are
  844.      considered good style.)
  845.  
  846.      BEGIN actions can also be given as indented code at the
  847.      beginning of the rules section.  For example, the following
  848.      will cause the scanner to enter the "SPECIAL" start condi-
  849.      tion whenever _y_y_l_e_x() is called and the global variable
  850.      _e_n_t_e_r__s_p_e_c_i_a_l is true:
  851.  
  852.                  int enter_special;
  853.  
  854.          %x SPECIAL
  855.          %%
  856.                  if ( enter_special )
  857.                      BEGIN(SPECIAL);
  858.  
  859.          <SPECIAL>blahblahblah
  860.          ...more rules follow...
  861.  
  862.  
  863.      To illustrate the uses of start conditions, here is a
  864.      scanner which provides two different interpretations of a
  865.      string like "123.456".  By default it will treat it as as
  866.      three tokens, the integer "123", a dot ('.'), and the
  867.      integer "456".  But if the string is preceded earlier in the
  868.      line by the string "expect-floats" it will treat it as a
  869.      single token, the floating-point number 123.456:
  870.  
  871.          %{
  872.          #include <math.h>
  873.          %}
  874.          %s expect
  875.  
  876.          %%
  877.          expect-floats        BEGIN(expect);
  878.  
  879.  
  880. Printed 4/3/91             26 May 1990                         14
  881.  
  882.  
  883.  
  884.  
  885. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  886.  
  887.  
  888.          <expect>[0-9]+"."[0-9]+      {
  889.                      printf( "found a float, = %f\n",
  890.                              atof( yytext ) );
  891.                      }
  892.          <expect>\n           {
  893.                      /* that's the end of the line, so
  894.                       * we need another "expect-number"
  895.                       * before we'll recognize any more
  896.                       * numbers
  897.                       */
  898.                      BEGIN(INITIAL);
  899.                      }
  900.  
  901.          [0-9]+      {
  902.                      printf( "found an integer, = %d\n",
  903.                              atoi( yytext ) );
  904.                      }
  905.  
  906.          "."         printf( "found a dot\n" );
  907.  
  908.      Here is a scanner which recognizes (and discards) C comments
  909.      while maintaining a count of the current input line.
  910.  
  911.          %x comment
  912.          %%
  913.                  int line_num = 1;
  914.  
  915.          "/*"         BEGIN(comment);
  916.  
  917.          <comment>[^*\n]*        /* eat anything that's not a '*' */
  918.          <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
  919.          <comment>\n             ++line_num;
  920.          <comment>"*"+"/"        BEGIN(INITIAL);
  921.  
  922.      Note that start-conditions names are really integer values
  923.      and can be stored as such.  Thus, the above could be
  924.      extended in the following fashion:
  925.  
  926.          %x comment foo
  927.          %%
  928.                  int line_num = 1;
  929.                  int comment_caller;
  930.  
  931.          "/*"         {
  932.                       comment_caller = INITIAL;
  933.                       BEGIN(comment);
  934.                       }
  935.  
  936.          ...
  937.  
  938.          <foo>"/*"    {
  939.                       comment_caller = foo;
  940.                       BEGIN(comment);
  941.  
  942.  
  943. Printed 4/3/91             26 May 1990                         15
  944.  
  945.  
  946.  
  947.  
  948. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  949.  
  950.  
  951.                       }
  952.  
  953.          <comment>[^*\n]*        /* eat anything that's not a '*' */
  954.          <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
  955.          <comment>\n             ++line_num;
  956.          <comment>"*"+"/"        BEGIN(comment_caller);
  957.  
  958.      One can then implement a "stack" of start conditions using
  959.      an array of integers.  (It is likely that such stacks will
  960.      become a full-fledged _f_l_e_x feature in the future.)  Note,
  961.      though, that start conditions do not have their own name-
  962.      space; %s's and %x's declare names in the same fashion as
  963.      #define's.
  964.  
  965. MULTIPLE INPUT BUFFERS
  966.      Some scanners (such as those which support "include" files)
  967.      require reading from several input streams.  As _f_l_e_x
  968.      scanners do a large amount of buffering, one cannot control
  969.      where the next input will be read from by simply writing a
  970.      YY_INPUT which is sensitive to the scanning context.
  971.      YY_INPUT is only called when the scanner reaches the end of
  972.      its buffer, which may be a long time after scanning a state-
  973.      ment such as an "include" which requires switching the input
  974.      source.
  975.  
  976.      To negotiate these sorts of problems, _f_l_e_x provides a
  977.      mechanism for creating and switching between multiple input
  978.      buffers.  An input buffer is created by using:
  979.  
  980.          YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )
  981.  
  982.      which takes a _F_I_L_E pointer and a size and creates a buffer
  983.      associated with the given file and large enough to hold _s_i_z_e
  984.      characters (when in doubt, use YY_BUF_SIZE for the size).
  985.      It returns a YY_BUFFER_STATE handle, which may then be
  986.      passed to other routines:
  987.  
  988.          void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
  989.  
  990.      switches the scanner's input buffer so subsequent tokens
  991.      will come from _n_e_w__b_u_f_f_e_r. Note that yy_switch_to_buffer()
  992.      may be used by yywrap() to sets things up for continued
  993.      scanning, instead of opening a new file and pointing _y_y_i_n at
  994.      it.
  995.  
  996.          void yy_delete_buffer( YY_BUFFER_STATE buffer )
  997.  
  998.      is used to reclaim the storage associated with a buffer.
  999.  
  1000.      yy_new_buffer() is an alias for yy_create_buffer(), provided
  1001.      for compatibility with the C++ use of _n_e_w and _d_e_l_e_t_e for
  1002.      creating and destroying dynamic objects.
  1003.  
  1004.  
  1005.  
  1006. Printed 4/3/91             26 May 1990                         16
  1007.  
  1008.  
  1009.  
  1010.  
  1011. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1012.  
  1013.  
  1014.      Finally, the YY_CURRENT_BUFFER macro returns a
  1015.      YY_BUFFER_STATE handle to the current buffer.
  1016.  
  1017.      Here is an example of using these features for writing a
  1018.      scanner which expands include files (the <<EOF>> feature is
  1019.      discussed below):
  1020.  
  1021.          /* the "incl" state is used for picking up the name
  1022.           * of an include file
  1023.           */
  1024.          %x incl
  1025.  
  1026.          %{
  1027.          #define MAX_INCLUDE_DEPTH 10
  1028.          YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  1029.          int include_stack_ptr = 0;
  1030.          %}
  1031.  
  1032.          %%
  1033.          include             BEGIN(incl);
  1034.  
  1035.          [a-z]+              ECHO;
  1036.          [^a-z\n]*\n?        ECHO;
  1037.  
  1038.          <incl>[ \t]*      /* eat the whitespace */
  1039.          <incl>[^ \t\n]+   { /* got the include file name */
  1040.                  if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
  1041.                      {
  1042.                      fprintf( stderr, "Includes nested too deeply" );
  1043.                      exit( 1 );
  1044.                      }
  1045.  
  1046.                  include_stack[include_stack_ptr++] =
  1047.                      YY_CURRENT_BUFFER;
  1048.  
  1049.                  yyin = fopen( yytext, "r" );
  1050.  
  1051.                  if ( ! yyin )
  1052.                      error( ... );
  1053.  
  1054.                  yy_switch_to_buffer(
  1055.                      yy_create_buffer( yyin, YY_BUF_SIZE ) );
  1056.  
  1057.                  BEGIN(INITIAL);
  1058.                  }
  1059.  
  1060.          <<EOF>> {
  1061.                  if ( --include_stack_ptr < 0 )
  1062.                      {
  1063.                      yyterminate();
  1064.                      }
  1065.  
  1066.                  else
  1067.  
  1068.  
  1069. Printed 4/3/91             26 May 1990                         17
  1070.  
  1071.  
  1072.  
  1073.  
  1074. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1075.  
  1076.  
  1077.                      yy_switch_to_buffer(
  1078.                           include_stack[include_stack_ptr] );
  1079.                  }
  1080.  
  1081.  
  1082. END-OF-FILE RULES
  1083.      The special rule "<<EOF>>" indicates actions which are to be
  1084.      taken when an end-of-file is encountered and yywrap()
  1085.      returns non-zero (i.e., indicates no further files to pro-
  1086.      cess).  The action must finish by doing one of four things:
  1087.  
  1088.      -    the special YY_NEW_FILE action, if _y_y_i_n has been
  1089.           pointed at a new file to process;
  1090.  
  1091.      -    a _r_e_t_u_r_n statement;
  1092.  
  1093.      -    the special yyterminate() action;
  1094.  
  1095.      -    or, switching to a new buffer using
  1096.           yy_switch_to_buffer() as shown in the example above.
  1097.  
  1098.      <<EOF>> rules may not be used with other patterns; they may
  1099.      only be qualified with a list of start conditions.  If an
  1100.      unqualified <<EOF>> rule is given, it applies to _a_l_l start
  1101.      conditions which do not already have <<EOF>> actions.  To
  1102.      specify an <<EOF>> rule for only the initial start condi-
  1103.      tion, use
  1104.  
  1105.          <INITIAL><<EOF>>
  1106.  
  1107.  
  1108.      These rules are useful for catching things like unclosed
  1109.      comments.  An example:
  1110.  
  1111.          %x quote
  1112.          %%
  1113.  
  1114.          ...other rules for dealing with quotes...
  1115.  
  1116.          <quote><<EOF>>   {
  1117.                   error( "unterminated quote" );
  1118.                   yyterminate();
  1119.                   }
  1120.          <<EOF>>  {
  1121.                   if ( *++filelist )
  1122.                       {
  1123.                       yyin = fopen( *filelist, "r" );
  1124.                       YY_NEW_FILE;
  1125.                       }
  1126.                   else
  1127.                      yyterminate();
  1128.                   }
  1129.  
  1130.  
  1131.  
  1132. Printed 4/3/91             26 May 1990                         18
  1133.  
  1134.  
  1135.  
  1136.  
  1137. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1138.  
  1139.  
  1140. MISCELLANEOUS MACROS
  1141.      The macro YY_USER_ACTION can be redefined to provide an
  1142.      action which is always executed prior to the matched rule's
  1143.      action.  For example, it could be #define'd to call a rou-
  1144.      tine to convert yytext to lower-case.
  1145.  
  1146.      The macro YY_USER_INIT may be redefined to provide an action
  1147.      which is always executed before the first scan (and before
  1148.      the scanner's internal initializations are done).  For exam-
  1149.      ple, it could be used to call a routine to read in a data
  1150.      table or open a logging file.
  1151.  
  1152.      In the generated scanner, the actions are all gathered in
  1153.      one large switch statement and separated using YY_BREAK,
  1154.      which may be redefined.  By default, it is simply a "break",
  1155.      to separate each rule's action from the following rule's.
  1156.      Redefining YY_BREAK allows, for example, C++ users to
  1157.      #define YY_BREAK to do nothing (while being very careful
  1158.      that every rule ends with a "break" or a "return"!) to avoid
  1159.      suffering from unreachable statement warnings where because
  1160.      a rule's action ends with "return", the YY_BREAK is inacces-
  1161.      sible.
  1162.  
  1163. INTERFACING WITH YACC
  1164.      One of the main uses of _f_l_e_x is as a companion to the _y_a_c_c
  1165.      parser-generator.  _y_a_c_c parsers expect to call a routine
  1166.      named yylex() to find the next input token.  The routine is
  1167.      supposed to return the type of the next token as well as
  1168.      putting any associated value in the global yylval. To use
  1169.      _f_l_e_x with _y_a_c_c, one specifies the -d option to _y_a_c_c to
  1170.      instruct it to generate the file y.tab.h containing defini-
  1171.      tions of all the %tokens appearing in the _y_a_c_c input.  This
  1172.      file is then included in the _f_l_e_x scanner.  For example, if
  1173.      one of the tokens is "TOK_NUMBER", part of the scanner might
  1174.      look like:
  1175.  
  1176.          %{
  1177.          #include "y.tab.h"
  1178.          %}
  1179.  
  1180.          %%
  1181.  
  1182.          [0-9]+        yylval = atoi( yytext ); return TOK_NUMBER;
  1183.  
  1184.  
  1185. TRANSLATION TABLE
  1186.      In the name of POSIX compliance, _f_l_e_x supports a _t_r_a_n_s_l_a_t_i_o_n
  1187.      _t_a_b_l_e for mapping input characters into groups.  The table
  1188.      is specified in the first section, and its format looks
  1189.      like:
  1190.  
  1191.          %t
  1192.          1        abcd
  1193.  
  1194.  
  1195. Printed 4/3/91             26 May 1990                         19
  1196.  
  1197.  
  1198.  
  1199.  
  1200. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1201.  
  1202.  
  1203.          2        ABCDEFGHIJKLMNOPQRSTUVWXYZ
  1204.          52       0123456789
  1205.          6        \t\ \n
  1206.          %t
  1207.  
  1208.      This example specifies that the characters 'a', 'b', 'c',
  1209.      and 'd' are to all be lumped into group #1, upper-case
  1210.      letters in group #2, digits in group #52, tabs, blanks, and
  1211.      newlines into group #6, and _n_o _o_t_h_e_r _c_h_a_r_a_c_t_e_r_s _w_i_l_l _a_p_p_e_a_r
  1212.      _i_n _t_h_e _p_a_t_t_e_r_n_s.  The group numbers are actually disregarded
  1213.      by _f_l_e_x; %t serves, though, to lump characters together.
  1214.      Given the above table, for example, the pattern "a(AA)*5" is
  1215.      equivalent to "d(ZQ)*0".  They both say, "match any charac-
  1216.      ter in group #1, followed by zero-or-more pairs of charac-
  1217.      ters from group #2, followed by a character from group #52."
  1218.      Thus %t provides a crude way for introducing equivalence
  1219.      classes into the scanner specification.
  1220.  
  1221.      Note that the -i option (see below) coupled with the
  1222.      equivalence classes which _f_l_e_x automatically generates take
  1223.      care of virtually all the instances when one might consider
  1224.      using %t. But what the hell, it's there if you want it.
  1225.  
  1226. OPTIONS
  1227.      _f_l_e_x has the following options:
  1228.  
  1229.      -b   Generate backtracking information to _l_e_x._b_a_c_k_t_r_a_c_k.
  1230.           This is a list of scanner states which require back-
  1231.           tracking and the input characters on which they do so.
  1232.           By adding rules one can remove backtracking states.  If
  1233.           all backtracking states are eliminated and -f or -F is
  1234.           used, the generated scanner will run faster (see the -p
  1235.           flag).  Only users who wish to squeeze every last cycle
  1236.           out of their scanners need worry about this option.
  1237.           (See the section on PERFORMANCE CONSIDERATIONS below.)
  1238.  
  1239.      -c   is a do-nothing, deprecated option included for POSIX
  1240.           compliance.
  1241.  
  1242.           NOTE: in previous releases of _f_l_e_x -c specified table-
  1243.           compression options.  This functionality is now given
  1244.           by the -C flag.  To ease the the impact of this change,
  1245.           when _f_l_e_x encounters -c, it currently issues a warning
  1246.           message and assumes that -C was desired instead.  In
  1247.           the future this "promotion" of -c to -C will go away in
  1248.           the name of full POSIX compliance (unless the POSIX
  1249.           meaning is removed first).
  1250.  
  1251.      -d   makes the generated scanner run in _d_e_b_u_g mode.  When-
  1252.           ever a pattern is recognized and the global
  1253.           yy_flex_debug is non-zero (which is the default), the
  1254.           scanner will write to _s_t_d_e_r_r a line of the form:
  1255.  
  1256.  
  1257.  
  1258. Printed 4/3/91             26 May 1990                         20
  1259.  
  1260.  
  1261.  
  1262.  
  1263. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1264.  
  1265.  
  1266.               --accepting rule at line 53 ("the matched text")
  1267.  
  1268.           The line number refers to the location of the rule in
  1269.           the file defining the scanner (i.e., the file that was
  1270.           fed to flex).  Messages are also generated when the
  1271.           scanner backtracks, accepts the default rule, reaches
  1272.           the end of its input buffer (or encounters a NUL; at
  1273.           this point, the two look the same as far as the
  1274.           scanner's concerned), or reaches an end-of-file.
  1275.  
  1276.      -f   specifies (take your pick) _f_u_l_l _t_a_b_l_e or _f_a_s_t _s_c_a_n_n_e_r.
  1277.           No table compression is done.  The result is large but
  1278.           fast.  This option is equivalent to -Cf (see below).
  1279.  
  1280.      -i   instructs _f_l_e_x to generate a _c_a_s_e-_i_n_s_e_n_s_i_t_i_v_e scanner.
  1281.           The case of letters given in the _f_l_e_x input patterns
  1282.           will be ignored, and tokens in the input will be
  1283.           matched regardless of case.  The matched text given in
  1284.           _y_y_t_e_x_t will have the preserved case (i.e., it will not
  1285.           be folded).
  1286.  
  1287.      -n   is another do-nothing, deprecated option included only
  1288.           for POSIX compliance.
  1289.  
  1290.      -p   generates a performance report to stderr.  The report
  1291.           consists of comments regarding features of the _f_l_e_x
  1292.           input file which will cause a loss of performance in
  1293.           the resulting scanner.  Note that the use of _R_E_J_E_C_T and
  1294.           variable trailing context (see the BUGS section in
  1295.           flex(1)) entails a substantial performance penalty; use
  1296.           of _y_y_m_o_r_e(), the ^ operator, and the -I flag entail
  1297.           minor performance penalties.
  1298.  
  1299.      -s   causes the _d_e_f_a_u_l_t _r_u_l_e (that unmatched scanner input
  1300.           is echoed to _s_t_d_o_u_t) to be suppressed.  If the scanner
  1301.           encounters input that does not match any of its rules,
  1302.           it aborts with an error.  This option is useful for
  1303.           finding holes in a scanner's rule set.
  1304.  
  1305.      -t   instructs _f_l_e_x to write the scanner it generates to
  1306.           standard output instead of lex.yy.c.
  1307.  
  1308.      -v   specifies that _f_l_e_x should write to _s_t_d_e_r_r a summary of
  1309.           statistics regarding the scanner it generates.  Most of
  1310.           the statistics are meaningless to the casual _f_l_e_x user,
  1311.           but the first line identifies the version of _f_l_e_x,
  1312.           which is useful for figuring out where you stand with
  1313.           respect to patches and new releases, and the next two
  1314.           lines give the date when the scanner was created and a
  1315.           summary of the flags which were in effect.
  1316.  
  1317.      -F   specifies that the _f_a_s_t scanner table representation
  1318.           should be used.  This representation is about as fast
  1319.  
  1320.  
  1321. Printed 4/3/91             26 May 1990                         21
  1322.  
  1323.  
  1324.  
  1325.  
  1326. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1327.  
  1328.  
  1329.           as the full table representation (-_f), and for some
  1330.           sets of patterns will be considerably smaller (and for
  1331.           others, larger).  In general, if the pattern set con-
  1332.           tains both "keywords" and a catch-all, "identifier"
  1333.           rule, such as in the set:
  1334.  
  1335.               "case"    return TOK_CASE;
  1336.               "switch"  return TOK_SWITCH;
  1337.               ...
  1338.               "default" return TOK_DEFAULT;
  1339.               [a-z]+    return TOK_ID;
  1340.  
  1341.           then you're better off using the full table representa-
  1342.           tion.  If only the "identifier" rule is present and you
  1343.           then use a hash table or some such to detect the key-
  1344.           words, you're better off using -_F.
  1345.  
  1346.           This option is equivalent to -CF (see below).
  1347.  
  1348.      -I   instructs _f_l_e_x to generate an _i_n_t_e_r_a_c_t_i_v_e scanner.
  1349.           Normally, scanners generated by _f_l_e_x always look ahead
  1350.           one character before deciding that a rule has been
  1351.           matched.  At the cost of some scanning overhead, _f_l_e_x
  1352.           will generate a scanner which only looks ahead when
  1353.           needed.  Such scanners are called _i_n_t_e_r_a_c_t_i_v_e because
  1354.           if you want to write a scanner for an interactive sys-
  1355.           tem such as a command shell, you will probably want the
  1356.           user's input to be terminated with a newline, and
  1357.           without -I the user will have to type a character in
  1358.           addition to the newline in order to have the newline
  1359.           recognized.  This leads to dreadful interactive perfor-
  1360.           mance.
  1361.  
  1362.           If all this seems to confusing, here's the general
  1363.           rule: if a human will be typing in input to your
  1364.           scanner, use -I, otherwise don't; if you don't care
  1365.           about squeezing the utmost performance from your
  1366.           scanner and you don't want to make any assumptions
  1367.           about the input to your scanner, use -I.
  1368.  
  1369.           Note, -I cannot be used in conjunction with _f_u_l_l or
  1370.           _f_a_s_t _t_a_b_l_e_s, i.e., the -f, -F, -Cf, or -CF flags.
  1371.  
  1372.      -L   instructs _f_l_e_x not to generate #line directives.
  1373.           Without this option, _f_l_e_x peppers the generated scanner
  1374.           with #line directives so error messages in the actions
  1375.           will be correctly located with respect to the original
  1376.           _f_l_e_x input file, and not to the fairly meaningless line
  1377.           numbers of lex.yy.c. (Unfortunately _f_l_e_x does not
  1378.           presently generate the necessary directives to "retar-
  1379.           get" the line numbers for those parts of lex.yy.c which
  1380.           it generated.  So if there is an error in the generated
  1381.           code, a meaningless line number is reported.)
  1382.  
  1383.  
  1384. Printed 4/3/91             26 May 1990                         22
  1385.  
  1386.  
  1387.  
  1388.  
  1389. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1390.  
  1391.  
  1392.      -T   makes _f_l_e_x run in _t_r_a_c_e mode.  It will generate a lot
  1393.           of messages to _s_t_d_o_u_t concerning the form of the input
  1394.           and the resultant non-deterministic and deterministic
  1395.           finite automata.  This option is mostly for use in
  1396.           maintaining _f_l_e_x.
  1397.  
  1398.      -8   instructs _f_l_e_x to generate an 8-bit scanner, i.e., one
  1399.           which can recognize 8-bit characters.  On some sites,
  1400.           _f_l_e_x is installed with this option as the default.  On
  1401.           others, the default is 7-bit characters.  To see which
  1402.           is the case, check the verbose (-v) output for
  1403.           "equivalence classes created".  If the denominator of
  1404.           the number shown is 128, then by default _f_l_e_x is gen-
  1405.           erating 7-bit characters.  If it is 256, then the
  1406.           default is 8-bit characters and the -8 flag is not
  1407.           required (but may be a good idea to keep the scanner
  1408.           specification portable).  Feeding a 7-bit scanner 8-bit
  1409.           characters will result in infinite loops, bus errors,
  1410.           or other such fireworks, so when in doubt, use the
  1411.           flag.  Note that if equivalence classes are used, 8-bit
  1412.           scanners take only slightly more table space than 7-bit
  1413.           scanners (128 bytes, to be exact); if equivalence
  1414.           classes are not used, however, then the tables may grow
  1415.           up to twice their 7-bit size.
  1416.  
  1417.      -C[efmF]
  1418.           controls the degree of table compression.
  1419.  
  1420.           -Ce directs _f_l_e_x to construct _e_q_u_i_v_a_l_e_n_c_e _c_l_a_s_s_e_s,
  1421.           i.e., sets of characters which have identical lexical
  1422.           properties (for example, if the only appearance of
  1423.           digits in the _f_l_e_x input is in the character class
  1424.           "[0-9]" then the digits '0', '1', ..., '9' will all be
  1425.           put in the same equivalence class).  Equivalence
  1426.           classes usually give dramatic reductions in the final
  1427.           table/object file sizes (typically a factor of 2-5) and
  1428.           are pretty cheap performance-wise (one array look-up
  1429.           per character scanned).
  1430.  
  1431.           -Cf specifies that the _f_u_l_l scanner tables should be
  1432.           generated - _f_l_e_x should not compress the tables by tak-
  1433.           ing advantages of similar transition functions for dif-
  1434.           ferent states.
  1435.  
  1436.           -CF specifies that the alternate fast scanner represen-
  1437.           tation (described above under the -F flag) should be
  1438.           used.
  1439.  
  1440.           -Cm directs _f_l_e_x to construct _m_e_t_a-_e_q_u_i_v_a_l_e_n_c_e _c_l_a_s_s_e_s,
  1441.           which are sets of equivalence classes (or characters,
  1442.           if equivalence classes are not being used) that are
  1443.           commonly used together.  Meta-equivalence classes are
  1444.           often a big win when using compressed tables, but they
  1445.  
  1446.  
  1447. Printed 4/3/91             26 May 1990                         23
  1448.  
  1449.  
  1450.  
  1451.  
  1452. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1453.  
  1454.  
  1455.           have a moderate performance impact (one or two "if"
  1456.           tests and one array look-up per character scanned).
  1457.  
  1458.           A lone -C specifies that the scanner tables should be
  1459.           compressed but neither equivalence classes nor meta-
  1460.           equivalence classes should be used.
  1461.  
  1462.           The options -Cf or -CF and -Cm do not make sense
  1463.           together - there is no opportunity for meta-equivalence
  1464.           classes if the table is not being compressed.  Other-
  1465.           wise the options may be freely mixed.
  1466.  
  1467.           The default setting is -Cem, which specifies that _f_l_e_x
  1468.           should generate equivalence classes and meta-
  1469.           equivalence classes.  This setting provides the highest
  1470.           degree of table compression.  You can trade off
  1471.           faster-executing scanners at the cost of larger tables
  1472.           with the following generally being true:
  1473.  
  1474.               slowest & smallest
  1475.                     -Cem
  1476.                     -Cm
  1477.                     -Ce
  1478.                     -C
  1479.                     -C{f,F}e
  1480.                     -C{f,F}
  1481.               fastest & largest
  1482.  
  1483.           Note that scanners with the smallest tables are usually
  1484.           generated and compiled the quickest, so during develop-
  1485.           ment you will usually want to use the default, maximal
  1486.           compression.
  1487.  
  1488.           -Cfe is often a good compromise between speed and size
  1489.           for production scanners.
  1490.  
  1491.           -C options are not cumulative; whenever the flag is
  1492.           encountered, the previous -C settings are forgotten.
  1493.  
  1494.      -Sskeleton_file
  1495.           overrides the default skeleton file from which _f_l_e_x
  1496.           constructs its scanners.  You'll never need this option
  1497.           unless you are doing _f_l_e_x maintenance or development.
  1498.  
  1499. PERFORMANCE CONSIDERATIONS
  1500.      The main design goal of _f_l_e_x is that it generate high-
  1501.      performance scanners.  It has been optimized for dealing
  1502.      well with large sets of rules.  Aside from the effects of
  1503.      table compression on scanner speed outlined above, there are
  1504.      a number of options/actions which degrade performance.
  1505.      These are, from most expensive to least:
  1506.  
  1507.          REJECT
  1508.  
  1509.  
  1510. Printed 4/3/91             26 May 1990                         24
  1511.  
  1512.  
  1513.  
  1514.  
  1515. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1516.  
  1517.  
  1518.          pattern sets that require backtracking
  1519.          arbitrary trailing context
  1520.  
  1521.          '^' beginning-of-line operator
  1522.          yymore()
  1523.  
  1524.      with the first three all being quite expensive and the last
  1525.      two being quite cheap.
  1526.  
  1527.      REJECT should be avoided at all costs when performance is
  1528.      important.  It is a particularly expensive option.
  1529.  
  1530.      Getting rid of backtracking is messy and often may be an
  1531.      enormous amount of work for a complicated scanner.  In prin-
  1532.      cipal, one begins by using the -b flag to generate a
  1533.      _l_e_x._b_a_c_k_t_r_a_c_k file.  For example, on the input
  1534.  
  1535.          %%
  1536.          foo        return TOK_KEYWORD;
  1537.          foobar     return TOK_KEYWORD;
  1538.  
  1539.      the file looks like:
  1540.  
  1541.          State #6 is non-accepting -
  1542.           associated rule line numbers:
  1543.                 2       3
  1544.           out-transitions: [ o ]
  1545.           jam-transitions: EOF [ \001-n  p-\177 ]
  1546.  
  1547.          State #8 is non-accepting -
  1548.           associated rule line numbers:
  1549.                 3
  1550.           out-transitions: [ a ]
  1551.           jam-transitions: EOF [ \001-`  b-\177 ]
  1552.  
  1553.          State #9 is non-accepting -
  1554.           associated rule line numbers:
  1555.                 3
  1556.           out-transitions: [ r ]
  1557.           jam-transitions: EOF [ \001-q  s-\177 ]
  1558.  
  1559.          Compressed tables always backtrack.
  1560.  
  1561.      The first few lines tell us that there's a scanner state in
  1562.      which it can make a transition on an 'o' but not on any
  1563.      other character, and that in that state the currently
  1564.      scanned text does not match any rule.  The state occurs when
  1565.      trying to match the rules found at lines 2 and 3 in the
  1566.      input file.  If the scanner is in that state and then reads
  1567.      something other than an 'o', it will have to backtrack to
  1568.      find a rule which is matched.  With a bit of headscratching
  1569.      one can see that this must be the state it's in when it has
  1570.      seen "fo".  When this has happened, if anything other than
  1571.  
  1572.  
  1573. Printed 4/3/91             26 May 1990                         25
  1574.  
  1575.  
  1576.  
  1577.  
  1578. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1579.  
  1580.  
  1581.      another 'o' is seen, the scanner will have to back up to
  1582.      simply match the 'f' (by the default rule).
  1583.  
  1584.      The comment regarding State #8 indicates there's a problem
  1585.      when "foob" has been scanned.  Indeed, on any character
  1586.      other than a 'b', the scanner will have to back up to accept
  1587.      "foo".  Similarly, the comment for State #9 concerns when
  1588.      "fooba" has been scanned.
  1589.  
  1590.      The final comment reminds us that there's no point going to
  1591.      all the trouble of removing backtracking from the rules
  1592.      unless we're using -f or -F, since there's no performance
  1593.      gain doing so with compressed scanners.
  1594.  
  1595.      The way to remove the backtracking is to add "error" rules:
  1596.  
  1597.          %%
  1598.          foo         return TOK_KEYWORD;
  1599.          foobar      return TOK_KEYWORD;
  1600.  
  1601.          fooba       |
  1602.          foob        |
  1603.          fo          {
  1604.                      /* false alarm, not really a keyword */
  1605.                      return TOK_ID;
  1606.                      }
  1607.  
  1608.  
  1609.      Eliminating backtracking among a list of keywords can also
  1610.      be done using a "catch-all" rule:
  1611.  
  1612.          %%
  1613.          foo         return TOK_KEYWORD;
  1614.          foobar      return TOK_KEYWORD;
  1615.  
  1616.          [a-z]+      return TOK_ID;
  1617.  
  1618.      This is usually the best solution when appropriate.
  1619.  
  1620.      Backtracking messages tend to cascade.  With a complicated
  1621.      set of rules it's not uncommon to get hundreds of messages.
  1622.      If one can decipher them, though, it often only takes a
  1623.      dozen or so rules to eliminate the backtracking (though it's
  1624.      easy to make a mistake and have an error rule accidentally
  1625.      match a valid token.  A possible future _f_l_e_x feature will be
  1626.      to automatically add rules to eliminate backtracking).
  1627.  
  1628.      _V_a_r_i_a_b_l_e trailing context (where both the leading and trail-
  1629.      ing parts do not have a fixed length) entails almost the
  1630.      same performance loss as _R_E_J_E_C_T (i.e., substantial).  So
  1631.      when possible a rule like:
  1632.  
  1633.          %%
  1634.  
  1635.  
  1636. Printed 4/3/91             26 May 1990                         26
  1637.  
  1638.  
  1639.  
  1640.  
  1641. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1642.  
  1643.  
  1644.          mouse|rat/(cat|dog)   run();
  1645.  
  1646.      is better written:
  1647.  
  1648.          %%
  1649.          mouse/cat|dog         run();
  1650.          rat/cat|dog           run();
  1651.  
  1652.      or as
  1653.  
  1654.          %%
  1655.          mouse|rat/cat         run();
  1656.          mouse|rat/dog         run();
  1657.  
  1658.      Note that here the special '|' action does _n_o_t provide any
  1659.      savings, and can even make things worse (see BUGS in
  1660.      flex(1)).
  1661.  
  1662.      Another area where the user can increase a scanner's perfor-
  1663.      mance (and one that's easier to implement) arises from the
  1664.      fact that the longer the tokens matched, the faster the
  1665.      scanner will run.  This is because with long tokens the pro-
  1666.      cessing of most input characters takes place in the (short)
  1667.      inner scanning loop, and does not often have to go through
  1668.      the additional work of setting up the scanning environment
  1669.      (e.g., yytext) for the action.  Recall the scanner for C
  1670.      comments:
  1671.  
  1672.          %x comment
  1673.          %%
  1674.                  int line_num = 1;
  1675.  
  1676.          "/*"         BEGIN(comment);
  1677.  
  1678.          <comment>[^*\n]*
  1679.          <comment>"*"+[^*/\n]*
  1680.          <comment>\n             ++line_num;
  1681.          <comment>"*"+"/"        BEGIN(INITIAL);
  1682.  
  1683.      This could be sped up by writing it as:
  1684.  
  1685.          %x comment
  1686.          %%
  1687.                  int line_num = 1;
  1688.  
  1689.          "/*"         BEGIN(comment);
  1690.  
  1691.          <comment>[^*\n]*
  1692.          <comment>[^*\n]*\n      ++line_num;
  1693.          <comment>"*"+[^*/\n]*
  1694.          <comment>"*"+[^*/\n]*\n ++line_num;
  1695.          <comment>"*"+"/"        BEGIN(INITIAL);
  1696.  
  1697.  
  1698.  
  1699. Printed 4/3/91             26 May 1990                         27
  1700.  
  1701.  
  1702.  
  1703.  
  1704. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1705.  
  1706.  
  1707.      Now instead of each newline requiring the processing of
  1708.      another action, recognizing the newlines is "distributed"
  1709.      over the other rules to keep the matched text as long as
  1710.      possible.  Note that _a_d_d_i_n_g rules does _n_o_t slow down the
  1711.      scanner!  The speed of the scanner is independent of the
  1712.      number of rules or (modulo the considerations given at the
  1713.      beginning of this section) how complicated the rules are
  1714.      with regard to operators such as '*' and '|'.
  1715.  
  1716.      A final example in speeding up a scanner: suppose you want
  1717.      to scan through a file containing identifiers and keywords,
  1718.      one per line and with no other extraneous characters, and
  1719.      recognize all the keywords.  A natural first approach is:
  1720.  
  1721.          %%
  1722.          asm      |
  1723.          auto     |
  1724.          break    |
  1725.          ... etc ...
  1726.          volatile |
  1727.          while    /* it's a keyword */
  1728.  
  1729.          .|\n     /* it's not a keyword */
  1730.  
  1731.      To eliminate the back-tracking, introduce a catch-all rule:
  1732.  
  1733.          %%
  1734.          asm      |
  1735.          auto     |
  1736.          break    |
  1737.          ... etc ...
  1738.          volatile |
  1739.          while    /* it's a keyword */
  1740.  
  1741.          [a-z]+   |
  1742.          .|\n     /* it's not a keyword */
  1743.  
  1744.      Now, if it's guaranteed that there's exactly one word per
  1745.      line, then we can reduce the total number of matches by a
  1746.      half by merging in the recognition of newlines with that of
  1747.      the other tokens:
  1748.  
  1749.          %%
  1750.          asm\n    |
  1751.          auto\n   |
  1752.          break\n  |
  1753.          ... etc ...
  1754.          volatile\n |
  1755.          while\n  /* it's a keyword */
  1756.  
  1757.          [a-z]+\n |
  1758.          .|\n     /* it's not a keyword */
  1759.  
  1760.  
  1761.  
  1762. Printed 4/3/91             26 May 1990                         28
  1763.  
  1764.  
  1765.  
  1766.  
  1767. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1768.  
  1769.  
  1770.      One has to be careful here, as we have now reintroduced
  1771.      backtracking into the scanner.  In particular, while _w_e know
  1772.      that there will never be any characters in the input stream
  1773.      other than letters or newlines, _f_l_e_x can't figure this out,
  1774.      and it will plan for possibly needing backtracking when it
  1775.      has scanned a token like "auto" and then the next character
  1776.      is something other than a newline or a letter.  Previously
  1777.      it would then just match the "auto" rule and be done, but
  1778.      now it has no "auto" rule, only a "auto\n" rule.  To elim-
  1779.      inate the possibility of backtracking, we could either
  1780.      duplicate all rules but without final newlines, or, since we
  1781.      never expect to encounter such an input and therefore don't
  1782.      how it's classified, we can introduce one more catch-all
  1783.      rule, this one which doesn't include a newline:
  1784.  
  1785.          %%
  1786.          asm\n    |
  1787.          auto\n   |
  1788.          break\n  |
  1789.          ... etc ...
  1790.          volatile\n |
  1791.          while\n  /* it's a keyword */
  1792.  
  1793.          [a-z]+\n |
  1794.          [a-z]+   |
  1795.          .|\n     /* it's not a keyword */
  1796.  
  1797.      Compiled with -Cf, this is about as fast as one can get a
  1798.      _f_l_e_x scanner to go for this particular problem.
  1799.  
  1800.      A final note: _f_l_e_x is slow when matching NUL's, particularly
  1801.      when a token contains multiple NUL's.  It's best to write
  1802.      rules which match _s_h_o_r_t amounts of text if it's anticipated
  1803.      that the text will often include NUL's.
  1804.  
  1805. INCOMPATIBILITIES WITH LEX AND POSIX
  1806.      _f_l_e_x is a rewrite of the Unix _l_e_x tool (the two implementa-
  1807.      tions do not share any code, though), with some extensions
  1808.      and incompatibilities, both of which are of concern to those
  1809.      who wish to write scanners acceptable to either implementa-
  1810.      tion.  At present, the POSIX _l_e_x draft is very close to the
  1811.      original _l_e_x implementation, so some of these incompatibili-
  1812.      ties are also in conflict with the POSIX draft.  But the
  1813.      intent is that except as noted below, _f_l_e_x as it presently
  1814.      stands will ultimately be POSIX conformant (i.e., that those
  1815.      areas of conflict with the POSIX draft will be resolved in
  1816.      _f_l_e_x'_s favor).  Please bear in mind that all the comments
  1817.      which follow are with regard to the POSIX _d_r_a_f_t standard of
  1818.      Summer 1989, and not the final document (or subsequent
  1819.      drafts); they are included so _f_l_e_x users can be aware of the
  1820.      standardization issues and those areas where _f_l_e_x may in the
  1821.      near future undergo changes incompatible with its current
  1822.      definition.
  1823.  
  1824.  
  1825. Printed 4/3/91             26 May 1990                         29
  1826.  
  1827.  
  1828.  
  1829.  
  1830. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1831.  
  1832.  
  1833.      _f_l_e_x is fully compatible with _l_e_x with the following excep-
  1834.      tions:
  1835.  
  1836.      -    The undocumented _l_e_x scanner internal variable yylineno
  1837.           is not supported.  It is difficult to support this
  1838.           option efficiently, since it requires examining every
  1839.           character scanned and reexamining the characters when
  1840.           the scanner backs up.  Things get more complicated when
  1841.           the end of buffer or file is reached or a NUL is
  1842.           scanned (since the scan must then be restarted with the
  1843.           proper line number count), or the user uses the
  1844.           yyless(), unput(), or REJECT actions, or the multiple
  1845.           input buffer functions.
  1846.  
  1847.           The fix is to add rules which, upon seeing a newline,
  1848.           increment yylineno.  This is usually an easy process,
  1849.           though it can be a drag if some of the patterns can
  1850.           match multiple newlines along with other characters.
  1851.  
  1852.           yylineno is not part of the POSIX draft.
  1853.  
  1854.      -    The input() routine is not redefinable, though it may
  1855.           be called to read characters following whatever has
  1856.           been matched by a rule.  If input() encounters an end-
  1857.           of-file the normal yywrap() processing is done.  A
  1858.           ``real'' end-of-file is returned by input() as _E_O_F.
  1859.  
  1860.           Input is instead controlled by redefining the YY_INPUT
  1861.           macro.
  1862.  
  1863.           The _f_l_e_x restriction that input() cannot be redefined
  1864.           is in accordance with the POSIX draft, but YY_INPUT has
  1865.           not yet been accepted into the draft (and probably
  1866.           won't; it looks like the draft will simply not specify
  1867.           any way of controlling the scanner's input other than
  1868.           by making an initial assignment to _y_y_i_n).
  1869.  
  1870.      -    _f_l_e_x scanners do not use stdio for input.  Because of
  1871.           this, when writing an interactive scanner one must
  1872.           explicitly call fflush() on the stream associated with
  1873.           the terminal after writing out a prompt.  With _l_e_x such
  1874.           writes are automatically flushed since _l_e_x scanners use
  1875.           getchar() for their input.  Also, when writing interac-
  1876.           tive scanners with _f_l_e_x, the -I flag must be used.
  1877.  
  1878.      -    _f_l_e_x scanners are not as reentrant as _l_e_x scanners.  In
  1879.           particular, if you have an interactive scanner and an
  1880.           interrupt handler which long-jumps out of the scanner,
  1881.           and the scanner is subsequently called again, you may
  1882.           get the following message:
  1883.  
  1884.               fatal flex scanner internal error--end of buffer missed
  1885.  
  1886.  
  1887.  
  1888. Printed 4/3/91             26 May 1990                         30
  1889.  
  1890.  
  1891.  
  1892.  
  1893. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1894.  
  1895.  
  1896.           To reenter the scanner, first use
  1897.  
  1898.               yyrestart( yyin );
  1899.  
  1900.  
  1901.      -    output() is not supported.  Output from the ECHO macro
  1902.           is done to the file-pointer _y_y_o_u_t (default _s_t_d_o_u_t).
  1903.  
  1904.           The POSIX draft mentions that an output() routine
  1905.           exists but currently gives no details as to what it
  1906.           does.
  1907.  
  1908.      -    _l_e_x does not support exclusive start conditions (%x),
  1909.           though they are in the current POSIX draft.
  1910.  
  1911.      -    When definitions are expanded, _f_l_e_x encloses them in
  1912.           parentheses.  With lex, the following:
  1913.  
  1914.               NAME    [A-Z][A-Z0-9]*
  1915.               %%
  1916.               foo{NAME}?      printf( "Found it\n" );
  1917.               %%
  1918.  
  1919.           will not match the string "foo" because when the macro
  1920.           is expanded the rule is equivalent to "foo[A-Z][A-Z0-
  1921.           9]*?" and the precedence is such that the '?' is asso-
  1922.           ciated with "[A-Z0-9]*".  With _f_l_e_x, the rule will be
  1923.           expanded to "foo([A-Z][A-Z0-9]*)?" and so the string
  1924.           "foo" will match.  Note that because of this, the ^, $,
  1925.           <s>, /, and <<EOF>> operators cannot be used in a _f_l_e_x
  1926.           definition.
  1927.  
  1928.           The POSIX draft interpretation is the same as _f_l_e_x'_s.
  1929.  
  1930.      -    To specify a character class which matches anything but
  1931.           a left bracket (']'), in _l_e_x one can use "[^]]" but
  1932.           with _f_l_e_x one must use "[^\]]".  The latter works with
  1933.           _l_e_x, too.
  1934.  
  1935.      -    The _l_e_x %r (generate a Ratfor scanner) option is not
  1936.           supported.  It is not part of the POSIX draft.
  1937.  
  1938.      -    If you are providing your own yywrap() routine, you
  1939.           must include a "#undef yywrap" in the definitions sec-
  1940.           tion (section 1).  Note that the "#undef" will have to
  1941.           be enclosed in %{}'s.
  1942.  
  1943.           The POSIX draft specifies that yywrap() is a function
  1944.           and this is very unlikely to change; so _f_l_e_x _u_s_e_r_s _a_r_e
  1945.           _w_a_r_n_e_d that yywrap() is likely to be changed to a func-
  1946.           tion in the near future.
  1947.  
  1948.      -    After a call to unput(), _y_y_t_e_x_t and _y_y_l_e_n_g are
  1949.  
  1950.  
  1951. Printed 4/3/91             26 May 1990                         31
  1952.  
  1953.  
  1954.  
  1955.  
  1956. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  1957.  
  1958.  
  1959.           undefined until the next token is matched.  This is not
  1960.           the case with _l_e_x or the present POSIX draft.
  1961.  
  1962.      -    The precedence of the {} (numeric range) operator is
  1963.           different.  _l_e_x interprets "abc{1,3}" as "match one,
  1964.           two, or three occurrences of 'abc'", whereas _f_l_e_x
  1965.           interprets it as "match 'ab' followed by one, two, or
  1966.           three occurrences of 'c'".  The latter is in agreement
  1967.           with the current POSIX draft.
  1968.  
  1969.      -    The precedence of the ^ operator is different.  _l_e_x
  1970.           interprets "^foo|bar" as "match either 'foo' at the
  1971.           beginning of a line, or 'bar' anywhere", whereas _f_l_e_x
  1972.           interprets it as "match either 'foo' or 'bar' if they
  1973.           come at the beginning of a line".  The latter is in
  1974.           agreement with the current POSIX draft.
  1975.  
  1976.      -    To refer to yytext outside of the scanner source file,
  1977.           the correct definition with _f_l_e_x is "extern char
  1978.           *yytext" rather than "extern char yytext[]".  This is
  1979.           contrary to the current POSIX draft but a point on
  1980.           which _f_l_e_x will not be changing, as the array represen-
  1981.           tation entails a serious performance penalty.  It is
  1982.           hoped that the POSIX draft will be emended to support
  1983.           the _f_l_e_x variety of declaration (as this is a fairly
  1984.           painless change to require of _l_e_x users).
  1985.  
  1986.      -    _y_y_i_n is _i_n_i_t_i_a_l_i_z_e_d by _l_e_x to be _s_t_d_i_n; _f_l_e_x, on the
  1987.           other hand, initializes _y_y_i_n to NULL and then _a_s_s_i_g_n_s
  1988.           it to _s_t_d_i_n the first time the scanner is called, pro-
  1989.           viding _y_y_i_n has not already been assigned to a non-NULL
  1990.           value.  The difference is subtle, but the net effect is
  1991.           that with _f_l_e_x scanners, _y_y_i_n does not have a valid
  1992.           value until the scanner has been called.
  1993.  
  1994.      -    The special table-size declarations such as %a sup-
  1995.           ported by _l_e_x are not required by _f_l_e_x scanners; _f_l_e_x
  1996.           ignores them.
  1997.  
  1998.      -    The name FLEX_SCANNER is #define'd so scanners may be
  1999.           written for use with either _f_l_e_x or _l_e_x.
  2000.  
  2001.      The following _f_l_e_x features are not included in _l_e_x or the
  2002.      POSIX draft standard:
  2003.  
  2004.          yyterminate()
  2005.          <<EOF>>
  2006.          YY_DECL
  2007.          #line directives
  2008.          %{}'s around actions
  2009.          yyrestart()
  2010.          comments beginning with '#' (deprecated)
  2011.          multiple actions on a line
  2012.  
  2013.  
  2014. Printed 4/3/91             26 May 1990                         32
  2015.  
  2016.  
  2017.  
  2018.  
  2019. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  2020.  
  2021.  
  2022.      This last feature refers to the fact that with _f_l_e_x you can
  2023.      put multiple actions on the same line, separated with semi-
  2024.      colons, while with _l_e_x, the following
  2025.  
  2026.          foo    handle_foo(); ++num_foos_seen;
  2027.  
  2028.      is (rather surprisingly) truncated to
  2029.  
  2030.          foo    handle_foo();
  2031.  
  2032.      _f_l_e_x does not truncate the action.  Actions that are not
  2033.      enclosed in braces are simply terminated at the end of the
  2034.      line.
  2035.  
  2036. DIAGNOSTICS
  2037.      _r_e_j_e_c_t__u_s_e_d__b_u_t__n_o_t__d_e_t_e_c_t_e_d _u_n_d_e_f_i_n_e_d or
  2038.      _y_y_m_o_r_e__u_s_e_d__b_u_t__n_o_t__d_e_t_e_c_t_e_d _u_n_d_e_f_i_n_e_d - These errors can
  2039.      occur at compile time.  They indicate that the scanner uses
  2040.      REJECT or yymore() but that _f_l_e_x failed to notice the fact,
  2041.      meaning that _f_l_e_x scanned the first two sections looking for
  2042.      occurrences of these actions and failed to find any, but
  2043.      somehow you snuck some in (via a #include file, for exam-
  2044.      ple).  Make an explicit reference to the action in your _f_l_e_x
  2045.      input file.  (Note that previously _f_l_e_x supported a
  2046.      %used/%unused mechanism for dealing with this problem; this
  2047.      feature is still supported but now deprecated, and will go
  2048.      away soon unless the author hears from people who can argue
  2049.      compellingly that they need it.)
  2050.  
  2051.      _f_l_e_x _s_c_a_n_n_e_r _j_a_m_m_e_d - a scanner compiled with -s has encoun-
  2052.      tered an input string which wasn't matched by any of its
  2053.      rules.
  2054.  
  2055.      _f_l_e_x _i_n_p_u_t _b_u_f_f_e_r _o_v_e_r_f_l_o_w_e_d - a scanner rule matched a
  2056.      string long enough to overflow the scanner's internal input
  2057.      buffer (16K bytes by default - controlled by YY_BUF_SIZE in
  2058.      "flex.skel".  Note that to redefine this macro, you must
  2059.      first #undefine it).
  2060.  
  2061.      _s_c_a_n_n_e_r _r_e_q_u_i_r_e_s -_8 _f_l_a_g - Your scanner specification
  2062.      includes recognizing 8-bit characters and you did not
  2063.      specify the -8 flag (and your site has not installed flex
  2064.      with -8 as the default).
  2065.  
  2066.      _f_a_t_a_l _f_l_e_x _s_c_a_n_n_e_r _i_n_t_e_r_n_a_l _e_r_r_o_r--_e_n_d _o_f _b_u_f_f_e_r _m_i_s_s_e_d -
  2067.      This can occur in an scanner which is reentered after a
  2068.      long-jump has jumped out (or over) the scanner's activation
  2069.      frame.  Before reentering the scanner, use:
  2070.  
  2071.          yyrestart( yyin );
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077. Printed 4/3/91             26 May 1990                         33
  2078.  
  2079.  
  2080.  
  2081.  
  2082. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  2083.  
  2084.  
  2085.      _t_o_o _m_a_n_y %_t _c_l_a_s_s_e_s! - You managed to put every single char-
  2086.      acter into its own %t class.  _f_l_e_x requires that at least
  2087.      one of the classes share characters.
  2088.  
  2089. DEFICIENCIES / BUGS
  2090.      See flex(1).
  2091.  
  2092. SEE ALSO
  2093.      flex(1), lex(1), yacc(1), sed(1), awk(1).
  2094.  
  2095.      M. E. Lesk and E. Schmidt, _L_E_X - _L_e_x_i_c_a_l _A_n_a_l_y_z_e_r _G_e_n_e_r_a_t_o_r
  2096.  
  2097. AUTHOR
  2098.      Vern Paxson, with the help of many ideas and much inspira-
  2099.      tion from Van Jacobson.  Original version by Jef Poskanzer.
  2100.      The fast table representation is a partial implementation of
  2101.      a design done by Van Jacobson.  The implementation was done
  2102.      by Kevin Gong and Vern Paxson.
  2103.  
  2104.      Thanks to the many _f_l_e_x beta-testers, feedbackers, and con-
  2105.      tributors, especially Casey Leedom, benson@odi.com, Keith
  2106.      Bostic, Frederic Brehm, Nick Christopher, Jason Coughlin,
  2107.      Scott David Daniels, Leo Eskin, Chris Faylor, Eric Goldman,
  2108.      Eric Hughes, Jeffrey R. Jones, Kevin B. Kenny, Ronald Lam-
  2109.      precht, Greg Lee, Craig Leres, Mohamed el Lozy, Jim Meyer-
  2110.      ing, Marc Nozell, Esmond Pitt, Jef Poskanzer, Jim Roskind,
  2111.      Dave Tallman, Frank Whaley, Ken Yap, and those whose names
  2112.      have slipped my marginal mail-archiving skills but whose
  2113.      contributions are appreciated all the same.
  2114.  
  2115.      Thanks to Keith Bostic, John Gilmore, Craig Leres, Bob Mul-
  2116.      cahy, Rich Salz, and Richard Stallman for help with various
  2117.      distribution headaches.
  2118.  
  2119.      Thanks to Esmond Pitt and Earle Horton for 8-bit character
  2120.      support; to Benson Margulies and Fred Burke for C++ support;
  2121.      to Ove Ewerlid for the basics of support for NUL's; and to
  2122.      Eric Hughes for the basics of support for multiple buffers.
  2123.  
  2124.      Work is being done on extending _f_l_e_x to generate scanners in
  2125.      which the state machine is directly represented in C code
  2126.      rather than tables.  These scanners may well be substan-
  2127.      tially faster than those generated using -f or -F.  If you
  2128.      are working in this area and are interested in comparing
  2129.      notes and seeing whether redundant work can be avoided, con-
  2130.      tact Ove Ewerlid (ewerlid@mizar.DoCS.UU.SE).
  2131.  
  2132.      This work was primarily done when I was at the Real Time
  2133.      Systems Group at the Lawrence Berkeley Laboratory in Berke-
  2134.      ley, CA.  Many thanks to all there for the support I
  2135.      received.
  2136.  
  2137.  
  2138.  
  2139.  
  2140. Printed 4/3/91             26 May 1990                         34
  2141.  
  2142.  
  2143.  
  2144.  
  2145. FLEX(1)             UNIX Programmer's Manual              FLEX(1)
  2146.  
  2147.  
  2148.      Send comments to:
  2149.  
  2150.           Vern Paxson
  2151.           Computer Science Department
  2152.           4126 Upson Hall
  2153.           Cornell University
  2154.           Ithaca, NY 14853-7501
  2155.  
  2156.           vern@cs.cornell.edu
  2157.           decvax!cornell!vern
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.  
  2194.  
  2195.  
  2196.  
  2197.  
  2198.  
  2199.  
  2200.  
  2201.  
  2202.  
  2203. Printed 4/3/91             26 May 1990                         35
  2204.  
  2205.  
  2206.