home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / flex237 / flexdoc < prev    next >
Encoding:
Text File  |  1991-11-06  |  71.8 KB  |  2,311 lines

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