1 .\" $NetBSD: flex.1,v 1.3 2010/09/15 06:52:33 wiz Exp $
3 .TH FLEX 1 "February 2008" "Version 2.5"
5 flex, lex \- fast lexical analyzer generator
8 .B [\-bcdfhilnpstvwBFILTV78+? \-C[aefFmr] \-ooutput \-Pprefix \-Sskeleton]
9 .B [\-\-help \-\-version]
14 a tool for generating programs that perform pattern-matching on text.
15 The manual includes both tutorial and reference sections:
19 a brief overview of the tool
23 Format Of The Input File
26 the extended regular expressions used by flex
28 How The Input Is Matched
29 the rules for determining what has been matched
32 how to specify what to do when a pattern is matched
35 details regarding the scanner that flex produces;
36 how to control the input source
39 introducing context into your scanners, and
40 managing "mini-scanners"
42 Multiple Input Buffers
43 how to manipulate multiple input sources; how to
44 scan from strings instead of files
47 special rules for matching the end of the input
50 a summary of macros available to the actions
52 Values Available To The User
53 a summary of values available to the actions
56 connecting flex scanners together with yacc parsers
59 flex command-line options, and the "%option"
62 Performance Considerations
63 how to make your scanner go as fast as possible
65 Generating C++ Scanners
66 the (experimental) facility for generating C++
69 Incompatibilities With Lex And POSIX
70 how flex differs from AT\*[Am]T lex and the POSIX lex
74 those error messages produced by flex (or scanners
75 it generates) whose meanings might not be apparent
81 known problems with flex
84 other documentation, related tools
87 includes contact information
92 is a tool for generating
94 programs which recognized lexical patterns in text.
97 the given input files, or its standard input if no file names are given,
98 for a description of a scanner to generate.
99 The description is in the form of pairs
100 of regular expressions and C code, called
103 generates as output a C source file,
105 which defines a routine
107 This file is compiled and linked with the
109 library to produce an executable.
110 When the executable is run,
111 it analyzes its input for occurrences
112 of the regular expressions.
113 Whenever it finds one, it executes
114 the corresponding C code.
115 .SH SOME SIMPLE EXAMPLES
117 First some simple examples to get the flavor of how one uses
121 input specifies a scanner which whenever it encounters the string
122 "username" will replace it with the user's login name:
126 username printf( "%s", getlogin() );
129 By default, any text not matched by a
132 is copied to the output, so the net effect of this scanner is
133 to copy its input file to its output with each occurrence
134 of "username" expanded.
135 In this input, there is just one rule.
138 and the "printf" is the
140 The "%%" marks the beginning of the rules.
142 Here's another simple example:
145 int num_lines = 0, num_chars = 0;
148 \\n ++num_lines; ++num_chars;
155 printf( "# of lines = %d, # of chars = %d\\n",
156 num_lines, num_chars );
160 This scanner counts the number of characters and the number
161 of lines in its input (it produces no output other than the
162 final report on the counts).
164 declares two globals, "num_lines" and "num_chars", which are accessible
169 routine declared after the second "%%".
170 There are two rules, one
171 which matches a newline ("\\n") and increments both the line count and
172 the character count, and one which matches any character other than
173 a newline (indicated by the "." regular expression).
175 A somewhat more complicated example:
178 /* scanner for a toy Pascal-like language */
181 /* need this for the call to atof() below */
182 #include \*[Lt]math.h\*[Gt]
191 printf( "An integer: %s (%d)\\n", yytext,
195 {DIGIT}+"."{DIGIT}* {
196 printf( "A float: %s (%g)\\n", yytext,
200 if|then|begin|end|procedure|function {
201 printf( "A keyword: %s\\n", yytext );
204 {ID} printf( "An identifier: %s\\n", yytext );
206 "+"|"-"|"*"|"/" printf( "An operator: %s\\n", yytext );
208 "{"[^}\\n]*"}" /* eat up one-line comments */
210 [ \\t\\n]+ /* eat up whitespace */
212 . printf( "Unrecognized character: %s\\n", yytext );
220 ++argv, --argc; /* skip over program name */
222 yyin = fopen( argv[0], "r" );
230 This is the beginnings of a simple scanner for a language like
232 It identifies different types of
234 and reports on what it has seen.
236 The details of this example will be explained in the following
238 .SH FORMAT OF THE INPUT FILE
241 input file consists of three sections, separated by a line with just
255 section contains declarations of simple
257 definitions to simplify the scanner specification, and declarations of
259 which are explained in a later section.
261 Name definitions have the form:
267 The "name" is a word beginning with a letter or an underscore ('_')
268 followed by zero or more letters, digits, '_', or '-' (dash).
269 The definition is taken to begin at the first non-white-space character
270 following the name and continuing to the end of the line.
271 The definition can subsequently be referred to using "{name}", which
272 will expand to "(definition)".
280 defines "DIGIT" to be a regular expression which matches a
282 "ID" to be a regular expression which matches a letter
283 followed by zero-or-more letters-or-digits.
284 A subsequent reference to
296 and matches one-or-more digits followed by a '.' followed
297 by zero-or-more digits.
303 input contains a series of rules of the form:
309 where the pattern must be unindented and the action must begin
312 See below for a further description of patterns and actions.
314 Finally, the user code section is simply copied to
317 It is used for companion routines which call or are called
319 The presence of this section is optional;
320 if it is missing, the second
322 in the input file may be skipped, too.
324 In the definitions and rules sections, any
326 text or text enclosed in
330 is copied verbatim to the output (with the %{}'s removed).
331 The %{}'s must appear unindented on lines by themselves.
333 In the rules section,
334 any indented or %{} text appearing before the
335 first rule may be used to declare variables
336 which are local to the scanning routine and (after the declarations)
337 code which is to be executed whenever the scanning routine is entered.
338 Other indented or %{} text in the rule section is still copied to the output,
339 but its meaning is not well-defined and it may well cause compile-time
340 errors (this feature is present for
342 compliance; see below for other such features).
344 In the definitions section (but not in the rules section),
345 an unindented comment (i.e., a line
346 beginning with "/*") is also copied verbatim to the output up
349 The patterns in the input are written using an extended set of regular
354 x match the character 'x'
355 . any character (byte) except newline
356 [xyz] a "character class"; in this case, the pattern
357 matches either an 'x', a 'y', or a 'z'
358 [abj-oZ] a "character class" with a range in it; matches
359 an 'a', a 'b', any letter from 'j' through 'o',
361 [^A-Z] a "negated character class", i.e., any character
362 but those in the class. In this case, any
363 character EXCEPT an uppercase letter.
364 [^A-Z\\n] any character EXCEPT an uppercase letter or
366 r* zero or more r's, where r is any regular expression
368 r? zero or one r's (that is, "an optional r")
369 r{2,5} anywhere from two to five r's
370 r{2,} two or more r's
372 {name} the expansion of the "name" definition
375 the literal string: [xyz]"foo
376 \\X if X is an 'a', 'b', 'f', 'n', 'r', 't', or 'v',
377 then the ANSI-C interpretation of \\x.
378 Otherwise, a literal 'X' (used to escape
379 operators such as '*')
380 \\0 a NUL character (ASCII code 0)
381 \\123 the character with octal value 123
382 \\x2a the character with hexadecimal value 2a
383 (r) match an r; parentheses are used to override
384 precedence (see below)
387 rs the regular expression r followed by the
388 regular expression s; called "concatenation"
391 r|s either an r or an s
394 r/s an r but only if it is followed by an s. The
395 text matched by s is included when determining
396 whether this rule is the "longest match",
397 but is then returned to the input before
398 the action is executed. So the action only
399 sees the text matched by r. This type
400 of pattern is called trailing context".
401 (There are some combinations of r/s that flex
402 cannot match correctly; see notes in the
403 Deficiencies / Bugs section below regarding
404 "dangerous trailing context".)
405 ^r an r, but only at the beginning of a line (i.e.,
406 which just starting to scan, or right after a
407 newline has been scanned).
408 r$ an r, but only at the end of a line (i.e., just
409 before a newline). Equivalent to "r/\\n".
411 Note that flex's notion of "newline" is exactly
412 whatever the C compiler used to compile flex
413 interprets '\\n' as; in particular, on some DOS
414 systems you must either filter out \\r's in the
415 input yourself, or explicitly use r/\\r\\n for "r$".
418 \*[Lt]s\*[Gt]r an r, but only in start condition s (see
419 below for discussion of start conditions)
420 \*[Lt]s1,s2,s3\*[Gt]r
421 same, but in any of start conditions s1,
423 \*[Lt]*\*[Gt]r an r in any start condition, even an exclusive one.
426 \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] an end-of-file
427 \*[Lt]s1,s2\*[Gt]\*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]
428 an end-of-file when in start condition s1 or s2
431 Note that inside of a character class, all regular expression operators
432 lose their special meaning except escape ('\\') and the character class
433 operators, '-', ']', and, at the beginning of the class, '^'.
435 The regular expressions listed above are grouped according to
436 precedence, from highest precedence at the top to lowest at the bottom.
437 Those grouped together have equal precedence.
450 since the '*' operator has higher precedence than concatenation,
451 and concatenation higher than alternation ('|').
457 the string "ba" followed by zero-or-more r's.
458 To match "foo" or zero-or-more "bar"'s, use:
464 and to match zero-or-more "foo"'s-or-"bar"'s:
471 In addition to characters and ranges of characters, character classes
472 can also contain character class
474 These are expressions enclosed inside
478 delimiters (which themselves must appear between the '[' and ']' of the
479 character class; other elements may occur inside the character class, too).
480 The valid expressions are:
483 [:alnum:] [:alpha:] [:blank:]
484 [:cntrl:] [:digit:] [:graph:]
485 [:lower:] [:print:] [:punct:]
486 [:space:] [:upper:] [:xdigit:]
489 These expressions all designate a set of characters equivalent to
490 the corresponding standard C
495 designates those characters for which
497 returns true - i.e., any alphabetic or numeric.
498 Some systems don't provide
504 For example, the following character classes are all equivalent:
513 If your scanner is case-insensitive (the
522 Some notes on patterns:
524 A negated character class such as the example "[^A-Z]"
526 .I will match a newline
527 unless "\\n" (or an equivalent escape sequence) is one of the
528 characters explicitly present in the negated character class
530 This is unlike how many other regular
531 expression tools treat negated character classes, but unfortunately
532 the inconsistency is historically entrenched.
533 Matching newlines means that a pattern like [^"]* can match the entire
534 input unless there's another quote in the input.
536 A rule can have at most one instance of trailing context (the '/' operator
537 or the '$' operator).
538 The start condition, '^', and "\*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]" patterns
539 can only occur at the beginning of a pattern, and, as well as with '/' and '$',
540 cannot be grouped inside parentheses.
541 A '^' which does not occur at
542 the beginning of a rule or a '$' which does not occur at the end of
543 a rule loses its special properties and is treated as a normal character.
545 The following are illegal:
549 \*[Lt]sc1\*[Gt]foo\*[Lt]sc2\*[Gt]bar
552 Note that the first of these, can be written "foo/bar\\n".
554 The following will result in '$' or '^' being treated as a normal character:
561 If what's wanted is a "foo" or a bar-followed-by-a-newline, the following
562 could be used (the special '|' action is explained below):
566 bar$ /* action goes here */
569 A similar trick will work for matching a foo or a
570 bar-at-the-beginning-of-a-line.
571 .SH HOW THE INPUT IS MATCHED
572 When the generated scanner is run, it analyzes its input looking
573 for strings which match any of its patterns.
574 If it finds more than
575 one match, it takes the one matching the most text (for trailing
576 context rules, this includes the length of the trailing part, even
577 though it will then be returned to the input).
579 or more matches of the same length, the
580 rule listed first in the
582 input file is chosen.
584 Once the match is determined, the text corresponding to the match
587 is made available in the global character pointer
589 and its length in the global integer
593 corresponding to the matched pattern is then executed (a more
594 detailed description of actions follows), and then the remaining
595 input is scanned for another match.
597 If no match is found, then the
599 is executed: the next character in the input is considered matched and
600 copied to the standard output.
601 Thus, the simplest legal
609 which generates a scanner that simply copies its input (one character
610 at a time) to its output.
614 can be defined in two different ways: either as a character
618 You can control which definition
620 uses by including one of the special directives
624 in the first (definitions) section of your flex input.
629 lex compatibility option, in which case
632 The advantage of using
634 is substantially faster scanning and no buffer overflow when matching
635 very large tokens (unless you run out of dynamic memory).
637 is that you are restricted in how your actions can modify
639 (see the next section), and calls to the
641 function destroys the present contents of
643 which can be a considerable porting headache when moving between different
649 is that you can then modify
651 to your heart's content, and calls to
656 Furthermore, existing
658 programs sometimes access
660 externally using declarations of the form:
662 extern char yytext[];
664 This definition is erroneous when used with
674 characters, which defaults to a fairly large value.
676 the size by simply #define'ing
678 to a different value in the first section of your
681 As mentioned above, with
683 yytext grows dynamically to accommodate large tokens.
684 While this means your
686 scanner can accommodate very large tokens (such as matching entire blocks
687 of comments), bear in mind that each time the scanner must resize
689 it also must rescan the entire token from the beginning, so matching such
690 tokens can prove slow.
694 dynamically grow if a call to
696 results in too much text being pushed back; instead, a run-time error results.
698 Also note that you cannot use
700 with C++ scanner classes
705 Each pattern in a rule has a corresponding action, which can be any
706 arbitrary C statement.
707 The pattern ends at the first non-escaped
708 whitespace character; the remainder of the line is its action.
710 action is empty, then when the pattern is matched the input token
712 For example, here is the specification for a program
713 which deletes all occurrences of "zap me" from its input:
720 (It will copy all other characters in the input to the output since
721 they will be matched by the default rule.)
723 Here is a program which compresses multiple blanks and tabs down to
724 a single blank, and throws away whitespace found at the end of a line:
728 [ \\t]+ putchar( ' ' );
729 [ \\t]+$ /* ignore this token */
733 If the action contains a '{', then the action spans till the balancing '}'
734 is found, and the action may cross multiple lines.
736 knows about C strings and comments and won't be fooled by braces found
737 within them, but also allows actions to begin with
739 and will consider the action to be all the text up to the next
741 (regardless of ordinary braces inside the action).
743 An action consisting solely of a vertical bar ('|') means "same as
744 the action for the next rule." See below for an illustration.
746 Actions can include arbitrary C code, including
748 statements to return a value to whatever routine called
752 is called it continues processing tokens from where it last left
753 off until it either reaches
754 the end of the file or executes a return.
756 Actions are free to modify
758 except for lengthening it (adding
759 characters to its end--these will overwrite later characters in the
761 This however does not apply when using
763 (see above); in that case,
765 may be freely modified in any way.
767 Actions are free to modify
769 except they should not do so if the action also includes use of
773 There are a number of special directives which can be included within
777 copies yytext to the scanner's output.
780 followed by the name of a start condition places the scanner in the
781 corresponding start condition (see below).
784 directs the scanner to proceed on to the "second best" rule which matched the
785 input (or a prefix of the input).
786 The rule is chosen as described
787 above in "How the Input is Matched", and
791 set up appropriately.
792 It may either be one which matched as much text
793 as the originally chosen rule but came later in the
795 input file, or one which matched less text.
796 For example, the following will both count the
797 words in the input and call the routine special() whenever "frob" is seen:
803 frob special(); REJECT;
804 [^ \\t\\n]+ ++word_count;
809 any "frob"'s in the input would not be counted as words, since the
810 scanner normally executes only one action per token.
813 are allowed, each one finding the next best choice to the currently
815 For example, when the following scanner scans the token
816 "abcd", it will write "abcdabcaba" to the output:
824 .|\\n /* eat up any unmatched character */
827 (The first three rules share the fourth's action since they use
828 the special '|' action.)
830 is a particularly expensive feature in terms of scanner performance;
833 of the scanner's actions it will slow down
835 of the scanner's matching.
838 cannot be used with the
844 Note also that unlike the other special actions,
848 code immediately following it in the action will
853 tells the scanner that the next time it matches a rule, the corresponding
856 onto the current value of
858 rather than replacing it.
859 For example, given the input "mega-kludge"
860 the following will write "mega-mega-kludge" to the output:
864 mega- ECHO; yymore();
868 First "mega-" is matched and echoed to the output.
870 is matched, but the previous "mega-" is still hanging around at the
875 for the "kludge" rule will actually write "mega-kludge".
877 Two notes regarding use of
881 depends on the value of
883 correctly reflecting the size of the current token, so you must not
888 Second, the presence of
890 in the scanner's action entails a minor performance penalty in the
891 scanner's matching speed.
894 returns all but the first
896 characters of the current token back to the input stream, where they
897 will be rescanned when the scanner looks for the next match.
901 are adjusted appropriately (e.g.,
906 For example, on the input "foobar" the following will write out
911 foobar ECHO; yyless(3);
917 will cause the entire current input string to be scanned again.
919 changed how the scanner will subsequently process its input (using
921 for example), this will result in an endless loop.
925 is a macro and can only be used in the flex input file, not from
931 back onto the input stream.
932 It will be the next character scanned.
933 The following action will take the current token and cause it
934 to be rescanned enclosed in parentheses.
939 /* Copy yytext because unput() trashes yytext */
940 char *yycopy = strdup( yytext );
942 for ( i = yyleng - 1; i \*[Ge] 0; --i )
951 puts the given character back at the
953 of the input stream, pushing back strings must be done back-to-front.
955 An important potential problem when using
957 is that if you are using
959 (the default), a call to
964 starting with its rightmost character and devouring one character to
965 the left with each call.
966 If you need the value of yytext preserved
969 (as in the above example),
970 you must either first copy it elsewhere, or build your scanner using
972 instead (see How The Input Is Matched).
974 Finally, note that you cannot put back
976 to attempt to mark the input stream with an end-of-file.
979 reads the next character from the input stream.
981 the following is one way to eat up C comments:
990 while ( (c = input()) != '*' \*[Am]\*[Am]
992 ; /* eat up text of comment */
996 while ( (c = input()) == '*' )
999 break; /* found the end */
1004 error( "EOF in comment" );
1011 (Note that if the scanner is compiled using
1015 is instead referred to as
1017 in order to avoid a name clash with the
1019 stream by the name of
1023 flushes the scanner's internal buffer
1024 so that the next time the scanner attempts to match a token, it will
1025 first refill the buffer using
1027 (see The Generated Scanner, below).
1028 This action is a special case
1030 .B yy_flush_buffer()
1031 function, described below in the section Multiple Input Buffers.
1034 can be used in lieu of a return statement in an action.
1036 the scanner and returns a 0 to the scanner's caller, indicating "all done".
1039 is also called when an end-of-file is encountered.
1040 It is a macro and may be redefined.
1041 .SH THE GENERATED SCANNER
1046 which contains the scanning routine
1048 a number of tables used by it for matching tokens, and a number
1049 of auxiliary routines and macros.
1052 is declared as follows:
1057 ... various definitions and the actions in here ...
1061 (If your environment supports function prototypes, then it will
1062 be "int yylex( void )".) This definition may be changed by defining
1063 the "YY_DECL" macro.
1064 For example, you could use:
1067 #define YY_DECL float lexscan( a, b ) float a, b;
1070 to give the scanning routine the name
1072 returning a float, and taking two floats as arguments.
1074 if you give arguments to the scanning routine using a
1075 K\*[Am]R-style/non-prototyped function declaration, you must terminate
1076 the definition with a semi-colon (;).
1080 is called, it scans tokens from the global input file
1082 (which defaults to stdin).
1083 It continues until it either reaches
1084 an end-of-file (at which point it returns the value 0) or
1085 one of its actions executes a
1089 If the scanner reaches an end-of-file, subsequent calls are undefined
1092 is pointed at a new input file (in which case scanning continues from
1097 takes one argument, a
1099 pointer (which can be nil, if you've set up
1101 to scan from a source other than
1105 for scanning from that file.
1106 Essentially there is no difference between
1109 to a new input file or using
1111 to do so; the latter is available for compatibility with previous versions
1114 and because it can be used to switch input files in the middle of scanning.
1115 It can also be used to throw away the current input buffer, by calling
1116 it with an argument of
1118 but better is to use
1125 reset the start condition to
1127 (see Start Conditions, below).
1131 stops scanning due to executing a
1133 statement in one of the actions, the scanner may then be called again and it
1134 will resume scanning where it left off.
1136 By default (and for purposes of efficiency), the scanner uses
1137 block-reads rather than simple
1139 calls to read characters from
1141 The nature of how it gets its input can be controlled by defining the
1144 YY_INPUT's calling sequence is "YY_INPUT(buf,result,max_size)".
1145 Its action is to place up to
1147 characters in the character array
1149 and return in the integer variable
1152 number of characters read or the constant YY_NULL (0 on Unix systems)
1154 The default YY_INPUT reads from the
1155 global file-pointer "yyin".
1157 A sample definition of YY_INPUT (in the definitions
1158 section of the input file):
1162 #define YY_INPUT(buf,result,max_size) \\
1164 int c = getchar(); \\
1165 result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \\
1170 This definition will change the input processing to occur
1171 one character at a time.
1173 When the scanner receives an end-of-file indication from YY_INPUT,
1179 returns false (zero), then it is assumed that the
1180 function has gone ahead and set up
1182 to point to another input file, and scanning continues.
1184 true (non-zero), then the scanner terminates, returning 0 to its
1186 Note that in either case, the start condition remains unchanged;
1192 If you do not supply your own version of
1194 then you must either use
1196 (in which case the scanner behaves as though
1198 returned 1), or you must link with
1200 to obtain the default version of the routine, which always returns 1.
1202 Three routines are available for scanning from in-memory buffers rather
1204 .B yy_scan_string(), yy_scan_bytes(),
1206 .B yy_scan_buffer().
1207 See the discussion of them below in the section Multiple Input Buffers.
1209 The scanner writes its
1213 global (default, stdout), which may be redefined by the user simply
1214 by assigning it to some other
1217 .SH START CONDITIONS
1219 provides a mechanism for conditionally activating rules.
1221 whose pattern is prefixed with "\*[Lt]sc\*[Gt]" will only be active when
1222 the scanner is in the start condition named "sc".
1226 \*[Lt]STRING\*[Gt][^"]* { /* eat up the string body ... */
1231 will be active only when the scanner is in the "STRING" start
1235 \*[Lt]INITIAL,STRING,QUOTE\*[Gt]\\. { /* handle an escape ... */
1240 will be active only when the current start condition is
1241 either "INITIAL", "STRING", or "QUOTE".
1244 are declared in the definitions (first) section of the input
1245 using unindented lines beginning with either
1249 followed by a list of names.
1252 start conditions, the latter
1255 A start condition is activated using the
1260 action is executed, rules with the given start
1261 condition will be active and
1262 rules with other start conditions will be inactive.
1263 If the start condition is
1265 then rules with no start conditions at all will also be active.
1270 rules qualified with the start condition will be active.
1271 A set of rules contingent on the same exclusive start condition
1272 describe a scanner which is independent of any of the other rules in the
1276 exclusive start conditions make it easy to specify "mini-scanners"
1277 which scan portions of the input that are syntactically different
1278 from the rest (e.g., comments).
1280 If the distinction between inclusive and exclusive start conditions
1281 is still a little vague, here's a simple example illustrating the
1282 connection between the two.
1289 \*[Lt]example\*[Gt]foo do_something();
1291 bar something_else();
1300 \*[Lt]example\*[Gt]foo do_something();
1302 \*[Lt]INITIAL,example\*[Gt]bar something_else();
1306 .B \*[Lt]INITIAL,example\*[Gt]
1309 pattern in the second example wouldn't be active (i.e., couldn't match)
1310 when in start condition
1313 .B \*[Lt]example\*[Gt]
1316 though, then it would only be active in
1320 while in the first example it's active in both, because in the first
1323 starting condition is an
1328 Also note that the special start-condition specifier
1330 matches every start condition.
1331 Thus, the above example could also have been written;
1337 \*[Lt]example\*[Gt]foo do_something();
1339 \*[Lt]*\*[Gt]bar something_else();
1343 The default rule (to
1345 any unmatched character) remains active in start conditions.
1350 \*[Lt]*\*[Gt].|\\n ECHO;
1355 returns to the original state where only the rules with
1356 no start conditions are active.
1357 This state can also be
1358 referred to as the start-condition "INITIAL", so
1362 (The parentheses around the start condition name are not required but
1363 are considered good style.)
1366 actions can also be given as indented code at the beginning
1367 of the rules section.
1368 For example, the following will cause
1369 the scanner to enter the "SPECIAL" start condition whenever
1371 is called and the global variable
1380 if ( enter_special )
1383 \*[Lt]SPECIAL\*[Gt]blahblahblah
1384 ...more rules follow...
1388 To illustrate the uses of start conditions,
1389 here is a scanner which provides two different interpretations
1390 of a string like "123.456".
1391 By default it will treat it as
1392 three tokens, the integer "123", a dot ('.'), and the integer "456".
1393 But if the string is preceded earlier in the line by the string
1395 it will treat it as a single token, the floating-point number
1400 #include \*[Lt]math.h\*[Gt]
1405 expect-floats BEGIN(expect);
1407 \*[Lt]expect\*[Gt][0-9]+"."[0-9]+ {
1408 printf( "found a float, = %f\\n",
1411 \*[Lt]expect\*[Gt]\\n {
1412 /* that's the end of the line, so
1413 * we need another "expect-number"
1414 * before we'll recognize any more
1421 printf( "found an integer, = %d\\n",
1425 "." printf( "found a dot\\n" );
1428 Here is a scanner which recognizes (and discards) C comments while
1429 maintaining a count of the current input line.
1436 "/*" BEGIN(comment);
1438 \*[Lt]comment\*[Gt][^*\\n]* /* eat anything that's not a '*' */
1439 \*[Lt]comment\*[Gt]"*"+[^*/\\n]* /* eat up '*'s not followed by '/'s */
1440 \*[Lt]comment\*[Gt]\\n ++line_num;
1441 \*[Lt]comment\*[Gt]"*"+"/" BEGIN(INITIAL);
1444 This scanner goes to a bit of trouble to match as much
1445 text as possible with each rule.
1446 In general, when attempting to write
1447 a high-speed scanner try to match as much possible in each rule, as
1450 Note that start-conditions names are really integer values and
1451 can be stored as such.
1452 Thus, the above could be extended in the
1462 comment_caller = INITIAL;
1468 \*[Lt]foo\*[Gt]"/*" {
1469 comment_caller = foo;
1473 \*[Lt]comment\*[Gt][^*\\n]* /* eat anything that's not a '*' */
1474 \*[Lt]comment\*[Gt]"*"+[^*/\\n]* /* eat up '*'s not followed by '/'s */
1475 \*[Lt]comment\*[Gt]\\n ++line_num;
1476 \*[Lt]comment\*[Gt]"*"+"/" BEGIN(comment_caller);
1479 Furthermore, you can access the current start condition using
1483 For example, the above assignments to
1485 could instead be written
1488 comment_caller = YY_START;
1495 (since that is what's used by AT\*[Am]T
1498 Note that start conditions do not have their own name-space; %s's and %x's
1499 declare names in the same fashion as #define's.
1501 Finally, here's an example of how to match C-style quoted strings using
1502 exclusive start conditions, including expanded escape sequences (but
1503 not including checking for a string that's too long):
1509 char string_buf[MAX_STR_CONST];
1510 char *string_buf_ptr;
1513 \\" string_buf_ptr = string_buf; BEGIN(str);
1515 \*[Lt]str\*[Gt]\\" { /* saw closing quote - all done */
1517 *string_buf_ptr = '\\0';
1518 /* return string constant token type and
1523 \*[Lt]str\*[Gt]\\n {
1524 /* error - unterminated string constant */
1525 /* generate error message */
1528 \*[Lt]str\*[Gt]\\\\[0-7]{1,3} {
1529 /* octal escape sequence */
1532 (void) sscanf( yytext + 1, "%o", \*[Am]result );
1534 if ( result \*[Gt] 0xff )
1535 /* error, constant is out-of-bounds */
1537 *string_buf_ptr++ = result;
1540 \*[Lt]str\*[Gt]\\\\[0-9]+ {
1541 /* generate error - bad escape sequence; something
1542 * like '\\48' or '\\0777777'
1546 \*[Lt]str\*[Gt]\\\\n *string_buf_ptr++ = '\\n';
1547 \*[Lt]str\*[Gt]\\\\t *string_buf_ptr++ = '\\t';
1548 \*[Lt]str\*[Gt]\\\\r *string_buf_ptr++ = '\\r';
1549 \*[Lt]str\*[Gt]\\\\b *string_buf_ptr++ = '\\b';
1550 \*[Lt]str\*[Gt]\\\\f *string_buf_ptr++ = '\\f';
1552 \*[Lt]str\*[Gt]\\\\(.|\\n) *string_buf_ptr++ = yytext[1];
1554 \*[Lt]str\*[Gt][^\\\\\\n\\"]+ {
1555 char *yptr = yytext;
1558 *string_buf_ptr++ = *yptr++;
1563 Often, such as in some of the examples above, you wind up writing a
1564 whole bunch of rules all preceded by the same start condition(s).
1565 Flex makes this a little easier and cleaner by introducing a notion of
1568 A start condition scope is begun with:
1576 is a list of one or more start conditions.
1577 Inside the start condition
1578 scope, every rule automatically has the prefix
1580 applied to it, until a
1582 which matches the initial
1588 "\\\\n" return '\\n';
1589 "\\\\r" return '\\r';
1590 "\\\\f" return '\\f';
1591 "\\\\0" return '\\0';
1598 \*[Lt]ESC\*[Gt]"\\\\n" return '\\n';
1599 \*[Lt]ESC\*[Gt]"\\\\r" return '\\r';
1600 \*[Lt]ESC\*[Gt]"\\\\f" return '\\f';
1601 \*[Lt]ESC\*[Gt]"\\\\0" return '\\0';
1604 Start condition scopes may be nested.
1606 Three routines are available for manipulating stacks of start conditions:
1608 .B void yy_push_state(int new_state)
1609 pushes the current start condition onto the top of the start condition
1610 stack and switches to
1612 as though you had used
1614 (recall that start condition names are also integers).
1616 .B void yy_pop_state()
1617 pops the top of the stack and switches to it via
1620 .B int yy_top_state()
1621 returns the top of the stack without altering the stack's contents.
1623 The start condition stack grows dynamically and so has no built-in
1625 If memory is exhausted, program execution aborts.
1627 To use start condition stacks, your scanner must include a
1629 directive (see Options below).
1630 .SH MULTIPLE INPUT BUFFERS
1631 Some scanners (such as those which support "include" files)
1632 require reading from several input streams.
1635 scanners do a large amount of buffering, one cannot control
1636 where the next input will be read from by simply writing a
1638 which is sensitive to the scanning context.
1640 is only called when the scanner reaches the end of its buffer, which
1641 may be a long time after scanning a statement such as an "include"
1642 which requires switching the input source.
1644 To negotiate these sorts of problems,
1646 provides a mechanism for creating and switching between multiple
1648 An input buffer is created by using:
1651 YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )
1656 pointer and a size and creates a buffer associated with the given
1657 file and large enough to hold
1659 characters (when in doubt, use
1664 handle, which may then be passed to other routines (see below).
1667 type is a pointer to an opaque
1668 .B struct yy_buffer_state
1669 structure, so you may safely initialize YY_BUFFER_STATE variables to
1670 .B ((YY_BUFFER_STATE) 0)
1671 if you wish, and also refer to the opaque structure in order to
1672 correctly declare input buffers in source files other than that
1676 pointer in the call to
1678 is only used as the value of
1684 so it no longer uses
1686 then you can safely pass a nil
1689 .B yy_create_buffer.
1690 You select a particular buffer to scan from using:
1693 void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
1696 switches the scanner's input buffer so subsequent tokens will
1700 .B yy_switch_to_buffer()
1701 may be used by yywrap() to set things up for continued scanning, instead
1702 of opening a new file and pointing
1705 Note also that switching input sources via either
1706 .B yy_switch_to_buffer()
1711 change the start condition.
1714 void yy_delete_buffer( YY_BUFFER_STATE buffer )
1717 is used to reclaim the storage associated with a buffer.
1720 can be nil, in which case the routine does nothing.)
1721 You can also clear the current contents of a buffer using:
1724 void yy_flush_buffer( YY_BUFFER_STATE buffer )
1727 This function discards the buffer's contents,
1728 so the next time the scanner attempts to match a token from the
1729 buffer, it will first fill the buffer anew using
1734 .B yy_create_buffer(),
1735 provided for compatibility with the C++ use of
1739 for creating and destroying dynamic objects.
1742 .B YY_CURRENT_BUFFER
1745 handle to the current buffer.
1747 Here is an example of using these features for writing a scanner
1748 which expands include files (the
1749 .B \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]
1750 feature is discussed below):
1753 /* the "incl" state is used for picking up the name
1754 * of an include file
1759 #define MAX_INCLUDE_DEPTH 10
1760 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
1761 int include_stack_ptr = 0;
1765 include BEGIN(incl);
1768 [^a-z\\n]*\\n? ECHO;
1770 \*[Lt]incl\*[Gt][ \\t]* /* eat the whitespace */
1771 \*[Lt]incl\*[Gt][^ \\t\\n]+ { /* got the include file name */
1772 if ( include_stack_ptr \*[Ge] MAX_INCLUDE_DEPTH )
1774 fprintf( stderr, "Includes nested too deeply" );
1778 include_stack[include_stack_ptr++] =
1781 yyin = fopen( yytext, "r" );
1786 yy_switch_to_buffer(
1787 yy_create_buffer( yyin, YY_BUF_SIZE ) );
1792 \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] {
1793 if ( --include_stack_ptr \*[Lt] 0 )
1800 yy_delete_buffer( YY_CURRENT_BUFFER );
1801 yy_switch_to_buffer(
1802 include_stack[include_stack_ptr] );
1807 Three routines are available for setting up input buffers for
1808 scanning in-memory strings instead of files.
1810 a new input buffer for scanning the string, and return a corresponding
1812 handle (which you should delete with
1813 .B yy_delete_buffer()
1815 They also switch to the new buffer using
1816 .B yy_switch_to_buffer(),
1819 will start scanning the string.
1821 .B yy_scan_string(const char *str)
1822 scans a NUL-terminated string.
1824 .B yy_scan_bytes(const char *bytes, int len)
1827 bytes (including possibly NUL's)
1828 starting at location
1831 Note that both of these functions create and scan a
1833 of the string or bytes.
1834 (This may be desirable, since
1836 modifies the contents of the buffer it is scanning.) You can avoid the
1839 .B yy_scan_buffer(char *base, yy_size_t size)
1840 which scans in place the buffer starting at
1844 bytes, the last two bytes of which
1847 .B YY_END_OF_BUFFER_CHAR
1849 These last two bytes are not scanned; thus, scanning
1856 If you fail to set up
1858 in this manner (i.e., forget the final two
1859 .B YY_END_OF_BUFFER_CHAR
1862 returns a nil pointer instead of creating a new input buffer.
1866 is an integral type to which you can cast an integer expression
1867 reflecting the size of the buffer.
1868 .SH END-OF-FILE RULES
1869 The special rule "\*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]" indicates
1870 actions which are to be taken when an end-of-file is
1871 encountered and yywrap() returns non-zero (i.e., indicates
1872 no further files to process).
1873 The action must finish
1874 by doing one of four things:
1878 to a new input file (in previous versions of flex, after doing the
1879 assignment you had to call the special action
1881 this is no longer necessary);
1887 executing the special
1891 or, switching to a new buffer using
1892 .B yy_switch_to_buffer()
1893 as shown in the example above.
1895 \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] rules may not be used with other
1896 patterns; they may only be qualified with a list of start
1898 If an unqualified \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] rule is given, it
1901 start conditions which do not already have \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] actions.
1903 specify an \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] rule for only the initial start condition, use
1906 \*[Lt]INITIAL\*[Gt]\*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]
1910 These rules are useful for catching things like unclosed comments.
1917 ...other rules for dealing with quotes...
1919 \*[Lt]quote\*[Gt]\*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] {
1920 error( "unterminated quote" );
1923 \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt] {
1925 yyin = fopen( *filelist, "r" );
1931 .SH MISCELLANEOUS MACROS
1934 can be defined to provide an action
1935 which is always executed prior to the matched rule's action.
1937 it could be #define'd to call a routine to convert yytext to lower-case.
1940 is invoked, the variable
1942 gives the number of the matched rule (rules are numbered starting with 1).
1943 Suppose you want to profile how often each of your rules is matched.
1944 The following would do the trick:
1947 #define YY_USER_ACTION ++ctr[yy_act]
1952 is an array to hold the counts for the different rules.
1955 gives the total number of rules (including the default rule, even if
1958 so a correct declaration for
1963 int ctr[YY_NUM_RULES];
1969 may be defined to provide an action which is always executed before
1970 the first scan (and before the scanner's internal initializations are done).
1971 For example, it could be used to call a routine to read
1972 in a data table or open a logging file.
1975 .B yy_set_interactive(is_interactive)
1976 can be used to control whether the current buffer is considered
1978 An interactive buffer is processed more slowly,
1979 but must be used when the scanner's input source is indeed
1980 interactive to avoid problems due to waiting to fill buffers
1981 (see the discussion of the
1985 in the macro invocation marks the buffer as interactive, a zero
1986 value as non-interactive.
1987 Note that use of this macro overrides
1988 .B %option always-interactive
1990 .B %option never-interactive
1991 (see Options below).
1992 .B yy_set_interactive()
1993 must be invoked prior to beginning to scan the buffer that is
1994 (or is not) to be considered interactive.
1997 .B yy_set_bol(at_bol)
1998 can be used to control whether the current buffer's scanning
1999 context for the next token match is done as though at the
2000 beginning of a line.
2001 A non-zero macro argument makes rules anchored with
2002 '^' active, while a zero argument makes '^' rules inactive.
2006 returns true if the next token scanned from the current buffer
2007 will have '^' rules active, false otherwise.
2009 In the generated scanner, the actions are all gathered in one large
2010 switch statement and separated using
2012 which may be redefined.
2013 By default, it is simply a "break", to separate
2014 each rule's action from the following rule's.
2017 allows, for example, C++ users to
2018 #define YY_BREAK to do nothing (while being very careful that every
2019 rule ends with a "break" or a "return"!) to avoid suffering from
2020 unreachable statement warnings where because a rule's action ends with
2024 .SH VALUES AVAILABLE TO THE USER
2025 This section summarizes the various values available to the user
2026 in the rule actions.
2029 holds the text of the current token.
2030 It may be modified but not lengthened
2031 (you cannot append characters to the end).
2033 If the special directive
2035 appears in the first section of the scanner description, then
2038 .B char yytext[YYLMAX],
2041 is a macro definition that you can redefine in the first section
2042 if you don't like the default value (generally 8KB).
2045 results in somewhat slower scanners, but the value of
2047 becomes immune to calls to
2051 which potentially destroy its value when
2053 is a character pointer.
2058 which is the default.
2062 when generating C++ scanner classes
2068 holds the length of the current token.
2071 is the file which by default
2074 It may be redefined but doing so only makes sense before
2075 scanning begins or after an EOF has been encountered.
2076 Changing it in the midst of scanning will have unexpected results since
2078 buffers its input; use
2081 Once scanning terminates because an end-of-file
2082 has been seen, you can assign
2084 at the new input file and then call the scanner again to continue scanning.
2086 .B void yyrestart( FILE *new_file )
2087 may be called to point
2089 at the new input file.
2090 The switch-over to the new file is immediate
2091 (any previously buffered-up input is lost).
2096 as an argument thus throws away the current input buffer and continues
2097 scanning the same input file.
2100 is the file to which
2103 It can be reassigned by the user.
2105 .B YY_CURRENT_BUFFER
2108 handle to the current buffer.
2111 returns an integer value corresponding to the current start
2113 You can subsequently use this value with
2115 to return to that start condition.
2116 .SH INTERFACING WITH YACC
2117 One of the main uses of
2119 is as a companion to the
2123 parsers expect to call a routine named
2125 to find the next input token.
2126 The routine is supposed to
2127 return the type of the next token as well as putting any associated
2138 to instruct it to generate the file
2140 containing definitions of all the
2145 This file is then included in the
2148 For example, if one of the tokens is "TOK_NUMBER",
2149 part of the scanner might look like:
2158 [0-9]+ yylval = atoi( yytext ); return TOK_NUMBER;
2163 has the following options:
2166 Generate backing-up information to
2168 This is a list of scanner states which require backing up
2169 and the input characters on which they do so.
2171 can remove backing-up states.
2174 backing-up states are eliminated and
2178 is used, the generated scanner will run faster (see the
2181 Only users who wish to squeeze every last cycle out of their
2182 scanners need worry about this option.
2183 (See the section on Performance Considerations below.)
2186 is a do-nothing, deprecated option included for POSIX compliance.
2189 makes the generated scanner run in
2192 Whenever a pattern is recognized and the global
2194 is non-zero (which is the default),
2195 the scanner will write to
2200 --accepting rule at line 53 ("the matched text")
2203 The line number refers to the location of the rule in the file
2204 defining the scanner (i.e., the file that was fed to flex).
2205 Messages are also generated when the scanner backs up, accepts the
2206 default rule, reaches the end of its input buffer (or encounters
2207 a NUL; at this point, the two look the same as far as the scanner's concerned),
2208 or reaches an end-of-file.
2213 No table compression is done and stdio is bypassed.
2214 The result is large but fast.
2215 This option is equivalent to
2220 generates a "help" summary of
2231 .B \-i, \-\-case-insensitive
2237 The case of letters given in the
2240 be ignored, and tokens in the input will be matched regardless of case.
2241 The matched text given in
2243 will have the preserved case (i.e., it will not be folded).
2245 .B \-l, \-\-lex\-compat
2246 turns on maximum compatibility with the original AT\*[Am]T
2249 Note that this does not mean
2252 Use of this option costs a considerable amount of
2253 performance, and it cannot be used with the
2254 .B \-+, -f, -F, -Cf,
2258 For details on the compatibilities it provides, see the section
2259 "Incompatibilities With Lex And POSIX" below.
2260 This option also results
2262 .B YY_FLEX_LEX_COMPAT
2263 being #define'd in the generated scanner.
2266 is another do-nothing, deprecated option included only for
2269 .B \-p, \-\-perf\-report
2270 generates a performance report to stderr.
2271 The report consists of comments regarding features of the
2273 input file which will cause a serious loss of performance in the resulting
2275 If you give the flag twice, you will also get comments regarding
2276 features that lead to minor performance losses.
2278 Note that the use of
2280 .B %option yylineno,
2281 and variable trailing context (see the Deficiencies / Bugs section below)
2282 entails a substantial performance penalty; use of
2289 flag entail minor performance penalties.
2291 .B \-s, \-\-no\-default
2294 (that unmatched scanner input is echoed to
2297 If the scanner encounters input that does not
2298 match any of its rules, it aborts with an error.
2300 useful for finding holes in a scanner's rule set.
2305 to write the scanner it generates to standard output instead
2314 a summary of statistics regarding the scanner it generates.
2315 Most of the statistics are meaningless to the casual
2317 user, but the first line identifies the version of
2319 (same as reported by
2321 and the next line the flags used when generating the scanner, including
2322 those that are on by default.
2325 suppresses warning messages.
2332 scanner, the opposite of
2334 scanners generated by
2341 that your scanner will never be used interactively, and you want to
2344 more performance out of it.
2345 If your goal is instead to squeeze out a
2347 more performance, you should be using the
2351 options (discussed below), which turn on
2353 automatically anyway.
2359 scanner table representation should be used (and stdio
2361 This representation is about as fast as the full table representation
2363 and for some sets of patterns will be considerably smaller (and for
2365 In general, if the pattern set contains both "keywords"
2366 and a catch-all, "identifier" rule, such as in the set:
2369 "case" return TOK_CASE;
2370 "switch" return TOK_SWITCH;
2372 "default" return TOK_DEFAULT;
2373 [a-z]+ return TOK_ID;
2376 then you're better off using the full table representation.
2378 the "identifier" rule is present and you then use a hash table or some such
2379 to detect the keywords, you're better off using
2382 This option is equivalent to
2385 It cannot be used with
2388 .B \-I, \-\-interactive
2394 An interactive scanner is one that only looks ahead to decide
2395 what token has been matched if it absolutely must.
2397 always looking one extra character ahead, even if the scanner has already
2398 seen enough text to disambiguate the current token, is a bit faster than
2399 only looking ahead when necessary.
2400 But scanners that always look ahead
2401 give dreadful interactive performance; for example, when a user types
2402 a newline, it is not recognized as a newline token until they enter
2404 token, which often means typing in another whole line.
2413 table-compression options (see below).
2414 That's because if you're looking
2415 for high-performance you should be using one of these options, so if you
2418 assumes you'd rather trade off a bit of run-time performance for intuitive
2419 interactive behavior.
2428 Thus, this option is not really needed; it is on by default for all those
2429 cases in which it is allowed.
2431 You can force a scanner to
2433 be interactive by using
2443 Without this option,
2445 peppers the generated scanner
2446 with #line directives so error messages in the actions will be correctly
2447 located with respect to either the original
2449 input file (if the errors are due to code in the input file), or
2453 fault -- you should report these sorts of errors to the email address
2462 It will generate a lot of messages to
2465 the form of the input and the resultant non-deterministic and deterministic
2467 This option is mostly for use in maintaining
2471 prints the version number to
2481 to generate a 7-bit scanner, i.e., one which can only recognized 7-bit
2482 characters in its input.
2483 The advantage of using
2485 is that the scanner's tables can be up to half the size of those generated
2489 The disadvantage is that such scanners often hang
2490 or crash if their input contains an 8-bit character.
2492 Note, however, that unless you generate your scanner using the
2496 table compression options, use of
2498 will save only a small amount of table space, and make your scanner
2499 considerably less portable.
2501 default behavior is to generate an 8-bit scanner unless you use the
2507 defaults to generating 7-bit scanners unless your site was always
2508 configured to generate 8-bit scanners (as will often be the case
2509 with non-USA sites).
2510 You can tell whether flex generated a 7-bit
2511 or an 8-bit scanner by inspecting the flag summary in the
2513 output as described above.
2515 Note that if you use
2519 (those table compression options, but also using equivalence classes as
2520 discussed see below), flex still defaults to generating an 8-bit
2521 scanner, since usually with these compression options full 8-bit tables
2522 are not much more expensive than 7-bit tables.
2527 to generate an 8-bit scanner, i.e., one which can recognize 8-bit
2529 This flag is only needed for scanners generated using
2533 as otherwise flex defaults to generating an 8-bit scanner anyway.
2535 See the discussion of
2537 above for flex's default behavior and the tradeoffs between 7-bit
2541 specifies that you want flex to generate a C++
2543 See the section on Generating C++ Scanners below for
2547 controls the degree of table compression and, more generally, trade-offs
2548 between small scanners and fast scanners.
2551 ("align") instructs flex to trade off larger tables in the
2552 generated scanner for faster performance because the elements of
2553 the tables are better aligned for memory access and computation.
2555 RISC architectures, fetching and manipulating longwords is more efficient
2556 than with smaller-sized units such as shortwords.
2558 double the size of the tables used by your scanner.
2564 .I equivalence classes,
2565 i.e., sets of characters
2566 which have identical lexical properties (for example, if the only
2567 appearance of digits in the
2569 input is in the character class
2570 "[0-9]" then the digits '0', '1', ..., '9' will all be put
2571 in the same equivalence class).
2572 Equivalence classes usually give
2573 dramatic reductions in the final table/object file sizes (typically
2574 a factor of 2-5) and are pretty cheap performance-wise (one array
2575 look-up per character scanned).
2580 scanner tables should be generated -
2582 should not compress the
2583 tables by taking advantages of similar transition functions for
2587 specifies that the alternative fast scanner representation (described
2592 This option cannot be used with
2595 .B \-Cm, \-\-meta-ecs
2599 .I meta-equivalence classes,
2600 which are sets of equivalence classes (or characters, if equivalence
2601 classes are not being used) that are commonly used together.
2603 classes are often a big win when using compressed tables, but they
2604 have a moderate performance impact (one or two "if" tests and one
2605 array look-up per character scanned).
2608 causes the generated scanner to
2610 use of the standard I/O library (stdio) for input.
2615 the scanner will use the
2617 system call, resulting in a performance gain which varies from system
2618 to system, but in general is probably negligible unless you are also using
2624 can cause strange behavior if, for example, you read from
2626 using stdio prior to calling the scanner (because the scanner will miss
2627 whatever text your previous reads left in the stdio input buffer).
2630 has no effect if you define
2632 (see The Generated Scanner above).
2636 specifies that the scanner tables should be compressed but neither
2637 equivalence classes nor meta-equivalence classes should be used.
2645 do not make sense together - there is no opportunity for meta-equivalence
2646 classes if the table is not being compressed.
2647 Otherwise the options
2648 may be freely mixed, and are cumulative.
2650 The default setting is
2652 which specifies that
2654 should generate equivalence classes
2655 and meta-equivalence classes.
2656 This setting provides the highest degree of table compression.
2658 faster-executing scanners at the cost of larger tables with
2659 the following generally being true:
2662 slowest \*[Am] smallest
2670 fastest \*[Am] largest
2673 Note that scanners with the smallest tables are usually generated and
2674 compiled the quickest, so
2675 during development you will usually want to use the default, maximal
2679 is often a good compromise between speed and size for production
2682 .B \-ooutput, \-\-outputfile=FILE
2683 directs flex to write the scanner to the file
2691 option, then the scanner is written to
2697 option above) refer to the file
2700 .B \-Pprefix, \-\-prefix=STRING
2705 for all globally-visible variable and function names to instead be
2713 It also changes the name of the default output file from
2717 Here are all of the names affected:
2725 yy_load_buffer_state
2737 (If you are using a C++ scanner, then only
2742 Within your scanner itself, you can still refer to the global variables
2743 and functions using either version of their name; but externally, they
2744 have the modified name.
2746 This option lets you easily link together multiple
2748 programs into the same executable.
2749 Note, though, that using this option also renames
2754 provide your own (appropriately-named) version of the routine for your
2756 .B %option noyywrap,
2759 no longer provides one for you by default.
2761 .B \-Sskeleton_file, \-\-skel=FILE
2762 overrides the default skeleton file from which
2764 constructs its scanners.
2765 You'll never need this option unless you are doing
2767 maintenance or development.
2769 .B \-X, \-\-posix\-compat
2770 maximal compatibility with POSIX lex.
2773 track line count in yylineno.
2778 .B \-\-header\-file=FILE
2779 create a C header file in addition to the scanner.
2781 .B \-\-tables\-file[=FILE]
2782 write tables to FILE.
2785 #define macro defn (default defn is '1').
2787 .B \-R, \-\-reentrant
2788 generate a reentrant C scanner
2790 .B \-\-bison\-bridge
2791 scanner for bison pure parser.
2793 .B \-\-bison\-locations
2794 include yylloc support.
2797 initialize yyin/yyout to stdin/stdout.
2799 .B \-\-noansi\-definitions old\-style function definitions.
2801 .B \-\-noansi\-prototypes
2802 empty parameter list in prototypes.
2805 do not include \*[Lt]unistd.h\*[Gt].
2808 do not generate a particular FUNCTION.
2811 also provides a mechanism for controlling options within the
2812 scanner specification itself, rather than from the flex command-line.
2813 This is done by including
2815 directives in the first section of the scanner specification.
2816 You can specify multiple options with a single
2818 directive, and multiple directives in the first section of your flex input
2821 Most options are given simply as names, optionally preceded by the
2822 word "no" (with no intervening whitespace) to negate their meaning.
2823 A number are equivalent to flex flags or their negation:
2834 case-sensitive opposite of -i (default)
2840 default opposite of -s option
2844 interactive -I option
2845 lex-compat -l option
2847 perf-report -p option
2851 warn opposite of -w option
2852 (use "%option nowarn" for -w)
2854 array equivalent to "%array"
2855 pointer equivalent to "%pointer" (default)
2860 provide features otherwise not available:
2862 .B always-interactive
2863 instructs flex to generate a scanner which always considers its input
2865 Normally, on each new input file the scanner calls
2867 in an attempt to determine whether
2868 the scanner's input source is interactive and thus should be read a
2869 character at a time.
2870 When this option is used, however, then no
2874 directs flex to provide a default
2876 program for the scanner, which simply calls
2882 .B never-interactive
2883 instructs flex to generate a scanner which never considers its input
2884 "interactive" (again, no call made to
2886 This is the opposite of
2887 .B always-interactive.
2890 enables the use of start condition stacks (see Start Conditions above).
2903 instead of the default of
2907 programs depend on this behavior, even though it is not compliant with
2908 ANSI C, which does not require
2912 to be compile-time constant.
2917 to generate a scanner that maintains the number of the current line
2918 read from its input in the global variable
2920 This option is implied by
2921 .B %option lex-compat.
2925 .B %option noyywrap),
2926 makes the scanner not call
2928 upon an end-of-file, but simply assume that there are no more
2929 files to scan (until the user points
2931 at a new file and calls
2936 scans your rule actions to determine whether you use the
2945 options are available to override its decision as to whether you use the
2946 options, either by setting them (e.g.,
2948 to indicate the feature is indeed used, or
2949 unsetting them to indicate it actually is not used
2951 .B %option noyymore).
2953 Three options take string-delimited values, offset with '=':
2956 %option outfile="ABC"
2964 %option prefix="XYZ"
2972 %option yyclass="foo"
2975 only applies when generating a C++ scanner (
2980 that you have derived
2986 will place your actions in the member function
2989 .B yyFlexLexer::yylex().
2991 .B yyFlexLexer::yylex()
2992 member function that emits a run-time error (by invoking
2993 .B yyFlexLexer::LexerError())
2995 See Generating C++ Scanners, below, for additional information.
2997 A number of options are available for lint purists who want to suppress
2998 the appearance of unneeded routines in the generated scanner.
2999 Each of the following, if unset
3002 ), results in the corresponding routine not appearing in
3003 the generated scanner:
3007 yy_push_state, yy_pop_state, yy_top_state
3008 yy_scan_buffer, yy_scan_bytes, yy_scan_string
3013 and friends won't appear anyway unless you use
3015 .SH PERFORMANCE CONSIDERATIONS
3016 The main design goal of
3018 is that it generate high-performance scanners.
3019 It has been optimized
3020 for dealing well with large sets of rules.
3021 Aside from the effects on scanner speed of the table compression
3023 options outlined above,
3024 there are a number of options/actions which degrade performance.
3025 These are, from most expensive to least:
3030 arbitrary trailing context
3032 pattern sets that require backing up
3035 %option always-interactive
3037 '^' beginning-of-line operator
3041 with the first three all being quite expensive and the last two
3045 is implemented as a routine call that potentially does quite a bit of
3048 is a quite-cheap macro; so if just putting back some excess text you
3053 should be avoided at all costs when performance is important.
3054 It is a particularly expensive option.
3056 Getting rid of backing up is messy and often may be an enormous
3057 amount of work for a complicated scanner.
3058 In principal, one begins by using the
3063 For example, on the input
3067 foo return TOK_KEYWORD;
3068 foobar return TOK_KEYWORD;
3071 the file looks like:
3074 State #6 is non-accepting -
3075 associated rule line numbers:
3077 out-transitions: [ o ]
3078 jam-transitions: EOF [ \\001-n p-\\177 ]
3080 State #8 is non-accepting -
3081 associated rule line numbers:
3083 out-transitions: [ a ]
3084 jam-transitions: EOF [ \\001-` b-\\177 ]
3086 State #9 is non-accepting -
3087 associated rule line numbers:
3089 out-transitions: [ r ]
3090 jam-transitions: EOF [ \\001-q s-\\177 ]
3092 Compressed tables always back up.
3095 The first few lines tell us that there's a scanner state in
3096 which it can make a transition on an 'o' but not on any other
3097 character, and that in that state the currently scanned text does not match
3099 The state occurs when trying to match the rules found
3100 at lines 2 and 3 in the input file.
3101 If the scanner is in that state and then reads
3102 something other than an 'o', it will have to back up to find
3103 a rule which is matched.
3104 With a bit of headscratching one can see that this must be the
3105 state it's in when it has seen "fo".
3106 When this has happened,
3107 if anything other than another 'o' is seen, the scanner will
3108 have to back up to simply match the 'f' (by the default rule).
3110 The comment regarding State #8 indicates there's a problem
3111 when "foob" has been scanned.
3112 Indeed, on any character other
3113 than an 'a', the scanner will have to back up to accept "foo".
3114 Similarly, the comment for State #9 concerns when "fooba" has
3115 been scanned and an 'r' does not follow.
3117 The final comment reminds us that there's no point going to
3118 all the trouble of removing backing up from the rules unless
3123 since there's no performance gain doing so with compressed scanners.
3125 The way to remove the backing up is to add "error" rules:
3129 foo return TOK_KEYWORD;
3130 foobar return TOK_KEYWORD;
3135 /* false alarm, not really a keyword */
3141 Eliminating backing up among a list of keywords can also be
3142 done using a "catch-all" rule:
3146 foo return TOK_KEYWORD;
3147 foobar return TOK_KEYWORD;
3149 [a-z]+ return TOK_ID;
3152 This is usually the best solution when appropriate.
3154 Backing up messages tend to cascade.
3155 With a complicated set of rules it's not uncommon to get hundreds
3157 If one can decipher them, though, it often
3158 only takes a dozen or so rules to eliminate the backing up (though
3159 it's easy to make a mistake and have an error rule accidentally match
3163 feature will be to automatically add rules to eliminate backing up).
3165 It's important to keep in mind that you gain the benefits of eliminating
3166 backing up only if you eliminate
3168 instance of backing up.
3169 Leaving just one means you gain nothing.
3172 trailing context (where both the leading and trailing parts do not have
3173 a fixed length) entails almost the same performance loss as
3175 (i.e., substantial).
3176 So when possible a rule like:
3180 mouse|rat/(cat|dog) run();
3187 mouse/cat|dog run();
3195 mouse|rat/cat run();
3196 mouse|rat/dog run();
3199 Note that here the special '|' action does
3201 provide any savings, and can even make things worse (see
3202 Deficiencies / Bugs below).
3204 Another area where the user can increase a scanner's performance
3205 (and one that's easier to implement) arises from the fact that
3206 the longer the tokens matched, the faster the scanner will run.
3207 This is because with long tokens the processing of most input
3208 characters takes place in the (short) inner scanning loop, and
3209 does not often have to go through the additional work of setting up
3210 the scanning environment (e.g.,
3213 Recall the scanner for C comments:
3220 "/*" BEGIN(comment);
3222 \*[Lt]comment\*[Gt][^*\\n]*
3223 \*[Lt]comment\*[Gt]"*"+[^*/\\n]*
3224 \*[Lt]comment\*[Gt]\\n ++line_num;
3225 \*[Lt]comment\*[Gt]"*"+"/" BEGIN(INITIAL);
3228 This could be sped up by writing it as:
3235 "/*" BEGIN(comment);
3237 \*[Lt]comment\*[Gt][^*\\n]*
3238 \*[Lt]comment\*[Gt][^*\\n]*\\n ++line_num;
3239 \*[Lt]comment\*[Gt]"*"+[^*/\\n]*
3240 \*[Lt]comment\*[Gt]"*"+[^*/\\n]*\\n ++line_num;
3241 \*[Lt]comment\*[Gt]"*"+"/" BEGIN(INITIAL);
3244 Now instead of each newline requiring the processing of another
3245 action, recognizing the newlines is "distributed" over the other rules
3246 to keep the matched text as long as possible.
3251 slow down the scanner! The speed of the scanner is independent
3252 of the number of rules or (modulo the considerations given at the
3253 beginning of this section) how complicated the rules are with
3254 regard to operators such as '*' and '|'.
3256 A final example in speeding up a scanner: suppose you want to scan
3257 through a file containing identifiers and keywords, one per line
3258 and with no other extraneous characters, and recognize all the
3260 A natural first approach is:
3269 while /* it's a keyword */
3271 .|\\n /* it's not a keyword */
3274 To eliminate the back-tracking, introduce a catch-all rule:
3283 while /* it's a keyword */
3286 .|\\n /* it's not a keyword */
3289 Now, if it's guaranteed that there's exactly one word per line,
3290 then we can reduce the total number of matches by a half by
3291 merging in the recognition of newlines with that of the other
3301 while\\n /* it's a keyword */
3304 .|\\n /* it's not a keyword */
3307 One has to be careful here, as we have now reintroduced backing up
3309 In particular, while
3311 know that there will never be any characters in the input stream
3312 other than letters or newlines,
3314 can't figure this out, and it will plan for possibly needing to back up
3315 when it has scanned a token like "auto" and then the next character
3316 is something other than a newline or a letter.
3318 then just match the "auto" rule and be done, but now it has no "auto"
3319 rule, only a "auto\\n" rule.
3320 To eliminate the possibility of backing up,
3321 we could either duplicate all rules but without final newlines, or,
3322 since we never expect to encounter such an input and therefore don't
3323 how it's classified, we can introduce one more catch-all rule, this
3324 one which doesn't include a newline:
3333 while\\n /* it's a keyword */
3337 .|\\n /* it's not a keyword */
3342 this is about as fast as one can get a
3344 scanner to go for this particular problem.
3348 is slow when matching NUL's, particularly when a token contains
3350 It's best to write rules which match
3352 amounts of text if it's anticipated that the text will often include NUL's.
3354 Another final note regarding performance: as mentioned above in the section
3355 How the Input is Matched, dynamically resizing
3357 to accommodate huge tokens is a slow process because it presently requires that
3358 the (huge) token be rescanned from the beginning.
3359 Thus if performance is
3360 vital, you should attempt to match "large" quantities of text but not
3361 "huge" quantities, where the cutoff between the two is at about 8K
3363 .SH GENERATING C++ SCANNERS
3365 provides two different ways to generate scanners for use with C++.
3366 The first way is to simply compile a scanner generated by
3368 using a C++ compiler instead of a C compiler.
3369 You should not encounter
3370 any compilations errors (please report any you find to the email address
3371 given in the Author section below).
3372 You can then use C++ code in your rule actions instead of C code.
3373 Note that the default input source for your scanner remains
3375 and default echoing is still done to
3377 Both of these remain
3379 variables and not C++
3384 to generate a C++ scanner class, using the
3386 option (or, equivalently,
3388 which is automatically specified if the name of the flex
3389 executable ends in a '+', such as
3391 When using this option, flex defaults to generating the scanner to the file
3395 The generated scanner includes the header file
3397 which defines the interface to two C++ classes.
3401 provides an abstract base class defining the general scanner class
3403 It provides the following member functions:
3405 .B const char* YYText()
3406 returns the text of the most recently matched token, the equivalent of
3410 returns the length of the most recently matched token, the equivalent of
3413 .B int lineno() const
3414 returns the current input line number
3416 .B %option yylineno),
3423 .B void set_debug( int flag )
3424 sets the debugging flag for the scanner, equivalent to assigning to
3426 (see the Options section above).
3427 Note that you must build the scanner using
3429 to include debugging information in it.
3431 .B int debug() const
3432 returns the current setting of the debugging flag.
3434 Also provided are member functions equivalent to
3435 .B yy_switch_to_buffer(),
3436 .B yy_create_buffer()
3437 (though the first argument is an
3439 object pointer and not a
3441 .B yy_flush_buffer(),
3442 .B yy_delete_buffer(),
3445 (again, the first argument is a
3449 The second class defined in
3453 which is derived from
3455 It defines the following additional member functions:
3458 yyFlexLexer( std::istream* arg_yyin = 0, std::ostream* arg_yyout = 0 )
3461 object using the given streams for input and output.
3462 If not specified, the streams default to
3468 .B virtual int yylex()
3469 performs the same role is
3471 does for ordinary flex scanners: it scans the input stream, consuming
3472 tokens, until a rule's action returns a value.
3473 If you derive a subclass
3477 and want to access the member functions and variables of
3481 then you need to use
3482 .B %option yyclass="S"
3485 that you will be using that subclass instead of
3487 In this case, rather than generating
3488 .B yyFlexLexer::yylex(),
3492 (and also generates a dummy
3493 .B yyFlexLexer::yylex()
3495 .B yyFlexLexer::LexerError()
3499 virtual void switch_streams(std::istream* new_in = 0,
3501 std::ostream* new_out = 0)
3511 (ditto), deleting the previous input buffer if
3516 int yylex( std::istream* new_in, std::ostream* new_out = 0 )
3517 first switches the input streams via
3518 .B switch_streams( new_in, new_out )
3519 and then returns the value of
3524 defines the following protected virtual functions which you can redefine
3525 in derived classes to tailor the scanner:
3528 virtual int LexerInput( char* buf, int max_size )
3533 and returns the number of characters read.
3534 To indicate end-of-input, return 0 characters.
3535 Note that "interactive" scanners (see the
3539 flags) define the macro
3543 and need to take different actions depending on whether or not
3544 the scanner might be scanning an interactive input source, you can
3545 test for the presence of this name via
3549 virtual void LexerOutput( const char* buf, int size )
3552 characters from the buffer
3554 which, while NUL-terminated, may also contain "internal" NUL's if
3555 the scanner's rules can match text with NUL's in them.
3558 virtual void LexerError( const char* msg )
3559 reports a fatal error message.
3560 The default version of this function writes the message to the stream
3569 Thus you can use such objects to create reentrant scanners.
3570 You can instantiate multiple instances of the same
3572 class, and you can also combine multiple C++ scanner classes together
3573 in the same program using the
3575 option discussed above.
3577 Finally, note that the
3579 feature is not available to C++ scanner classes; you must use
3583 Here is an example of a simple C++ scanner:
3586 // An example of using the flex C++ scanner class.
3592 string \\"[^\\n"]+\\"
3598 name ({alpha}|{dig}|\\$)({alpha}|{dig}|[_.\\-/$])*
3599 num1 [-+]?{dig}+\\.?([eE][-+]?{dig}+)?
3600 num2 [-+]?{dig}*\\.{dig}+([eE][-+]?{dig}+)?
3601 number {num1}|{num2}
3605 {ws} /* skip blanks and tabs */
3610 while((c = yyinput()) != 0)
3617 if((c = yyinput()) == '/')
3625 {number} cout \*[Lt]\*[Lt] "number " \*[Lt]\*[Lt] YYText() \*[Lt]\*[Lt] '\\n';
3629 {name} cout \*[Lt]\*[Lt] "name " \*[Lt]\*[Lt] YYText() \*[Lt]\*[Lt] '\\n';
3631 {string} cout \*[Lt]\*[Lt] "string " \*[Lt]\*[Lt] YYText() \*[Lt]\*[Lt] '\\n';
3635 int main( int /* argc */, char** /* argv */ )
3637 FlexLexer* lexer = new yyFlexLexer;
3638 while(lexer-\*[Gt]yylex() != 0)
3643 If you want to create multiple (different) lexer classes, you use the
3647 option) to rename each
3651 You then can include
3652 .B \*[Lt]FlexLexer.h\*[Gt]
3653 in your other sources once per lexer class, first renaming
3659 #define yyFlexLexer xxFlexLexer
3660 #include \*[Lt]FlexLexer.h\*[Gt]
3663 #define yyFlexLexer zzFlexLexer
3664 #include \*[Lt]FlexLexer.h\*[Gt]
3667 if, for example, you used
3668 .B %option prefix="xx"
3669 for one of your scanners and
3670 .B %option prefix="zz"
3673 IMPORTANT: the present form of the scanning class is
3675 and may change considerably between major releases.
3676 .SH INCOMPATIBILITIES WITH LEX AND POSIX
3678 is a rewrite of the AT\*[Am]T Unix
3680 tool (the two implementations do not share any code, though),
3681 with some extensions and incompatibilities, both of which
3682 are of concern to those who wish to write scanners acceptable
3683 to either implementation.
3684 Flex is fully compliant with the POSIX
3686 specification, except that when using
3688 (the default), a call to
3690 destroys the contents of
3692 which is counter to the POSIX specification.
3694 In this section we discuss all of the known areas of incompatibility
3695 between flex, AT\*[Am]T lex, and the POSIX specification.
3699 option turns on maximum compatibility with the original AT\*[Am]T
3701 implementation, at the cost of a major loss in the generated scanner's
3703 We note below which incompatibilities can be overcome
3709 is fully compatible with
3711 with the following exceptions:
3715 scanner internal variable
3717 is not supported unless
3724 should be maintained on a per-buffer basis, rather than a per-scanner
3725 (single global variable) basis.
3728 is not part of the POSIX specification.
3732 routine is not redefinable, though it may be called to read characters
3733 following whatever has been matched by a rule.
3736 encounters an end-of-file the normal
3739 A ``real'' end-of-file is returned by
3744 Input is instead controlled by defining the
3752 cannot be redefined is in accordance with the POSIX specification,
3753 which simply does not specify any way of controlling the
3754 scanner's input other than by making an initial assignment to
3759 routine is not redefinable.
3760 This restriction is in accordance with POSIX.
3763 scanners are not as reentrant as
3766 In particular, if you have an interactive scanner and
3767 an interrupt handler which long-jumps out of the scanner, and
3768 the scanner is subsequently called again, you may get the following
3772 fatal flex scanner internal error--end of buffer missed
3775 To reenter the scanner, first use
3781 Note that this call will throw away any buffered input; usually this
3782 isn't a problem with an interactive scanner.
3784 Also note that flex C++ scanner classes
3786 reentrant, so if using C++ is an option for you, you should use
3788 See "Generating C++ Scanners" above for details.
3794 macro is done to the file-pointer
3800 is not part of the POSIX specification.
3803 does not support exclusive start conditions (%x), though they
3804 are in the POSIX specification.
3806 When definitions are expanded,
3808 encloses them in parentheses.
3809 With lex, the following:
3814 foo{NAME}? printf( "Found it\\n" );
3818 will not match the string "foo" because when the macro
3819 is expanded the rule is equivalent to "foo[A-Z][A-Z0-9]*?"
3820 and the precedence is such that the '?' is associated with
3824 the rule will be expanded to
3825 "foo([A-Z][A-Z0-9]*)?" and so the string "foo" will match.
3827 Note that if the definition begins with
3833 expanded with parentheses, to allow these operators to appear in
3834 definitions without losing their special meanings.
3836 .B \*[Lt]s\*[Gt], /,
3838 .B \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]
3839 operators cannot be used in a
3847 behavior of no parentheses around the definition.
3849 The POSIX specification is that the definition be enclosed in parentheses.
3851 Some implementations of
3853 allow a rule's action to begin on a separate line, if the rule's pattern
3854 has trailing whitespace:
3858 foo|bar\*[Lt]space here\*[Gt]
3859 { foobar_action(); }
3863 does not support this feature.
3868 (generate a Ratfor scanner) option is not supported.
3870 of the POSIX specification.
3875 is undefined until the next token is matched, unless the scanner
3878 This is not the case with
3880 or the POSIX specification.
3883 option does away with this incompatibility.
3885 The precedence of the
3887 (numeric range) operator is different.
3889 interprets "abc{1,3}" as "match one, two, or
3890 three occurrences of 'abc'", whereas
3892 interprets it as "match 'ab'
3893 followed by one, two, or three occurrences of 'c'".
3894 The latter is in agreement with the POSIX specification.
3896 The precedence of the
3898 operator is different.
3900 interprets "^foo|bar" as "match either 'foo' at the beginning of a line,
3901 or 'bar' anywhere", whereas
3903 interprets it as "match either 'foo' or 'bar' if they come at the beginning
3905 The latter is in agreement with the POSIX specification.
3907 The special table-size declarations such as
3920 is #define'd so scanners may be written for use with either
3924 Scanners also include
3925 .B YY_FLEX_MAJOR_VERSION
3927 .B YY_FLEX_MINOR_VERSION
3928 indicating which version of
3930 generated the scanner
3931 (for example, for the 2.5 release, these defines would be 2 and 5
3936 features are not included in
3938 or the POSIX specification:
3943 start condition scopes
3944 start condition stacks
3945 interactive/non-interactive scanners
3946 yy_scan_string() and friends
3948 yy_set_interactive()
3951 \*[Lt]\*[Lt]EOF\*[Gt]\*[Gt]
3958 %{}'s around actions
3959 multiple actions on a line
3962 plus almost all of the flex flags.
3963 The last feature in the list refers to the fact that with
3965 you can put multiple actions on the same line, separated with
3966 semi-colons, while with
3971 foo handle_foo(); ++num_foos_seen;
3974 is (rather surprisingly) truncated to
3981 does not truncate the action.
3982 Actions that are not enclosed in
3983 braces are simply terminated at the end of the line.
3986 .I warning, rule cannot be matched
3987 indicates that the given rule
3988 cannot be matched because it follows other rules that will
3989 always match the same text as it.
3991 example, in the following "foo" cannot be matched because it comes after
3992 an identifier "catch-all" rule:
3995 [a-z]+ got_identifier();
4001 in a scanner suppresses this warning.
4006 option given but default rule can be matched
4007 means that it is possible (perhaps only in a particular start condition)
4008 that the default rule (match any single character) is the only one
4009 that will match a particular input.
4012 was given, presumably this is not intended.
4014 .I reject_used_but_not_detected undefined
4016 .I yymore_used_but_not_detected undefined -
4017 These errors can occur at compile time.
4018 They indicate that the scanner uses
4024 failed to notice the fact, meaning that
4026 scanned the first two sections looking for occurrences of these actions
4027 and failed to find any, but somehow you snuck some in (via a #include
4033 to indicate to flex that you really do use these features.
4035 .I flex scanner jammed -
4036 a scanner compiled with
4038 has encountered an input string which wasn't matched by
4040 This error can also occur due to internal problems.
4042 .I token too large, exceeds YYLMAX -
4045 and one of its rules matched a string longer than the
4047 constant (8K bytes by default).
4048 You can increase the value by
4051 in the definitions section of your
4055 .I scanner requires \-8 flag to
4056 .I use the character 'x' -
4057 Your scanner specification includes recognizing the 8-bit character
4059 and you did not specify the \-8 flag, and your scanner defaulted to 7-bit
4060 because you used the
4064 table compression options.
4065 See the discussion of the
4069 .I flex scanner push-back overflow -
4072 to push back so much text that the scanner's buffer could not hold
4073 both the pushed-back text and the current token in
4075 Ideally the scanner should dynamically resize the buffer in this case, but at
4076 present it does not.
4079 input buffer overflow, can't enlarge buffer because scanner uses REJECT -
4080 the scanner was working on matching an extremely large token and needed
4081 to expand the input buffer.
4082 This doesn't work with scanners that use
4087 fatal flex scanner internal error--end of buffer missed -
4088 This can occur in an scanner which is reentered after a long-jump
4089 has jumped out (or over) the scanner's activation frame.
4090 Before reentering the scanner, use:
4096 or, as noted above, switch to using the C++ scanner class.
4098 .I too many start conditions in \*[Lt]\*[Gt] construct! -
4099 you listed more start conditions in a \*[Lt]\*[Gt] construct than exist (so
4100 you must have listed at least one of them twice).
4104 library with which scanners must be linked.
4107 generated scanner (called
4112 generated C++ scanner class, when using
4115 .I \*[Lt]FlexLexer.h\*[Gt]
4116 header file defining the C++ scanner base class,
4118 and its derived class,
4123 This file is only used when building flex, not when flex executes.
4126 backing-up information for
4131 .SH DEFICIENCIES / BUGS
4133 Some trailing context
4134 patterns cannot be properly matched and generate
4135 warning messages ("dangerous trailing context").
4136 These are patterns where the ending of the
4137 first part of the rule matches the beginning of the second
4138 part, such as "zx*/xy*", where the 'x*' matches the 'x' at
4139 the beginning of the trailing context.
4140 (Note that the POSIX draft
4141 states that the text matched by such patterns is undefined.)
4143 For some trailing context rules, parts which are actually fixed-length are
4144 not recognized as such, leading to the above mentioned performance loss.
4145 In particular, parts using '|' or {n} (such as "foo{3}") are always
4146 considered variable-length.
4148 Combining trailing context with the special '|' action can result in
4150 trailing context being turned into the more expensive
4153 For example, in the following:
4164 invalidates yytext and yyleng, unless the
4169 option has been used.
4171 Pattern-matching of NUL's is substantially slower than matching other
4174 Dynamic resizing of the input buffer is slow, as it entails rescanning
4175 all the text matched so far by the current (generally huge) token.
4177 Due to both buffering of input and read-ahead, you cannot intermix
4178 calls to \*[Lt]stdio.h\*[Gt] routines, such as, for example,
4182 rules and expect it to work.
4187 The total table entries listed by the
4189 flag excludes the number of table entries needed to determine
4190 what rule has been matched.
4191 The number of entries is equal
4192 to the number of DFA states if the scanner does not use
4194 and somewhat greater than the number of states if it does.
4197 cannot be used with the
4205 internal algorithms need documentation.
4208 lex(1), yacc(1), sed(1), awk(1).
4210 John Levine, Tony Mason, and Doug Brown,
4212 O'Reilly and Associates.
4213 Be sure to get the 2nd edition.
4215 M. E. Lesk and E. Schmidt,
4216 .I LEX \- Lexical Analyzer Generator
4218 Alfred Aho, Ravi Sethi and Jeffrey Ullman,
4219 .I Compilers: Principles, Techniques and Tools,
4220 Addison-Wesley (1986).
4221 Describes the pattern-matching techniques used by
4223 (deterministic finite automata).
4225 Vern Paxson, with the help of many ideas and much inspiration from
4227 Original version by Jef Poskanzer.
4229 representation is a partial implementation of a design done by Van
4231 The implementation was done by Kevin Gong and Vern Paxson.
4235 beta-testers, feedbackers, and contributors, especially Francois Pinard,
4238 Stan Adermann, Terry Allen, David Barker-Plummer, John Basrai,
4239 Neal Becker, Nelson H.F. Beebe, benson@odi.com,
4240 Karl Berry, Peter A. Bigot, Simon Blanchard,
4241 Keith Bostic, Frederic Brehm, Ian Brockbank, Kin Cho, Nick Christopher,
4242 Brian Clapper, J.T. Conklin,
4243 Jason Coughlin, Bill Cox, Nick Cropper, Dave Curtis, Scott David
4244 Daniels, Chris G. Demetriou, Theo de Raadt,
4245 Mike Donahue, Chuck Doucette, Tom Epperly, Leo Eskin,
4246 Chris Faylor, Chris Flatters, Jon Forrest, Jeffrey Friedl,
4247 Joe Gayda, Kaveh R. Ghazi, Wolfgang Glunz,
4248 Eric Goldman, Christopher M. Gould, Ulrich Grepel, Peer Griebel,
4249 Jan Hajic, Charles Hemphill, NORO Hideo,
4250 Jarkko Hietaniemi, Scott Hofmann,
4251 Jeff Honig, Dana Hudes, Eric Hughes, John Interrante,
4252 Ceriel Jacobs, Michal Jaegermann, Sakari Jalovaara, Jeffrey R. Jones,
4253 Henry Juengst, Klaus Kaempf, Jonathan I. Kamens, Terrence O Kane,
4254 Amir Katz, ken@ken.hilco.com, Kevin B. Kenny,
4255 Steve Kirsch, Winfried Koenig, Marq Kole, Ronald Lamprecht,
4256 Greg Lee, Rohan Lenard, Craig Leres, John Levine, Steve Liddle,
4257 David Loffredo, Mike Long,
4258 Mohamed el Lozy, Brian Madsen, Malte, Joe Marshall,
4259 Bengt Martensson, Chris Metcalf,
4260 Luke Mewburn, Jim Meyering, R. Alexander Milowski, Erik Naggum,
4261 G.T. Nicol, Landon Noll, James Nordby, Marc Nozell,
4262 Richard Ohnemus, Karsten Pahnke,
4263 Sven Panne, Roland Pesch, Walter Pelissero, Gaumond
4264 Pierre, Esmond Pitt, Jef Poskanzer, Joe Rahmeh, Jarmo Raiha,
4265 Frederic Raimbault, Pat Rankin, Rick Richardson,
4266 Kevin Rodgers, Kai Uwe Rommel, Jim Roskind, Alberto Santini,
4267 Andreas Scherer, Darrell Schiebel, Raf Schietekat,
4268 Doug Schmidt, Philippe Schnoebelen, Andreas Schwab,
4269 Larry Schwimmer, Alex Siegel, Eckehard Stolz, Jan-Erik Strvmquist,
4270 Mike Stump, Paul Stuart, Dave Tallman, Ian Lance Taylor,
4271 Chris Thewalt, Richard M. Timoney, Jodi Tsai,
4272 Paul Tuinenga, Gary Weik, Frank Whaley, Gerhard Wilhelms, Kent Williams, Ken
4273 Yap, Ron Zellar, Nathan Zelle, David Zuhn,
4274 and those whose names have slipped my marginal
4275 mail-archiving skills but whose contributions are appreciated all the
4278 Thanks to Keith Bostic, Jon Forrest, Noah Friedman,
4279 John Gilmore, Craig Leres, John Levine, Bob Mulcahy, G.T.
4280 Nicol, Francois Pinard, Rich Salz, and Richard Stallman for help with various
4281 distribution headaches.
4283 Thanks to Esmond Pitt and Earle Horton for 8-bit character support; to
4284 Benson Margulies and Fred Burke for C++ support; to Kent Williams and Tom
4285 Epperly for C++ class support; to Ove Ewerlid for support of NUL's; and to
4286 Eric Hughes for support of multiple buffers.
4288 This work was primarily done when I was with the Real Time Systems Group
4289 at the Lawrence Berkeley Laboratory in Berkeley, CA.
4290 Many thanks to all there for the support I received.
4292 Send comments to vern@ee.lbl.gov.