home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d704 / flex.lha / Flex / text-doc.lha / flexdoc.text < prev    next >
Text File  |  1992-07-21  |  70KB  |  2,311 lines

  1.  
  2.  
  3.  
  4. FLEX(1)             USER COMMANDS              FLEX(1)
  5.  
  6.  
  7.  
  8. NAME
  9.      flex - fast lexical analyzer generator
  10.  
  11. SYNOPSIS
  12.      flex [-bcdfinpstvFILT8 -C[efmF] -Sskeleton] [filename ...]
  13.  
  14. DESCRIPTION
  15.      flex is a    tool  for  generating  scanners:  programs  which
  16.      recognized     lexical  patterns in text.  flex 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  rules.  flex  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.      flex.  The     following  flex  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 flex    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  "user-
  39.      name"  expanded.    In  this  input,  there    is just    one rule.
  40.      "username"    is the pattern and the "printf"     is  the  action.
  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. Version    2.3        Last change: 26 May    1990            1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. FLEX(1)             USER COMMANDS              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. Version    2.3        Last change: 26 May    1990            2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. FLEX(1)             USER COMMANDS              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 tokens and
  152.      reports on    what it    has seen.
  153.  
  154.      The details of this example will be explained in the follow-
  155.      ing sections.
  156.  
  157. FORMAT OF THE INPUT FILE
  158.      The flex 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 definitions section contains declarations of simple name
  168.      definitions  to  simplify    the  scanner  specification,  and
  169.      declarations of start conditions, 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  under-
  177.      score  ('_')  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 con-
  180.      tinuing to    the end    of the line.  The definition  can  subse-
  181.      quently  be referred to using "{name}", which will    expand to
  182.      "(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. Version    2.3        Last change: 26 May    1990            3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. FLEX(1)             USER COMMANDS              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 rules section of the flex 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    indented 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     declara-
  239.      tions)  code  which  is to    be executed whenever the scanning
  240.      routine is    entered.  Other    indented or %{}    text in    the  rule
  241.      section  is  still     copied    to the output, but its meaning is
  242.      not well-defined and it may well cause  compile-time  errors
  243.      (this feature is present for POSIX    compliance; see    below for
  244.      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  defini-
  249.      tions  section  beginning    with  '#' is ignored, though this
  250.      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. Version    2.3        Last change: 26 May    1990            4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. FLEX(1)             USER COMMANDS              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. Version    2.3        Last change: 26 May    1990            5
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. FLEX(1)             USER COMMANDS              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  pre-
  345.      cedence.  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 concatena-
  354.      tion, and concatenation higher than alternation ('|').  This
  355.      pattern therefore matches either the  string  "foo"  or  the
  356.      string "ba" followed by zero-or-more r's.    To match "foo" or
  357.      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      will     match    a  newline  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 reg-
  373.       ular    expression tools treat negated character classes,
  374.       but unfortunately  the  inconsistency     is  historically
  375.       entrenched.    Matching  newlines  means  that    a pattern
  376.       like [^"]* can match an entire input    (overflowing  the
  377.       scanner's input buffer) unless there's another quote in
  378.       the input.
  379.  
  380.      -      A rule can have at most one instance of  trailing  con-
  381.       text (the '/'    operator or the    '$' operator).    The start
  382.       condition, '^', and "<<EOF>>"    patterns can  only  occur
  383.       at the beginning of a    pattern, and, as well as with '/'
  384.       and '$', cannot be grouped inside parentheses.   A  '^'
  385.       which     does  not  occur at the beginning of a    rule or    a
  386.       '$' which does not occur at the end of a rule    loses its
  387.       special  properties  and is treated as a normal charac-
  388.       ter.
  389.  
  390.  
  391.  
  392.  
  393. Version    2.3        Last change: 26 May    1990            6
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. FLEX(1)             USER COMMANDS              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  flex  input
  436.      file is chosen.
  437.  
  438.      Once the match is determined, the text corresponding to  the
  439.      match  (called  the  token)  is made available in the global
  440.      character pointer yytext,    and  its  length  in  the  global
  441.      integer yyleng. The action    corresponding to the matched pat-
  442.      tern 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 default rule is executed: the
  447.      next character in the input is considered matched and copied
  448.      to    the standard output.  Thus, the    simplest legal flex 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. Version    2.3        Last change: 26 May    1990            7
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. FLEX(1)             USER COMMANDS              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 pat-
  475.      tern 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  out-
  483.      put 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.  flex 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. Version    2.3        Last change: 26 May    1990            8
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. FLEX(1)             USER COMMANDS              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 flex 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 exe-
  559.       cutes    only one action    per token.  Multiple REJECT's are
  560.       allowed,  each  one finding the next best choice to the
  561.       currently active rule.  For example, when the    following
  562.       scanner  scans the token "abcd", it will write "abcdab-
  563.       caba"    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 particu-
  574.       larly    expensive feature in terms  scanner  performance;
  575.       if  it  is used in any of the    scanner's actions it will
  576.       slow down all    of the scanner's matching.   Furthermore,
  577.       REJECT  cannot  be  used with    the -f or -F options (see
  578.       below).
  579.  
  580.       Note also that unlike    the other special actions, REJECT
  581.       is  a     branch;  code    immediately  following    it in the
  582.       action will not be executed.
  583.  
  584.      -      yymore() tells  the  scanner    that  the  next     time  it
  585.       matches  a  rule,  the  corresponding     token    should be
  586.       appended onto    the current value of yytext  rather  than
  587.       replacing  it.   For    example,  given     the input "mega-
  588.  
  589.  
  590.  
  591. Version    2.3        Last change: 26 May    1990            9
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. FLEX(1)             USER COMMANDS              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 "foobar-
  623.       bar":
  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  pro-
  632.       cess    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 beginning    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. Version    2.3        Last change: 26 May    1990               10
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. FLEX(1)             USER COMMANDS              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 input.)
  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".  Sub-
  704.       sequent  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 rede-
  708.       fined.
  709.  
  710. THE GENERATED SCANNER
  711.      The output    of flex    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 mac-
  714.      ros.  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. Version    2.3        Last change: 26 May    1990               11
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. FLEX(1)             USER COMMANDS              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 lexscan,  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 ter-
  746.      minate the    definition with    a semi-colon (;).
  747.  
  748.      Whenever yylex() is called, it scans tokens from the  global
  749.      input  file  yyin    (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 return
  752.      statement.     In  the  former  case,     when  called  again  the
  753.      scanner will immediately return unless yyrestart()    is called
  754.      to    point yyin 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 getc() calls to read
  761.      characters    from yyin. 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    max_size characters in the character array buf and return
  766.      in     the integer variable result either the    number of charac-
  767.      ters read or the constant YY_NULL (0  on  Unix  systems)  to
  768.      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  sec-
  772.      tion 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. Version    2.3        Last change: 26 May    1990               12
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. FLEX(1)             USER COMMANDS              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 yyin    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  rede-
  813.      fine  it  you must    first "#undef yywrap", as it is    currently
  814.      implemented as a macro.  As indicated by the hedging in  the
  815.      previous  sentence,  it may be changed to a true function in
  816.      the near future.
  817.  
  818.      The scanner writes    its  ECHO  output  to  the  yyout  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.      flex  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  inclusive  start     conditions, the latter    exclusive
  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    inclusive, then    rules with no start  con-
  851.      ditions  at  all  will  also be active.  If it is exclusive,
  852.  
  853.  
  854.  
  855. Version    2.3        Last change: 26 May    1990               13
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. FLEX(1)             USER COMMANDS              FLEX(1)
  863.  
  864.  
  865.  
  866.      then only 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 flex input.     Because of this,
  870.      exclusive start conditions    make it    easy  to  specify  "mini-
  871.      scanners"    which scan portions of the input that are syntac-
  872.      tically 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  condi-
  903.      tion  whenever  yylex()  is  called  and the global variable
  904.      enter_special 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. Version    2.3        Last change: 26 May    1990               14
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. FLEX(1)             USER COMMANDS              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. Version    2.3        Last change: 26 May    1990               15
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. FLEX(1)             USER COMMANDS              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 flex    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  flex
  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 state-
  1037.      ment such as an "include" which requires switching    the input
  1038.      source.
  1039.  
  1040.      To    negotiate  these  sorts     of  problems,    flex  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 FILE    pointer    and a size and creates    a  buffer
  1047.      associated    with the given file and    large enough to    hold size
  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. Version    2.3        Last change: 26 May    1990               16
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. FLEX(1)             USER COMMANDS              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 new_buffer. 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 yyin 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 new and delete 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. Version    2.3        Last change: 26 May    1990               17
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. FLEX(1)             USER COMMANDS              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  pro-
  1157.      cess).  The action    must finish by doing one of four things:
  1158.  
  1159.      -      the  special    YY_NEW_FILE  action,  if  yyin    has  been
  1160.       pointed at a new file    to process;
  1161.  
  1162.      -      a return statement;
  1163.  
  1164.      -      the special yyterminate() action;
  1165.  
  1166.      -      or,     switching    to    a     new     buffer        using
  1167.       yy_switch_to_buffer()    as shown in the    example    above.
  1168.  
  1169.      <<EOF>> rules may not be used with    other patterns;    they  may
  1170.      only  be  qualified  with a list of start conditions.  If an
  1171.      unqualified <<EOF>> rule is given,    it applies to  all  start
  1172.      conditions     which    do  not    already    have <<EOF>> actions.  To
  1173.      specify an    <<EOF>>    rule for only the  initial  start  condi-
  1174.      tion, use
  1175.  
  1176.      <INITIAL><<EOF>>
  1177.  
  1178.  
  1179.      These rules are useful for     catching  things  like     unclosed
  1180.      comments.    An example:
  1181.  
  1182.  
  1183.  
  1184.  
  1185. Version    2.3        Last change: 26 May    1990               18
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. FLEX(1)             USER COMMANDS              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  rou-
  1220.      tine 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 exam-
  1225.      ple,  it  could  be used to call a    routine    to read    in a data
  1226.      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 inacces-
  1237.      sible.
  1238.  
  1239. INTERFACING WITH YACC
  1240.      One of the    main uses of flex is as    a companion to    the  yacc
  1241.      parser-generator.     yacc  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.      flex  with     yacc,    one  specifies    the  -d    option to yacc to
  1246.      instruct it to generate the file y.tab.h containing  defini-
  1247.      tions  of all the %tokens appearing in the    yacc input.  This
  1248.  
  1249.  
  1250.  
  1251. Version    2.3        Last change: 26 May    1990               19
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. FLEX(1)             USER COMMANDS              FLEX(1)
  1259.  
  1260.  
  1261.  
  1262.      file is then included in the flex scanner.     For example,  if
  1263.      one of the    tokens is "TOK_NUMBER",    part of    the scanner might
  1264.      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, flex supports a translation
  1277.      table  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 no other characters will appear
  1292.      in    the patterns.  The group numbers are actually disregarded
  1293.      by     flex;    %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 charac-
  1296.      ter in group #1, followed by zero-or-more pairs  of  charac-
  1297.      ters from group #2, followed by a character from group #52."
  1298.      Thus %t provides a    crude  way  for     introducing  equivalence
  1299.      classes into the scanner specification.
  1300.  
  1301.      Note that    the  -i     option     (see  below)  coupled    with  the
  1302.      equivalence  classes which    flex 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.      flex has the following options:
  1308.  
  1309.      -b      Generate  backtracking  information  to  lex.backtrack.
  1310.       This    is  a  list of scanner states which require back-
  1311.       tracking and the input characters on which they do  so.
  1312.       By adding rules one can remove backtracking states.  If
  1313.       all backtracking states are eliminated and -f    or -F  is
  1314.  
  1315.  
  1316.  
  1317. Version    2.3        Last change: 26 May    1990               20
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. FLEX(1)             USER COMMANDS              FLEX(1)
  1325.  
  1326.  
  1327.  
  1328.       used,    the generated scanner will run faster (see the -p
  1329.       flag).  Only users who wish to squeeze every last cycle
  1330.       out  of  their  scanners  need worry about this option.
  1331.       (See the section on PERFORMANCE CONSIDERATIONS below.)
  1332.  
  1333.      -c      is a do-nothing, deprecated option included  for  POSIX
  1334.       compliance.
  1335.  
  1336.       NOTE:    in previous releases of    flex -c    specified  table-
  1337.       compression  options.      This functionality is    now given
  1338.       by the -C flag.  To ease the the impact of this change,
  1339.       when    flex encounters    -c, it currently issues    a warning
  1340.       message and assumes that -C was  desired  instead.   In
  1341.       the future this "promotion" of -c to -C will go away in
  1342.       the name of full POSIX  compliance  (unless  the  POSIX
  1343.       meaning is removed first).
  1344.  
  1345.      -d      makes    the generated scanner run in debug  mode.   When-
  1346.       ever     a   pattern   is   recognized     and  the  global
  1347.       yy_flex_debug    is non-zero (which is the  default),  the
  1348.       scanner will write to    stderr a line of the form:
  1349.  
  1350.           --accepting rule at line 53 ("the    matched    text")
  1351.  
  1352.       The line number refers to the    location of the     rule  in
  1353.       the  file defining the scanner (i.e.,    the file that was
  1354.       fed to flex).     Messages are  also  generated    when  the
  1355.       scanner  backtracks,    accepts    the default rule, reaches
  1356.       the end of its input buffer (or encounters  a     NUL;  at
  1357.       this    point,    the  two  look    the  same  as  far as the
  1358.       scanner's concerned),    or reaches an end-of-file.
  1359.  
  1360.      -f      specifies (take your pick) full table    or fast     scanner.
  1361.       No  table compression    is done.  The result is    large but
  1362.       fast.     This option is    equivalent to -Cf (see below).
  1363.  
  1364.      -i      instructs flex to generate a case-insensitive     scanner.
  1365.       The  case  of     letters given in the flex input patterns
  1366.       will be ignored,  and     tokens     in  the  input     will  be
  1367.       matched  regardless of case.    The matched text given in
  1368.       yytext will have the preserved case (i.e., it    will  not
  1369.       be folded).
  1370.  
  1371.      -n      is another do-nothing, deprecated option included  only
  1372.       for POSIX compliance.
  1373.  
  1374.      -p      generates a performance report to stderr.   The  report
  1375.       consists  of    comments  regarding  features of the flex
  1376.       input    file which will    cause a    loss  of  performance  in
  1377.       the resulting    scanner.  Note that the    use of REJECT and
  1378.       variable trailing context  (see  the    BUGS  section  in
  1379.       flex(1)) entails a substantial performance penalty; use
  1380.  
  1381.  
  1382.  
  1383. Version    2.3        Last change: 26 May    1990               21
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. FLEX(1)             USER COMMANDS              FLEX(1)
  1391.  
  1392.  
  1393.  
  1394.       of yymore(), the ^ operator, and  the     -I  flag  entail
  1395.       minor    performance penalties.
  1396.  
  1397.      -s      causes the default rule (that    unmatched  scanner  input
  1398.       is  echoed to    stdout)    to be suppressed.  If the scanner
  1399.       encounters input that    does not match any of its  rules,
  1400.       it  aborts  with  an    error.    This option is useful for
  1401.       finding holes    in a scanner's rule set.
  1402.  
  1403.      -t      instructs flex to write the  scanner    it  generates  to
  1404.       standard output instead of lex.yy.c.
  1405.  
  1406.      -v      specifies that flex should write to stderr a summary of
  1407.       statistics regarding the scanner it generates.  Most of
  1408.       the statistics are meaningless to the    casual flex user,
  1409.       but  the  first  line     identifies  the version of flex,
  1410.       which    is useful for figuring out where you  stand  with
  1411.       respect  to  patches and new releases, and the next two
  1412.       lines    give the date when the scanner was created and    a
  1413.       summary of the flags which were in effect.
  1414.  
  1415.      -F      specifies that the fast  scanner  table  representation
  1416.       should  be  used.  This representation is about as fast
  1417.       as the full table representation  (-f),  and    for  some
  1418.       sets    of patterns will be considerably smaller (and for
  1419.       others, larger).  In general,    if the pattern    set  con-
  1420.       tains     both  "keywords"  and    a catch-all, "identifier"
  1421.       rule,    such as    in the set:
  1422.  
  1423.           "case"    return TOK_CASE;
  1424.           "switch"    return TOK_SWITCH;
  1425.           ...
  1426.           "default"    return TOK_DEFAULT;
  1427.           [a-z]+    return TOK_ID;
  1428.  
  1429.       then you're better off using the full    table representa-
  1430.       tion.     If only the "identifier" rule is present and you
  1431.       then use a hash table    or some    such to    detect    the  key-
  1432.       words, you're    better off using -F.
  1433.  
  1434.       This option is equivalent to -CF (see    below).
  1435.  
  1436.      -I      instructs flex  to  generate    an  interactive     scanner.
  1437.       Normally,  scanners generated    by flex    always look ahead
  1438.       one character    before deciding     that  a  rule    has  been
  1439.       matched.   At     the cost of some scanning overhead, flex
  1440.       will generate    a scanner which     only  looks  ahead  when
  1441.       needed.   Such  scanners are called interactive because
  1442.       if you want to write a scanner for an    interactive  sys-
  1443.       tem such as a    command    shell, you will    probably want the
  1444.       user's input to  be  terminated  with     a  newline,  and
  1445.       without  -I  the  user will have to type a character in
  1446.  
  1447.  
  1448.  
  1449. Version    2.3        Last change: 26 May    1990               22
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. FLEX(1)             USER COMMANDS              FLEX(1)
  1457.  
  1458.  
  1459.  
  1460.       addition to the newline in order to  have  the  newline
  1461.       recognized.  This leads to dreadful interactive perfor-
  1462.       mance.
  1463.  
  1464.       If all this seems  to     confusing,  here's  the  general
  1465.       rule:     if  a    human  will  be     typing     in input to your
  1466.       scanner, use -I, otherwise don't;  if     you  don't  care
  1467.       about      squeezing  the  utmost  performance  from  your
  1468.       scanner and you don't     want  to  make     any  assumptions
  1469.       about    the input to your scanner, use -I.
  1470.  
  1471.       Note,    -I cannot be used in  conjunction  with     full  or
  1472.       fast tables, i.e., the -f, -F, -Cf, or -CF flags.
  1473.  
  1474.      -L      instructs  flex  not    to  generate  #line   directives.
  1475.       Without this option, flex peppers the    generated scanner
  1476.       with #line directives    so error messages in the  actions
  1477.       will    be correctly located with respect to the original
  1478.       flex input file, and not to the fairly meaningless line
  1479.       numbers  of  lex.yy.c.  (Unfortunately  flex    does  not
  1480.       presently generate the necessary directives to  "retar-
  1481.       get" the line    numbers    for those parts    of lex.yy.c which
  1482.       it generated.     So if there is    an error in the    generated
  1483.       code,    a meaningless line number is reported.)
  1484.  
  1485.      -T      makes    flex run in trace mode.     It will generate  a  lot
  1486.       of  messages to stdout concerning the    form of    the input
  1487.       and the resultant non-deterministic  and  deterministic
  1488.       finite  automata.   This  option  is    mostly for use in
  1489.       maintaining flex.
  1490.  
  1491.      -8      instructs flex to generate an    8-bit scanner, i.e.,  one
  1492.       which     can  recognize    8-bit characters.  On some sites,
  1493.       flex is installed with this option as    the default.   On
  1494.       others,  the default is 7-bit    characters.  To    see which
  1495.       is  the  case,  check     the  verbose  (-v)  output   for
  1496.       "equivalence    classes     created".  If the denominator of
  1497.       the number shown is 128, then    by default flex     is  gen-
  1498.       erating  7-bit  characters.    If  it    is  256, then the
  1499.       default is 8-bit characters and  the    -8  flag  is  not
  1500.       required  (but  may  be a good idea to keep the scanner
  1501.       specification    portable).  Feeding a 7-bit scanner 8-bit
  1502.       characters  will  result in infinite loops, bus errors,
  1503.       or other such    fireworks, so  when  in     doubt,     use  the
  1504.       flag.     Note that if equivalence classes are used, 8-bit
  1505.       scanners take    only slightly more table space than 7-bit
  1506.       scanners  (128  bytes,  to  be  exact);  if equivalence
  1507.       classes are not used,    however, then the tables may grow
  1508.       up to    twice their 7-bit size.
  1509.  
  1510.      -C[efmF]
  1511.       controls the degree of table compression.
  1512.  
  1513.  
  1514.  
  1515. Version    2.3        Last change: 26 May    1990               23
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. FLEX(1)             USER COMMANDS              FLEX(1)
  1523.  
  1524.  
  1525.  
  1526.       -Ce directs  flex  to     construct  equivalence     classes,
  1527.       i.e.,     sets  of characters which have    identical lexical
  1528.       properties (for example,  if    the  only  appearance  of
  1529.       digits  in  the  flex     input    is in the character class
  1530.       "[0-9]" then the digits '0', '1', ..., '9' will all  be
  1531.       put    in  the     same  equivalence  class).   Equivalence
  1532.       classes usually give dramatic    reductions in  the  final
  1533.       table/object file sizes (typically a factor of 2-5) and
  1534.       are pretty cheap performance-wise  (one  array  look-up
  1535.       per character    scanned).
  1536.  
  1537.       -Cf specifies    that the full scanner  tables  should  be
  1538.       generated - flex should not compress the tables by tak-
  1539.       ing advantages of similar transition functions for dif-
  1540.       ferent states.
  1541.  
  1542.       -CF specifies    that the alternate fast    scanner    represen-
  1543.       tation  (described  above  under the -F flag)    should be
  1544.       used.
  1545.  
  1546.       -Cm directs flex to construct    meta-equivalence classes,
  1547.       which     are  sets of equivalence classes (or characters,
  1548.       if equivalence classes are not  being     used)    that  are
  1549.       commonly  used  together.  Meta-equivalence classes are
  1550.       often    a big win when using compressed    tables,    but  they
  1551.       have    a  moderate  performance  impact (one or two "if"
  1552.       tests    and one    array look-up per character scanned).
  1553.  
  1554.       A lone -C specifies that the scanner tables  should  be
  1555.       compressed  but  neither  equivalence    classes    nor meta-
  1556.       equivalence classes should be    used.
  1557.  
  1558.       The options -Cf or  -CF  and    -Cm  do     not  make  sense
  1559.       together - there is no opportunity for meta-equivalence
  1560.       classes if the table is not being  compressed.   Other-
  1561.       wise the options may be freely mixed.
  1562.  
  1563.       The default setting is -Cem, which specifies that  flex
  1564.       should   generate   equivalence   classes   and   meta-
  1565.       equivalence classes.    This setting provides the highest
  1566.       degree   of  table  compression.   You  can  trade  off
  1567.       faster-executing scanners at the cost    of larger  tables
  1568.       with the following generally being true:
  1569.  
  1570.           slowest &    smallest
  1571.             -Cem
  1572.             -Cm
  1573.             -Ce
  1574.             -C
  1575.             -C{f,F}e
  1576.             -C{f,F}
  1577.           fastest &    largest
  1578.  
  1579.  
  1580.  
  1581. Version    2.3        Last change: 26 May    1990               24
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. FLEX(1)             USER COMMANDS              FLEX(1)
  1589.  
  1590.  
  1591.  
  1592.       Note that scanners with the smallest tables are usually
  1593.       generated and    compiled the quickest, so during develop-
  1594.       ment you will    usually    want to    use the    default,  maximal
  1595.       compression.
  1596.  
  1597.       -Cfe is often    a good compromise between speed    and  size
  1598.       for production scanners.
  1599.  
  1600.       -C options are not cumulative;  whenever  the     flag  is
  1601.       encountered, the previous -C settings    are forgotten.
  1602.  
  1603.      -Sskeleton_file
  1604.       overrides the    default    skeleton  file    from  which  flex
  1605.       constructs its scanners.  You'll never need this option
  1606.       unless you are doing flex maintenance    or development.
  1607.  
  1608. PERFORMANCE CONSIDERATIONS
  1609.      The main design goal of  flex  is    that  it  generate  high-
  1610.      performance  scanners.   It  has  been optimized for dealing
  1611.      well with large sets of rules.  Aside from     the  effects  of
  1612.      table compression on scanner speed    outlined above,    there are
  1613.      a    number    of  options/actions  which  degrade  performance.
  1614.      These are,    from most expensive to least:
  1615.  
  1616.      REJECT
  1617.  
  1618.      pattern sets that require backtracking
  1619.      arbitrary trailing context
  1620.  
  1621.      '^' beginning-of-line operator
  1622.      yymore()
  1623.  
  1624.      with the first three all being quite expensive and    the  last
  1625.      two being quite cheap.
  1626.  
  1627.      REJECT should be avoided at all costs  when  performance  is
  1628.      important.     It is a particularly expensive    option.
  1629.  
  1630.      Getting rid of backtracking is messy and  often  may  be  an
  1631.      enormous amount of    work for a complicated scanner.     In prin-
  1632.      cipal, one    begins    by  using  the    -b  flag  to  generate    a
  1633.      lex.backtrack file.  For example, on the input
  1634.  
  1635.      %%
  1636.      foo        return TOK_KEYWORD;
  1637.      foobar        return TOK_KEYWORD;
  1638.  
  1639.      the file looks like:
  1640.  
  1641.      State #6 is non-accepting -
  1642.       associated rule line numbers:
  1643.         2    3
  1644.  
  1645.  
  1646.  
  1647. Version    2.3        Last change: 26 May    1990               25
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654. FLEX(1)             USER COMMANDS              FLEX(1)
  1655.  
  1656.  
  1657.  
  1658.       out-transitions: [ o ]
  1659.       jam-transitions: EOF [ \001-n     p-\177    ]
  1660.  
  1661.      State #8 is non-accepting -
  1662.       associated rule line numbers:
  1663.         3
  1664.       out-transitions: [ a ]
  1665.       jam-transitions: EOF [ \001-`     b-\177    ]
  1666.  
  1667.      State #9 is non-accepting -
  1668.       associated rule line numbers:
  1669.         3
  1670.       out-transitions: [ r ]
  1671.       jam-transitions: EOF [ \001-q     s-\177    ]
  1672.  
  1673.      Compressed tables always backtrack.
  1674.  
  1675.      The first few lines tell us that there's a    scanner    state  in
  1676.      which  it    can  make  a  transition on an 'o' but not on any
  1677.      other character,  and  that  in  that  state  the    currently
  1678.      scanned text does not match any rule.  The    state occurs when
  1679.      trying to match the rules found at    lines  2  and  3  in  the
  1680.      input  file.  If the scanner is in    that state and then reads
  1681.      something other than an 'o', it will have    to  backtrack  to
  1682.      find  a rule which    is matched.  With a bit    of headscratching
  1683.      one can see that this must    be the state it's in when it  has
  1684.      seen  "fo".   When    this has happened, if anything other than
  1685.      another 'o' is seen, the scanner will have     to  back  up  to
  1686.      simply match the 'f' (by the default rule).
  1687.  
  1688.      The comment regarding State #8 indicates there's  a  problem
  1689.      when  "foob"  has    been  scanned.     Indeed, on any    character
  1690.      other than    a 'b', the scanner will    have to    back up    to accept
  1691.      "foo".   Similarly,  the  comment for State #9 concerns when
  1692.      "fooba" has been scanned.
  1693.  
  1694.      The final comment reminds us that there's no point    going  to
  1695.      all  the  trouble    of  removing  backtracking from    the rules
  1696.      unless we're using    -f or -F, since     there's  no  performance
  1697.      gain doing    so with    compressed scanners.
  1698.  
  1699.      The way to    remove the backtracking    is to add "error" rules:
  1700.  
  1701.      %%
  1702.      foo         return TOK_KEYWORD;
  1703.      foobar         return TOK_KEYWORD;
  1704.  
  1705.      fooba         |
  1706.      foob         |
  1707.      fo         {
  1708.              /*    false alarm, not really    a keyword */
  1709.              return TOK_ID;
  1710.  
  1711.  
  1712.  
  1713. Version    2.3        Last change: 26 May    1990               26
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720. FLEX(1)             USER COMMANDS              FLEX(1)
  1721.  
  1722.  
  1723.  
  1724.              }
  1725.  
  1726.  
  1727.      Eliminating backtracking among a list of keywords    can  also
  1728.      be    done using a "catch-all" rule:
  1729.  
  1730.      %%
  1731.      foo         return TOK_KEYWORD;
  1732.      foobar         return TOK_KEYWORD;
  1733.  
  1734.      [a-z]+         return TOK_ID;
  1735.  
  1736.      This is usually the best solution when appropriate.
  1737.  
  1738.      Backtracking messages tend    to cascade.  With  a  complicated
  1739.      set  of rules it's    not uncommon to    get hundreds of    messages.
  1740.      If    one can    decipher them, though,    it  often  only     takes    a
  1741.      dozen or so rules to eliminate the    backtracking (though it's
  1742.      easy to make a mistake and    have an    error  rule  accidentally
  1743.      match a valid token.  A possible future flex feature will be
  1744.      to    automatically add rules    to eliminate backtracking).
  1745.  
  1746.      Variable trailing context (where both the leading and trail-
  1747.      ing  parts     do  not  have a fixed length) entails almost the
  1748.      same performance loss as  REJECT  (i.e.,  substantial).   So
  1749.      when possible a rule like:
  1750.  
  1751.      %%
  1752.      mouse|rat/(cat|dog)   run();
  1753.  
  1754.      is    better written:
  1755.  
  1756.      %%
  1757.      mouse/cat|dog           run();
  1758.      rat/cat|dog           run();
  1759.  
  1760.      or    as
  1761.  
  1762.      %%
  1763.      mouse|rat/cat           run();
  1764.      mouse|rat/dog           run();
  1765.  
  1766.      Note that here the    special    '|' action does    not  provide  any
  1767.      savings,  and  can     even  make  things  worse  (see  BUGS in
  1768.      flex(1)).
  1769.  
  1770.      Another area where    the user can increase a    scanner's perfor-
  1771.      mance  (and  one that's easier to implement) arises from the
  1772.      fact that the longer the  tokens  matched,     the  faster  the
  1773.      scanner will run.    This is    because    with long tokens the pro-
  1774.      cessing of    most input characters takes place in the  (short)
  1775.      inner  scanning  loop, and    does not often have to go through
  1776.  
  1777.  
  1778.  
  1779. Version    2.3        Last change: 26 May    1990               27
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786. FLEX(1)             USER COMMANDS              FLEX(1)
  1787.  
  1788.  
  1789.  
  1790.      the additional work of setting up the  scanning  environment
  1791.      (e.g.,  yytext)  for  the    action.     Recall    the scanner for    C
  1792.      comments:
  1793.  
  1794.      %x comment
  1795.      %%
  1796.          int line_num =    1;
  1797.  
  1798.      "/*"          BEGIN(comment);
  1799.  
  1800.      <comment>[^*\n]*
  1801.      <comment>"*"+[^*/\n]*
  1802.      <comment>\n         ++line_num;
  1803.      <comment>"*"+"/"     BEGIN(INITIAL);
  1804.  
  1805.      This could    be sped    up by writing it as:
  1806.  
  1807.      %x comment
  1808.      %%
  1809.          int line_num =    1;
  1810.  
  1811.      "/*"          BEGIN(comment);
  1812.  
  1813.      <comment>[^*\n]*
  1814.      <comment>[^*\n]*\n     ++line_num;
  1815.      <comment>"*"+[^*/\n]*
  1816.      <comment>"*"+[^*/\n]*\n ++line_num;
  1817.      <comment>"*"+"/"     BEGIN(INITIAL);
  1818.  
  1819.      Now instead of each  newline  requiring  the  processing  of
  1820.      another  action,  recognizing  the    newlines is "distributed"
  1821.      over the other rules to keep the matched  text  as     long  as
  1822.      possible.     Note  that  adding  rules does    not slow down the
  1823.      scanner!  The speed of the    scanner     is  independent  of  the
  1824.      number  of     rules or (modulo the considerations given at the
  1825.      beginning of this section)    how  complicated  the  rules  are
  1826.      with regard to operators such as '*' and '|'.
  1827.  
  1828.      A final example in    speeding up a scanner: suppose    you  want
  1829.      to     scan through a    file containing    identifiers and    keywords,
  1830.      one per line and with no other  extraneous     characters,  and
  1831.      recognize all the keywords.  A natural first approach is:
  1832.  
  1833.      %%
  1834.      asm      |
  1835.      auto      |
  1836.      break      |
  1837.      ... etc ...
  1838.      volatile |
  1839.      while      /* it's a keyword */
  1840.  
  1841.      .|\n      /* it's not a    keyword    */
  1842.  
  1843.  
  1844.  
  1845. Version    2.3        Last change: 26 May    1990               28
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852. FLEX(1)             USER COMMANDS              FLEX(1)
  1853.  
  1854.  
  1855.  
  1856.      To    eliminate the back-tracking, introduce a catch-all rule:
  1857.  
  1858.      %%
  1859.      asm      |
  1860.      auto      |
  1861.      break      |
  1862.      ... etc ...
  1863.      volatile |
  1864.      while      /* it's a keyword */
  1865.  
  1866.      [a-z]+      |
  1867.      .|\n      /* it's not a    keyword    */
  1868.  
  1869.      Now, if it's guaranteed that there's exactly  one    word  per
  1870.      line,  then  we  can reduce the total number of matches by    a
  1871.      half by merging in    the recognition    of newlines with that  of
  1872.      the other tokens:
  1873.  
  1874.      %%
  1875.      asm\n      |
  1876.      auto\n      |
  1877.      break\n  |
  1878.      ... etc ...
  1879.      volatile\n |
  1880.      while\n  /* it's a keyword */
  1881.  
  1882.      [a-z]+\n |
  1883.      .|\n      /* it's not a    keyword    */
  1884.  
  1885.      One has to    be careful here,  as  we  have    now  reintroduced
  1886.      backtracking into the scanner.  In    particular, while we know
  1887.      that there    will never be any characters in    the input  stream
  1888.      other  than letters or newlines, flex can't figure    this out,
  1889.      and it will plan for possibly needing backtracking     when  it
  1890.      has  scanned a token like "auto" and then the next    character
  1891.      is    something other    than a newline or a  letter.   Previously
  1892.      it     would    then  just match the "auto" rule and be    done, but
  1893.      now it has    no "auto" rule,    only a "auto\n"    rule.    To  elim-
  1894.      inate  the     possibility  of  backtracking,     we  could either
  1895.      duplicate all rules but without final newlines, or, since we
  1896.      never  expect to encounter    such an    input and therefore don't
  1897.      how it's classified, we can  introduce  one  more    catch-all
  1898.      rule, this    one which doesn't include a newline:
  1899.  
  1900.      %%
  1901.      asm\n      |
  1902.      auto\n      |
  1903.      break\n  |
  1904.      ... etc ...
  1905.      volatile\n |
  1906.      while\n  /* it's a keyword */
  1907.  
  1908.  
  1909.  
  1910.  
  1911. Version    2.3        Last change: 26 May    1990               29
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918. FLEX(1)             USER COMMANDS              FLEX(1)
  1919.  
  1920.  
  1921.  
  1922.      [a-z]+\n |
  1923.      [a-z]+      |
  1924.      .|\n      /* it's not a    keyword    */
  1925.  
  1926.      Compiled with -Cf,    this is    about as fast as one  can  get    a
  1927.      flex scanner to go    for this particular problem.
  1928.  
  1929.      A final note: flex    is slow    when matching NUL's, particularly
  1930.      when  a  token  contains multiple NUL's.  It's best to write
  1931.      rules which match short amounts of    text if    it's  anticipated
  1932.      that the text will    often include NUL's.
  1933.  
  1934. INCOMPATIBILITIES WITH LEX AND POSIX
  1935.      flex is a rewrite of the Unix lex tool (the two  implementa-
  1936.      tions  do    not share any code, though), with some extensions
  1937.      and incompatibilities, both of which are of concern to those
  1938.      who  wish to write    scanners acceptable to either implementa-
  1939.      tion.  At present,    the POSIX lex draft is very close to  the
  1940.      original lex implementation, so some of these incompatibili-
  1941.      ties are also in conflict with the     POSIX    draft.     But  the
  1942.      intent  is     that except as    noted below, flex as it    presently
  1943.      stands will ultimately be POSIX conformant    (i.e., that those
  1944.      areas  of    conflict with the POSIX    draft will be resolved in
  1945.      flex's favor).  Please bear in mind that  all  the     comments
  1946.      which  follow are with regard to the POSIX    draft standard of
  1947.      Summer 1989, and  not  the     final    document  (or  subsequent
  1948.      drafts); they are included    so flex    users can be aware of the
  1949.      standardization issues and    those areas where flex may in the
  1950.      near  future  undergo  changes incompatible with its current
  1951.      definition.
  1952.  
  1953.      flex is fully compatible with lex with the    following  excep-
  1954.      tions:
  1955.  
  1956.      -      The undocumented lex scanner internal    variable yylineno
  1957.       is  not  supported.    It  is    difficult to support this
  1958.       option efficiently, since it requires     examining  every
  1959.       character  scanned  and reexamining the characters when
  1960.       the scanner backs up.     Things    get more complicated when
  1961.       the  end  of    buffer    or  file  is  reached or a NUL is
  1962.       scanned (since the scan must then be restarted with the
  1963.       proper  line    number    count),     or  the  user    uses  the
  1964.       yyless(), unput(), or    REJECT actions,    or  the     multiple
  1965.       input    buffer functions.
  1966.  
  1967.       The fix is to    add rules which, upon seeing  a     newline,
  1968.       increment  yylineno.     This is usually an easy process,
  1969.       though it can    be a drag if some  of  the  patterns  can
  1970.       match    multiple newlines along    with other characters.
  1971.  
  1972.       yylineno is not part of the POSIX draft.
  1973.  
  1974.  
  1975.  
  1976.  
  1977. Version    2.3        Last change: 26 May    1990               30
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984. FLEX(1)             USER COMMANDS              FLEX(1)
  1985.  
  1986.  
  1987.  
  1988.      -      The input() routine is not redefinable, though  it  may
  1989.       be  called  to  read    characters following whatever has
  1990.       been matched by a rule.  If input() encounters an  end-
  1991.       of-file  the    normal    yywrap()  processing  is done.    A
  1992.       ``real'' end-of-file is returned by input() as EOF.
  1993.  
  1994.       Input    is instead controlled by redefining the     YY_INPUT
  1995.       macro.
  1996.  
  1997.       The flex restriction that input() cannot  be    redefined
  1998.       is in    accordance with    the POSIX draft, but YY_INPUT has
  1999.       not yet been accepted     into  the  draft  (and     probably
  2000.       won't;  it looks like    the draft will simply not specify
  2001.       any way of controlling the scanner's input  other  than
  2002.       by making an initial assignment to yyin).
  2003.  
  2004.      -      flex scanners    do not use stdio for input.   Because  of
  2005.       this,     when  writing    an  interactive     scanner one must
  2006.       explicitly call fflush() on the stream associated  with
  2007.       the terminal after writing out a prompt.  With lex such
  2008.       writes are automatically flushed since lex scanners use
  2009.       getchar() for    their input.  Also, when writing interac-
  2010.       tive scanners    with flex, the -I flag must be used.
  2011.  
  2012.      -      flex scanners    are not    as reentrant as    lex scanners.  In
  2013.       particular,  if  you have an interactive scanner and an
  2014.       interrupt handler which long-jumps out of the     scanner,
  2015.       and  the  scanner is subsequently called again, you may
  2016.       get the following message:
  2017.  
  2018.           fatal flex scanner internal error--end of    buffer missed
  2019.  
  2020.       To reenter the scanner, first    use
  2021.  
  2022.           yyrestart( yyin );
  2023.  
  2024.  
  2025.      -      output() is not supported.  Output from the ECHO  macro
  2026.       is done to the file-pointer yyout (default stdout).
  2027.  
  2028.       The POSIX  draft  mentions  that  an    output()  routine
  2029.       exists  but  currently  gives     no details as to what it
  2030.       does.
  2031.  
  2032.      -      lex does not support exclusive start    conditions  (%x),
  2033.       though they are in the current POSIX draft.
  2034.  
  2035.      -      When definitions are expanded, flex  encloses     them  in
  2036.       parentheses.    With lex, the following:
  2037.  
  2038.           NAME    [A-Z][A-Z0-9]*
  2039.           %%
  2040.  
  2041.  
  2042.  
  2043. Version    2.3        Last change: 26 May    1990               31
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050. FLEX(1)             USER COMMANDS              FLEX(1)
  2051.  
  2052.  
  2053.  
  2054.           foo{NAME}?      printf( "Found it\n" );
  2055.           %%
  2056.  
  2057.       will not match the string "foo" because when the  macro
  2058.       is  expanded    the rule is equivalent to "foo[A-Z][A-Z0-
  2059.       9]*?"    and the    precedence is such that    the '?'    is  asso-
  2060.       ciated  with    "[A-Z0-9]*".  With flex, the rule will be
  2061.       expanded to "foo([A-Z][A-Z0-9]*)?" and  so  the  string
  2062.       "foo"    will match.  Note that because of this,    the ^, $,
  2063.       <s>, /, and <<EOF>> operators    cannot be used in a  flex
  2064.       definition.
  2065.  
  2066.       The POSIX draft interpretation is the    same as    flex's.
  2067.  
  2068.      -      To specify a character class which matches anything but
  2069.       a  left  bracket  (']'),  in lex one can use "[^]]" but
  2070.       with flex one    must use "[^\]]".  The latter works  with
  2071.       lex, too.
  2072.  
  2073.      -      The lex %r (generate a Ratfor    scanner)  option  is  not
  2074.       supported.  It is not    part of    the POSIX draft.
  2075.  
  2076.      -      If you are providing your  own  yywrap()  routine,  you
  2077.       must    include    a "#undef yywrap" in the definitions sec-
  2078.       tion (section    1).  Note that the "#undef" will have  to
  2079.       be enclosed in %{}'s.
  2080.  
  2081.       The POSIX draft specifies that yywrap() is  a     function
  2082.       and  this is very unlikely to    change;    so flex    users are
  2083.       warned that yywrap() is likely to be changed to a func-
  2084.       tion in the near future.
  2085.  
  2086.      -      After    a call to unput(), yytext and  yyleng  are  unde-
  2087.       fined    until the next token is    matched.  This is not the
  2088.       case with lex    or the present POSIX draft.
  2089.  
  2090.      -      The precedence of the    {} (numeric  range)  operator  is
  2091.       different.   lex  interprets    "abc{1,3}" as "match one,
  2092.       two, or  three  occurrences  of  'abc'",  whereas  flex
  2093.       interprets  it  as "match 'ab' followed by one, two, or
  2094.       three    occurrences of 'c'".  The latter is in    agreement
  2095.       with the current POSIX draft.
  2096.  
  2097.      -      The precedence of the    ^  operator  is     different.   lex
  2098.       interprets  "^foo|bar"  as  "match  either 'foo' at the
  2099.       beginning of a line, or 'bar'    anywhere",  whereas  flex
  2100.       interprets  it  as "match either 'foo' or 'bar' if they
  2101.       come at the beginning    of a line".   The  latter  is  in
  2102.       agreement with the current POSIX draft.
  2103.  
  2104.      -      To refer to yytext outside of    the scanner source  file,
  2105.       the  correct    definition  with  flex    is  "extern  char
  2106.  
  2107.  
  2108.  
  2109. Version    2.3        Last change: 26 May    1990               32
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116. FLEX(1)             USER COMMANDS              FLEX(1)
  2117.  
  2118.  
  2119.  
  2120.       *yytext" rather than "extern char yytext[]".     This  is
  2121.       contrary  to    the  current  POSIX  draft but a point on
  2122.       which    flex will not be changing, as the array    represen-
  2123.       tation  entails  a  serious performance penalty.  It is
  2124.       hoped    that the POSIX draft will be emended  to  support
  2125.       the  flex  variety  of declaration (as this is a fairly
  2126.       painless change to require of    lex users).
  2127.  
  2128.      -      yyin is initialized by lex to    be stdin;  flex,  on  the
  2129.       other     hand,    initializes yyin to NULL and then assigns
  2130.       it to    stdin the first    time the scanner is called,  pro-
  2131.       viding yyin has not already been assigned to a non-NULL
  2132.       value.  The difference is subtle, but    the net    effect is
  2133.       that    with  flex  scanners,  yyin does not have a valid
  2134.       value    until the scanner has been called.
  2135.  
  2136.      -      The special table-size declarations  such  as     %a  sup-
  2137.       ported  by  lex are not required by flex scanners; flex
  2138.       ignores them.
  2139.  
  2140.      -      The name FLEX_SCANNER    is #define'd so    scanners  may  be
  2141.       written for use with either flex or lex.
  2142.  
  2143.      The following flex    features are not included in lex  or  the
  2144.      POSIX draft standard:
  2145.  
  2146.      yyterminate()
  2147.      <<EOF>>
  2148.      YY_DECL
  2149.      #line directives
  2150.      %{}'s around actions
  2151.      yyrestart()
  2152.      comments beginning with '#' (deprecated)
  2153.      multiple actions on a line
  2154.  
  2155.      This last feature refers to the fact that with flex you  can
  2156.      put  multiple actions on the same line, separated with semi-
  2157.      colons, while with    lex, the following
  2158.  
  2159.      foo    handle_foo(); ++num_foos_seen;
  2160.  
  2161.      is    (rather    surprisingly) truncated    to
  2162.  
  2163.      foo    handle_foo();
  2164.  
  2165.      flex does not truncate the    action.      Actions  that     are  not
  2166.      enclosed  in  braces are simply terminated    at the end of the
  2167.      line.
  2168.  
  2169. DIAGNOSTICS
  2170.      reject_used_but_not_detected       undefined           or
  2171.      yymore_used_but_not_detected  undefined  -     These errors can
  2172.  
  2173.  
  2174.  
  2175. Version    2.3        Last change: 26 May    1990               33
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182. FLEX(1)             USER COMMANDS              FLEX(1)
  2183.  
  2184.  
  2185.  
  2186.      occur at compile time.  They indicate that    the scanner  uses
  2187.      REJECT  or    yymore() but that flex failed to notice    the fact,
  2188.      meaning that flex scanned the first two sections looking for
  2189.      occurrences  of  these  actions  and failed to find any, but
  2190.      somehow you snuck some in (via a #include    file,  for  exam-
  2191.      ple).  Make an explicit reference to the action in    your flex
  2192.      input  file.   (Note  that     previously  flex   supported    a
  2193.      %used/%unused  mechanism for dealing with this problem; this
  2194.      feature is    still supported    but now    deprecated, and     will  go
  2195.      away  soon    unless the author hears    from people who    can argue
  2196.      compellingly that they need it.)
  2197.  
  2198.      flex scanner jammed - a scanner compiled with -s has encoun-
  2199.      tered  an    input  string  which wasn't matched by any of its
  2200.      rules.
  2201.  
  2202.      flex input    buffer overflowed -  a    scanner     rule  matched    a
  2203.      string  long enough to overflow the scanner's internal input
  2204.      buffer (16K bytes by default - controlled by YY_BUF_SIZE  in
  2205.      "flex.skel".   Note  that    to  redefine this macro, you must
  2206.      first #undefine it).
  2207.  
  2208.      scanner  requires    -8  flag  -  Your  scanner  specification
  2209.      includes  recognizing  8-bit  characters  and  you     did  not
  2210.      specify the -8 flag (and your site    has  not  installed  flex
  2211.      with -8 as    the default).
  2212.  
  2213.      fatal flex    scanner    internal error--end of    buffer    missed    -
  2214.      This  can    occur  in  an  scanner which is    reentered after    a
  2215.      long-jump has jumped out (or over)    the scanner's  activation
  2216.      frame.  Before reentering the scanner, use:
  2217.  
  2218.      yyrestart( yyin );
  2219.  
  2220.  
  2221.      too many %t classes! - You    managed    to put every single char-
  2222.      acter  into  its  own %t class.  flex requires that at least
  2223.      one of the    classes    share characters.
  2224.  
  2225. DEFICIENCIES / BUGS
  2226.      See flex(1).
  2227.  
  2228. SEE ALSO
  2229.      flex(1), lex(1), yacc(1), sed(1), awk(1).
  2230.  
  2231.      M.    E. Lesk    and E. Schmidt,    LEX - Lexical Analyzer Generator
  2232.  
  2233. AUTHOR
  2234.      Vern Paxson, with the help    of many    ideas and  much     inspira-
  2235.      tion  from    Van Jacobson.  Original    version    by Jef Poskanzer.
  2236.      The fast table representation is a    partial    implementation of
  2237.      a    design done by Van Jacobson.  The implementation was done
  2238.  
  2239.  
  2240.  
  2241. Version    2.3        Last change: 26 May    1990               34
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248. FLEX(1)             USER COMMANDS              FLEX(1)
  2249.  
  2250.  
  2251.  
  2252.      by    Kevin Gong and Vern Paxson.
  2253.  
  2254.      Thanks to the many    flex beta-testers, feedbackers,    and  con-
  2255.      tributors,     especially  Casey  Leedom, benson@odi.com, Keith
  2256.      Bostic, Frederic Brehm, Nick  Christopher,     Jason    Coughlin,
  2257.      Scott  David Daniels, Leo Eskin, Chris Faylor, Eric Goldman,
  2258.      Eric Hughes, Jeffrey R. Jones, Kevin B. Kenny,  Ronald  Lam-
  2259.      precht,  Greg  Lee, Craig Leres, Mohamed el Lozy, Jim Meyer-
  2260.      ing, Marc Nozell, Esmond Pitt, Jef    Poskanzer,  Jim     Roskind,
  2261.      Dave  Tallman,  Frank Whaley, Ken Yap, and    those whose names
  2262.      have slipped my marginal  mail-archiving  skills  but  whose
  2263.      contributions are appreciated all the same.
  2264.  
  2265.      Thanks to Keith Bostic, John Gilmore, Craig Leres,    Bob  Mul-
  2266.      cahy,  Rich Salz, and Richard Stallman for    help with various
  2267.      distribution headaches.
  2268.  
  2269.      Thanks to Esmond Pitt and Earle Horton for     8-bit    character
  2270.      support; to Benson    Margulies and Fred Burke for C++ support;
  2271.      to    Ove Ewerlid for    the basics of support for NUL's;  and  to
  2272.      Eric Hughes for the basics    of support for multiple    buffers.
  2273.  
  2274.      Work is being done    on extending flex to generate scanners in
  2275.      which  the     state    machine    is directly represented    in C code
  2276.      rather than tables.  These    scanners  may  well  be     substan-
  2277.      tially  faster  than those    generated using    -f or -F.  If you
  2278.      are working in this area and  are    interested  in    comparing
  2279.      notes and seeing whether redundant    work can be avoided, con-
  2280.      tact Ove Ewerlid (ewerlid@mizar.DoCS.UU.SE).
  2281.  
  2282.      This work was primarily done when I was  at  the  Real  Time
  2283.      Systems  Group at the Lawrence Berkeley Laboratory    in Berke-
  2284.      ley, CA.  Many  thanks  to     all  there  for  the  support    I
  2285.      received.
  2286.  
  2287.      Send comments to:
  2288.  
  2289.       Vern Paxson
  2290.       Computer Science Department
  2291.       4126 Upson Hall
  2292.       Cornell University
  2293.       Ithaca, NY 14853-7501
  2294.  
  2295.       vern@cs.cornell.edu
  2296.       decvax!cornell!vern
  2297.  
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307. Version    2.3        Last change: 26 May    1990               35
  2308.  
  2309.  
  2310.  
  2311.