OSSP CVS Repository |
|
<HTML> <HEAD> <!-- Created by texi2html 1.56k from flex.texi on 9 November 2002 --> <TITLE>flex: a fast lexical analyzer generator</TITLE> </HEAD> <BODY> <H1>Flex, version 2.5.23</H1> <H2>A fast scanner generator</H2> <H2>Edition 2.5.23, 21 October 2002</H2> <ADDRESS>Vern Paxson</ADDRESS> <ADDRESS>W. L. Estes</ADDRESS> <ADDRESS>John Millaway</ADDRESS> <P> <P><HR><P> <H1><A NAME="SEC1" HREF="flex_toc.html#TOC1">Copyright</A></H1> <P> <A NAME="IDX1"></A> <A NAME="IDX2"></A> <P> The flex manual is placed under the same licensing conditions as the rest of flex: <P> Copyright (C) 1990, 1997 The Regents of the University of California. All rights reserved. <P> This code is derived from software contributed to Berkeley by Vern Paxson. <P> The United States Government has rights in this work pursuant to contract no. DE-AC03-76SF00098 between the United States Department of Energy and the University of California. <P> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: <OL> <LI> Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. <LI> Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. </OL> <P> Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. <P> THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. <H1><A NAME="SEC2" HREF="flex_toc.html#TOC2">Reporting Bugs</A></H1> <P> <A NAME="IDX3"></A> <A NAME="IDX4"></A> <P> If you have problems with <CODE>flex</CODE> or think you have found a bug, please send mail detailing your problem to <A HREF="mailto:help-flex@gnu.org">help-flex@gnu.org</A>. Patches are always welcome. <H1><A NAME="SEC3" HREF="flex_toc.html#TOC3">Introduction</A></H1> <P> <A NAME="IDX5"></A> <CODE>flex</CODE> is a tool for generating <EM>scanners</EM>. A scanner is a program which recognizes lexical patterns in text. The <CODE>flex</CODE> program reads the given input files, or its standard input if no file names are given, for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called <EM>rules</EM>. <CODE>flex</CODE> generates as output a C source file, <TT>`lex.yy.c'</TT> by default, which defines a routine <CODE>yylex()</CODE>. This file can be compiled and linked with the flex runtime library to produce an executable. When the executable is run, it analyzes its input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding C code. <H1><A NAME="SEC4" HREF="flex_toc.html#TOC4">Some Simple Examples</A></H1> <P> First some simple examples to get the flavor of how one uses <CODE>flex</CODE>. <P> <A NAME="IDX6"></A> The following <CODE>flex</CODE> input specifies a scanner which, when it encounters the string <SAMP>`username'</SAMP> will replace it with the user's login name: <PRE> @verbatim %% username printf( "%s", getlogin() ); </PRE> <P> <A NAME="IDX7"></A> <A NAME="IDX8"></A> By default, any text not matched by a <CODE>flex</CODE> scanner is copied to the output, so the net effect of this scanner is to copy its input file to its output with each occurrence of <SAMP>`username'</SAMP> expanded. In this input, there is just one rule. <SAMP>`username'</SAMP> is the <EM>pattern</EM> and the <SAMP>`printf'</SAMP> is the <EM>action</EM>. The <SAMP>`%%'</SAMP> symbol marks the beginning of the rules. <P> Here's another simple example: <P> <A NAME="IDX9"></A> <PRE> @verbatim int num_lines = 0, num_chars = 0; %% \n ++num_lines; ++num_chars; . ++num_chars; %% main() { yylex(); printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars ); } </PRE> <P> This scanner counts the number of characters and the number of lines in its input. It produces no output other than the final report on the character and line counts. The first line declares two globals, <CODE>num_lines</CODE> and <CODE>num_chars</CODE>, which are accessible both inside <CODE>yylex()</CODE> and in the <CODE>main()</CODE> routine declared after the second <SAMP>`%%'</SAMP>. There are two rules, one which matches a newline (<SAMP>`\n'</SAMP>) and increments both the line count and the character count, and one which matches any character other than a newline (indicated by the <SAMP>`.'</SAMP> regular expression). <P> A somewhat more complicated example: <P> <A NAME="IDX10"></A> <PRE> @verbatim /* scanner for a toy Pascal-like language */ %{ /* need this for the call to atof() below */ #include math.h> %} DIGIT [0-9] ID [a-z][a-z0-9]* %% {DIGIT}+ { printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) ); } {DIGIT}+"."{DIGIT}* { printf( "A float: %s (%g)\n", yytext, atof( yytext ) ); } if|then|begin|end|procedure|function { printf( "A keyword: %s\n", yytext ); } {ID} printf( "An identifier: %s\n", yytext ); "+"|"-"|"*"|"/" printf( "An operator: %s\n", yytext ); "{"[\^{}}\n]*"}" /* eat up one-line comments */ [ \t\n]+ /* eat up whitespace */ . printf( "Unrecognized character: %s\n", yytext ); %% main( argc, argv ) int argc; char **argv; { ++argv, --argc; /* skip over program name */ if ( argc > 0 ) yyin = fopen( argv[0], "r" ); else yyin = stdin; yylex(); } </PRE> <P> This is the beginnings of a simple scanner for a language like Pascal. It identifies different types of <EM>tokens</EM> and reports on what it has seen. <P> The details of this example will be explained in the following sections. <H1><A NAME="SEC5" HREF="flex_toc.html#TOC5">Format of the Input File</A></H1> <P> <A NAME="IDX11"></A> <A NAME="IDX12"></A> <A NAME="IDX13"></A> <A NAME="IDX14"></A> <P> The <CODE>flex</CODE> input file consists of three sections, separated by a line containing only <SAMP>`%%'</SAMP>. <P> <A NAME="IDX15"></A> <PRE> @verbatim definitions %% rules %% user code </PRE> <H2><A NAME="SEC6" HREF="flex_toc.html#TOC6">Format of the Definitions Section</A></H2> <P> <A NAME="IDX16"></A> <A NAME="IDX17"></A> The <EM>definitions section</EM> contains declarations of simple <EM>name</EM> definitions to simplify the scanner specification, and declarations of <EM>start conditions</EM>, which are explained in a later section. <P> <A NAME="IDX18"></A> <A NAME="IDX19"></A> Name definitions have the form: <PRE> @verbatim name definition </PRE> <P> The <SAMP>`name'</SAMP> is a word beginning with a letter or an underscore (<SAMP>`_'</SAMP>) followed by zero or more letters, digits, <SAMP>`_'</SAMP>, or <SAMP>`-'</SAMP> (dash). The definition is taken to begin at the first non-whitespace character following the name and continuing to the end of the line. The definition can subsequently be referred to using <SAMP>`{name}'</SAMP>, which will expand to <SAMP>`(definition)'</SAMP>. For example, <P> <A NAME="IDX20"></A> <A NAME="IDX21"></A> <PRE> @verbatim DIGIT [0-9] ID [a-z][a-z0-9]* </PRE> <P> Defines <SAMP>`DIGIT'</SAMP> to be a regular expression which matches a single digit, and <SAMP>`ID'</SAMP> to be a regular expression which matches a letter followed by zero-or-more letters-or-digits. A subsequent reference to <P> <A NAME="IDX22"></A> <PRE> @verbatim {DIGIT}+"."{DIGIT}* </PRE> <P> is identical to <PRE> @verbatim ([0-9])+"."([0-9])* </PRE> <P> and matches one-or-more digits followed by a <SAMP>`.'</SAMP> followed by zero-or-more digits. <P> <A NAME="IDX23"></A> An unindented comment (i.e., a line beginning with <SAMP>`/*'</SAMP>) is copied verbatim to the output up to the next <SAMP>`*/'</SAMP>. <P> <A NAME="IDX24"></A> <A NAME="IDX25"></A> <A NAME="IDX26"></A> Any <EM>indented</EM> text or text enclosed in <SAMP>`%{'</SAMP> and <SAMP>`%}'</SAMP> is also copied verbatim to the output (with the %{ and %} symbols removed). The %{ and %} symbols must appear unindented on lines by themselves. <H2><A NAME="SEC7" HREF="flex_toc.html#TOC7">Format of the Rules Section</A></H2> <P> <A NAME="IDX27"></A> <A NAME="IDX28"></A> The <EM>rules</EM> section of the <CODE>flex</CODE> input contains a series of rules of the form: <PRE> @verbatim pattern action </PRE> <P> where the pattern must be unindented and the action must begin on the same line. See section <A HREF="flex.html#SEC10">Patterns</A>, for a further description of patterns and actions. <P> In the rules section, any indented or %{ %} enclosed text appearing before the first rule may be used to declare variables which are local to the scanning routine and (after the declarations) code which is to be executed whenever the scanning routine is entered. Other indented or %{ %} text in the rule section is still copied to the output, but its meaning is not well-defined and it may well cause compile-time errors (this feature is present for @acronym{POSIX} compliance. See section <A HREF="flex.html#SEC42">Incompatibilities with Lex and Posix</A>, for other such features). <P> Any <EM>indented</EM> text or text enclosed in <SAMP>`%{'</SAMP> and <SAMP>`%}'</SAMP> is copied verbatim to the output (with the %{ and %} symbols removed). The %{ and %} symbols must appear unindented on lines by themselves. <H2><A NAME="SEC8" HREF="flex_toc.html#TOC8">Format of the User Code Section</A></H2> <P> <A NAME="IDX29"></A> <A NAME="IDX30"></A> The user code section is simply copied to <TT>`lex.yy.c'</TT> verbatim. It is used for companion routines which call or are called by the scanner. The presence of this section is optional; if it is missing, the second <SAMP>`%%'</SAMP> in the input file may be skipped, too. <H2><A NAME="SEC9" HREF="flex_toc.html#TOC9">Comments in the Input</A></H2> <P> <A NAME="IDX31"></A> Flex supports C-style comments, that is, anything between /* and */ is considered a comment. Whenever flex encounters a comment, it copies the entire comment verbatim to the generated source code. Comments may appear just about anywhere, but with the following exceptions: <UL> <LI> <A NAME="IDX32"></A> Comments may not appear in the Rules Section wherever flex is expecting a regular expression. This means comments may not appear at the beginning of a line, or immediately following a list of scanner states. <LI> Comments may not appear on an <SAMP>`%option'</SAMP> line in the Definitions Section. </UL> <P> If you want to follow a simple rule, then always begin a comment on a new line, with one or more whitespace characters before the initial <SAMP>`/*'</SAMP>). This rule will work anywhere in the input file. <P> All the comments in the following example are valid: <P> <A NAME="IDX33"></A> <A NAME="IDX34"></A> <PRE> @verbatim %{ /* code block */ %} /* Definitions Section */ %x STATE_X %% /* Rules Section */ ruleA /* after regex */ { /* code block */ } /* after code block */ /* Rules Section (indented) */ <STATE_X>{ ruleC ECHO; ruleD ECHO; %{ /* code block */ %} } %% /* User Code Section */ </PRE> <H1><A NAME="SEC10" HREF="flex_toc.html#TOC10">Patterns</A></H1> <P> <A NAME="IDX35"></A> <A NAME="IDX36"></A> The patterns in the input (see section <A HREF="flex.html#SEC7">Format of the Rules Section</A>) are written using an extended set of regular expressions. These are: <P> <A NAME="IDX37"></A> <A NAME="IDX38"></A> <DL COMPACT> <DT><SAMP>`x'</SAMP> <DD> match the character 'x' <DT><SAMP>`.'</SAMP> <DD> any character (byte) except newline <A NAME="IDX39"></A> <A NAME="IDX40"></A> <A NAME="IDX41"></A> <DT><SAMP>`[xyz]'</SAMP> <DD> a <EM>character class</EM>; in this case, the pattern matches either an 'x', a 'y', or a 'z' <A NAME="IDX42"></A> <DT><SAMP>`[abj-oZ]'</SAMP> <DD> a "character class" with a range in it; matches an 'a', a 'b', any letter from 'j' through 'o', or a 'Z' <A NAME="IDX43"></A> <A NAME="IDX44"></A> <DT><SAMP>`[^A-Z]'</SAMP> <DD> a "negated character class", i.e., any character but those in the class. In this case, any character EXCEPT an uppercase letter. <DT><SAMP>`[^A-Z\n]'</SAMP> <DD> any character EXCEPT an uppercase letter or a newline <DT><SAMP>`r*'</SAMP> <DD> zero or more r's, where r is any regular expression <DT><SAMP>`r+'</SAMP> <DD> one or more r's <DT><SAMP>`r?'</SAMP> <DD> zero or one r's (that is, "an optional r") <A NAME="IDX45"></A> <DT><SAMP>`r{2,5}'</SAMP> <DD> anywhere from two to five r's <DT><SAMP>`r{2,}'</SAMP> <DD> two or more r's <DT><SAMP>`r{4}'</SAMP> <DD> exactly 4 r's <A NAME="IDX46"></A> <DT><SAMP>`{name}'</SAMP> <DD> the expansion of the <SAMP>`name'</SAMP> definition (see section <A HREF="flex.html#SEC5">Format of the Input File</A>). <A NAME="IDX47"></A> <A NAME="IDX48"></A> <DT><SAMP>`"[xyz]\"foo"'</SAMP> <DD> the literal string: <SAMP>`[xyz]"foo'</SAMP> <A NAME="IDX49"></A> <DT><SAMP>`\X'</SAMP> <DD> if X is <SAMP>`a'</SAMP>, <SAMP>`b'</SAMP>, <SAMP>`f'</SAMP>, <SAMP>`n'</SAMP>, <SAMP>`r'</SAMP>, <SAMP>`t'</SAMP>, or <SAMP>`v'</SAMP>, then the ANSI-C interpretation of <SAMP>`\x'</SAMP>. Otherwise, a literal <SAMP>`X'</SAMP> (used to escape operators such as <SAMP>`*'</SAMP>) <A NAME="IDX50"></A> <DT><SAMP>`\0'</SAMP> <DD> a NUL character (ASCII code 0) <A NAME="IDX51"></A> <DT><SAMP>`\123'</SAMP> <DD> the character with octal value 123 <DT><SAMP>`\x2a'</SAMP> <DD> the character with hexadecimal value 2a <DT><SAMP>`(r)'</SAMP> <DD> match an <SAMP>`r'</SAMP>; parentheses are used to override precedence (see below) <A NAME="IDX52"></A> <DT><SAMP>`rs'</SAMP> <DD> the regular expression <SAMP>`r'</SAMP> followed by the regular expression <SAMP>`s'</SAMP>; called <EM>concatenation</EM> <DT><SAMP>`r|s'</SAMP> <DD> either an <SAMP>`r'</SAMP> or an <SAMP>`s'</SAMP> <A NAME="IDX53"></A> <DT><SAMP>`r/s'</SAMP> <DD> an <SAMP>`r'</SAMP> but only if it is followed by an <SAMP>`s'</SAMP>. The text matched by <SAMP>`s'</SAMP> is included when determining whether this rule is the longest match, but is then returned to the input before the action is executed. So the action only sees the text matched by <SAMP>`r'</SAMP>. This type of pattern is called <EM>trailing context</EM>. (There are some combinations of <SAMP>`r/s'</SAMP> that flex cannot match correctly. See section <A HREF="flex.html#SEC52">Limitations</A>, regarding dangerous trailing context.) <A NAME="IDX54"></A> <A NAME="IDX55"></A> <DT><SAMP>`^r'</SAMP> <DD> an <SAMP>`r'</SAMP>, but only at the beginning of a line (i.e., when just starting to scan, or right after a newline has been scanned). <A NAME="IDX56"></A> <A NAME="IDX57"></A> <DT><SAMP>`r$'</SAMP> <DD> an <SAMP>`r'</SAMP>, but only at the end of a line (i.e., just before a newline). Equivalent to <SAMP>`r/\n'</SAMP>. <A NAME="IDX58"></A> Note that <CODE>flex</CODE>'s notion of "newline" is exactly whatever the C compiler used to compile <CODE>flex</CODE> interprets <SAMP>`\n'</SAMP> as; in particular, on some DOS systems you must either filter out <SAMP>`\r'</SAMP>s in the input yourself, or explicitly use <SAMP>`r/\r\n'</SAMP> for <SAMP>`r$'</SAMP>. <A NAME="IDX59"></A> <DT><SAMP>`<s>r'</SAMP> <DD> an <SAMP>`r'</SAMP>, but only in start condition <CODE>s</CODE> (see section <A HREF="flex.html#SEC14">Start Conditions</A> for discussion of start conditions). <DT><SAMP>`<s1,s2,s3>r'</SAMP> <DD> same, but in any of start conditions <CODE>s1</CODE>, <CODE>s2</CODE>, or <CODE>s3</CODE>. <DT><SAMP>`<*>r'</SAMP> <DD> an <SAMP>`r'</SAMP> in any start condition, even an exclusive one. <A NAME="IDX60"></A> <A NAME="IDX61"></A> <DT><SAMP>`<<EOF>>'</SAMP> <DD> an end-of-file. <DT><SAMP>`<s1,s2><<EOF>>'</SAMP> <DD> an end-of-file when in start condition <CODE>s1</CODE> or <CODE>s2</CODE> </DL> <P> Note that inside of a character class, all regular expression operators lose their special meaning except escape (<SAMP>`\'</SAMP>) and the character class operators, <SAMP>`-'</SAMP>, <SAMP>`]]'</SAMP>, and, at the beginning of the class, <SAMP>`^'</SAMP>. <P> <A NAME="IDX62"></A> The regular expressions listed above are grouped according to precedence, from highest precedence at the top to lowest at the bottom. Those grouped together have equal precedence (see special note on the precedence of the repeat operator, <SAMP>`{}'</SAMP>, under the documentation for the <SAMP>`--posix'</SAMP> POSIX compliance option). For example, <P> <A NAME="IDX63"></A> <PRE> @verbatim foo|bar* </PRE> <P> is the same as <PRE> @verbatim (foo)|(ba(r*)) </PRE> <P> since the <SAMP>`*'</SAMP> operator has higher precedence than concatenation, and concatenation higher than alternation (<SAMP>`|'</SAMP>). This pattern therefore matches <EM>either</EM> the string <SAMP>`foo'</SAMP> <EM>or</EM> the string <SAMP>`ba'</SAMP> followed by zero-or-more <SAMP>`r'</SAMP>'s. To match <SAMP>`foo'</SAMP> or zero-or-more repetitions of the string <SAMP>`bar'</SAMP>, use: <PRE> @verbatim foo|(bar)* </PRE> <P> And to match a sequence of zero or more repetitions of <SAMP>`foo'</SAMP> and <SAMP>`bar'</SAMP>: <P> <A NAME="IDX64"></A> <PRE> @verbatim (foo|bar)* </PRE> <P> <A NAME="IDX65"></A> In addition to characters and ranges of characters, character classes can also contain <EM>character class expressions</EM>. These are expressions enclosed inside <SAMP>`['</SAMP>: and <SAMP>`:]'</SAMP> delimiters (which themselves must appear between the <SAMP>`['</SAMP> and <SAMP>`]'</SAMP> of the character class. Other elements may occur inside the character class, too). The valid expressions are: <P> <A NAME="IDX66"></A> <PRE> @verbatim [:alnum:] [:alpha:] [:blank:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:] </PRE> <P> These expressions all designate a set of characters equivalent to the corresponding standard C <CODE>isXXX</CODE> function. For example, <SAMP>`[:alnum:]'</SAMP> designates those characters for which <CODE>isalnum()</CODE> returns true - i.e., any alphabetic or numeric character. Some systems don't provide <CODE>isblank()</CODE>, so flex defines <SAMP>`[:blank:]'</SAMP> as a blank or a tab. <P> For example, the following character classes are all equivalent: <P> <A NAME="IDX67"></A> <A NAME="IDX68"></A> <PRE> @verbatim [[:alnum:]] [[:alpha:][:digit:]] [[:alpha:][0-9]] [a-zA-Z0-9] </PRE> <P> <A NAME="IDX69"></A> If your scanner is case-insensitive (the <SAMP>`-i'</SAMP> flag), then <SAMP>`[:upper:]'</SAMP> and <SAMP>`[:lower:]'</SAMP> are equivalent to <SAMP>`[:alpha:]'</SAMP>. <P> Some notes on patterns are in order. <UL> <LI> <A NAME="IDX70"></A> <A NAME="IDX71"></A> A negated character class such as the example <SAMP>`[^A-Z]'</SAMP> above <EM>will</EM> match a newline unless <SAMP>`\n'</SAMP> (or an equivalent escape sequence) is one of the characters explicitly present in the negated character class (e.g., <SAMP>`[^A-Z\n]'</SAMP>). This is unlike how many other regular expression tools treat negated character classes, but unfortunately the inconsistency is historically entrenched. Matching newlines means that a pattern like <SAMP>`[^"]*'</SAMP> can match the entire input unless there's another quote in the input. <A NAME="IDX72"></A> <A NAME="IDX73"></A> <A NAME="IDX74"></A> <LI> A rule can have at most one instance of trailing context (the <SAMP>`/'</SAMP> operator or the <SAMP>`$'</SAMP> operator). The start condition, <SAMP>`^'</SAMP>, and <SAMP>`<<EOF>>'</SAMP> patterns can only occur at the beginning of a pattern, and, as well as with <SAMP>`/'</SAMP> and <SAMP>`$'</SAMP>, cannot be grouped inside parentheses. A <SAMP>`^'</SAMP> which does not occur at the beginning of a rule or a <SAMP>`$'</SAMP> which does not occur at the end of a rule loses its special properties and is treated as a normal character. <LI> The following are invalid: <A NAME="IDX75"></A> <PRE> @verbatim foo/bar$ <sc1>foo<sc2>bar </PRE> Note that the first of these can be written <SAMP>`foo/bar\n'</SAMP>. <LI> The following will result in <SAMP>`$'</SAMP> or <SAMP>`^'</SAMP> being treated as a normal character: <A NAME="IDX76"></A> <PRE> @verbatim foo|(bar$) foo|^bar </PRE> If the desired meaning is a <SAMP>`foo'</SAMP> or a <SAMP>`bar'</SAMP>-followed-by-a-newline, the following could be used (the special <CODE>|</CODE> action is explained below, see section <A HREF="flex.html#SEC12">Actions</A>): <A NAME="IDX77"></A> <PRE> @verbatim foo | bar$ /* action goes here */ </PRE> A similar trick will work for matching a <SAMP>`foo'</SAMP> or a <SAMP>`bar'</SAMP>-at-the-beginning-of-a-line. </UL> <H1><A NAME="SEC11" HREF="flex_toc.html#TOC11">How the Input Is Matched</A></H1> <P> <A NAME="IDX78"></A> <A NAME="IDX79"></A> <A NAME="IDX80"></A> <A NAME="IDX81"></A> <A NAME="IDX82"></A> <A NAME="IDX83"></A> When the generated scanner is run, it analyzes its input looking for strings which match any of its patterns. If it finds more than one match, it takes the one matching the most text (for trailing context rules, this includes the length of the trailing part, even though it will then be returned to the input). If it finds two or more matches of the same length, the rule listed first in the <CODE>flex</CODE> input file is chosen. <P> <A NAME="IDX84"></A> <A NAME="IDX85"></A> <A NAME="IDX86"></A> Once the match is determined, the text corresponding to the match (called the <EM>token</EM>) is made available in the global character pointer <CODE>yytext</CODE>, and its length in the global integer <CODE>yyleng</CODE>. The <EM>action</EM> corresponding to the matched pattern is then executed (see section <A HREF="flex.html#SEC12">Actions</A>), and then the remaining input is scanned for another match. <P> <A NAME="IDX87"></A> If no match is found, then the <EM>default rule</EM> is executed: the next character in the input is considered matched and copied to the standard output. Thus, the simplest valid <CODE>flex</CODE> input is: <P> <A NAME="IDX88"></A> <PRE> @verbatim %% </PRE> <P> which generates a scanner that simply copies its input (one character at a time) to its output. <P> <A NAME="IDX89"></A> <A NAME="IDX90"></A> <A NAME="IDX91"></A> <A NAME="IDX92"></A> Note that <CODE>yytext</CODE> can be defined in two different ways: either as a character <EM>pointer</EM> or as a character <EM>array</EM>. You can control which definition <CODE>flex</CODE> uses by including one of the special directives <CODE>%pointer</CODE> or <CODE>%array</CODE> in the first (definitions) section of your flex input. The default is <CODE>%pointer</CODE>, unless you use the <SAMP>`-l'</SAMP> lex compatibility option, in which case <CODE>yytext</CODE> will be an array. The advantage of using <CODE>%pointer</CODE> is substantially faster scanning and no buffer overflow when matching very large tokens (unless you run out of dynamic memory). The disadvantage is that you are restricted in how your actions can modify <CODE>yytext</CODE> (see section <A HREF="flex.html#SEC12">Actions</A>), and calls to the <CODE>unput()</CODE> function destroys the present contents of <CODE>yytext</CODE>, which can be a considerable porting headache when moving between different <CODE>lex</CODE> versions. <P> <A NAME="IDX93"></A> The advantage of <CODE>%array</CODE> is that you can then modify <CODE>yytext</CODE> to your heart's content, and calls to <CODE>unput()</CODE> do not destroy <CODE>yytext</CODE> (see section <A HREF="flex.html#SEC12">Actions</A>). Furthermore, existing <CODE>lex</CODE> programs sometimes access <CODE>yytext</CODE> externally using declarations of the form: <PRE> @verbatim extern char yytext[]; </PRE> <P> This definition is erroneous when used with <CODE>%pointer</CODE>, but correct for <CODE>%array</CODE>. <P> The <CODE>%array</CODE> declaration defines <CODE>yytext</CODE> to be an array of <CODE>YYLMAX</CODE> characters, which defaults to a fairly large value. You can change the size by simply #define'ing <CODE>YYLMAX</CODE> to a different value in the first section of your <CODE>flex</CODE> input. As mentioned above, with <CODE>%pointer</CODE> yytext grows dynamically to accommodate large tokens. While this means your <CODE>%pointer</CODE> scanner can accommodate very large tokens (such as matching entire blocks of comments), bear in mind that each time the scanner must resize <CODE>yytext</CODE> it also must rescan the entire token from the beginning, so matching such tokens can prove slow. <CODE>yytext</CODE> presently does <EM>not</EM> dynamically grow if a call to <CODE>unput()</CODE> results in too much text being pushed back; instead, a run-time error results. <P> <A NAME="IDX94"></A> Also note that you cannot use <CODE>%array</CODE> with C++ scanner classes (see section <A HREF="flex.html#SEC28">Generating C++ Scanners</A>). <H1><A NAME="SEC12" HREF="flex_toc.html#TOC12">Actions</A></H1> <P> <A NAME="IDX95"></A> <P> Each pattern in a rule has a corresponding action, which can be any arbitrary C statement. The pattern ends at the first non-escaped whitespace character; the remainder of the line is its action. If the action is empty, then when the pattern is matched the input token is simply discarded. For example, here is the specification for a program which deletes all occurrences of <SAMP>`zap me'</SAMP> from its input: <P> <A NAME="IDX96"></A> <PRE> @verbatim %% "zap me" </PRE> <P> (It will copy all other characters in the input to the output since they will be matched by the default rule.) <P> Here is a program which compresses multiple blanks and tabs down to a single blank, and throws away whitespace found at the end of a line: <P> <A NAME="IDX97"></A> <A NAME="IDX98"></A> <PRE> @verbatim %% [ \t]+ putchar( ' ' ); [ \t]+$ /* ignore this token */ </PRE> <P> <A NAME="IDX99"></A> <A NAME="IDX100"></A> <A NAME="IDX101"></A> <A NAME="IDX102"></A> <A NAME="IDX103"></A> <P> If the action contains a <SAMP>`}'</SAMP>, then the action spans till the balancing <SAMP>`}'</SAMP> is found, and the action may cross multiple lines. <CODE>flex</CODE> knows about C strings and comments and won't be fooled by braces found within them, but also allows actions to begin with <SAMP>`%{'</SAMP> and will consider the action to be all the text up to the next <SAMP>`%}'</SAMP> (regardless of ordinary braces inside the action). <P> An action consisting solely of a vertical bar ('|') means "same as the action for the next rule". See below for an illustration. <P> Actions can include arbitrary C code, including <CODE>return</CODE> statements to return a value to whatever routine called <CODE>yylex()</CODE>. Each time <CODE>yylex()</CODE> is called it continues processing tokens from where it last left off until it either reaches the end of the file or executes a return. <P> <A NAME="IDX104"></A> Actions are free to modify <CODE>yytext</CODE> except for lengthening it (adding characters to its end--these will overwrite later characters in the input stream). This however does not apply when using <CODE>%array</CODE> (see section <A HREF="flex.html#SEC11">How the Input Is Matched</A>). In that case, <CODE>yytext</CODE> may be freely modified in any way. <P> <A NAME="IDX105"></A> <A NAME="IDX106"></A> Actions are free to modify <CODE>yyleng</CODE> except they should not do so if the action also includes use of <CODE>yymore()</CODE> (see below). <P> <A NAME="IDX107"></A> <P> There are a number of special directives which can be included within an action: <DL COMPACT> <DT><CODE>ECHO</CODE> <DD> <A NAME="IDX108"></A> copies yytext to the scanner's output. <A NAME="IDX109"></A> <DT><CODE>BEGIN</CODE> <DD> followed by the name of a start condition places the scanner in the corresponding start condition (see below). <A NAME="IDX110"></A> <DT><CODE>REJECT</CODE> <DD> directs the scanner to proceed on to the "second best" rule which matched the input (or a prefix of the input). The rule is chosen as described above in section <A HREF="flex.html#SEC11">How the Input Is Matched</A>, and <CODE>yytext</CODE> and <CODE>yyleng</CODE> set up appropriately. It may either be one which matched as much text as the originally chosen rule but came later in the <CODE>flex</CODE> input file, or one which matched less text. For example, the following will both count the words in the input and call the routine <CODE>special()</CODE> whenever <SAMP>`frob'</SAMP> is seen: <A NAME="IDX111"></A> <A NAME="IDX112"></A> <PRE> @verbatim int word_count = 0; %% frob special(); REJECT; [^ \t\n]+ ++word_count; </PRE> Without the <CODE>REJECT</CODE>, any occurences of <SAMP>`frob'</SAMP> in the input would not be counted as words, since the scanner normally executes only one action per token. Multiple uses of <CODE>REJECT</CODE> are allowed, each one finding the next best choice to the currently active rule. For example, when the following scanner scans the token <SAMP>`abcd'</SAMP>, it will write <SAMP>`abcdabcaba'</SAMP> to the output: <A NAME="IDX113"></A> <PRE> @verbatim %% a | ab | abc | abcd ECHO; REJECT; .|\n /* eat up any unmatched character */ </PRE> (The first three rules share the fourth's action since they use the special '|' action.) <CODE>REJECT</CODE> is a particularly expensive feature in terms of scanner performance; if it is used in <EM>any</EM> of the scanner's actions it will slow down <EM>all</EM> of the scanner's matching. Furthermore, <CODE>REJECT</CODE> cannot be used with the <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP> options (see section <A HREF="flex.html#SEC20">Scanner Options</A>). Note also that unlike the other special actions, <CODE>REJECT</CODE> is a <EM>branch</EM>. code immediately following it in the action will <EM>not</EM> be executed. <A NAME="IDX114"></A> <DT><CODE>yymore()</CODE> <DD> tells the scanner that the next time it matches a rule, the corresponding token should be <EM>appended</EM> onto the current value of <CODE>yytext</CODE> rather than replacing it. For example, given the input <SAMP>`mega-kludge'</SAMP> the following will write <SAMP>`mega-mega-kludge'</SAMP> to the output: <A NAME="IDX115"></A> <A NAME="IDX116"></A> <PRE> @verbatim %% mega- ECHO; yymore(); kludge ECHO; </PRE> First <SAMP>`mega-'</SAMP> is matched and echoed to the output. Then <SAMP>`kludge'</SAMP> is matched, but the previous <SAMP>`mega-'</SAMP> is still hanging around at the beginning of <CODE>yytext</CODE> so the <CODE>ECHO</CODE> for the <SAMP>`kludge'</SAMP> rule will actually write <SAMP>`mega-kludge'</SAMP>. </DL> <P> Two notes regarding use of <CODE>yymore()</CODE>. First, <CODE>yymore()</CODE> depends on the value of <CODE>yyleng</CODE> correctly reflecting the size of the current token, so you must not modify <CODE>yyleng</CODE> if you are using <CODE>yymore()</CODE>. Second, the presence of <CODE>yymore()</CODE> in the scanner's action entails a minor performance penalty in the scanner's matching speed. <P> <A NAME="IDX117"></A> <CODE>yyless(n)</CODE> returns all but the first <CODE>n</CODE> characters of the current token back to the input stream, where they will be rescanned when the scanner looks for the next match. <CODE>yytext</CODE> and <CODE>yyleng</CODE> are adjusted appropriately (e.g., <CODE>yyleng</CODE> will now be equal to <CODE>n</CODE>). For example, on the input <SAMP>`foobar'</SAMP> the following will write out <SAMP>`foobarbar'</SAMP>: <P> <A NAME="IDX118"></A> <A NAME="IDX119"></A> <A NAME="IDX120"></A> <PRE> @verbatim %% foobar ECHO; yyless(3); [a-z]+ ECHO; </PRE> <P> An argument of 0 to <CODE>yyless()</CODE> will cause the entire current input string to be scanned again. Unless you've changed how the scanner will subsequently process its input (using <CODE>BEGIN</CODE>, for example), this will result in an endless loop. <P> Note that <CODE>yyless()</CODE> is a macro and can only be used in the flex input file, not from other source files. <P> <A NAME="IDX121"></A> <A NAME="IDX122"></A> <CODE>unput(c)</CODE> puts the character <CODE>c</CODE> back onto the input stream. It will be the next character scanned. The following action will take the current token and cause it to be rescanned enclosed in parentheses. <P> <A NAME="IDX123"></A> <PRE> @verbatim { int i; /* Copy yytext because unput() trashes yytext */ char *yycopy = strdup( yytext ); unput( ')' ); for ( i = yyleng - 1; i >= 0; --i ) unput( yycopy[i] ); unput( '(' ); free( yycopy ); } </PRE> <P> Note that since each <CODE>unput()</CODE> puts the given character back at the <EM>beginning</EM> of the input stream, pushing back strings must be done back-to-front. <P> <A NAME="IDX124"></A> <A NAME="IDX125"></A> <P> An important potential problem when using <CODE>unput()</CODE> is that if you are using <CODE>%pointer</CODE> (the default), a call to <CODE>unput()</CODE> <EM>destroys</EM> the contents of <CODE>yytext</CODE>, starting with its rightmost character and devouring one character to the left with each call. If you need the value of yytext preserved after a call to <CODE>unput()</CODE> (as in the above example), you must either first copy it elsewhere, or build your scanner using <CODE>%array</CODE> instead (see section <A HREF="flex.html#SEC11">How the Input Is Matched</A>). <P> <A NAME="IDX126"></A> <A NAME="IDX127"></A> Finally, note that you cannot put back <SAMP>`EOF'</SAMP> to attempt to mark the input stream with an end-of-file. <P> <A NAME="IDX128"></A> <CODE>input()</CODE> reads the next character from the input stream. For example, the following is one way to eat up C comments: <P> <A NAME="IDX129"></A> <A NAME="IDX130"></A> <PRE> @verbatim %% "/*" { register int c; for ( ; ; ) { while ( (c = input()) != '*' && c != EOF ) ; /* eat up text of comment */ if ( c == '*' ) { while ( (c = input()) == '*' ) ; if ( c == '/' ) break; /* found the end */ } if ( c == EOF ) { error( "EOF in comment" ); break; } } } </PRE> <P> <A NAME="IDX131"></A> (Note that if the scanner is compiled using <CODE>C++</CODE>, then <CODE>input()</CODE> is instead referred to as <B>yyinput()</B>, in order to avoid a name clash with the <CODE>C++</CODE> stream by the name of <CODE>input</CODE>.) <P> <A NAME="IDX132"></A> <CODE>YY_FLUSH_BUFFER()</CODE> flushes the scanner's internal buffer so that the next time the scanner attempts to match a token, it will first refill the buffer using <CODE>YY_INPUT()</CODE> (see section <A HREF="flex.html#SEC13">The Generated Scanner</A>). This action is a special case of the more general <CODE>yy_flush_buffer()</CODE> function, described below (see section <A HREF="flex.html#SEC15">Multiple Input Buffers</A>) <P> <A NAME="IDX133"></A> <A NAME="IDX134"></A> <A NAME="IDX135"></A> <A NAME="IDX136"></A> <A NAME="IDX137"></A> <P> <CODE>yyterminate()</CODE> can be used in lieu of a return statement in an action. It terminates the scanner and returns a 0 to the scanner's caller, indicating "all done". By default, <CODE>yyterminate()</CODE> is also called when an end-of-file is encountered. It is a macro and may be redefined. <H1><A NAME="SEC13" HREF="flex_toc.html#TOC13">The Generated Scanner</A></H1> <P> <A NAME="IDX138"></A> The output of <CODE>flex</CODE> is the file <TT>`lex.yy.c'</TT>, which contains the scanning routine <CODE>yylex()</CODE>, a number of tables used by it for matching tokens, and a number of auxiliary routines and macros. By default, <CODE>yylex()</CODE> is declared as follows: <PRE> @verbatim int yylex() { ... various definitions and the actions in here ... } </PRE> <P> <A NAME="IDX139"></A> (If your environment supports function prototypes, then it will be <CODE>int yylex( void )</CODE>.) This definition may be changed by defining the <CODE>YY_DECL</CODE> macro. For example, you could use: <P> <A NAME="IDX140"></A> <PRE> @verbatim #define YY_DECL float lexscan( a, b ) float a, b; </PRE> <P> to give the scanning routine the name <CODE>lexscan</CODE>, returning a float, and taking two floats as arguments. Note that if you give arguments to the scanning routine using a K&R-style/non-prototyped function declaration, you must terminate the definition with a semi-colon (;). <P> <CODE>flex</CODE> generates <SAMP>`C99'</SAMP> function definitions by default. However flex does have the ability to generate obsolete, er, <SAMP>`traditional'</SAMP>, function definitions. This is to support bootstrapping gcc on old systems. Unfortunately, traditional definitions prevent us from using any standard data types smaller than int (such as short, char, or bool) as function arguments. For this reason, future versions of <CODE>flex</CODE> may generate standard C99 code only, leaving K&R-style functions to the historians. Currently, if you do <STRONG>not</STRONG> want <SAMP>`C99'</SAMP> definitions, then you must define <CODE>YY_TRADITIONAL_FUNC_DEFS</CODE>. <P> <A NAME="IDX141"></A> Whenever <CODE>yylex()</CODE> is called, it scans tokens from the global input file <TT>`yyin'</TT> (which defaults to stdin). It continues until it either reaches an end-of-file (at which point it returns the value 0) or one of its actions executes a <CODE>return</CODE> statement. <P> <A NAME="IDX142"></A> If the scanner reaches an end-of-file, subsequent calls are undefined unless either <TT>`yyin'</TT> is pointed at a new input file (in which case scanning continues from that file), or <CODE>yyrestart()</CODE> is called. <CODE>yyrestart()</CODE> takes one argument, a <CODE>FILE *</CODE> pointer (which can be nil, if you've set up <CODE>YY_INPUT</CODE> to scan from a source other than <CODE>yyin</CODE>), and initializes <TT>`yyin'</TT> for scanning from that file. Essentially there is no difference between just assigning <TT>`yyin'</TT> to a new input file or using <CODE>yyrestart()</CODE> to do so; the latter is available for compatibility with previous versions of <CODE>flex</CODE>, and because it can be used to switch input files in the middle of scanning. It can also be used to throw away the current input buffer, by calling it with an argument of <TT>`yyin'</TT>; but it would be better to use <CODE>YY_FLUSH_BUFFER</CODE> (see section <A HREF="flex.html#SEC12">Actions</A>). Note that <CODE>yyrestart()</CODE> does <EM>not</EM> reset the start condition to <CODE>INITIAL</CODE> (see section <A HREF="flex.html#SEC14">Start Conditions</A>). <P> <A NAME="IDX143"></A> If <CODE>yylex()</CODE> stops scanning due to executing a <CODE>return</CODE> statement in one of the actions, the scanner may then be called again and it will resume scanning where it left off. <P> By default (and for purposes of efficiency), the scanner uses block-reads rather than simple <CODE>getc()</CODE> calls to read characters from <TT>`yyin'</TT>. The nature of how it gets its input can be controlled by defining the <CODE>YY_INPUT</CODE> macro. The calling sequence for <CODE>YY_INPUT()</CODE> is <CODE>YY_INPUT(buf,result,max_size)</CODE>. Its action is to place up to <CODE>max_size</CODE> characters in the character array <CODE>buf</CODE> and return in the integer variable <CODE>result</CODE> either the number of characters read or the constant <CODE>YY_NULL</CODE> (0 on Unix systems) to indicate <SAMP>`EOF'</SAMP>. The default <CODE>YY_INPUT</CODE> reads from the global file-pointer <TT>`yyin'</TT>. <P> <A NAME="IDX144"></A> Here is a sample definition of <CODE>YY_INPUT</CODE> (in the definitions section of the input file): <P> <A NAME="IDX145"></A> <PRE> @verbatim %{ #define YY_INPUT(buf,result,max_size) \ { \ int c = getchar(); \ result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \ } %} </PRE> <P> This definition will change the input processing to occur one character at a time. <P> <A NAME="IDX146"></A> When the scanner receives an end-of-file indication from YY_INPUT, it then checks the <CODE>yywrap()</CODE> function. If <CODE>yywrap()</CODE> returns false (zero), then it is assumed that the function has gone ahead and set up <TT>`yyin'</TT> to point to another input file, and scanning continues. If it returns true (non-zero), then the scanner terminates, returning 0 to its caller. Note that in either case, the start condition remains unchanged; it does <EM>not</EM> revert to <CODE>INITIAL</CODE>. <P> If you do not supply your own version of <CODE>yywrap()</CODE>, then you must either use <CODE>%option noyywrap</CODE> (in which case the scanner behaves as though <CODE>yywrap()</CODE> returned 1), or you must link with <SAMP>`-lfl'</SAMP> to obtain the default version of the routine, which always returns 1. <P> For scanning from in-memory buffers (e.g., scanning strings), see @xref{Scanning Strings} See section <A HREF="flex.html#SEC15">Multiple Input Buffers</A>. <P> The scanner writes its <CODE>ECHO</CODE> output to the <TT>`yyout'</TT> global (default, <TT>`stdout'</TT>), which may be redefined by the user simply by assigning it to some other <CODE>FILE</CODE> pointer. <H1><A NAME="SEC14" HREF="flex_toc.html#TOC14">Start Conditions</A></H1> <P> <A NAME="IDX147"></A> <CODE>flex</CODE> provides a mechanism for conditionally activating rules. Any rule whose pattern is prefixed with <SAMP>`<sc>'</SAMP> will only be active when the scanner is in the start condition named <CODE>sc</CODE>. For example, <P> <A NAME="IDX148"></A> <PRE> @verbatim <STRING>[^"]* { /* eat up the string body ... */ ... } </PRE> <P> will be active only when the scanner is in the <CODE>STRING</CODE> start condition, and <P> <A NAME="IDX149"></A> <PRE> @verbatim <INITIAL,STRING,QUOTE>\. { /* handle an escape ... */ ... } </PRE> <P> will be active only when the current start condition is either <CODE>INITIAL</CODE>, <CODE>STRING</CODE>, or <CODE>QUOTE</CODE>. <P> <A NAME="IDX150"></A> Start conditions are declared in the definitions (first) section of the input using unindented lines beginning with either <SAMP>`%s'</SAMP> or <SAMP>`%x'</SAMP> followed by a list of names. The former declares <EM>inclusive</EM> start conditions, the latter <EM>exclusive</EM> start conditions. A start condition is activated using the <CODE>BEGIN</CODE> action. Until the next <CODE>BEGIN</CODE> action is executed, rules with the given start condition will be active and rules with other start conditions will be inactive. If the start condition is inclusive, then rules with no start conditions at all will also be active. If it is exclusive, then <EM>only</EM> rules qualified with the start condition will be active. A set of rules contingent on the same exclusive start condition describe a scanner which is independent of any of the other rules in the <CODE>flex</CODE> input. Because of this, exclusive start conditions make it easy to specify "mini-scanners" which scan portions of the input that are syntactically different from the rest (e.g., comments). <P> If the distinction between inclusive and exclusive start conditions is still a little vague, here's a simple example illustrating the connection between the two. The set of rules: <P> <A NAME="IDX151"></A> <PRE> @verbatim %s example %% <example>foo do_something(); bar something_else(); </PRE> <P> is equivalent to <P> <A NAME="IDX152"></A> <PRE> @verbatim %x example %% <example>foo do_something(); <INITIAL,example>bar something_else(); </PRE> <P> Without the <CODE><INITIAL,example></CODE> qualifier, the <CODE>bar</CODE> pattern in the second example wouldn't be active (i.e., couldn't match) when in start condition <CODE>example</CODE>. If we just used <CODE>example></CODE> to qualify <CODE>bar</CODE>, though, then it would only be active in <CODE>example</CODE> and not in <CODE>INITIAL</CODE>, while in the first example it's active in both, because in the first example the <CODE>example</CODE> start condition is an inclusive <CODE>(%s)</CODE> start condition. <P> <A NAME="IDX153"></A> Also note that the special start-condition specifier <CODE><*></CODE> matches every start condition. Thus, the above example could also have been written: <P> <A NAME="IDX154"></A> <PRE> @verbatim %x example %% <example>foo do_something(); <*>bar something_else(); </PRE> <P> The default rule (to <CODE>ECHO</CODE> any unmatched character) remains active in start conditions. It is equivalent to: <P> <A NAME="IDX155"></A> <PRE> @verbatim <*>.|\n ECHO; </PRE> <P> <A NAME="IDX156"></A> <A NAME="IDX157"></A> <A NAME="IDX158"></A> <CODE>BEGIN(0)</CODE> returns to the original state where only the rules with no start conditions are active. This state can also be referred to as the start-condition <CODE>INITIAL</CODE>, so <CODE>BEGIN(INITIAL)</CODE> is equivalent to <CODE>BEGIN(0)</CODE>. (The parentheses around the start condition name are not required but are considered good style.) <P> <CODE>BEGIN</CODE> actions can also be given as indented code at the beginning of the rules section. For example, the following will cause the scanner to enter the <CODE>SPECIAL</CODE> start condition whenever <CODE>yylex()</CODE> is called and the global variable <CODE>enter_special</CODE> is true: <P> <A NAME="IDX159"></A> <PRE> @verbatim int enter_special; %x SPECIAL %% if ( enter_special ) BEGIN(SPECIAL); <SPECIAL>blahblahblah ...more rules follow... </PRE> <P> To illustrate the uses of start conditions, here is a scanner which provides two different interpretations of a string like <SAMP>`123.456'</SAMP>. By default it will treat it as three tokens, the integer <SAMP>`123'</SAMP>, a dot (<SAMP>`.'</SAMP>), and the integer <SAMP>`456'</SAMP>. But if the string is preceded earlier in the line by the string <SAMP>`expect-floats'</SAMP> it will treat it as a single token, the floating-point number <SAMP>`123.456'</SAMP>: <P> <A NAME="IDX160"></A> <PRE> @verbatim %{ #include <math.h> %} %s expect %% expect-floats BEGIN(expect); <expect>[0-9]+<SAMP>`.'</SAMP>[0-9]+ { printf( "found a float, = %f\n", atof( yytext ) ); } <expect>\n { /* that's the end of the line, so * we need another "expect-number" * before we'll recognize any more * numbers */ BEGIN(INITIAL); } [0-9]+ { printf( "found an integer, = %d\n", atoi( yytext ) ); } "." printf( "found a dot\n" ); </PRE> <P> <A NAME="IDX161"></A> Here is a scanner which recognizes (and discards) C comments while maintaining a count of the current input line. <P> <A NAME="IDX162"></A> <PRE> @verbatim %x comment %% int line_num = 1; "/*" BEGIN(comment); <comment>[^*\n]* /* eat anything that's not a '*' */ <comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ <comment>\n ++line_num; <comment>"*"+"/" BEGIN(INITIAL); </PRE> <P> This scanner goes to a bit of trouble to match as much text as possible with each rule. In general, when attempting to write a high-speed scanner try to match as much possible in each rule, as it's a big win. <P> Note that start-conditions names are really integer values and can be stored as such. Thus, the above could be extended in the following fashion: <P> <A NAME="IDX163"></A> <A NAME="IDX164"></A> <PRE> @verbatim %x comment foo %% int line_num = 1; int comment_caller; "/*" { comment_caller = INITIAL; BEGIN(comment); } ... <foo>"/*" { comment_caller = foo; BEGIN(comment); } <comment>[^*\n]* /* eat anything that's not a '*' */ <comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ <comment>\n ++line_num; <comment>"*"+"/" BEGIN(comment_caller); </PRE> <P> <A NAME="IDX165"></A> Furthermore, you can access the current start condition using the integer-valued <CODE>YY_START</CODE> macro. For example, the above assignments to <CODE>comment_caller</CODE> could instead be written <P> <A NAME="IDX166"></A> <PRE> @verbatim comment_caller = YY_START; </PRE> <P> <A NAME="IDX167"></A> Flex provides <CODE>YYSTATE</CODE> as an alias for <CODE>YY_START</CODE> (since that is what's used by AT&T <CODE>lex</CODE>). <P> For historical reasons, start conditions do not have their own name-space within the generated scanner. The start condition names are unmodified in the generated scanner and generated header. @xref{option-header}. @xref{option-prefix}. <P> Finally, here's an example of how to match C-style quoted strings using exclusive start conditions, including expanded escape sequences (but not including checking for a string that's too long): <P> <A NAME="IDX168"></A> <PRE> @verbatim %x str %% char string_buf[MAX_STR_CONST]; char *string_buf_ptr; \" string_buf_ptr = string_buf; BEGIN(str); <str>\" { /* saw closing quote - all done */ BEGIN(INITIAL); *string_buf_ptr = '\0'; /* return string constant token type and * value to parser */ } <str>\n { /* error - unterminated string constant */ /* generate error message */ } <str>\\[0-7]{1,3} { /* octal escape sequence */ int result; (void) sscanf( yytext + 1, "%o", &result ); if ( result > 0xff ) /* error, constant is out-of-bounds */ *string_buf_ptr++ = result; } <str>\\[0-9]+ { /* generate error - bad escape sequence; something * like '\48' or '\0777777' */ } <str>\\n *string_buf_ptr++ = '\n'; <str>\\t *string_buf_ptr++ = '\t'; <str>\\r *string_buf_ptr++ = '\r'; <str>\\b *string_buf_ptr++ = '\b'; <str>\\f *string_buf_ptr++ = '\f'; <str>\\(.|\n) *string_buf_ptr++ = yytext[1]; <str>[^\\\n\"]+ { char *yptr = yytext; while ( *yptr ) *string_buf_ptr++ = *yptr++; } </PRE> <P> <A NAME="IDX169"></A> Often, such as in some of the examples above, you wind up writing a whole bunch of rules all preceded by the same start condition(s). Flex makes this a little easier and cleaner by introducing a notion of start condition <EM>scope</EM>. A start condition scope is begun with: <PRE> @verbatim <SCs>{ </PRE> <P> where <CODE>SCs</CODE> is a list of one or more start conditions. Inside the start condition scope, every rule automatically has the prefix <CODE>SCs></CODE> applied to it, until a <SAMP>`}'</SAMP> which matches the initial <SAMP>`{'</SAMP>. So, for example, <P> <A NAME="IDX170"></A> <PRE> @verbatim <ESC>{ "\\n" return '\n'; "\\r" return '\r'; "\\f" return '\f'; "\\0" return '\0'; } </PRE> <P> is equivalent to: <PRE> @verbatim <ESC>"\\n" return '\n'; <ESC>"\\r" return '\r'; <ESC>"\\f" return '\f'; <ESC>"\\0" return '\0'; </PRE> <P> Start condition scopes may be nested. <P> <A NAME="IDX171"></A> <A NAME="IDX172"></A> <P> The following routines are available for manipulating stacks of start conditions: <P> <DL> <DT><U>Function:</U> void <B>yy_push_state</B> <I>( int <CODE>new_state</CODE> )</I> <DD><A NAME="IDX173"></A> pushes the current start condition onto the top of the start condition stack and switches to <CODE>new_state</CODE> as though you had used <CODE>BEGIN new_state</CODE> (recall that start condition names are also integers). </DL> <P> <DL> <DT><U>Function:</U> void <B>yy_pop_state</B> <I>()</I> <DD><A NAME="IDX174"></A> pops the top of the stack and switches to it via <CODE>BEGIN</CODE>. </DL> <P> <DL> <DT><U>Function:</U> int <B>yy_top_state</B> <I>()</I> <DD><A NAME="IDX175"></A> returns the top of the stack without altering the stack's contents. </DL> <P> <A NAME="IDX176"></A> The start condition stack grows dynamically and so has no built-in size limitation. If memory is exhausted, program execution aborts. <P> To use start condition stacks, your scanner must include a <CODE>%option stack</CODE> directive (see section <A HREF="flex.html#SEC20">Scanner Options</A>). <H1><A NAME="SEC15" HREF="flex_toc.html#TOC15">Multiple Input Buffers</A></H1> <P> <A NAME="IDX177"></A> Some scanners (such as those which support "include" files) require reading from several input streams. As <CODE>flex</CODE> scanners do a large amount of buffering, one cannot control where the next input will be read from by simply writing a <CODE>YY_INPUT()</CODE> which is sensitive to the scanning context. <CODE>YY_INPUT()</CODE> is only called when the scanner reaches the end of its buffer, which may be a long time after scanning a statement such as an <CODE>include</CODE> statement which requires switching the input source. <P> To negotiate these sorts of problems, <CODE>flex</CODE> provides a mechanism for creating and switching between multiple input buffers. An input buffer is created by using: <P> <A NAME="IDX178"></A> <DL> <DT><U>Function:</U> YY_BUFFER_STATE <B>yy_create_buffer</B> <I>( FILE *file, int size )</I> <DD><A NAME="IDX179"></A> </DL> <P> which takes a <CODE>FILE</CODE> pointer and a size and creates a buffer associated with the given file and large enough to hold <CODE>size</CODE> characters (when in doubt, use <CODE>YY_BUF_SIZE</CODE> for the size). It returns a <CODE>YY_BUFFER_STATE</CODE> handle, which may then be passed to other routines (see below). <A NAME="IDX180"></A> The <CODE>YY_BUFFER_STATE</CODE> type is a pointer to an opaque <CODE>struct yy_buffer_state</CODE> structure, so you may safely initialize <CODE>YY_BUFFER_STATE</CODE> variables to <CODE>((YY_BUFFER_STATE) 0)</CODE> if you wish, and also refer to the opaque structure in order to correctly declare input buffers in source files other than that of your scanner. Note that the <CODE>FILE</CODE> pointer in the call to <CODE>yy_create_buffer</CODE> is only used as the value of <TT>`yyin'</TT> seen by <CODE>YY_INPUT</CODE>. If you redefine <CODE>YY_INPUT()</CODE> so it no longer uses <TT>`yyin'</TT>, then you can safely pass a nil <CODE>FILE</CODE> pointer to <CODE>yy_create_buffer</CODE>. You select a particular buffer to scan from using: <P> <DL> <DT><U>Function:</U> void <B>yy_switch_to_buffer</B> <I>( YY_BUFFER_STATE new_buffer )</I> <DD><A NAME="IDX181"></A> </DL> <P> The above switches the scanner's input buffer so subsequent tokens will come from <CODE>new_buffer</CODE>. Note that <CODE>yy_switch_to_buffer()</CODE> may be used by <CODE>yywrap()</CODE> to set things up for continued scanning, instead of opening a new file and pointing <TT>`yyin'</TT> at it. Note also that switching input sources via either <CODE>yy_switch_to_buffer()</CODE> or <CODE>yywrap()</CODE> does <EM>not</EM> change the start condition. <P> <A NAME="IDX182"></A> <DL> <DT><U>Function:</U> void <B>yy_delete_buffer</B> <I>( YY_BUFFER_STATE buffer )</I> <DD><A NAME="IDX183"></A> </DL> <P> is used to reclaim the storage associated with a buffer. (<CODE>buffer</CODE> can be nil, in which case the routine does nothing.) You can also clear the current contents of a buffer using: <P> <A NAME="IDX184"></A> <A NAME="IDX185"></A> <DL> <DT><U>Function:</U> void <B>yy_flush_buffer</B> <I>( YY_BUFFER_STATE buffer )</I> <DD><A NAME="IDX186"></A> </DL> <P> This function discards the buffer's contents, so the next time the scanner attempts to match a token from the buffer, it will first fill the buffer anew using <CODE>YY_INPUT()</CODE>. <P> <DL> <DT><U>Function:</U> YY_BUFFER_STATE <B>yy_new_buffer</B> <I>( FILE *file, int size )</I> <DD><A NAME="IDX187"></A> </DL> <P> is an alias for <CODE>yy_create_buffer()</CODE>, provided for compatibility with the C++ use of <CODE>new</CODE> and <CODE>delete</CODE> for creating and destroying dynamic objects. <P> <A NAME="IDX188"></A> Finally, the macro <CODE>YY_CURRENT_BUFFER</CODE> macro returns a <CODE>YY_BUFFER_STATE</CODE> handle to the current buffer. <P> <A NAME="IDX189"></A> Here is an example of using these features for writing a scanner which expands include files (the <CODE><<EOF>></CODE> feature is discussed below): <P> <A NAME="IDX190"></A> <PRE> @verbatim /* the "incl" state is used for picking up the name * of an include file */ %x incl %{ #define MAX_INCLUDE_DEPTH 10 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; int include_stack_ptr = 0; %} %% include BEGIN(incl); [a-z]+ ECHO; [^a-z\n]*\n? ECHO; <incl>[ \t]* /* eat the whitespace */ <incl>[^ \t\n]+ { /* got the include file name */ if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) { fprintf( stderr, "Includes nested too deeply" ); exit( 1 ); } include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER; yyin = fopen( yytext, "r" ); if ( ! yyin ) error( ... ); yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) ); BEGIN(INITIAL); } <<EOF>> { if ( --include_stack_ptr 0 ) { yyterminate(); } else { yy_delete_buffer( YY_CURRENT_BUFFER ); yy_switch_to_buffer( include_stack[include_stack_ptr] ); } } </PRE> <P> @anchor{Scanning Strings} <A NAME="IDX191"></A> The following routines are available for setting up input buffers for scanning in-memory strings instead of files. All of them create a new input buffer for scanning the string, and return a corresponding <CODE>YY_BUFFER_STATE</CODE> handle (which you should delete with <CODE>yy_delete_buffer()</CODE> when done with it). They also switch to the new buffer using <CODE>yy_switch_to_buffer()</CODE>, so the next call to <CODE>yylex()</CODE> will start scanning the string. <P> <DL> <DT><U>Function:</U> YY_BUFFER_STATE <B>yy_scan_string</B> <I>( const char *str )</I> <DD><A NAME="IDX192"></A> scans a NUL-terminated string. </DL> <P> <DL> <DT><U>Function:</U> YY_BUFFER_STATE <B>yy_scan_bytes</B> <I>( const char *bytes, int len )</I> <DD><A NAME="IDX193"></A> scans <CODE>len</CODE> bytes (including possibly <CODE>NUL</CODE>s) starting at location <CODE>bytes</CODE>. </DL> <P> Note that both of these functions create and scan a <EM>copy</EM> of the string or bytes. (This may be desirable, since <CODE>yylex()</CODE> modifies the contents of the buffer it is scanning.) You can avoid the copy by using: <P> <A NAME="IDX194"></A> <DL> <DT><U>Function:</U> YY_BUFFER_STATE <B>yy_scan_buffer</B> <I>(char *base, yy_size_t size)</I> <DD><A NAME="IDX195"></A> which scans in place the buffer starting at <CODE>base</CODE>, consisting of <CODE>size</CODE> bytes, the last two bytes of which <EM>must</EM> be <CODE>YY_END_OF_BUFFER_CHAR</CODE> (ASCII NUL). These last two bytes are not scanned; thus, scanning consists of <CODE>base[0]</CODE> through <CODE>base[size-2]</CODE>, inclusive. </DL> <P> If you fail to set up <CODE>base</CODE> in this manner (i.e., forget the final two <CODE>YY_END_OF_BUFFER_CHAR</CODE> bytes), then <CODE>yy_scan_buffer()</CODE> returns a nil pointer instead of creating a new input buffer. <P> <DL> <DT><U>Data type:</U> <B>yy_size_t</B> <DD><A NAME="IDX196"></A> is an integral type to which you can cast an integer expression reflecting the size of the buffer. </DL> <H1><A NAME="SEC16" HREF="flex_toc.html#TOC16">End-of-File Rules</A></H1> <P> <A NAME="IDX197"></A> The special rule <CODE><<EOF>></CODE> indicates actions which are to be taken when an end-of-file is encountered and <CODE>yywrap()</CODE> returns non-zero (i.e., indicates no further files to process). The action must finish by doing one of the following things: <UL> <LI> <A NAME="IDX198"></A> assigning <TT>`yyin'</TT> to a new input file (in previous versions of <CODE>flex</CODE>, after doing the assignment you had to call the special action <CODE>YY_NEW_FILE</CODE>. This is no longer necessary.) <LI> executing a <CODE>return</CODE> statement; <LI> executing the special <CODE>yyterminate()</CODE> action. <LI> or, switching to a new buffer using <CODE>yy_switch_to_buffer()</CODE> as shown in the example above. </UL> <P> <<EOF>> rules may not be used with other patterns; they may only be qualified with a list of start conditions. If an unqualified <<EOF>> rule is given, it applies to <EM>all</EM> start conditions which do not already have <<EOF>> actions. To specify an <<EOF>> rule for only the initial start condition, use: <PRE> @verbatim <INITIAL><<EOF>> </PRE> <P> These rules are useful for catching things like unclosed comments. An example: <P> <A NAME="IDX199"></A> <PRE> @verbatim %x quote %% ...other rules for dealing with quotes... <quote><<EOF>> { error( "unterminated quote" ); yyterminate(); } <<EOF>> { if ( *++filelist ) yyin = fopen( *filelist, "r" ); else yyterminate(); } </PRE> <H1><A NAME="SEC17" HREF="flex_toc.html#TOC17">Miscellaneous Macros</A></H1> <P> <A NAME="IDX200"></A> The macro <CODE>YY_USER_ACTION</CODE> can be defined to provide an action which is always executed prior to the matched rule's action. For example, it could be #define'd to call a routine to convert yytext to lower-case. When <CODE>YY_USER_ACTION</CODE> is invoked, the variable <CODE>yy_act</CODE> gives the number of the matched rule (rules are numbered starting with 1). Suppose you want to profile how often each of your rules is matched. The following would do the trick: <P> <A NAME="IDX201"></A> <PRE> @verbatim #define YY_USER_ACTION ++ctr[yy_act] </PRE> <P> <A NAME="IDX202"></A> where <CODE>ctr</CODE> is an array to hold the counts for the different rules. Note that the macro <CODE>YY_NUM_RULES</CODE> gives the total number of rules (including the default rule), even if you use <SAMP>`-s)'</SAMP>, so a correct declaration for <CODE>ctr</CODE> is: <PRE> @verbatim int ctr[YY_NUM_RULES]; </PRE> <P> <A NAME="IDX203"></A> The macro <CODE>YY_USER_INIT</CODE> may be defined to provide an action which is always executed before the first scan (and before the scanner's internal initializations are done). For example, it could be used to call a routine to read in a data table or open a logging file. <P> <A NAME="IDX204"></A> The macro <CODE>yy_set_interactive(is_interactive)</CODE> can be used to control whether the current buffer is considered <EM>interactive</EM>. An interactive buffer is processed more slowly, but must be used when the scanner's input source is indeed interactive to avoid problems due to waiting to fill buffers (see the discussion of the <SAMP>`-I'</SAMP> flag in section <A HREF="flex.html#SEC20">Scanner Options</A>). A non-zero value in the macro invocation marks the buffer as interactive, a zero value as non-interactive. Note that use of this macro overrides <CODE>%option always-interactive</CODE> or <CODE>%option never-interactive</CODE> (see section <A HREF="flex.html#SEC20">Scanner Options</A>). <CODE>yy_set_interactive()</CODE> must be invoked prior to beginning to scan the buffer that is (or is not) to be considered interactive. <P> <A NAME="IDX205"></A> <A NAME="IDX206"></A> The macro <CODE>yy_set_bol(at_bol)</CODE> can be used to control whether the current buffer's scanning context for the next token match is done as though at the beginning of a line. A non-zero macro argument makes rules anchored with <SAMP>`^'</SAMP> active, while a zero argument makes <SAMP>`^'</SAMP> rules inactive. <P> <A NAME="IDX207"></A> <A NAME="IDX208"></A> The macro <CODE>YY_AT_BOL()</CODE> returns true if the next token scanned from the current buffer will have <SAMP>`^'</SAMP> rules active, false otherwise. <P> <A NAME="IDX209"></A> <A NAME="IDX210"></A> In the generated scanner, the actions are all gathered in one large switch statement and separated using <CODE>YY_BREAK</CODE>, which may be redefined. By default, it is simply a <CODE>break</CODE>, to separate each rule's action from the following rule's. Redefining <CODE>YY_BREAK</CODE> allows, for example, C++ users to #define YY_BREAK to do nothing (while being very careful that every rule ends with a <CODE>break</CODE>" or a <CODE>return</CODE>!) to avoid suffering from unreachable statement warnings where because a rule's action ends with <CODE>return</CODE>, the <CODE>YY_BREAK</CODE> is inaccessible. <H1><A NAME="SEC18" HREF="flex_toc.html#TOC18">Values Available To the User</A></H1> <P> This chapter summarizes the various values available to the user in the rule actions. <DL COMPACT> <DT><CODE>char *yytext</CODE> <DD> <A NAME="IDX211"></A> holds the text of the current token. It may be modified but not lengthened (you cannot append characters to the end). <A NAME="IDX212"></A> <A NAME="IDX213"></A> <A NAME="IDX214"></A> If the special directive <CODE>%array</CODE> appears in the first section of the scanner description, then <CODE>yytext</CODE> is instead declared <CODE>char yytext[YYLMAX]</CODE>, where <CODE>YYLMAX</CODE> is a macro definition that you can redefine in the first section if you don't like the default value (generally 8KB). Using <CODE>%array</CODE> results in somewhat slower scanners, but the value of <CODE>yytext</CODE> becomes immune to calls to <CODE>unput()</CODE>, which potentially destroy its value when <CODE>yytext</CODE> is a character pointer. The opposite of <CODE>%array</CODE> is <CODE>%pointer</CODE>, which is the default. <A NAME="IDX215"></A> You cannot use <CODE>%array</CODE> when generating C++ scanner classes (the <SAMP>`-+'</SAMP> flag). <A NAME="IDX216"></A> <DT><CODE>int yyleng</CODE> <DD> holds the length of the current token. <A NAME="IDX217"></A> <DT><CODE>FILE *yyin</CODE> <DD> is the file which by default <CODE>flex</CODE> reads from. It may be redefined but doing so only makes sense before scanning begins or after an EOF has been encountered. Changing it in the midst of scanning will have unexpected results since <CODE>flex</CODE> buffers its input; use <CODE>yyrestart()</CODE> instead. Once scanning terminates because an end-of-file has been seen, you can assign <TT>`yyin'</TT> at the new input file and then call the scanner again to continue scanning. <A NAME="IDX218"></A> <DT><CODE>void yyrestart( FILE *new_file )</CODE> <DD> may be called to point <TT>`yyin'</TT> at the new input file. The switch-over to the new file is immediate (any previously buffered-up input is lost). Note that calling <CODE>yyrestart()</CODE> with <TT>`yyin'</TT> as an argument thus throws away the current input buffer and continues scanning the same input file. <A NAME="IDX219"></A> <DT><CODE>FILE *yyout</CODE> <DD> is the file to which <CODE>ECHO</CODE> actions are done. It can be reassigned by the user. <A NAME="IDX220"></A> <DT><CODE>YY_CURRENT_BUFFER</CODE> <DD> returns a <CODE>YY_BUFFER_STATE</CODE> handle to the current buffer. <A NAME="IDX221"></A> <DT><CODE>YY_START</CODE> <DD> returns an integer value corresponding to the current start condition. You can subsequently use this value with <CODE>BEGIN</CODE> to return to that start condition. </DL> <H1><A NAME="SEC19" HREF="flex_toc.html#TOC19">Interfacing with Yacc</A></H1> <P> <A NAME="IDX222"></A> <P> <A NAME="IDX223"></A> One of the main uses of <CODE>flex</CODE> is as a companion to the <CODE>yacc</CODE> parser-generator. <CODE>yacc</CODE> parsers expect to call a routine named <CODE>yylex()</CODE> to find the next input token. The routine is supposed to return the type of the next token as well as putting any associated value in the global <CODE>yylval</CODE>. To use <CODE>flex</CODE> with <CODE>yacc</CODE>, one specifies the <SAMP>`-d'</SAMP> option to <CODE>yacc</CODE> to instruct it to generate the file <TT>`y.tab.h'</TT> containing definitions of all the <CODE>%tokens</CODE> appearing in the <CODE>yacc</CODE> input. This file is then included in the <CODE>flex</CODE> scanner. For example, if one of the tokens is <CODE>TOK_NUMBER</CODE>, part of the scanner might look like: <P> <A NAME="IDX224"></A> <PRE> @verbatim %{ #include "y.tab.h" %} %% [0-9]+ yylval = atoi( yytext ); return TOK_NUMBER; </PRE> <H1><A NAME="SEC20" HREF="flex_toc.html#TOC20">Scanner Options</A></H1> <P> <A NAME="IDX225"></A> <A NAME="IDX226"></A> <A NAME="IDX227"></A> <P> The various <CODE>flex</CODE> options are categorized by function in the following menu. If you want to lookup a particular option by name, See section <A HREF="flex.html#SEC163">Index of Scanner Options</A>. <P> Even though there are many scanner options, a typical scanner might only specify the following options: <PRE> @verbatim %option 8-bit reentrant bison-bridge %option warn nodefault %option yylineno %option outfile="scanner.c" header-file="scanner.h" </PRE> <P> The first line specifies the general type of scanner we want. The second line specifies that we are being careful. The third line asks flex to track line numbers. The last line tells flex what to name the files. (The options can be specified in any order. We just dividied them.) <P> <CODE>flex</CODE> also provides a mechanism for controlling options within the scanner specification itself, rather than from the flex command-line. This is done by including <CODE>%option</CODE> directives in the first section of the scanner specification. You can specify multiple options with a single <CODE>%option</CODE> directive, and multiple directives in the first section of your flex input file. <P> Most options are given simply as names, optionally preceded by the word <SAMP>`no'</SAMP> (with no intervening whitespace) to negate their meaning. The names are the same as their long-option equivalents (but without the leading <SAMP>`--'</SAMP> ). <P> <CODE>flex</CODE> scans your rule actions to determine whether you use the <CODE>REJECT</CODE> or <CODE>yymore()</CODE> features. The <CODE>REJECT</CODE> and <CODE>yymore</CODE> options are available to override its decision as to whether you use the options, either by setting them (e.g., <CODE>%option reject)</CODE> to indicate the feature is indeed used, or unsetting them to indicate it actually is not used (e.g., <CODE>%option noyymore)</CODE>. <P> A number of options are available for lint purists who want to suppress the appearance of unneeded routines in the generated scanner. Each of the following, if unset (e.g., <CODE>%option nounput</CODE>), results in the corresponding routine not appearing in the generated scanner: <PRE> @verbatim input, unput yy_push_state, yy_pop_state, yy_top_state yy_scan_buffer, yy_scan_bytes, yy_scan_string yyget_extra, yyset_extra, yyget_leng, yyget_text, yyget_lineno, yyset_lineno, yyget_in, yyset_in, yyget_out, yyset_out, yyget_lval, yyset_lval, yyget_lloc, yyset_lloc, yyget_debug, yyset_debug </PRE> <P> (though <CODE>yy_push_state()</CODE> and friends won't appear anyway unless you use <CODE>%option stack)</CODE>. <H2><A NAME="SEC21" HREF="flex_toc.html#TOC21">Options for Specifing Filenames</A></H2> <DL COMPACT> @anchor{option-header} <DT><SAMP>`--header-file=FILE, <CODE>%option header-file="FILE"</CODE>'</SAMP> <DD> <A NAME="IDX228"></A> <A NAME="IDX229"></A> instructs flex to write a C header to <TT>`FILE'</TT>. This file contains function prototypes, extern variables, and types used by the scanner. Only the external API is exported by the header file. Many macros that are usable from within scanner actions are not exported to the header file. This is due to namespace problems and the goal of a clean external API. While in the header, the macro <CODE>yyIN_HEADER</CODE> is defined, where <SAMP>`yy'</SAMP> is substituted with the appropriate prefix. The <SAMP>`--header-file'</SAMP> option is not compatible with the <SAMP>`--c++'</SAMP> option, since the C++ scanner provides its own header in <TT>`yyFlexLexer.h'</TT>. @anchor{option-outfile} <A NAME="IDX230"></A> <A NAME="IDX231"></A> <A NAME="IDX232"></A> <DT><SAMP>`-oFILE, --outfile=FILE, <CODE>%option outfile="FILE"</CODE>'</SAMP> <DD> directs flex to write the scanner to the file <TT>`FILE'</TT> instead of <TT>`lex.yy.c'</TT>. If you combine <SAMP>`--outfile'</SAMP> with the <SAMP>`--stdout'</SAMP> option, then the scanner is written to <TT>`stdout'</TT> but its <CODE>#line</CODE> directives (see the <SAMP>`-l'</SAMP> option above) refer to the file <TT>`FILE'</TT>. @anchor{option-stdout} <A NAME="IDX233"></A> <A NAME="IDX234"></A> <A NAME="IDX235"></A> <DT><SAMP>`-t, --stdout, <CODE>%option stdout</CODE>'</SAMP> <DD> instructs <CODE>flex</CODE> to write the scanner it generates to standard output instead of <TT>`lex.yy.c'</TT>. <A NAME="IDX236"></A> <DT><SAMP>`-SFILE, --skel=FILE'</SAMP> <DD> overrides the default skeleton file from which <CODE>flex</CODE> constructs its scanners. You'll never need this option unless you are doing <CODE>flex</CODE> maintenance or development. <A NAME="IDX237"></A> <A NAME="IDX238"></A> <DT><SAMP>`--tables-file=FILE'</SAMP> <DD> Write serialized scanner dfa tables to FILE. The generated scanner will not contain the tables, and requires them to be loaded at runtime. @xref{serialization}. <A NAME="IDX239"></A> <A NAME="IDX240"></A> <DT><SAMP>`--tables-verify'</SAMP> <DD> This option is for flex development. We document it here in case you stumble upon it by accident or in case you suspect some inconsistency in the serialized tables. Flex will serialize the scanner dfa tables but will also generate the in-code tables as it normally does. At runtime, the scanner will verify that the serialized tables match the in-code tables, instead of loading them. </DL> <H2><A NAME="SEC22" HREF="flex_toc.html#TOC22">Options Affecting Scanner Behavior</A></H2> <DL COMPACT> @anchor{option-case-insensitive} <DT><SAMP>`-i, --case-insensitive, <CODE>%option case-insensitive</CODE>'</SAMP> <DD> <A NAME="IDX241"></A> <A NAME="IDX242"></A> <A NAME="IDX243"></A> instructs <CODE>flex</CODE> to generate a <EM>case-insensitive</EM> scanner. The case of letters given in the <CODE>flex</CODE> input patterns will be ignored, and tokens in the input will be matched regardless of case. The matched text given in <CODE>yytext</CODE> will have the preserved case (i.e., it will not be folded). @anchor{option-lex-compat} <A NAME="IDX244"></A> <A NAME="IDX245"></A> <A NAME="IDX246"></A> <DT><SAMP>`-l, --lex-compat, <CODE>%option lex-compat</CODE>'</SAMP> <DD> turns on maximum compatibility with the original AT&T <CODE>lex</CODE> implementation. Note that this does not mean <EM>full</EM> compatibility. Use of this option costs a considerable amount of performance, and it cannot be used with the <SAMP>`--c++'</SAMP>, <SAMP>`--full'</SAMP>, <SAMP>`--fast'</SAMP>, <SAMP>`-Cf'</SAMP>, or <SAMP>`-CF'</SAMP> options. For details on the compatibilities it provides, see section <A HREF="flex.html#SEC42">Incompatibilities with Lex and Posix</A>. This option also results in the name <CODE>YY_FLEX_LEX_COMPAT</CODE> being <CODE>#define</CODE>'d in the generated scanner. @anchor{option-batch} <A NAME="IDX247"></A> <A NAME="IDX248"></A> <A NAME="IDX249"></A> <DT><SAMP>`-B, --batch, <CODE>%option batch</CODE>'</SAMP> <DD> instructs <CODE>flex</CODE> to generate a <EM>batch</EM> scanner, the opposite of <EM>interactive</EM> scanners generated by <SAMP>`--interactive'</SAMP> (see below). In general, you use <SAMP>`-B'</SAMP> when you are <EM>certain</EM> that your scanner will never be used interactively, and you want to squeeze a <EM>little</EM> more performance out of it. If your goal is instead to squeeze out a <EM>lot</EM> more performance, you should be using the <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP> options, which turn on <SAMP>`--batch'</SAMP> automatically anyway. @anchor{option-interactive} <A NAME="IDX250"></A> <A NAME="IDX251"></A> <A NAME="IDX252"></A> <DT><SAMP>`-I, --interactive, <CODE>%option interactive</CODE>'</SAMP> <DD> instructs <CODE>flex</CODE> to generate an <I>interactive</I> scanner. An interactive scanner is one that only looks ahead to decide what token has been matched if it absolutely must. It turns out that always looking one extra character ahead, even if the scanner has already seen enough text to disambiguate the current token, is a bit faster than only looking ahead when necessary. But scanners that always look ahead give dreadful interactive performance; for example, when a user types a newline, it is not recognized as a newline token until they enter <EM>another</EM> token, which often means typing in another whole line. <CODE>flex</CODE> scanners default to <CODE>interactive</CODE> unless you use the <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP> table-compression options (see section <A HREF="flex.html#SEC27">Performance Considerations</A>). That's because if you're looking for high-performance you should be using one of these options, so if you didn't, <CODE>flex</CODE> assumes you'd rather trade off a bit of run-time performance for intuitive interactive behavior. Note also that you <EM>cannot</EM> use <SAMP>`--interactive'</SAMP> in conjunction with <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP>. Thus, this option is not really needed; it is on by default for all those cases in which it is allowed. You can force a scanner to <EM>not</EM> be interactive by using <SAMP>`--batch'</SAMP> @anchor{option-7bit} <A NAME="IDX253"></A> <A NAME="IDX254"></A> <A NAME="IDX255"></A> <DT><SAMP>`-7, --7bit, <CODE>%option 7bit</CODE>'</SAMP> <DD> instructs <CODE>flex</CODE> to generate a 7-bit scanner, i.e., one which can only recognize 7-bit characters in its input. The advantage of using <SAMP>`--7bit'</SAMP> is that the scanner's tables can be up to half the size of those generated using the <SAMP>`--8bit'</SAMP>. The disadvantage is that such scanners often hang or crash if their input contains an 8-bit character. Note, however, that unless you generate your scanner using the <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP> table compression options, use of <SAMP>`--7bit'</SAMP> will save only a small amount of table space, and make your scanner considerably less portable. <CODE>Flex</CODE>'s default behavior is to generate an 8-bit scanner unless you use the <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP>, in which case <CODE>flex</CODE> defaults to generating 7-bit scanners unless your site was always configured to generate 8-bit scanners (as will often be the case with non-USA sites). You can tell whether flex generated a 7-bit or an 8-bit scanner by inspecting the flag summary in the <SAMP>`--verbose'</SAMP> output as described above. Note that if you use <SAMP>`-Cfe'</SAMP> or <SAMP>`-CFe'</SAMP> <CODE>flex</CODE> still defaults to generating an 8-bit scanner, since usually with these compression options full 8-bit tables are not much more expensive than 7-bit tables. @anchor{option-8bit} <A NAME="IDX256"></A> <A NAME="IDX257"></A> <A NAME="IDX258"></A> <DT><SAMP>`-8, --8bit, <CODE>%option 8bit</CODE>'</SAMP> <DD> instructs <CODE>flex</CODE> to generate an 8-bit scanner, i.e., one which can recognize 8-bit characters. This flag is only needed for scanners generated using <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP>, as otherwise flex defaults to generating an 8-bit scanner anyway. See the discussion of <SAMP>`--7bit'</SAMP> above for <CODE>flex</CODE>'s default behavior and the tradeoffs between 7-bit and 8-bit scanners. @anchor{option-default} <A NAME="IDX259"></A> <A NAME="IDX260"></A> <DT><SAMP>`--default, <CODE>%option default</CODE>'</SAMP> <DD> generate the default rule. @anchor{option-always-interactive} <A NAME="IDX261"></A> <A NAME="IDX262"></A> <DT><SAMP>`--always-interactive, <CODE>%option always-interactive</CODE>'</SAMP> <DD> instructs flex to generate a scanner which always considers its input <EM>interactive</EM>. Normally, on each new input file the scanner calls <CODE>isatty()</CODE> in an attempt to determine whether the scanner's input source is interactive and thus should be read a character at a time. When this option is used, however, then no such call is made. <A NAME="IDX263"></A> <DT><SAMP>`--never-interactive, <CODE>--never-interactive</CODE>'</SAMP> <DD> instructs flex to generate a scanner which never considers its input interactive. This is the opposite of <CODE>always-interactive</CODE>. @anchor{option-posix} <A NAME="IDX264"></A> <A NAME="IDX265"></A> <A NAME="IDX266"></A> <DT><SAMP>`-X, --posix, <CODE>%option posix</CODE>'</SAMP> <DD> turns on maximum compatibility with the POSIX 1003.2-1992 definition of <CODE>lex</CODE>. Since <CODE>flex</CODE> was originally designed to implement the POSIX definition of <CODE>lex</CODE> this generally involves very few changes in behavior. At the current writing the known differences between <CODE>flex</CODE> and the POSIX standard are: <UL> <LI> In POSIX and AT&T <CODE>lex</CODE>, the repeat operator, <SAMP>`{}'</SAMP>, has lower precedence than concatenation (thus <SAMP>`ab{3}'</SAMP> yields <SAMP>`ababab'</SAMP>). Most POSIX utilities use an Extended Regular Expression (ERE) precedence that has the precedence of the repeat operator higher than concatenation (which causes <SAMP>`ab{3}'</SAMP> to yield <SAMP>`abbb'</SAMP>). By default, <CODE>flex</CODE> places the precedence of the repeat operator higher than concatenation which matches the ERE processing of other POSIX utilities. When either <SAMP>`--posix'</SAMP> or <SAMP>`-l'</SAMP> are specified, <CODE>flex</CODE> will use the traditional AT&T and POSIX-compliant precedence for the repeat operator where concatenation has higher precedence than the repeat operator. </UL> @anchor{option-stack} <A NAME="IDX267"></A> <A NAME="IDX268"></A> <DT><SAMP>`--stack, <CODE>%option stack</CODE>'</SAMP> <DD> enables the use of start condition stacks (see section <A HREF="flex.html#SEC14">Start Conditions</A>). @anchor{option-stdinit} <A NAME="IDX269"></A> <A NAME="IDX270"></A> <DT><SAMP>`--stdinit, <CODE>%option stdinit</CODE>'</SAMP> <DD> if set (i.e., <B>%option stdinit)</B> initializes <CODE>yyin</CODE> and <CODE>yyout</CODE> to <TT>`stdin'</TT> and <TT>`stdout'</TT>, instead of the default of <TT>`nil'</TT>. Some existing <CODE>lex</CODE> programs depend on this behavior, even though it is not compliant with ANSI C, which does not require <TT>`stdin'</TT> and <TT>`stdout'</TT> to be compile-time constant. In a reentrant scanner, however, this is not a problem since initialization is performed in <CODE>yylex_init</CODE> at runtime. @anchor{option-yylineno} <A NAME="IDX271"></A> <A NAME="IDX272"></A> <DT><SAMP>`--yylineno, <CODE>%option yylineno</CODE>'</SAMP> <DD> directs <CODE>flex</CODE> to generate a scanner that maintains the number of the current line read from its input in the global variable <CODE>yylineno</CODE>. This option is implied by <CODE>%option lex-compat</CODE>. In a reentrant C scanner, the macro <CODE>yylineno</CODE> is accessible regardless of the value of <CODE>%option yylineno</CODE>, however, its value is not modified by <CODE>flex</CODE> unless <CODE>%option yylineno</CODE> is enabled. @anchor{option-yywrap} <A NAME="IDX273"></A> <A NAME="IDX274"></A> <DT><SAMP>`--yywrap, <CODE>%option yywrap</CODE>'</SAMP> <DD> if unset (i.e., <CODE>--noyywrap)</CODE>, makes the scanner not call <CODE>yywrap()</CODE> upon an end-of-file, but simply assume that there are no more files to scan (until the user points <TT>`yyin'</TT> at a new file and calls <CODE>yylex()</CODE> again). </DL> <H2><A NAME="SEC23" HREF="flex_toc.html#TOC23">Code-Level And API Options</A></H2> <DL COMPACT> @anchor{option-bison-bridge} <DT><SAMP>`--bison-bridge, <CODE>%option bison-bridge</CODE>'</SAMP> <DD> <A NAME="IDX275"></A> <A NAME="IDX276"></A> instructs flex to generate a C scanner that is meant to be called by a <CODE>GNU bison</CODE> parser. The scanner has minor API changes for <CODE>bison</CODE> compatibility. In particular, the declaration of <CODE>yylex</CODE> is modified, and support for <CODE>yylval</CODE> and <CODE>yylloc</CODE> is incorporated. See section <A HREF="flex.html#SEC156">C Scanners with Bison Parsers</A>. @anchor{option-noline} <A NAME="IDX277"></A> <A NAME="IDX278"></A> <A NAME="IDX279"></A> <DT><SAMP>`-L, --noline, <CODE>%option noline</CODE>'</SAMP> <DD> instructs <CODE>flex</CODE> not to generate <CODE>#line</CODE> directives. Without this option, <CODE>flex</CODE> peppers the generated scanner with <CODE>#line</CODE> directives so error messages in the actions will be correctly located with respect to either the original <CODE>flex</CODE> input file (if the errors are due to code in the input file), or <TT>`lex.yy.c'</TT> (if the errors are <CODE>flex</CODE>'s fault -- you should report these sorts of errors to the email address given in section <A HREF="flex.html#SEC2">Reporting Bugs</A>). @anchor{option-reentrant} <A NAME="IDX280"></A> <A NAME="IDX281"></A> <A NAME="IDX282"></A> <DT><SAMP>`-R, --reentrant, <CODE>%option reentrant</CODE>'</SAMP> <DD> instructs flex to generate a reentrant C scanner. The generated scanner may safely be used in a multi-threaded environment. The API for a reentrant scanner is different than for a non-reentrant scanner see section <A HREF="flex.html#SEC29">Reentrant C Scanners</A>). Because of the API difference between reentrant and non-reentrant <CODE>flex</CODE> scanners, non-reentrant flex code must be modified before it is suitable for use with this option. This option is not compatible with the <SAMP>`--c++'</SAMP> option. The option <SAMP>`--reentrant'</SAMP> does not affect the performance of the scanner. @anchor{option-c++} <A NAME="IDX283"></A> <A NAME="IDX284"></A> <A NAME="IDX285"></A> <DT><SAMP>`-+, --c++, <CODE>%option c++</CODE>'</SAMP> <DD> specifies that you want flex to generate a C++ scanner class. See section <A HREF="flex.html#SEC28">Generating C++ Scanners</A>, for details. @anchor{option-array} <A NAME="IDX286"></A> <A NAME="IDX287"></A> <DT><SAMP>`--array, <CODE>%option array</CODE>'</SAMP> <DD> specifies that you want yytext to be an array instead of a char* @anchor{option-pointer} <A NAME="IDX288"></A> <A NAME="IDX289"></A> <DT><SAMP>`--pointer, <CODE>%option pointer</CODE>'</SAMP> <DD> specify that <CODE>yytext</CODE> should be a <CODE>char *</CODE>, not an array. This default is <CODE>char *</CODE>. @anchor{option-prefix} <A NAME="IDX290"></A> <A NAME="IDX291"></A> <A NAME="IDX292"></A> <DT><SAMP>`-PPREFIX, --prefix=PREFIX, <CODE>%option prefix="PREFIX"</CODE>'</SAMP> <DD> changes the default <SAMP>`yy'</SAMP> prefix used by <CODE>flex</CODE> for all globally-visible variable and function names to instead be <SAMP>`PREFIX'</SAMP>. For example, <SAMP>`--prefix=foo'</SAMP> changes the name of <CODE>yytext</CODE> to <CODE>footext</CODE>. It also changes the name of the default output file from <TT>`lex.yy.c'</TT> to <TT>`lex.foo.c'</TT>. Here is a partial list of the names affected: <PRE> @verbatim yy_create_buffer yy_delete_buffer yy_flex_debug yy_init_buffer yy_flush_buffer yy_load_buffer_state yy_switch_to_buffer yyin yyleng yylex yylineno yyout yyrestart yytext yywrap yyalloc yyrealloc yyfree </PRE> (If you are using a C++ scanner, then only <CODE>yywrap</CODE> and <CODE>yyFlexLexer</CODE> are affected.) Within your scanner itself, you can still refer to the global variables and functions using either version of their name; but externally, they have the modified name. This option lets you easily link together multiple <CODE>flex</CODE> programs into the same executable. Note, though, that using this option also renames <CODE>yywrap()</CODE>, so you now <EM>must</EM> either provide your own (appropriately-named) version of the routine for your scanner, or use <CODE>%option noyywrap</CODE>, as linking with <SAMP>`-lfl'</SAMP> no longer provides one for you by default. @anchor{option-main} <A NAME="IDX293"></A> <A NAME="IDX294"></A> <DT><SAMP>`--main, <CODE>%option main</CODE>'</SAMP> <DD> directs flex to provide a default <CODE>main()</CODE> program for the scanner, which simply calls <CODE>yylex()</CODE>. This option implies <CODE>noyywrap</CODE> (see below). @anchor{option-nounistd} <A NAME="IDX295"></A> <A NAME="IDX296"></A> <DT><SAMP>`--nounistd, <CODE>%option nounistd</CODE>'</SAMP> <DD> suppresses inclusion of the non-ANSI header file <TT>`unistd.h'</TT>. This option is meant to target environments in which <TT>`unistd.h'</TT> does not exist. Be aware that certain options may cause flex to generate code that relies on functions normally found in <TT>`unistd.h'</TT>, (e.g. <CODE>isatty()</CODE>, <CODE>read()</CODE>.) If you wish to use these functions, you will have to inform your compiler where to find them. @xref{option-always-interactive}. @xref{option-read}. @anchor{option-yyclass} <A NAME="IDX297"></A> <A NAME="IDX298"></A> <DT><SAMP>`--yyclass, <CODE>%option yyclass="NAME"</CODE>'</SAMP> <DD> only applies when generating a C++ scanner (the <SAMP>`--c++'</SAMP> option). It informs <CODE>flex</CODE> that you have derived <CODE>foo</CODE> as a subclass of <CODE>yyFlexLexer</CODE>, so <CODE>flex</CODE> will place your actions in the member function <CODE>foo::yylex()</CODE> instead of <CODE>yyFlexLexer::yylex()</CODE>. It also generates a <CODE>yyFlexLexer::yylex()</CODE> member function that emits a run-time error (by invoking <CODE>yyFlexLexer::LexerError())</CODE> if called. See section <A HREF="flex.html#SEC28">Generating C++ Scanners</A>. </DL> <H2><A NAME="SEC24" HREF="flex_toc.html#TOC24">Options for Scanner Speed and Size</A></H2> <DL COMPACT> <DT><SAMP>`-C[aefFmr]'</SAMP> <DD> controls the degree of table compression and, more generally, trade-offs between small scanners and fast scanners. <DL COMPACT> <DT><SAMP>`-C'</SAMP> <DD> <A NAME="IDX299"></A> A lone <SAMP>`-C'</SAMP> specifies that the scanner tables should be compressed but neither equivalence classes nor meta-equivalence classes should be used. @anchor{option-align} <A NAME="IDX300"></A> <A NAME="IDX301"></A> <A NAME="IDX302"></A> <DT><SAMP>`-Ca, --align, <CODE>%option align</CODE>'</SAMP> <DD> ("align") instructs flex to trade off larger tables in the generated scanner for faster performance because the elements of the tables are better aligned for memory access and computation. On some RISC architectures, fetching and manipulating longwords is more efficient than with smaller-sized units such as shortwords. This option can quadruple the size of the tables used by your scanner. @anchor{option-ecs} <A NAME="IDX303"></A> <A NAME="IDX304"></A> <A NAME="IDX305"></A> <DT><SAMP>`-Ce, --ecs, <CODE>%option ecs</CODE>'</SAMP> <DD> directs <CODE>flex</CODE> to construct <EM>equivalence classes</EM>, i.e., sets of characters which have identical lexical properties (for example, if the only appearance of digits in the <CODE>flex</CODE> input is in the character class "[0-9]" then the digits '0', '1', ..., '9' will all be put in the same equivalence class). Equivalence classes usually give dramatic reductions in the final table/object file sizes (typically a factor of 2-5) and are pretty cheap performance-wise (one array look-up per character scanned). <A NAME="IDX306"></A> <DT><SAMP>`-Cf'</SAMP> <DD> specifies that the <EM>full</EM> scanner tables should be generated - <CODE>flex</CODE> should not compress the tables by taking advantages of similar transition functions for different states. <A NAME="IDX307"></A> <DT><SAMP>`-CF'</SAMP> <DD> specifies that the alternate fast scanner representation (described above under the <SAMP>`--fast'</SAMP> flag) should be used. This option cannot be used with <SAMP>`--c++'</SAMP>. @anchor{option-meta-ecs} <A NAME="IDX308"></A> <A NAME="IDX309"></A> <A NAME="IDX310"></A> <DT><SAMP>`-Cm, --meta-ecs, <CODE>%option meta-ecs</CODE>'</SAMP> <DD> directs <CODE>flex</CODE> to construct <EM>meta-equivalence classes</EM>, which are sets of equivalence classes (or characters, if equivalence classes are not being used) that are commonly used together. Meta-equivalence classes are often a big win when using compressed tables, but they have a moderate performance impact (one or two <CODE>if</CODE> tests and one array look-up per character scanned). @anchor{option-read} <A NAME="IDX311"></A> <A NAME="IDX312"></A> <A NAME="IDX313"></A> <DT><SAMP>`-Cr, --read, <CODE>%option read</CODE>'</SAMP> <DD> causes the generated scanner to <EM>bypass</EM> use of the standard I/O library (<CODE>stdio</CODE>) for input. Instead of calling <CODE>fread()</CODE> or <CODE>getc()</CODE>, the scanner will use the <CODE>read()</CODE> system call, resulting in a performance gain which varies from system to system, but in general is probably negligible unless you are also using <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP>. Using <SAMP>`-Cr'</SAMP> can cause strange behavior if, for example, you read from <TT>`yyin'</TT> using <CODE>stdio</CODE> prior to calling the scanner (because the scanner will miss whatever text your previous reads left in the <CODE>stdio</CODE> input buffer). <SAMP>`-Cr'</SAMP> has no effect if you define <CODE>YY_INPUT()</CODE> (see section <A HREF="flex.html#SEC13">The Generated Scanner</A>). </DL> The options <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP> and <SAMP>`-Cm'</SAMP> do not make sense together - there is no opportunity for meta-equivalence classes if the table is not being compressed. Otherwise the options may be freely mixed, and are cumulative. The default setting is <SAMP>`-Cem'</SAMP>, which specifies that <CODE>flex</CODE> should generate equivalence classes and meta-equivalence classes. This setting provides the highest degree of table compression. You can trade off faster-executing scanners at the cost of larger tables with the following generally being true: <PRE> @verbatim slowest & smallest -Cem -Cm -Ce -C -C{f,F}e -C{f,F} -C{f,F}a fastest & largest </PRE> Note that scanners with the smallest tables are usually generated and compiled the quickest, so during development you will usually want to use the default, maximal compression. <SAMP>`-Cfe'</SAMP> is often a good compromise between speed and size for production scanners. @anchor{option-full} <A NAME="IDX314"></A> <A NAME="IDX315"></A> <A NAME="IDX316"></A> <DT><SAMP>`-f, --full, <CODE>%option full</CODE>'</SAMP> <DD> specifies <EM>fast scanner</EM>. No table compression is done and <CODE>stdio</CODE> is bypassed. The result is large but fast. This option is equivalent to <SAMP>`--Cfr'</SAMP> @anchor{option-fast} <A NAME="IDX317"></A> <A NAME="IDX318"></A> <A NAME="IDX319"></A> <DT><SAMP>`-F, --fast, <CODE>%option fast</CODE>'</SAMP> <DD> specifies that the <EM>fast</EM> scanner table representation should be used (and <CODE>stdio</CODE> bypassed). This representation is about as fast as the full table representation <SAMP>`--full'</SAMP>, and for some sets of patterns will be considerably smaller (and for others, larger). In general, if the pattern set contains both <EM>keywords</EM> and a catch-all, <EM>identifier</EM> rule, such as in the set: <PRE> @verbatim "case" return TOK_CASE; "switch" return TOK_SWITCH; ... "default" return TOK_DEFAULT; [a-z]+ return TOK_ID; </PRE> then you're better off using the full table representation. If only the <EM>identifier</EM> rule is present and you then use a hash table or some such to detect the keywords, you're better off using <SAMP>`--fast'</SAMP>. This option is equivalent to <SAMP>`-CFr'</SAMP> (see below). It cannot be used with <SAMP>`--c++'</SAMP>. </DL> <H2><A NAME="SEC25" HREF="flex_toc.html#TOC25">Debugging Options</A></H2> <DL COMPACT> @anchor{option-backup} <DT><SAMP>`-b, --backup, <CODE>%option backup</CODE>'</SAMP> <DD> <A NAME="IDX320"></A> <A NAME="IDX321"></A> <A NAME="IDX322"></A> Generate backing-up information to <TT>`lex.backup'</TT>. This is a list of scanner states which require backing up and the input characters on which they do so. By adding rules one can remove backing-up states. If <EM>all</EM> backing-up states are eliminated and <SAMP>`-Cf'</SAMP> or <CODE>-CF</CODE> is used, the generated scanner will run faster (see the <SAMP>`--perf-report'</SAMP> flag). Only users who wish to squeeze every last cycle out of their scanners need worry about this option. (see section <A HREF="flex.html#SEC27">Performance Considerations</A>). @anchor{option-debug} <A NAME="IDX323"></A> <A NAME="IDX324"></A> <A NAME="IDX325"></A> <DT><SAMP>`-d, --debug, <CODE>%option debug</CODE>'</SAMP> <DD> makes the generated scanner run in <EM>debug</EM> mode. Whenever a pattern is recognized and the global variable <CODE>yy_flex_debug</CODE> is non-zero (which is the default), the scanner will write to <TT>`stderr'</TT> a line of the form: <PRE> @verbatim -accepting rule at line 53 ("the matched text") </PRE> The line number refers to the location of the rule in the file defining the scanner (i.e., the file that was fed to flex). Messages are also generated when the scanner backs up, accepts the default rule, reaches the end of its input buffer (or encounters a NUL; at this point, the two look the same as far as the scanner's concerned), or reaches an end-of-file. @anchor{option-perf-report} <A NAME="IDX326"></A> <A NAME="IDX327"></A> <A NAME="IDX328"></A> <DT><SAMP>`-p, --perf-report, <CODE>%option perf-report</CODE>'</SAMP> <DD> generates a performance report to <TT>`stderr'</TT>. The report consists of comments regarding features of the <CODE>flex</CODE> input file which will cause a serious loss of performance in the resulting scanner. If you give the flag twice, you will also get comments regarding features that lead to minor performance losses. Note that the use of <CODE>REJECT</CODE>, and variable trailing context (see section <A HREF="flex.html#SEC52">Limitations</A>) entails a substantial performance penalty; use of <CODE>yymore()</CODE>, the <SAMP>`^'</SAMP> operator, and the <SAMP>`--interactive'</SAMP> flag entail minor performance penalties. @anchor{option-nodefault} <A NAME="IDX329"></A> <A NAME="IDX330"></A> <A NAME="IDX331"></A> <DT><SAMP>`-s, --nodefault, <CODE>%option nodefault</CODE>'</SAMP> <DD> causes the <EM>default rule</EM> (that unmatched scanner input is echoed to <TT>`stdout)'</TT> to be suppressed. If the scanner encounters input that does not match any of its rules, it aborts with an error. This option is useful for finding holes in a scanner's rule set. @anchor{option-trace} <A NAME="IDX332"></A> <A NAME="IDX333"></A> <A NAME="IDX334"></A> <DT><SAMP>`-T, --trace, <CODE>%option trace</CODE>'</SAMP> <DD> makes <CODE>flex</CODE> run in <EM>trace</EM> mode. It will generate a lot of messages to <TT>`stderr'</TT> concerning the form of the input and the resultant non-deterministic and deterministic finite automata. This option is mostly for use in maintaining <CODE>flex</CODE>. @anchor{option-nowarn} <A NAME="IDX335"></A> <A NAME="IDX336"></A> <A NAME="IDX337"></A> <DT><SAMP>`-w, --nowarn, <CODE>%option nowarn</CODE>'</SAMP> <DD> suppresses warning messages. @anchor{option-verbose} <A NAME="IDX338"></A> <A NAME="IDX339"></A> <A NAME="IDX340"></A> <DT><SAMP>`-v, --verbose, <CODE>%option verbose</CODE>'</SAMP> <DD> specifies that <CODE>flex</CODE> should write to <TT>`stderr'</TT> a summary of statistics regarding the scanner it generates. Most of the statistics are meaningless to the casual <CODE>flex</CODE> user, but the first line identifies the version of <CODE>flex</CODE> (same as reported by <SAMP>`--version'</SAMP>), and the next line the flags used when generating the scanner, including those that are on by default. @anchor{option-warn} <A NAME="IDX341"></A> <A NAME="IDX342"></A> <DT><SAMP>`--warn, <CODE>%option warn</CODE>'</SAMP> <DD> warn about certain things. In particular, if the default rule can be matched but no defualt rule has been given, the flex will warn you. We recommend using this option always. </DL> <H2><A NAME="SEC26" HREF="flex_toc.html#TOC26">Miscellaneous Options</A></H2> <DL COMPACT> <DT><SAMP>`-c'</SAMP> <DD> <A NAME="IDX343"></A> is a do-nothing option included for POSIX compliance. <A NAME="IDX344"></A> <A NAME="IDX345"></A> generates <DT><SAMP>`-h, -?, --help'</SAMP> <DD> generates a "help" summary of <CODE>flex</CODE>'s options to <TT>`stdout'</TT> and then exits. <A NAME="IDX346"></A> <DT><SAMP>`-n'</SAMP> <DD> is another do-nothing option included only for POSIX compliance. <A NAME="IDX347"></A> <A NAME="IDX348"></A> <DT><SAMP>`-V, --version'</SAMP> <DD> prints the version number to <TT>`stdout'</TT> and exits. </DL> <H1><A NAME="SEC27" HREF="flex_toc.html#TOC27">Performance Considerations</A></H1> <P> <A NAME="IDX349"></A> The main design goal of <CODE>flex</CODE> is that it generate high-performance scanners. It has been optimized for dealing well with large sets of rules. Aside from the effects on scanner speed of the table compression <SAMP>`-C'</SAMP> options outlined above, there are a number of options/actions which degrade performance. These are, from most expensive to least: <P> <A NAME="IDX350"></A> <A NAME="IDX351"></A> <A NAME="IDX352"></A> <PRE> @verbatim REJECT arbitrary trailing context pattern sets that require backing up %option yylineno %array %option interactive %option always-interactive <SAMP>`^'</SAMP> beginning-of-line operator yymore() </PRE> <P> with the first two all being quite expensive and the last two being quite cheap. Note also that <CODE>unput()</CODE> is implemented as a routine call that potentially does quite a bit of work, while <CODE>yyless()</CODE> is a quite-cheap macro. So if you are just putting back some excess text you scanned, use <CODE>ss()</CODE>. <P> <CODE>REJECT</CODE> should be avoided at all costs when performance is important. It is a particularly expensive option. <P> There is one case when <CODE>%option yylineno</CODE> can be expensive. That is when your patterns match long tokens that could <EM>possibly</EM> contain a newline character. There is no performance penalty for rules that can not possibly match newlines, since flex does not need to check them for newlines. In general, you should avoid rules such as <CODE>[^f]+</CODE>, which match very long tokens, including newlines, and may possibly match your entire file! A better approach is to separate <CODE>[^f]+</CODE> into two rules: <PRE> @verbatim %option yylineno %% [^f\n]+ \n+ </PRE> <P> The above scanner does not incur a performance penalty. <P> <A NAME="IDX353"></A> <A NAME="IDX354"></A> <A NAME="IDX355"></A> Getting rid of backing up is messy and often may be an enormous amount of work for a complicated scanner. In principal, one begins by using the <SAMP>`-b'</SAMP> flag to generate a <TT>`lex.backup'</TT> file. For example, on the input: <P> <A NAME="IDX356"></A> <PRE> @verbatim %% foo return TOK_KEYWORD; foobar return TOK_KEYWORD; </PRE> <P> the file looks like: <PRE> @verbatim State #6 is non-accepting - associated rule line numbers: 2 3 out-transitions: [ o ] jam-transitions: EOF [ \001-n p-\177 ] State #8 is non-accepting - associated rule line numbers: 3 out-transitions: [ a ] jam-transitions: EOF [ \001-` b-\177 ] State #9 is non-accepting - associated rule line numbers: 3 out-transitions: [ r ] jam-transitions: EOF [ \001-q s-\177 ] Compressed tables always back up. </PRE> <P> The first few lines tell us that there's a scanner state in which it can make a transition on an 'o' but not on any other character, and that in that state the currently scanned text does not match any rule. The state occurs when trying to match the rules found at lines 2 and 3 in the input file. If the scanner is in that state and then reads something other than an 'o', it will have to back up to find a rule which is matched. With a bit of headscratching one can see that this must be the state it's in when it has seen <SAMP>`fo'</SAMP>. When this has happened, if anything other than another <SAMP>`o'</SAMP> is seen, the scanner will have to back up to simply match the <SAMP>`f'</SAMP> (by the default rule). <P> The comment regarding State #8 indicates there's a problem when <SAMP>`foob'</SAMP> has been scanned. Indeed, on any character other than an <SAMP>`a'</SAMP>, the scanner will have to back up to accept "foo". Similarly, the comment for State #9 concerns when <SAMP>`fooba'</SAMP> has been scanned and an <SAMP>`r'</SAMP> does not follow. <P> The final comment reminds us that there's no point going to all the trouble of removing backing up from the rules unless we're using <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP>, since there's no performance gain doing so with compressed scanners. <P> <A NAME="IDX357"></A> The way to remove the backing up is to add "error" rules: <P> <A NAME="IDX358"></A> <PRE> @verbatim %% foo return TOK_KEYWORD; foobar return TOK_KEYWORD; fooba | foob | fo { /* false alarm, not really a keyword */ return TOK_ID; } </PRE> <P> Eliminating backing up among a list of keywords can also be done using a "catch-all" rule: <P> <A NAME="IDX359"></A> <PRE> @verbatim %% foo return TOK_KEYWORD; foobar return TOK_KEYWORD; [a-z]+ return TOK_ID; </PRE> <P> This is usually the best solution when appropriate. <P> Backing up messages tend to cascade. With a complicated set of rules it's not uncommon to get hundreds of messages. If one can decipher them, though, it often only takes a dozen or so rules to eliminate the backing up (though it's easy to make a mistake and have an error rule accidentally match a valid token. A possible future <CODE>flex</CODE> feature will be to automatically add rules to eliminate backing up). <P> It's important to keep in mind that you gain the benefits of eliminating backing up only if you eliminate <EM>every</EM> instance of backing up. Leaving just one means you gain nothing. <P> <EM>Variable</EM> trailing context (where both the leading and trailing parts do not have a fixed length) entails almost the same performance loss as <CODE>REJECT</CODE> (i.e., substantial). So when possible a rule like: <P> <A NAME="IDX360"></A> <PRE> @verbatim %% mouse|rat/(cat|dog) run(); </PRE> <P> is better written: <PRE> @verbatim %% mouse/cat|dog run(); rat/cat|dog run(); </PRE> <P> or as <PRE> @verbatim %% mouse|rat/cat run(); mouse|rat/dog run(); </PRE> <P> Note that here the special '|' action does <EM>not</EM> provide any savings, and can even make things worse (see section <A HREF="flex.html#SEC52">Limitations</A>). <P> Another area where the user can increase a scanner's performance (and one that's easier to implement) arises from the fact that the longer the tokens matched, the faster the scanner will run. This is because with long tokens the processing of most input characters takes place in the (short) inner scanning loop, and does not often have to go through the additional work of setting up the scanning environment (e.g., <CODE>yytext</CODE>) for the action. Recall the scanner for C comments: <P> <A NAME="IDX361"></A> <PRE> @verbatim %x comment %% int line_num = 1; "/*" BEGIN(comment); <comment>[^*\n]* <comment>"*"+[^*/\n]* <comment>\n ++line_num; <comment>"*"+"/" BEGIN(INITIAL); </PRE> <P> This could be sped up by writing it as: <PRE> @verbatim %x comment %% int line_num = 1; "/*" BEGIN(comment); <comment>[^*\n]* <comment>[^*\n]*\n ++line_num; <comment>"*"+[^*/\n]* <comment>"*"+[^*/\n]*\n ++line_num; <comment>"*"+"/" BEGIN(INITIAL); </PRE> <P> Now instead of each newline requiring the processing of another action, recognizing the newlines is distributed over the other rules to keep the matched text as long as possible. Note that <EM>adding</EM> rules does <EM>not</EM> slow down the scanner! The speed of the scanner is independent of the number of rules or (modulo the considerations given at the beginning of this section) how complicated the rules are with regard to operators such as <SAMP>`*'</SAMP> and <SAMP>`|'</SAMP>. <P> <A NAME="IDX362"></A> <A NAME="IDX363"></A> A final example in speeding up a scanner: suppose you want to scan through a file containing identifiers and keywords, one per line and with no other extraneous characters, and recognize all the keywords. A natural first approach is: <P> <A NAME="IDX364"></A> <PRE> @verbatim %% asm | auto | break | ... etc ... volatile | while /* it's a keyword */ .|\n /* it's not a keyword */ </PRE> <P> To eliminate the back-tracking, introduce a catch-all rule: <PRE> @verbatim %% asm | auto | break | ... etc ... volatile | while /* it's a keyword */ [a-z]+ | .|\n /* it's not a keyword */ </PRE> <P> Now, if it's guaranteed that there's exactly one word per line, then we can reduce the total number of matches by a half by merging in the recognition of newlines with that of the other tokens: <PRE> @verbatim %% asm\n | auto\n | break\n | ... etc ... volatile\n | while\n /* it's a keyword */ [a-z]+\n | .|\n /* it's not a keyword */ </PRE> <P> One has to be careful here, as we have now reintroduced backing up into the scanner. In particular, while <EM>we</EM> know that there will never be any characters in the input stream other than letters or newlines, <CODE>flex</CODE> can't figure this out, and it will plan for possibly needing to back up when it has scanned a token like <SAMP>`auto'</SAMP> and then the next character is something other than a newline or a letter. Previously it would then just match the <SAMP>`auto'</SAMP> rule and be done, but now it has no <SAMP>`auto'</SAMP> rule, only a <SAMP>`auto\n'</SAMP> rule. To eliminate the possibility of backing up, we could either duplicate all rules but without final newlines, or, since we never expect to encounter such an input and therefore don't how it's classified, we can introduce one more catch-all rule, this one which doesn't include a newline: <PRE> @verbatim %% asm\n | auto\n | break\n | ... etc ... volatile\n | while\n /* it's a keyword */ [a-z]+\n | [a-z]+ | .|\n /* it's not a keyword */ </PRE> <P> Compiled with <SAMP>`-Cf'</SAMP>, this is about as fast as one can get a <CODE>flex</CODE> scanner to go for this particular problem. <P> A final note: <CODE>flex</CODE> is slow when matching <CODE>NUL</CODE>s, particularly when a token contains multiple <CODE>NUL</CODE>s. It's best to write rules which match <EM>short</EM> amounts of text if it's anticipated that the text will often include <CODE>NUL</CODE>s. <P> Another final note regarding performance: as mentioned in section <A HREF="flex.html#SEC11">How the Input Is Matched</A>, dynamically resizing <CODE>yytext</CODE> to accommodate huge tokens is a slow process because it presently requires that the (huge) token be rescanned from the beginning. Thus if performance is vital, you should attempt to match "large" quantities of text but not "huge" quantities, where the cutoff between the two is at about 8K characters per token. <H1><A NAME="SEC28" HREF="flex_toc.html#TOC28">Generating C++ Scanners</A></H1> <P> <A NAME="IDX365"></A> <A NAME="IDX366"></A> <STRONG>IMPORTANT</STRONG>: the present form of the scanning class is <EM>experimental</EM> and may change considerably between major releases. <P> <A NAME="IDX367"></A> <A NAME="IDX368"></A> <A NAME="IDX369"></A> <CODE>flex</CODE> provides two different ways to generate scanners for use with C++. The first way is to simply compile a scanner generated by <CODE>flex</CODE> using a C++ compiler instead of a C compiler. You should not encounter any compilation errors (see section <A HREF="flex.html#SEC2">Reporting Bugs</A>). You can then use C++ code in your rule actions instead of C code. Note that the default input source for your scanner remains <TT>`yyin'</TT>, and default echoing is still done to <TT>`yyout'</TT>. Both of these remain <CODE>FILE *</CODE> variables and not C++ <EM>streams</EM>. <P> You can also use <CODE>flex</CODE> to generate a C++ scanner class, using the <SAMP>`-+'</SAMP> option (or, equivalently, <CODE>%option c++)</CODE>, which is automatically specified if the name of the <CODE>flex</CODE> executable ends in a '+', such as <CODE>flex++</CODE>. When using this option, <CODE>flex</CODE> defaults to generating the scanner to the file <TT>`lex.yy.cc'</TT> instead of <TT>`lex.yy.c'</TT>. The generated scanner includes the header file <TT>`FlexLexer.h'</TT>, which defines the interface to two C++ classes. <P> The first class, <CODE>FlexLexer</CODE>, provides an abstract base class defining the general scanner class interface. It provides the following member functions: <DL COMPACT> <DT><CODE>const char* YYText()</CODE> <DD> <A NAME="IDX370"></A> returns the text of the most recently matched token, the equivalent of <CODE>yytext</CODE>. <A NAME="IDX371"></A> <DT><CODE>int YYLeng()</CODE> <DD> returns the length of the most recently matched token, the equivalent of <CODE>yyleng</CODE>. <A NAME="IDX372"></A> <DT><CODE>int lineno() const</CODE> <DD> returns the current input line number (see <CODE>%option yylineno)</CODE>, or <CODE>1</CODE> if <CODE>%option yylineno</CODE> was not used. <A NAME="IDX373"></A> <DT><CODE>void set_debug( int flag )</CODE> <DD> sets the debugging flag for the scanner, equivalent to assigning to <CODE>yy_flex_debug</CODE> (see section <A HREF="flex.html#SEC20">Scanner Options</A>). Note that you must build the scannerusing <CODE>%option debug</CODE> to include debugging information in it. <A NAME="IDX374"></A> <DT><CODE>int debug() const</CODE> <DD> returns the current setting of the debugging flag. </DL> <P> Also provided are member functions equivalent to <CODE>yy_switch_to_buffer()</CODE>, <CODE>yy_create_buffer()</CODE> (though the first argument is an <CODE>istream*</CODE> object pointer and not a <CODE>FILE*)</CODE>, <CODE>yy_flush_buffer()</CODE>, <CODE>yy_delete_buffer()</CODE>, and <CODE>yyrestart()</CODE> (again, the first argument is a <CODE>istream*</CODE> object pointer). <P> <A NAME="IDX375"></A> <A NAME="IDX376"></A> The second class defined in <TT>`FlexLexer.h'</TT> is <CODE>yyFlexLexer</CODE>, which is derived from <CODE>FlexLexer</CODE>. It defines the following additional member functions: <DL COMPACT> <DT><CODE>yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 )</CODE> <DD> <A NAME="IDX377"></A> constructs a <CODE>yyFlexLexer</CODE> object using the given streams for input and output. If not specified, the streams default to <CODE>cin</CODE> and <CODE>cout</CODE>, respectively. <A NAME="IDX378"></A> <DT><CODE>virtual int yylex()</CODE> <DD> performs the same role is <CODE>yylex()</CODE> does for ordinary <CODE>flex</CODE> scanners: it scans the input stream, consuming tokens, until a rule's action returns a value. If you derive a subclass <CODE>S</CODE> from <CODE>yyFlexLexer</CODE> and want to access the member functions and variables of <CODE>S</CODE> inside <CODE>yylex()</CODE>, then you need to use <CODE>%option yyclass="S"</CODE> to inform <CODE>flex</CODE> that you will be using that subclass instead of <CODE>yyFlexLexer</CODE>. In this case, rather than generating <CODE>yyFlexLexer::yylex()</CODE>, <CODE>flex</CODE> generates <CODE>S::yylex()</CODE> (and also generates a dummy <CODE>yyFlexLexer::yylex()</CODE> that calls <CODE>yyFlexLexer::LexerError()</CODE> if called). <A NAME="IDX379"></A> <DT><CODE>virtual void switch_streams(istream* new_in = 0, ostream* new_out = 0)</CODE> <DD> reassigns <CODE>yyin</CODE> to <CODE>new_in</CODE> (if non-nil) and <CODE>yyout</CODE> to <CODE>new_out</CODE> (if non-nil), deleting the previous input buffer if <CODE>yyin</CODE> is reassigned. <DT><CODE>int yylex( istream* new_in, ostream* new_out = 0 )</CODE> <DD> first switches the input streams via <CODE>switch_streams( new_in, new_out )</CODE> and then returns the value of <CODE>yylex()</CODE>. </DL> <P> In addition, <CODE>yyFlexLexer</CODE> defines the following protected virtual functions which you can redefine in derived classes to tailor the scanner: <DL COMPACT> <DT><CODE>virtual int LexerInput( char* buf, int max_size )</CODE> <DD> <A NAME="IDX380"></A> reads up to <CODE>max_size</CODE> characters into <CODE>buf</CODE> and returns the number of characters read. To indicate end-of-input, return 0 characters. Note that <CODE>interactive</CODE> scanners (see the <SAMP>`-B'</SAMP> and <SAMP>`-I'</SAMP> flags in section <A HREF="flex.html#SEC20">Scanner Options</A>) define the macro <CODE>YY_INTERACTIVE</CODE>. If you redefine <CODE>LexerInput()</CODE> and need to take different actions depending on whether or not the scanner might be scanning an interactive input source, you can test for the presence of this name via <CODE>#ifdef</CODE> statements. <A NAME="IDX381"></A> <DT><CODE>virtual void LexerOutput( const char* buf, int size )</CODE> <DD> writes out <CODE>size</CODE> characters from the buffer <CODE>buf</CODE>, which, while <CODE>NUL</CODE>-terminated, may also contain internal <CODE>NUL</CODE>s if the scanner's rules can match text with <CODE>NUL</CODE>s in them. <A NAME="IDX382"></A> <A NAME="IDX383"></A> <DT><CODE>virtual void LexerError( const char* msg )</CODE> <DD> reports a fatal error message. The default version of this function writes the message to the stream <CODE>cerr</CODE> and exits. </DL> <P> Note that a <CODE>yyFlexLexer</CODE> object contains its <EM>entire</EM> scanning state. Thus you can use such objects to create reentrant scanners, but see also section <A HREF="flex.html#SEC29">Reentrant C Scanners</A>. You can instantiate multiple instances of the same <CODE>yyFlexLexer</CODE> class, and you can also combine multiple C++ scanner classes together in the same program using the <SAMP>`-P'</SAMP> option discussed above. <P> Finally, note that the <CODE>%array</CODE> feature is not available to C++ scanner classes; you must use <CODE>%pointer</CODE> (the default). <P> Here is an example of a simple C++ scanner: <P> <A NAME="IDX384"></A> <PRE> @verbatim // An example of using the flex C++ scanner class. %{ int mylineno = 0; %} string \"[^\n"]+\" ws [ \t]+ alpha [A-Za-z] dig [0-9] name ({alpha}|{dig}|\$)({alpha}|{dig}|[_.\-/$])* num1 [-+]?{dig}+\.?([eE][-+]?{dig}+)? num2 [-+]?{dig}*\.{dig}+([eE][-+]?{dig}+)? number {num1}|{num2} %% {ws} /* skip blanks and tabs */ "/*" { int c; while((c = yyinput()) != 0) { if(c == '\n') ++mylineno; else if(c == <SAMP>`*'</SAMP>) { if((c = yyinput()) == '/') break; else unput(c); } } } {number} cout "number " YYText() '\n'; \n mylineno++; {name} cout "name " YYText() '\n'; {string} cout "string " YYText() '\n'; %% int main( int /* argc */, char** /* argv */ ) { <CODE>flex</CODE>Lexer* lexer = new yyFlexLexer; while(lexer->yylex() != 0) ; return 0; } </PRE> <P> <A NAME="IDX385"></A> If you want to create multiple (different) lexer classes, you use the <SAMP>`-P'</SAMP> flag (or the <CODE>prefix=</CODE> option) to rename each <CODE>yyFlexLexer</CODE> to some other <SAMP>`xxFlexLexer'</SAMP>. You then can include <TT>`FlexLexer.h>'</TT> in your other sources once per lexer class, first renaming <CODE>yyFlexLexer</CODE> as follows: <P> <A NAME="IDX386"></A> <A NAME="IDX387"></A> <A NAME="IDX388"></A> <PRE> @verbatim #undef yyFlexLexer #define yyFlexLexer xxFlexLexer #include <FflexLexer.h> #undef yyFlexLexer #define yyFlexLexer zzFlexLexer #include FlexLexer.h> </PRE> <P> if, for example, you used <CODE>%option prefix="xx"</CODE> for one of your scanners and <CODE>%option prefix="zz"</CODE> for the other. <H1><A NAME="SEC29" HREF="flex_toc.html#TOC29">Reentrant C Scanners</A></H1> <P> <A NAME="IDX389"></A> <CODE>flex</CODE> has the ability to generate a reentrant C scanner. This is accomplished by specifying <CODE>%option reentrant</CODE> (<SAMP>`-R'</SAMP>) The generated scanner is both portable, and safe to use in one or more separate threads of control. The most common use for reentrant scanners is from within multi-threaded applications. Any thread may create and execute a reentrant <CODE>flex</CODE> scanner without the need for synchronization with other threads. <H2><A NAME="SEC30" HREF="flex_toc.html#TOC30">Uses for Reentrant Scanners</A></H2> <P> However, there are other uses for a reentrant scanner. For example, you could scan two or more files simultaneously to implement a <CODE>diff</CODE> at the token level (i.e., instead of at the character level): <P> <A NAME="IDX390"></A> <PRE> @verbatim /* Example of maintaining more than one active scanner. */ do { int tok1, tok2; tok1 = yylex( scanner_1 ); tok2 = yylex( scanner_2 ); if( tok1 != tok2 ) printf("Files are different."); } while ( tok1 && tok2 ); </PRE> <P> Another use for a reentrant scanner is recursion. (Note that a recursive scanner can also be created using a non-reentrant scanner and buffer states. See section <A HREF="flex.html#SEC15">Multiple Input Buffers</A>.) <P> The following crude scanner supports the <SAMP>`eval'</SAMP> command by invoking another instance of itself. <P> <A NAME="IDX391"></A> <PRE> @verbatim /* Example of recursive invocation. */ %option reentrant %% "eval(".+")" { yyscan_t scanner; YY_BUFFER_STATE buf; yylex_init( &scanner ); yytext[yyleng-1] = ' '; buf = yy_scan_string( yytext + 5, scanner ); yylex( scanner ); yy_delete_buffer(buf,scanner); yylex_destroy( scanner ); } ... %% </PRE> <H2><A NAME="SEC31" HREF="flex_toc.html#TOC31">An Overview of the Reentrant API</A></H2> <P> <A NAME="IDX392"></A> The API for reentrant scanners is different than for non-reentrant scanners. Here is a quick overview of the API: <UL> <CODE>%option reentrant</CODE> must be specified. <LI> All functions take one additional argument: <CODE>yyscanner</CODE> <LI> All global variables are replaced by their macro equivalents. (We tell you this because it may be important to you during debugging.) <LI> <CODE>yylex_init</CODE> and <CODE>yylex_destroy</CODE> must be called before and after <CODE>yylex</CODE>, respectively. <LI> Accessor methods (get/set functions) provide access to common <CODE>flex</CODE> variables. <LI> User-specific data can be stored in <CODE>yyextra</CODE>. </UL> <H2><A NAME="SEC32" HREF="flex_toc.html#TOC32">Reentrant Example</A></H2> <P> First, an example of a reentrant scanner: <A NAME="IDX393"></A> <PRE> @verbatim /* This scanner prints "//" comments. */ %option reentrant stack %x COMMENT %% "//" yy_push_state( COMMENT, yyscanner); .|\n <COMMENT>\n yy_pop_state( yyscanner ); <COMMENT>[^\n]+ fprintf( yyout, "%s\n", yytext); %% int main ( int argc, char * argv[] ) { yyscan_t scanner; yylex_init ( &scanner ); yylex ( scanner ); yylex_destroy ( scanner ); return 0; } </PRE> <H2><A NAME="SEC33" HREF="flex_toc.html#TOC33">The Reentrant API in Detail</A></H2> <P> Here are the things you need to do or know to use the reentrant C API of <CODE>flex</CODE>. <H3><A NAME="SEC34" HREF="flex_toc.html#TOC34">Declaring a Scanner As Reentrant</A></H3> <P> %option reentrant (--reentrant) must be specified. <P> Notice that <CODE>%option reentrant</CODE> is specified in the above example (see section <A HREF="flex.html#SEC32">Reentrant Example</A>. Had this option not been specified, <CODE>flex</CODE> would have happily generated a non-reentrant scanner without complaining. You may explicitly specify <CODE>%option noreentrant</CODE>, if you do <EM>not</EM> want a reentrant scanner, although it is not necessary. The default is to generate a non-reentrant scanner. <H3><A NAME="SEC35" HREF="flex_toc.html#TOC35">The Extra Argument</A></H3> <P> <A NAME="IDX394"></A> <A NAME="IDX395"></A> All functions take one additional argument: <CODE>yyscanner</CODE>. <P> Notice that the calls to <CODE>yy_push_state</CODE> and <CODE>yy_pop_state</CODE> both have an argument, <CODE>yyscanner</CODE> , that is not present in a non-reentrant scanner. Here are the declarations of <CODE>yy_push_state</CODE> and <CODE>yy_pop_state</CODE> in the generated scanner: <PRE> @verbatim static void yy_push_state ( int new_state , yyscan_t yyscanner ) ; static void yy_pop_state ( yyscan_t yyscanner ) ; </PRE> <P> Notice that the argument <CODE>yyscanner</CODE> appears in the declaration of both functions. In fact, all <CODE>flex</CODE> functions in a reentrant scanner have this additional argument. It is always the last argument in the argument list, it is always of type <CODE>yyscan_t</CODE> (which is typedef'd to <CODE>void *</CODE>) and it is always named <CODE>yyscanner</CODE>. As you may have guessed, <CODE>yyscanner</CODE> is a pointer to an opaque data structure encapsulating the current state of the scanner. For a list of function declarations, see section <A HREF="flex.html#SEC41">Functions and Macros Available in Reentrant C Scanners</A>. Note that preprocessor macros, such as <CODE>BEGIN</CODE>, <CODE>ECHO</CODE>, and <CODE>REJECT</CODE>, do not take this additional argument. <H3><A NAME="SEC36" HREF="flex_toc.html#TOC36">Global Variables Replaced By Macros</A></H3> <P> <A NAME="IDX396"></A> All global variables in traditional flex have been replaced by macro equivalents. <P> Note that in the above example, <CODE>yyout</CODE> and <CODE>yytext</CODE> are not plain variables. These are macros that will expand to their equivalent lvalue. All of the familiar <CODE>flex</CODE> globals have been replaced by their macro equivalents. In particular, <CODE>yytext</CODE>, <CODE>yyleng</CODE>, <CODE>yylineno</CODE>, <CODE>yyin</CODE>, <CODE>yyout</CODE>, <CODE>yyextra</CODE>, <CODE>yylval</CODE>, and <CODE>yylloc</CODE> are macros. You may safely use these macros in actions as if they were plain variables. We only tell you this so you don't expect to link to these variables externally. Currently, each macro expands to a member of an internal struct, e.g., <PRE> @verbatim #define yytext (((struct yyguts_t*)yyscanner)->yytext_r) </PRE> <P> One important thing to remember about <CODE>yytext</CODE> and friends is that <CODE>yytext</CODE> is not a global variable in a reentrant scanner, you can not access it directly from outside an action or from other functions. You must use an accessor method, e.g., <CODE>yyget_text</CODE>, to accomplish this. (See below). <H3><A NAME="SEC37" HREF="flex_toc.html#TOC37">Init and Destroy Functions</A></H3> <P> <A NAME="IDX397"></A> <A NAME="IDX398"></A> <A NAME="IDX399"></A> <A NAME="IDX400"></A> <P> <CODE>yylex_init</CODE> and <CODE>yylex_destroy</CODE> must be called before and after <CODE>yylex</CODE>, respectively. <PRE> @verbatim int yylex_init ( yyscan_t * ptr_yy_globals ) ; int yylex ( yyscan_t yyscanner ) ; int yylex_destroy ( yyscan_t yyscanner ) ; </PRE> <P> The function <CODE>yylex_init</CODE> must be called before calling any other function. The argument to <CODE>yylex_init</CODE> is the address of an uninitialized pointer to be filled in by <CODE>flex</CODE>. The contents of <CODE>ptr_yy_globals</CODE> need not be initialized, since <CODE>flex</CODE> will overwrite it anyway. The value stored in <CODE>ptr_yy_globals</CODE> should thereafter be passed to <CODE>yylex()</CODE> and <B>yylex_destroy()</B>. Flex does not save the argument passed to <CODE>yylex_init</CODE>, so it is safe to pass the address of a local pointer to <CODE>yylex_init</CODE>. The function <CODE>yylex</CODE> should be familiar to you by now. The reentrant version takes one argument, which is the value returned (via an argument) by <CODE>yylex_init</CODE>. Otherwise, it behaves the same as the non-reentrant version of <CODE>yylex</CODE>. <P> <CODE>yylex_init</CODE> returns 0 (zero) on success, or non-zero on failure, in which case, errno is set to one of the following values: <UL> <LI>ENOMEM Memory allocation error. @xref{memory-management}. <LI>EINVAL Invalid argument. </UL> <P> The function <CODE>yylex_destroy</CODE> should be called to free resources used by the scanner. After <CODE>yylex_destroy</CODE> is called, the contents of <CODE>yyscanner</CODE> should not be used. Of course, there is no need to destroy a scanner if you plan to reuse it. A <CODE>flex</CODE> scanner (both reentrant and non-reentrant) may be restarted by calling <CODE>yyrestart</CODE>. <P> Below is an example of a program that creates a scanner, uses it, then destroys it when done: <PRE> @verbatim int main () { yyscan_t scanner; int tok; yylex_init(&scanner); while ((tok=yylex()) > 0) printf("tok=%d yytext=%s\n", tok, yyget_text(scanner)); yylex_destroy(scanner); return 0; } </PRE> <H3><A NAME="SEC38" HREF="flex_toc.html#TOC38">Accessing Variables with Reentrant Scanners</A></H3> <P> <A NAME="IDX401"></A> Accessor methods (get/set functions) provide access to common <CODE>flex</CODE> variables. <P> Many scanners that you build will be part of a larger project. Portions of your project will need access to <CODE>flex</CODE> values, such as <CODE>yytext</CODE>. In a non-reentrant scanner, these values are global, so there is no problem accessing them. However, in a reentrant scanner, there are no global <CODE>flex</CODE> values. You can not access them directly. Instead, you must access <CODE>flex</CODE> values using accessor methods (get/set functions). Each accessor method is named <CODE>yyget_NAME</CODE> or <CODE>yyset_NAME</CODE>, where <CODE>NAME</CODE> is the name of the <CODE>flex</CODE> variable you want. For example: <P> <A NAME="IDX402"></A> <PRE> @verbatim /* Set the last character of yytext to NULL. */ void chop ( yyscan_t scanner ) { int len = yyget_leng( scanner ); yyget_text( scanner )[len - 1] = '\0'; } </PRE> <P> The above code may be called from within an action like this: <PRE> @verbatim %% .+\n { chop( yyscanner );} </PRE> <P> You may find that <CODE>%option header-file</CODE> is particularly useful for generating prototypes of all the accessor functions. @xref{option-header}. <H3><A NAME="SEC39" HREF="flex_toc.html#TOC39">Extra Data</A></H3> <P> <A NAME="IDX403"></A> <A NAME="IDX404"></A> User-specific data can be stored in <CODE>yyextra</CODE>. <P> In a reentrant scanner, it is unwise to use global variables to communicate with or maintain state between different pieces of your program. However, you may need access to external data or invoke external functions from within the scanner actions. Likewise, you may need to pass information to your scanner (e.g., open file descriptors, or database connections). In a non-reentrant scanner, the only way to do this would be through the use of global variables. <CODE>Flex</CODE> allows you to store arbitrary, "extra" data in a scanner. This data is accessible through the accessor methods <CODE>yyget_extra</CODE> and <CODE>yyset_extra</CODE> from outside the scanner, and through the shortcut macro <CODE>yyextra</CODE> from within the scanner itself. They are defined as follows: <P> <A NAME="IDX405"></A> <A NAME="IDX406"></A> <A NAME="IDX407"></A> <PRE> @verbatim #define YY_EXTRA_TYPE void* YY_EXTRA_TYPE yyget_extra ( yyscan_t scanner ); void yyset_extra ( YY_EXTRA_TYPE arbitrary_data , yyscan_t scanner); </PRE> <P> By default, <CODE>YY_EXTRA_TYPE</CODE> is defined as type <CODE>void *</CODE>. You will have to cast <CODE>yyextra</CODE> and the return value from <CODE>yyget_extra</CODE> to the appropriate value each time you access the extra data. To avoid casting, you may override the default type by defining <CODE>YY_EXTRA_TYPE</CODE> in section 1 of your scanner: <P> <A NAME="IDX408"></A> <PRE> @verbatim /* An example of overriding YY_EXTRA_TYPE. */ %{ #include <sys/stat.h> #include <unistd.h> #define YY_EXTRA_TYPE struct stat* %} %option reentrant %% __filesize__ printf( "%ld", yyextra->st_size ); __lastmod__ printf( "%ld", yyextra->st_mtime ); %% void scan_file( char* filename ) { yyscan_t scanner; struct stat buf; yylex_init ( &scanner ); yyset_in( fopen(filename,"r"), scanner ); stat( filename, &buf); yyset_extra( &buf, scanner ); yylex ( scanner ); yylex_destroy( scanner ); } </PRE> <H3><A NAME="SEC40" HREF="flex_toc.html#TOC40">About yyscan_t</A></H3> <P> <A NAME="IDX409"></A> <CODE>yyscan_t</CODE> is defined as: <PRE> @verbatim typedef void* yyscan_t; </PRE> <P> It is initialized by <CODE>yylex_init()</CODE> to point to an internal structure. You should never access this value directly. In particular, you should never attempt to free it (use <CODE>yylex_destroy()</CODE> instead.) <H2><A NAME="SEC41" HREF="flex_toc.html#TOC41">Functions and Macros Available in Reentrant C Scanners</A></H2> <P> The following Functions are available in a reentrant scanner: <P> <A NAME="IDX410"></A> <A NAME="IDX411"></A> <A NAME="IDX412"></A> <A NAME="IDX413"></A> <A NAME="IDX414"></A> <A NAME="IDX415"></A> <A NAME="IDX416"></A> <A NAME="IDX417"></A> <A NAME="IDX418"></A> <A NAME="IDX419"></A> <A NAME="IDX420"></A> <A NAME="IDX421"></A> <PRE> @verbatim char *yyget_text ( yyscan_t scanner ); int yyget_leng ( yyscan_t scanner ); FILE *yyget_in ( yyscan_t scanner ); FILE *yyget_out ( yyscan_t scanner ); int yyget_lineno ( yyscan_t scanner ); YY_EXTRA_TYPE yyget_extra ( yyscan_t scanner ); int yyget_debug ( yyscan_t scanner ); void yyset_debug ( int flag, yyscan_t scanner ); void yyset_in ( FILE * in_str , yyscan_t scanner ); void yyset_out ( FILE * out_str , yyscan_t scanner ); void yyset_lineno ( int line_number , yyscan_t scanner ); void yyset_extra ( YY_EXTRA_TYPE user_defined , yyscan_t scanner ); </PRE> <P> There are no "set" functions for yytext and yyleng. This is intentional. <P> The following Macro shortcuts are available in actions in a reentrant scanner: <PRE> @verbatim yytext yyleng yyin yyout yylineno yyextra yy_flex_debug </PRE> <P> <A NAME="IDX422"></A> In a reentrant C scanner, support for yylineno is always present (i.e., you may access yylineno), but the value is never modified by <CODE>flex</CODE> unless <CODE>%option yylineno</CODE> is enabled. This is to allow the user to maintain the line count independently of <CODE>flex</CODE>. <P> The following functions and macros are made available when <CODE>%option bison-bridge</CODE> (<SAMP>`--bison-bridge'</SAMP>) is specified: <PRE> @verbatim YYSTYPE * yyget_lval ( yyscan_t scanner ); YYLTYPE *yyget_lloc ( yyscan_t scanner ); void yyset_lval ( YYSTYPE * yylvalp , yyscan_t scanner ); void yyset_lloc ( YYLTYPE * yyllocp , yyscan_t scanner ); yylval yylloc </PRE> <P> Support for yylloc is dependent upon the presence of the preprocessor symbol <CODE>YYLTYPE</CODE>. Support for yylval relies on the type <CODE>YYSTYPE</CODE> to be defined. Typically, these definitions are generated by <CODE>bison</CODE>, in a .h file, and are included in section 1 of the <CODE>flex</CODE> input. <H1><A NAME="SEC42" HREF="flex_toc.html#TOC42">Incompatibilities with Lex and Posix</A></H1> <P> <A NAME="IDX423"></A> <A NAME="IDX424"></A> <P> <CODE>flex</CODE> is a rewrite of the AT&T Unix <EM>lex</EM> tool (the two implementations do not share any code, though), with some extensions and incompatibilities, both of which are of concern to those who wish to write scanners acceptable to both implementations. <CODE>flex</CODE> is fully compliant with the POSIX <CODE>lex</CODE> specification, except that when using <CODE>%pointer</CODE> (the default), a call to <CODE>unput()</CODE> destroys the contents of <CODE>yytext</CODE>, which is counter to the POSIX specification. In this section we discuss all of the known areas of incompatibility between <CODE>flex</CODE>, AT&T <CODE>lex</CODE>, and the POSIX specification. <CODE>flex</CODE>'s <SAMP>`-l'</SAMP> option turns on maximum compatibility with the original AT&T <CODE>lex</CODE> implementation, at the cost of a major loss in the generated scanner's performance. We note below which incompatibilities can be overcome using the <SAMP>`-l'</SAMP> option. <CODE>flex</CODE> is fully compatible with <CODE>lex</CODE> with the following exceptions: <UL> <LI> The undocumented <CODE>lex</CODE> scanner internal variable <CODE>yylineno</CODE> is not supported unless <SAMP>`-l'</SAMP> or <CODE>%option yylineno</CODE> is used. <LI> <CODE>yylineno</CODE> should be maintained on a per-buffer basis, rather than a per-scanner (single global variable) basis. <LI> <CODE>yylineno</CODE> is not part of the POSIX specification. <LI> The <CODE>input()</CODE> routine is not redefinable, though it may be called to read characters following whatever has been matched by a rule. If <CODE>input()</CODE> encounters an end-of-file the normal <CODE>yywrap()</CODE> processing is done. A "real" end-of-file is returned by <CODE>input()</CODE> as <CODE>EOF</CODE>. <LI> Input is instead controlled by defining the <CODE>YY_INPUT()</CODE> macro. <LI> The <CODE>flex</CODE> restriction that <CODE>input()</CODE> cannot be redefined is in accordance with the POSIX specification, which simply does not specify any way of controlling the scanner's input other than by making an initial assignment to <TT>`yyin'</TT>. <LI> The <CODE>unput()</CODE> routine is not redefinable. This restriction is in accordance with POSIX. <LI> <CODE>flex</CODE> scanners are not as reentrant as <CODE>lex</CODE> scanners. In particular, if you have an interactive scanner and an interrupt handler which long-jumps out of the scanner, and the scanner is subsequently called again, you may get the following message: <A NAME="IDX425"></A> <PRE> @verbatim fatal <CODE>flex</CODE> scanner internal error--end of buffer missed </PRE> To reenter the scanner, first use: <A NAME="IDX426"></A> <PRE> @verbatim yyrestart( yyin ); </PRE> Note that this call will throw away any buffered input; usually this isn't a problem with an interactive scanner. See section <A HREF="flex.html#SEC29">Reentrant C Scanners</A>, for <CODE>flex</CODE>'s reentrant API. <LI> Also note that <CODE>flex</CODE> C++ scanner classes <EM>are</EM> reentrant, so if using C++ is an option for you, you should use them instead. See section <A HREF="flex.html#SEC28">Generating C++ Scanners</A>, and section <A HREF="flex.html#SEC29">Reentrant C Scanners</A> for details. <LI> <CODE>output()</CODE> is not supported. Output from the <B>ECHO</B> macro is done to the file-pointer <CODE>yyout</CODE> (default <TT>`stdout)'</TT>. <LI> <CODE>output()</CODE> is not part of the POSIX specification. <LI> <CODE>lex</CODE> does not support exclusive start conditions (%x), though they are in the POSIX specification. <LI> When definitions are expanded, <CODE>flex</CODE> encloses them in parentheses. With <CODE>lex</CODE>, the following: <A NAME="IDX427"></A> <PRE> @verbatim NAME [A-Z][A-Z0-9]* %% foo{NAME}? printf( "Found it\n" ); %% </PRE> will not match the string <SAMP>`foo'</SAMP> because when the macro is expanded the rule is equivalent to <SAMP>`foo[A-Z][A-Z0-9]*?'</SAMP> and the precedence is such that the <SAMP>`?'</SAMP> is associated with <SAMP>`[A-Z0-9]*'</SAMP>. With <CODE>flex</CODE>, the rule will be expanded to <SAMP>`foo([A-Z][A-Z0-9]*)?'</SAMP> and so the string <SAMP>`foo'</SAMP> will match. <LI> Note that if the definition begins with <SAMP>`^'</SAMP> or ends with <SAMP>`$'</SAMP> then it is <EM>not</EM> expanded with parentheses, to allow these operators to appear in definitions without losing their special meanings. But the <SAMP>`<s>'</SAMP>, <SAMP>`/'</SAMP>, and <CODE><<EOF>></CODE> operators cannot be used in a <CODE>flex</CODE> definition. <LI> Using <SAMP>`-l'</SAMP> results in the <CODE>lex</CODE> behavior of no parentheses around the definition. <LI> The POSIX specification is that the definition be enclosed in parentheses. <LI> Some implementations of <CODE>lex</CODE> allow a rule's action to begin on a separate line, if the rule's pattern has trailing whitespace: <A NAME="IDX428"></A> <PRE> @verbatim %% foo|bar<space here> { foobar_action();} </PRE> <CODE>flex</CODE> does not support this feature. <LI> The <CODE>lex</CODE> <CODE>%r</CODE> (generate a Ratfor scanner) option is not supported. It is not part of the POSIX specification. <LI> After a call to <CODE>unput()</CODE>, <EM>yytext</EM> is undefined until the next token is matched, unless the scanner was built using <CODE>%array</CODE>. This is not the case with <CODE>lex</CODE> or the POSIX specification. The <SAMP>`-l'</SAMP> option does away with this incompatibility. <LI> The precedence of the <SAMP>`{,}'</SAMP> (numeric range) operator is different. The AT&T and POSIX specifications of <CODE>lex</CODE> interpret <SAMP>`abc{1,3}'</SAMP> as match one, two, or three occurrences of <SAMP>`abc'</SAMP>", whereas <CODE>flex</CODE> interprets it as "match <SAMP>`ab'</SAMP> followed by one, two, or three occurrences of <SAMP>`c'</SAMP>". The <SAMP>`-l'</SAMP> and <SAMP>`--posix'</SAMP> options do away with this incompatibility. <LI> The precedence of the <SAMP>`^'</SAMP> operator is different. <CODE>lex</CODE> interprets <SAMP>`^foo|bar'</SAMP> as "match either 'foo' at the beginning of a line, or 'bar' anywhere", whereas <CODE>flex</CODE> interprets it as "match either <SAMP>`foo'</SAMP> or <SAMP>`bar'</SAMP> if they come at the beginning of a line". The latter is in agreement with the POSIX specification. <LI> The special table-size declarations such as <CODE>%a</CODE> supported by <CODE>lex</CODE> are not required by <CODE>flex</CODE> scanners.. <CODE>flex</CODE> ignores them. <LI> The name <CODE>FLEX_SCANNER</CODE> is <CODE>#define</CODE>'d so scanners may be written for use with either <CODE>flex</CODE> or <CODE>lex</CODE>. Scanners also include <CODE>YY_FLEX_MAJOR_VERSION</CODE>, <CODE>YY_FLEX_MINOR_VERSION</CODE> and <CODE>YY_FLEX_SUBMINOR_VERSION</CODE> indicating which version of <CODE>flex</CODE> generated the scanner. For example, for the 2.5.22 release, these defines would be 2, 5 and 22 respectively. If the version of <CODE>flex</CODE> being used is a beta version, then the symbol <CODE>FLEX_BETA</CODE> is defined. </UL> <P> <A NAME="IDX429"></A> <A NAME="IDX430"></A> The following <CODE>flex</CODE> features are not included in <CODE>lex</CODE> or the POSIX specification: <UL> <LI> C++ scanners <LI> %option <LI> start condition scopes <LI> start condition stacks <LI> interactive/non-interactive scanners <LI> yy_scan_string() and friends <LI> yyterminate() <LI> yy_set_interactive() <LI> yy_set_bol() <LI> YY_AT_BOL() <<EOF>> <LI> <*> <LI> YY_DECL <LI> YY_START <LI> YY_USER_ACTION <LI> YY_USER_INIT <LI> #line directives <LI> %{}'s around actions <LI> reentrant C API <LI> multiple actions on a line <LI> almost all of the <CODE>flex</CODE> command-line options </UL> <P> The feature "multiple actions on a line" refers to the fact that with <CODE>flex</CODE> you can put multiple actions on the same line, separated with semi-colons, while with <CODE>lex</CODE>, the following: <PRE> @verbatim foo handle_foo(); ++num_foos_seen; </PRE> <P> is (rather surprisingly) truncated to <PRE> @verbatim foo handle_foo(); </PRE> <P> <CODE>flex</CODE> does not truncate the action. Actions that are not enclosed in braces are simply terminated at the end of the line. <H1><A NAME="SEC43" HREF="flex_toc.html#TOC43">Memory Management</A></H1> <P> <A NAME="IDX431"></A> @anchor{memory-management} This chapter describes how flex handles dynamic memory, and how you can override the default behavior. <H2><A NAME="SEC44" HREF="flex_toc.html#TOC44">The Default Memory Management</A></H2> <P> Flex allocates dynamic memory during initialization, and once in a while from within a call to yylex(). Initialization takes place during the first call to yylex(). Thereafter, flex may reallocate more memory if it needs to enlarge a buffer. As of version 2.5.9 Flex will clean up all memory when you call <CODE>yylex_destroy</CODE> @xref{faq-memory-leak}. <P> Flex allocates dynamic memory for four purposes, listed below <A NAME="DOCF1" HREF="flex_foot.html#FOOT1">(1)</A> <DL COMPACT> <DT>16kB for the input buffer. <DD> Flex allocates memory for the character buffer used to perform pattern matching. Flex must read ahead from the input stream and store it in a large character buffer. This buffer is typically the largest chunk of dynamic memory flex consumes. This buffer will grow if necessary, doubling the size each time. Flex frees this memory when you call yylex_destroy(). The default size of this buffer (16384 bytes) is almost always too large. The ideal size for this buffer is the length of the longest token expected. Flex will allocate a few extra bytes for housekeeping. <DT>16kb for the REJECT state. This will only be allocated if you use REJECT. <DD> The size is the same as the input buffer, so if you override the size of the input buffer, then you automatically override the size of this buffer as well. <DT>100 bytes for the start condition stack. <DD> Flex allocates memory for the start condition stack. This is the stack used for pushing start states, i.e., with yy_push_state(). It will grow if necessary. Since the states are simply integers, this stack doesn't consume much memory. This stack is not present if <CODE>%option stack</CODE> is not specified. You will rarely need to tune this buffer. The ideal size for this stack is the maximum depth expected. The memory for this stack is automatically destroyed when you call yylex_destroy(). @xref{option-stack}. <DT>40 bytes for each YY_BUFFER_STATE. <DD> Flex allocates memory for each YY_BUFFER_STATE. The buffer state itself is about 40 bytes, plus an additional large character buffer (described above.) The initial buffer state is created during initialization, and with each call to yy_create_buffer(). You can't tune the size of this, but you can tune the character buffer as described above. Any buffer state that you explicitly create by calling yy_create_buffer() is <EM>NOT</EM> destroyed automatically. You must call yy_delete_buffer() to free the memory. The exception to this rule is that flex will delete the current buffer automatically when you call yylex_destroy(). If you delete the current buffer, be sure to set it to NULL. That way, flex will not try to delete the buffer a second time (possibly crashing your program!) At the time of this writing, flex does not provide a growable stack for the buffer states. You have to manage that yourself. See section <A HREF="flex.html#SEC15">Multiple Input Buffers</A>. <DT>84 bytes for the reentrant scanner guts <DD> Flex allocates about 84 bytes for the reentrant scanner structure when you call yylex_init(). It is destroyed when the user calls yylex_destroy(). </DL> <H2><A NAME="SEC45" HREF="flex_toc.html#TOC45">Overriding The Default Memory Management</A></H2> <P> <A NAME="IDX432"></A> <A NAME="IDX433"></A> <A NAME="IDX434"></A> <P> Flex calls the functions <CODE>yyalloc</CODE>, <CODE>yyrealloc</CODE>, and <CODE>yyfree</CODE> when it needs to allocate or free memory. By default, these functions are wrappers around the standard C functions, <CODE>malloc</CODE>, <CODE>realloc</CODE>, and <CODE>free</CODE>, respectively. You can override the default implementations by telling flex that you will provide your own implementations. <P> To override the default implementations, you must do two things: <OL> <LI>Suppress the default implementations by specifying one or more of the following options: <UL> <LI><CODE>%option noyyalloc</CODE> <A NAME="IDX435"></A> <LI><CODE>%option noyyrealloc</CODE> <LI><CODE>%option noyyfree</CODE>. </UL> <LI>Provide your own implementation of the following functions: <A NAME="DOCF2" HREF="flex_foot.html#FOOT2">(2)</A> <PRE> @verbatim // For a non-reentrant scanner void * yyalloc (size_t bytes); void * yyrealloc (void * ptr, size_t bytes); void yyfree (void * ptr); // For a reentrant scanner void * yyalloc (size_t bytes, void * yyscanner); void * yyrealloc (void * ptr, size_t bytes, void * yyscanner); void yyfree (void * ptr, void * yyscanner); </PRE> </OL> <P> In the following example, we will override all three memory routines. We assume that there is a custom allocator with garbage collection. In order to make this example interesting, we will use a reentrant scanner, passing a pointer to the custom allocator through <CODE>yyextra</CODE>. <P> <A NAME="IDX436"></A> <PRE> @verbatim %{ #include "some_allocator.h" %} /* Suppress the default implementations. */ %option noyyalloc noyyrealloc noyyfree %option reentrant /* Initialize the allocator. */ #define YY_EXTRA_TYPE struct allocator* #define YY_USER_INIT yyextra = allocator_create(); %% .|\n ; %% /* Provide our own implementations. */ void * yyalloc (size_t bytes, void* yyscanner) { return allocator_alloc (yyextra, bytes); } void * yyrealloc (void * ptr, size_t bytes, void* yyscanner) { return allocator_realloc (yyextra, bytes); } void yyfree (void * ptr, void * yyscanner) { /* Do nothing -- we leave it to the garbage collector. */ } </PRE> <H2><A NAME="SEC46" HREF="flex_toc.html#TOC46">A Note About yytext And Memory</A></H2> <P> <A NAME="IDX437"></A> <P> When flex finds a match, <CODE>yytext</CODE> points to the first character of the match in the input buffer. The string itself is part of the input buffer, and is <EM>NOT</EM> allocated separately. The value of yytext will be overwritten the next time yylex() is called. In short, the value of yytext is only valid from within the matched rule's action. <P> Often, you want the value of yytext to persist for later processing, i.e., by a parser with non-zero lookahead. In order to preserve yytext, you will have to copy it with strdup() or a similar function. But this introduces some headache because your parser is now responsible for freeing the copy of yytext. If you use a yacc or bison parser, (commonly used with flex), you will discover that the error recovery mechanisms can cause memory to be leaked. <P> To prevent memory leaks from strdup'd yytext, you will have to track the memory somehow. Our experience has shown that a garbage collection mechanism or a pooled memory mechanism will save you a lot of grief when writing parsers. <H1><A NAME="SEC47" HREF="flex_toc.html#TOC47">Serialized Tables</A></H1> <P> <A NAME="IDX438"></A> <A NAME="IDX439"></A> <P> @anchor{serialization} A <CODE>flex</CODE> scanner has the ability to save the DFA tables to a file, and load them at runtime when needed. The motivation for this feature is to reduce the runtime memory footprint. Traditionally, these tables have been compiled into the scanner as C arrays, and are sometimes quite large. Since the tables are compiled into the scanner, the memory used by the tables can never be freed. This is a waste of memory, especially if an application uses several scanners, but none of them at the same time. <P> The serialization feature allows the tables to be loaded at runtime, before scanning begins. The tables may be discarded when scanning is finished. <H2><A NAME="SEC48" HREF="flex_toc.html#TOC48">Creating Serialized Tables</A></H2> <P> <A NAME="IDX440"></A> <A NAME="IDX441"></A> <P> You may create a scanner with serialized tables by specifying: <PRE> @verbatim %option tables-file=FILE or --tables-file=FILE </PRE> <P> These options instruct flex to save the DFA tables to the file <VAR>FILE</VAR>. The tables will <EM>not</EM> be embedded in the generated scanner. The scanner will not function on its own. The scanner will be dependent upon the serialized tables. You must load the tables from this file at runtime before you can scan anything. <P> If you do not specify a filename to <CODE>--tables-file</CODE>, the tables will be saved to <TT>`lex.yy.tables'</TT>, where <SAMP>`yy'</SAMP> is the appropriate prefix. <P> If your project uses several different scanners, you can concatenate the serialized tables into one file, and flex will find the correct set of tables, using the scanner prefix as part of the lookup key. An example follows: <P> <A NAME="IDX442"></A> <PRE> @verbatim $ flex --tables-file --prefix=cpp cpp.l $ flex --tables-file --prefix=c c.l $ cat lex.cpp.tables lex.c.tables > all.tables </PRE> <P> The above example created two scanners, <SAMP>`cpp'</SAMP>, and <SAMP>`c'</SAMP>. Since we did not specify a filename, the tables were serialized to <TT>`lex.c.tables'</TT> and <TT>`lex.cpp.tables'</TT>, respectively. Then, we concatenated the two files together into <TT>`all.tables'</TT>, which we will distribute with our project. At runtime, we will open the file and tell flex to load the tables from it. Flex will find the correct tables automatically. (See next section). <H2><A NAME="SEC49" HREF="flex_toc.html#TOC49">Loading and Unloading Serialized Tables</A></H2> <P> <A NAME="IDX443"></A> <A NAME="IDX444"></A> <A NAME="IDX445"></A> <A NAME="IDX446"></A> <A NAME="IDX447"></A> <P> If you've built your scanner with <CODE>%option tables-file</CODE>, then you must load the scanner tables at runtime. This can be accomplished with the following function: <P> <DL> <DT><U>Function:</U> int <B>yytables_fload</B> <I>(FILE* <VAR>fp</VAR> [, yyscan_t <VAR>scanner</VAR>])</I> <DD><A NAME="IDX448"></A> Locates scanner tables in the stream pointed to by <VAR>fp</VAR> and loads them. Memory for the tables is allocated via <CODE>yyalloc</CODE>. You must call this function before the first call to <CODE>yylex</CODE>. The argument <VAR>scanner</VAR> only appears in the reentrant scanner. This function returns <SAMP>`0'</SAMP> (zero) on success, or non-zero on error. </DL> <P> The loaded tables are <STRONG>not</STRONG> automatically destroyed (unloaded) when you call <CODE>yylex_destroy</CODE>. The reason is that you may create several scanners of the same type (in a reentrant scanner), each of which needs access to these tables. To avoid a nasty memory leak, you must call the following function: <P> <DL> <DT><U>Function:</U> int <B>yytables_destroy</B> <I>([yyscan_t <VAR>scanner</VAR>])</I> <DD><A NAME="IDX449"></A> Unloads the scanner tables. The tables must be loaded again before you can scan any more data. The argument <VAR>scanner</VAR> only appears in the reentrant scanner. This function returns <SAMP>`0'</SAMP> (zero) on success, or non-zero on error. </DL> <P> <STRONG>The functions <CODE>yytables_fload</CODE> and <CODE>yytables_destroy</CODE> are not thread-safe.</STRONG> You must ensure that these functions are called exactly once (for each scanner type) in a threaded program, before any thread calls <CODE>yylex</CODE>. After the tables are loaded, they are never written to, and no thread protection is required thereafter -- until you destroy them. <H2><A NAME="SEC50" HREF="flex_toc.html#TOC50">Tables File Format</A></H2> <P> <A NAME="IDX450"></A> <A NAME="IDX451"></A> <P> This section defines the file format of serialized <CODE>flex</CODE> tables. <P> The tables format allows for one or more sets of tables to be specified, where each set corresponds to a given scanner. Scanners are indexed by name, as described below. The file format is as follows: <PRE> @verbatim TABLE SET 1 +-------------------------------+ Header | uint32 th_magic; | | uint32 th_hsize; | | uint32 th_ssize; | | uint16 th_flags; | | char th_version[]; | | char th_name[]; | | uint8 th_pad64[]; | +-------------------------------+ Table 1 | uint16 td_id; | | uint16 td_flags; | | uint32 td_lolen; | | uint32 td_hilen; | | void td_data[]; | | uint8 td_pad64[]; | +-------------------------------+ Table 2 | | . . . . . . . . . . . . Table n | | +-------------------------------+ TABLE SET 2 . . . TABLE SET N </PRE> <P> The above diagram shows that a complete set of tables consists of a header followed by multiple individual tables. Furthermore, multiple complete sets may be present in the same file, each set with its own header and tables. The sets are contiguous in the file. The only way to know if another set follows is to check the next four bytes for the magic number (or check for EOF). The header and tables sections are padded to 64-bit boundaries. Below we describe each field in detail. This format does not specify how the scanner will expand the given data, i.e., data may be serialized as int8, but expanded to an int32 array at runtime. This is to reduce the size of the serialized data where possible. Remember, <EM>all integer values are in network byte order</EM>. <P> Fields of a table header: <DL COMPACT> <DT><CODE>th_magic</CODE> <DD> Magic number, always 0xF13C57B1. <DT><CODE>th_hsize</CODE> <DD> Size of this entire header, in bytes, including all fields plus any padding. <DT><CODE>th_ssize</CODE> <DD> Size of this entire set, in bytes, including the header, all tables, plus any padding. <DT><CODE>th_flags</CODE> <DD> Bit flags for this table set. Currently unused. <DT><CODE>th_version[]</CODE> <DD> Flex version in NULL-termninated string format. e.g., <SAMP>`2.5.13a'</SAMP>. This is the version of flex that was used to create the serialized tables. <DT><CODE>th_name[]</CODE> <DD> Contains the name of this table set. The default is <SAMP>`yytables'</SAMP>, and is prefixed accordingly, e.g., <SAMP>`footables'</SAMP>. Must be NULL-terminated. <DT><CODE>th_pad64[]</CODE> <DD> Zero or more NULL bytes, padding the entire header to the next 64-bit boundary as calculated from the beginning of the header. </DL> <P> Fields of a table: <DL COMPACT> <DT><CODE>td_id</CODE> <DD> Specifies the table identifier. Possible values are: <DL COMPACT> <DT><CODE>YYTD_ID_ACCEPT (0x01)</CODE> <DD> <CODE>yy_accept</CODE> <DT><CODE>YYTD_ID_BASE (0x02)</CODE> <DD> <CODE>yy_base</CODE> <DT><CODE>YYTD_ID_CHK (0x03)</CODE> <DD> <CODE>yy_chk</CODE> <DT><CODE>YYTD_ID_DEF (0x04)</CODE> <DD> <CODE>yy_def</CODE> <DT><CODE>YYTD_ID_EC (0x05)</CODE> <DD> <CODE>yy_ec </CODE> <DT><CODE>YYTD_ID_META (0x06)</CODE> <DD> <CODE>yy_meta</CODE> <DT><CODE>YYTD_ID_NUL_TRANS (0x07)</CODE> <DD> <CODE>yy_NUL_trans</CODE> <DT><CODE>YYTD_ID_NXT (0x08)</CODE> <DD> <CODE>yy_nxt</CODE>. This array may be two dimensional. See the <CODE>td_hilen</CODE> field below. <DT><CODE>YYTD_ID_RULE_CAN_MATCH_EOL (0x09)</CODE> <DD> <CODE>yy_rule_can_match_eol</CODE> <DT><CODE>YYTD_ID_START_STATE_LIST (0x0A)</CODE> <DD> <CODE>yy_start_state_list</CODE>. This array is handled specially because it is an array of pointers to structs. See the <CODE>td_flags</CODE> field below. <DT><CODE>YYTD_ID_TRANSITION (0x0B)</CODE> <DD> <CODE>yy_transition</CODE>. This array is handled specially because it is an array of structs. See the <CODE>td_lolen</CODE> field below. <DT><CODE>YYTD_ID_ACCLIST (0x0C)</CODE> <DD> <CODE>yy_acclist</CODE> </DL> <DT><CODE>td_flags</CODE> <DD> Bit flags describing how to interpret the data in <CODE>td_data</CODE>. The data arrays are one-dimensional by default, but may be two dimensional as specified in the <CODE>td_hilen</CODE> field. <DL COMPACT> <DT><CODE>YYTD_DATA8 (0x01)</CODE> <DD> The data is serialized as an array of type int8. <DT><CODE>YYTD_DATA16 (0x02)</CODE> <DD> The data is serialized as an array of type int16. <DT><CODE>YYTD_DATA32 (0x04)</CODE> <DD> The data is serialized as an array of type int32. <DT><CODE>YYTD_PTRANS (0x08)</CODE> <DD> The data is a list of indexes of entries in the expanded <CODE>yy_transition</CODE> array. Each index should be expanded to a pointer to the corresponding entry in the <CODE>yy_transition</CODE> array. We count on the fact that the <CODE>yy_transition</CODE> array has already been seen. <DT><CODE>YYTD_STRUCT (0x10)</CODE> <DD> The data is a list of yy_trans_info structs, each of which consists of two integers. There is no padding between struct elements or between structs. The type of each member is determined by the <CODE>YYTD_DATA*</CODE> bits. </DL> <DT><CODE>td_lolen</CODE> <DD> Specifies the number of elements in the lowest dimension array. If this is a one-dimensional array, then it is simply the number of elements in this array. The element size is determined by the <CODE>td_flags</CODE> field. <DT><CODE>td_hilen</CODE> <DD> If <CODE>td_hilen</CODE> is non-zero, then the data is a two-dimensional array. Otherwise, the data is a one-dimensional array. <CODE>td_hilen</CODE> contains the number of elements in the higher dimensional array, and <CODE>td_lolen</CODE> contains the number of elements in the lowest dimension. Conceptually, <CODE>td_data</CODE> is either <CODE>sometype td_data[td_lolen]</CODE>, or <CODE>sometype td_data[td_hilen][td_lolen]</CODE>, where <CODE>sometype</CODE> is specified by the <CODE>td_flags</CODE> field. It is possible for both <CODE>td_lolen</CODE> and <CODE>td_hilen</CODE> to be zero, in which case <CODE>td_data</CODE> is a zero length array, and no data is loaded, i.e., this table is simply skipped. Flex does not currently generate tables of zero length. <DT><CODE>td_data[]</CODE> <DD> The table data. This array may be a one- or two-dimensional array, of type <CODE>int8</CODE>, <CODE>int16</CODE>, <CODE>int32</CODE>, <CODE>struct yy_trans_info</CODE>, or <CODE>struct yy_trans_info*</CODE>, depending upon the values in the <CODE>td_flags</CODE>, <CODE>td_lolen</CODE>, and <CODE>td_hilen</CODE> fields. <DT><CODE>td_pad64[]</CODE> <DD> Zero or more NULL bytes, padding the entire table to the next 64-bit boundary as calculated from the beginning of this table. </DL> <H1><A NAME="SEC51" HREF="flex_toc.html#TOC51">Diagnostics</A></H1> <P> <A NAME="IDX452"></A> <A NAME="IDX453"></A> <P> The following is a list of <CODE>flex</CODE> diagnostic messages: <UL> <LI> <SAMP>`warning, rule cannot be matched'</SAMP> indicates that the given rule cannot be matched because it follows other rules that will always match the same text as it. For example, in the following <SAMP>`foo'</SAMP> cannot be matched because it comes after an identifier "catch-all" rule: <A NAME="IDX454"></A> <PRE> @verbatim [a-z]+ got_identifier(); foo got_foo(); </PRE> Using <CODE>REJECT</CODE> in a scanner suppresses this warning. <LI> <SAMP>`warning, -s option given but default rule can be matched'</SAMP> means that it is possible (perhaps only in a particular start condition) that the default rule (match any single character) is the only one that will match a particular input. Since <SAMP>`-s'</SAMP> was given, presumably this is not intended. <LI> <CODE>reject_used_but_not_detected undefined</CODE> or <CODE>yymore_used_but_not_detected undefined</CODE>. These errors can occur at compile time. They indicate that the scanner uses <CODE>REJECT</CODE> or <CODE>yymore()</CODE> but that <CODE>flex</CODE> failed to notice the fact, meaning that <CODE>flex</CODE> scanned the first two sections looking for occurrences of these actions and failed to find any, but somehow you snuck some in (via a #include file, for example). Use <CODE>%option reject</CODE> or <CODE>%option yymore</CODE> to indicate to <CODE>flex</CODE> that you really do use these features. <LI> <SAMP>`flex scanner jammed'</SAMP>. a scanner compiled with <SAMP>`-s'</SAMP> has encountered an input string which wasn't matched by any of its rules. This error can also occur due to internal problems. <LI> <SAMP>`token too large, exceeds YYLMAX'</SAMP>. your scanner uses <CODE>%array</CODE> and one of its rules matched a string longer than the <CODE>YYLMAX</CODE> constant (8K bytes by default). You can increase the value by #define'ing <CODE>YYLMAX</CODE> in the definitions section of your <CODE>flex</CODE> input. <LI> <SAMP>`scanner requires -8 flag to use the character 'x''</SAMP>. Your scanner specification includes recognizing the 8-bit character <SAMP>`'x''</SAMP> and you did not specify the -8 flag, and your scanner defaulted to 7-bit because you used the <SAMP>`-Cf'</SAMP> or <SAMP>`-CF'</SAMP> table compression options. See the discussion of the <SAMP>`-7'</SAMP> flag, section <A HREF="flex.html#SEC20">Scanner Options</A>, for details. <LI> <SAMP>`flex scanner push-back overflow'</SAMP>. you used <CODE>unput()</CODE> to push back so much text that the scanner's buffer could not hold both the pushed-back text and the current token in <CODE>yytext</CODE>. Ideally the scanner should dynamically resize the buffer in this case, but at present it does not. <LI> <SAMP>`input buffer overflow, can't enlarge buffer because scanner uses REJECT'</SAMP>. the scanner was working on matching an extremely large token and needed to expand the input buffer. This doesn't work with scanners that use <CODE>REJECT</CODE>. <LI> <SAMP>`fatal flex scanner internal error--end of buffer missed'</SAMP>. This can occur in a scanner which is reentered after a long-jump has jumped out (or over) the scanner's activation frame. Before reentering the scanner, use: <PRE> @verbatim yyrestart( yyin ); </PRE> or, as noted above, switch to using the C++ scanner class. <LI> <SAMP>`too many start conditions in <> construct!'</SAMP> you listed more start conditions in a <> construct than exist (so you must have listed at least one of them twice). </UL> <H1><A NAME="SEC52" HREF="flex_toc.html#TOC52">Limitations</A></H1> <P> <A NAME="IDX455"></A> <P> Some trailing context patterns cannot be properly matched and generate warning messages (<SAMP>`dangerous trailing context'</SAMP>). These are patterns where the ending of the first part of the rule matches the beginning of the second part, such as <SAMP>`zx*/xy*'</SAMP>, where the 'x*' matches the 'x' at the beginning of the trailing context. (Note that the POSIX draft states that the text matched by such patterns is undefined.) For some trailing context rules, parts which are actually fixed-length are not recognized as such, leading to the abovementioned performance loss. In particular, parts using <SAMP>`|'</SAMP> or <SAMP>`{n}'</SAMP> (such as <SAMP>`foo{3}'</SAMP>) are always considered variable-length. Combining trailing context with the special <SAMP>`|'</SAMP> action can result in <EM>fixed</EM> trailing context being turned into the more expensive <EM>variable</EM> trailing context. For example, in the following: <P> <A NAME="IDX456"></A> <PRE> @verbatim %% abc | xyz/def </PRE> <P> Use of <CODE>unput()</CODE> invalidates yytext and yyleng, unless the <CODE>%array</CODE> directive or the <SAMP>`-l'</SAMP> option has been used. Pattern-matching of <CODE>NUL</CODE>s is substantially slower than matching other characters. Dynamic resizing of the input buffer is slow, as it entails rescanning all the text matched so far by the current (generally huge) token. Due to both buffering of input and read-ahead, you cannot intermix calls to <TT>`<stdio.h>'</TT> routines, such as, <B>getchar()</B>, with <CODE>flex</CODE> rules and expect it to work. Call <CODE>input()</CODE> instead. The total table entries listed by the <SAMP>`-v'</SAMP> flag excludes the number of table entries needed to determine what rule has been matched. The number of entries is equal to the number of DFA states if the scanner does not use <CODE>REJECT</CODE>, and somewhat greater than the number of states if it does. <CODE>REJECT</CODE> cannot be used with the <SAMP>`-f'</SAMP> or <SAMP>`-F'</SAMP> options. <P> The <CODE>flex</CODE> internal algorithms need documentation. <H1><A NAME="SEC53" HREF="flex_toc.html#TOC53">Additional Reading</A></H1> <P> You may wish to read more about the following programs: <UL> <LI>lex <LI>yacc <LI>sed <LI>awk </UL> <P> The following books may contain material of interest: <P> John Levine, Tony Mason, and Doug Brown, <EM>Lex & Yacc</EM>, O'Reilly and Associates. Be sure to get the 2nd edition. <P> M. E. Lesk and E. Schmidt, <EM>LEX -- Lexical Analyzer Generator</EM> <P> Alfred Aho, Ravi Sethi and Jeffrey Ullman, <EM>Compilers: Principles, Techniques and Tools</EM>, Addison-Wesley (1986). Describes the pattern-matching techniques used by <CODE>flex</CODE> (deterministic finite automata). <H1><A NAME="SEC54" HREF="flex_toc.html#TOC54">FAQ</A></H1> <P> From time to time, the <CODE>flex</CODE> maintainer receives certain questions. Rather than repeat answers to well-understood problems, we publish them here. <H2><A NAME="SEC55" HREF="flex_toc.html#TOC55">When was flex born?</A></H2> <P> Vern Paxson took over the <CITE>Software Tools</CITE> lex project from Jef Poskanzer in 1982. At that point it was written in Ratfor. Around 1987 or so, Paxson translated it into C, and a legend was born :-). <H2><A NAME="SEC56" HREF="flex_toc.html#TOC56">How do I expand \ escape sequences in C-style quoted strings?</A></H2> <P> A key point when scanning quoted strings is that you cannot (easily) write a single rule that will precisely match the string if you allow things like embedded escape sequences and newlines. If you try to match strings with a single rule then you'll wind up having to rescan the string anyway to find any escape sequences. <P> Instead you can use exclusive start conditions and a set of rules, one for matching non-escaped text, one for matching a single escape, one for matching an embedded newline, and one for recognizing the end of the string. Each of these rules is then faced with the question of where to put its intermediary results. The best solution is for the rules to append their local value of <CODE>yytext</CODE> to the end of a "string literal" buffer. A rule like the escape-matcher will append to the buffer the meaning of the escape sequence rather than the literal text in <CODE>yytext</CODE>. In this way, <CODE>yytext</CODE> does not need to be modified at all. <H2><A NAME="SEC57" HREF="flex_toc.html#TOC57">Why do flex scanners call fileno if it is not ANSI compatible?</A></H2> <P> Flex scanners call <CODE>fileno()</CODE> in order to get the file descriptor corresponding to <CODE>yyin</CODE>. The file descriptor may be passed to <CODE>isatty()</CODE> or <CODE>read()</CODE>, depending upon which <CODE>%options</CODE> you specified. If your system does not have <CODE>fileno()</CODE> support, to get rid of the <CODE>read()</CODE> call, do not specify <CODE>%option read</CODE>. To get rid of the <CODE>isatty()</CODE> call, you must specify one of <CODE>%option always-interactive</CODE> or <CODE>%option never-interactive</CODE>. <H2><A NAME="SEC58" HREF="flex_toc.html#TOC58">Does flex support recursive pattern definitions?</A></H2> <P> e.g., <PRE> @verbatim %% block "{"({block}|{statement})*"}" </PRE> <P> No. You cannot have recursive definitions. The pattern-matching power of regular expressions in general (and therefore flex scanners, too) is limited. In particular, regular expressions cannot "balance" parentheses to an arbitrary degree. For example, it's impossible to write a regular expression that matches all strings containing the same number of '{'s as '}'s. For more powerful pattern matching, you need a parser, such as <CITE>GNU bison</CITE>. <H2><A NAME="SEC59" HREF="flex_toc.html#TOC59">How do I skip huge chunks of input (tens of megabytes) while using flex?</A></H2> <P> Use <CODE>fseek()</CODE> (or <CODE>lseek()</CODE>) to position yyin, then call <CODE>yyrestart()</CODE>. <H2><A NAME="SEC60" HREF="flex_toc.html#TOC60">Flex is not matching my patterns in the same order that I defined them.</A></H2> <P> <CODE>flex</CODE> picks the rule that matches the most text (i.e., the longest possible input string). This is because <CODE>flex</CODE> uses an entirely different matching technique ("deterministic finite automata") that actually does all of the matching simultaneously, in parallel. (Seems impossible, but it's actually a fairly simple technique once you understand the principles.) <P> A side-effect of this parallel matching is that when the input matches more than one rule, <CODE>flex</CODE> scanners pick the rule that matched the <EM>most</EM> text. This is explained further in the manual, in the section See section <A HREF="flex.html#SEC11">How the Input Is Matched</A>. <P> If you want <CODE>flex</CODE> to choose a shorter match, then you can work around this behavior by expanding your short rule to match more text, then put back the extra: <PRE> @verbatim data_.* yyless( 5 ); BEGIN BLOCKIDSTATE; </PRE> <P> Another fix would be to make the second rule active only during the <CODE><BLOCKIDSTATE></CODE> start condition, and make that start condition exclusive by declaring it with <CODE>%x</CODE> instead of <CODE>%s</CODE>. <P> A final fix is to change the input language so that the ambiguity for <SAMP>`data_'</SAMP> is removed, by adding characters to it that don't match the identifier rule, or by removing characters (such as <SAMP>`_'</SAMP>) from the identifier rule so it no longer matches <SAMP>`data_'</SAMP>. (Of course, you might also not have the option of changing the input language.) <H2><A NAME="SEC61" HREF="flex_toc.html#TOC61">My actions are executing out of order or sometimes not at all.</A></H2> <P> Most likely, you have (in error) placed the opening <SAMP>`{'</SAMP> of the action block on a different line than the rule, e.g., <PRE> @verbatim ^(foo|bar) { <<<--- WRONG! } </PRE> <P> <CODE>flex</CODE> requires that the opening <SAMP>`{'</SAMP> of an action associated with a rule begin on the same line as does the rule. You need instead to write your rules as follows: <PRE> @verbatim ^(foo|bar) { // CORRECT! } </PRE> <H2><A NAME="SEC62" HREF="flex_toc.html#TOC62">How can I have multiple input sources feed into the same scanner at the same time?</A></H2> <P> If ... <UL> <LI> your scanner is free of backtracking (verified using <CODE>flex</CODE>'s <SAMP>`-b'</SAMP> flag), <LI> AND you run your scanner interactively (<SAMP>`-I'</SAMP> option; default unless using special table compression options), <LI> AND you feed it one character at a time by redefining <CODE>YY_INPUT</CODE> to do so, </UL> <P> then every time it matches a token, it will have exhausted its input buffer (because the scanner is free of backtracking). This means you can safely use <CODE>select()</CODE> at the point and only call <CODE>yylex()</CODE> for another token if <CODE>select()</CODE> indicates there's data available. <P> That is, move the <CODE>select()</CODE> out from the input function to a point where it determines whether <CODE>yylex()</CODE> gets called for the next token. <P> With this approach, you will still have problems if your input can arrive piecemeal; <CODE>select()</CODE> could inform you that the beginning of a token is available, you call <CODE>yylex()</CODE> to get it, but it winds up blocking waiting for the later characters in the token. <P> Here's another way: Move your input multiplexing inside of <CODE>YY_INPUT</CODE>. That is, whenever <CODE>YY_INPUT</CODE> is called, it <CODE>select()</CODE>'s to see where input is available. If input is available for the scanner, it reads and returns the next byte. If input is available from another source, it calls whatever function is responsible for reading from that source. (If no input is available, it blocks until some input is available.) I've used this technique in an interpreter I wrote that both reads keyboard input using a <CODE>flex</CODE> scanner and IPC traffic from sockets, and it works fine. <H2><A NAME="SEC63" HREF="flex_toc.html#TOC63">Can I build nested parsers that work with the same input file?</A></H2> <P> This is not going to work without some additional effort. The reason is that <CODE>flex</CODE> block-buffers the input it reads from <CODE>yyin</CODE>. This means that the "outermost" <CODE>yylex()</CODE>, when called, will automatically slurp up the first 8K of input available on yyin, and subsequent calls to other <CODE>yylex()</CODE>'s won't see that input. You might be tempted to work around this problem by redefining <CODE>YY_INPUT</CODE> to only return a small amount of text, but it turns out that that approach is quite difficult. Instead, the best solution is to combine all of your scanners into one large scanner, using a different exclusive start condition for each. <H2><A NAME="SEC64" HREF="flex_toc.html#TOC64">How can I match text only at the end of a file?</A></H2> <P> There is no way to write a rule which is "match this text, but only if it comes at the end of the file". You can fake it, though, if you happen to have a character lying around that you don't allow in your input. Then you redefine <CODE>YY_INPUT</CODE> to call your own routine which, if it sees an <SAMP>`EOF'</SAMP>, returns the magic character first (and remembers to return a real <CODE>EOF</CODE> next time it's called). Then you could write: <PRE> @verbatim <COMMENT>(.|\n)*{EOF_CHAR} /* saw comment at EOF */ </PRE> <H2><A NAME="SEC65" HREF="flex_toc.html#TOC65">How can I make REJECT cascade across start condition boundaries?</A></H2> <P> You can do this as follows. Suppose you have a start condition <SAMP>`A'</SAMP>, and after exhausting all of the possible matches in <SAMP>`<A>'</SAMP>, you want to try matches in <SAMP>`<INITIAL>'</SAMP>. Then you could use the following: <PRE> @verbatim %x A %% <A>rule_that_is_long ...; REJECT; <A>rule ...; REJECT; /* shorter rule */ <A>etc. ... <A>.|\n { /* Shortest and last rule in <A>, so * cascaded REJECT's will eventually * wind up matching this rule. We want * to now switch to the initial state * and try matching from there instead. */ yyless(0); /* put back matched text */ BEGIN(INITIAL); } </PRE> <H2><A NAME="SEC66" HREF="flex_toc.html#TOC66">Why can't I use fast or full tables with interactive mode?</A></H2> <P> One of the assumptions flex makes is that interactive applications are inherently slow (they're waiting on a human after all). It has to do with how the scanner detects that it must be finished scanning a token. For interactive scanners, after scanning each character the current state is looked up in a table (essentially) to see whether there's a chance of another input character possibly extending the length of the match. If not, the scanner halts. For non-interactive scanners, the end-of-token test is much simpler, basically a compare with 0, so no memory bus cycles. Since the test occurs in the innermost scanning loop, one would like to make it go as fast as possible. <P> Still, it seems reasonable to allow the user to choose to trade off a bit of performance in this area to gain the corresponding flexibility. There might be another reason, though, why fast scanners don't support the interactive option. <H2><A NAME="SEC67" HREF="flex_toc.html#TOC67">How much faster is -F or -f than -C?</A></H2> <P> Much faster (factor of 2-3). <H2><A NAME="SEC68" HREF="flex_toc.html#TOC68">If I have a simple grammar can't I just parse it with flex?</A></H2> <P> Is your grammar recursive? That's almost always a sign that you're better off using a parser/scanner rather than just trying to use a scanner alone. <H2><A NAME="SEC69" HREF="flex_toc.html#TOC69">Why doesn't yyrestart() set the start state back to INITIAL?</A></H2> <P> There are two reasons. The first is that there might be programs that rely on the start state not changing across file changes. The second is that beginning with <CODE>flex</CODE> version 2.4, use of <CODE>yyrestart()</CODE> is no longer required, so fixing the problem there doesn't solve the more general problem. <H2><A NAME="SEC70" HREF="flex_toc.html#TOC70">How can I match C-style comments?</A></H2> <P> You might be tempted to try something like this: <PRE> @verbatim "/*".*"*/" // WRONG! </PRE> <P> or, worse, this: <PRE> @verbatim "/*"(.|\n)"*/" // WRONG! </PRE> <P> The above rules will eat too much input, and blow up on things like: <PRE> @verbatim /* a comment */ do_my_thing( "oops */" ); </PRE> <P> Here is one way which allows you to track line information: <PRE> @verbatim <INITIAL>{ "/*" BEGIN(IN_COMMENT); } <IN_COMMENT>{ "*/" BEGIN(INITIAL); [^*\n]+ // eat comment in chunks "*" // eat the lone star \n yylineno++; } </PRE> <H2><A NAME="SEC71" HREF="flex_toc.html#TOC71">The '.' isn't working the way I expected.</A></H2> <P> Here are some tips for using <SAMP>`.'</SAMP>: <UL> <LI> A common mistake is to place the grouping parenthesis AFTER an operator, when you really meant to place the parenthesis BEFORE the operator, e.g., you probably want this <CODE>(foo|bar)+</CODE> and NOT this <CODE>(foo|bar+)</CODE>. The first pattern matches the words <SAMP>`foo'</SAMP> or <SAMP>`bar'</SAMP> any number of times, e.g., it matches the text <SAMP>`barfoofoobarfoo'</SAMP>. The second pattern matches a single instance of <CODE>foo</CODE> or a single instance of <CODE>bar</CODE> followed by one or more <SAMP>`r'</SAMP>s, e.g., it matches the text <CODE>barrrr</CODE> . <LI> A <SAMP>`.'</SAMP> inside <SAMP>`[]'</SAMP>'s just means a literal<SAMP>`.'</SAMP> (period), and NOT "any character except newline". <LI> Remember that <SAMP>`.'</SAMP> matches any character EXCEPT <SAMP>`\n'</SAMP> (and <SAMP>`EOF'</SAMP>). If you really want to match ANY character, including newlines, then use <CODE>(.|\n)</CODE> Beware that the regex <CODE>(.|\n)+</CODE> will match your entire input! <LI> Finally, if you want to match a literal <SAMP>`.'</SAMP> (a period), then use <SAMP>`[.]'</SAMP> or <SAMP>`"."'</SAMP> </UL> <H2><A NAME="SEC72" HREF="flex_toc.html#TOC72">Can I get the flex manual in another format?</A></H2> <P> The <CODE>flex</CODE> source distribution includes a texinfo manual. You are free to convert that texinfo into whatever format you desire. The <CODE>texinfo</CODE> package includes tools for conversion to a number of formats. <H2><A NAME="SEC73" HREF="flex_toc.html#TOC73">Does there exist a "faster" NDFA->DFA algorithm?</A></H2> <P> There's no way around the potential exponential running time - it can take you exponential time just to enumerate all of the DFA states. In practice, though, the running time is closer to linear, or sometimes quadratic. <H2><A NAME="SEC74" HREF="flex_toc.html#TOC74">How does flex compile the DFA so quickly?</A></H2> <P> There are two big speed wins that <CODE>flex</CODE> uses: <OL> <LI> It analyzes the input rules to construct equivalence classes for those characters that always make the same transitions. It then rewrites the NFA using equivalence classes for transitions instead of characters. This cuts down the NFA->DFA computation time dramatically, to the point where, for uncompressed DFA tables, the DFA generation is often I/O bound in writing out the tables. <LI> It maintains hash values for previously computed DFA states, so testing whether a newly constructed DFA state is equivalent to a previously constructed state can be done very quickly, by first comparing hash values. </OL> <H2><A NAME="SEC75" HREF="flex_toc.html#TOC75">How can I use more than 8192 rules?</A></H2> <P> <CODE>Flex</CODE> is compiled with an upper limit of 8192 rules per scanner. If you need more than 8192 rules in your scanner, you'll have to recompile <CODE>flex</CODE> with the following changes in <TT>`flexdef.h'</TT>: <PRE> @verbatim < #define YY_TRAILING_MASK 0x2000 < #define YY_TRAILING_HEAD_MASK 0x4000 -- > #define YY_TRAILING_MASK 0x20000000 > #define YY_TRAILING_HEAD_MASK 0x40000000 </PRE> <P> This should work okay as long as your C compiler uses 32 bit integers. But you might want to think about whether using such a huge number of rules is the best way to solve your problem. <P> The following may also be relevant: <P> With luck, you should be able to increase the definitions in flexdef.h for: <PRE> @verbatim #define JAMSTATE -32766 /* marks a reference to the state that always jams */ #define MAXIMUM_MNS 31999 #define BAD_SUBSCRIPT -32767 </PRE> <P> recompile everything, and it'll all work. Flex only has these 16-bit-like values built into it because a long time ago it was developed on a machine with 16-bit ints. I've given this advice to others in the past but haven't heard back from them whether it worked okay or not... <H2><A NAME="SEC76" HREF="flex_toc.html#TOC76">How do I abandon a file in the middle of a scan and switch to a new file?</A></H2> <P> Just call <CODE>yyrestart(newfile)</CODE>. Be sure to reset the start state if you want a "fresh start, since <CODE>yyrestart</CODE> does NOT reset the start state back to <CODE>INITIAL</CODE>. <H2><A NAME="SEC77" HREF="flex_toc.html#TOC77">How do I execute code only during initialization (only before the first scan)?</A></H2> <P> You can specify an initial action by defining the macro <CODE>YY_USER_INIT</CODE> (though note that <CODE>yyout</CODE> may not be available at the time this macro is executed). Or you can add to the beginning of your rules section: <PRE> @verbatim %% /* Must be indented! */ static int did_init = 0; if ( ! did_init ){ do_my_init(); did_init = 1; } </PRE> <H2><A NAME="SEC78" HREF="flex_toc.html#TOC78">How do I execute code at termination?</A></H2> <P> You can specify an action for the <CODE><<EOF>></CODE> rule. <H2><A NAME="SEC79" HREF="flex_toc.html#TOC79">Where else can I find help?</A></H2> <P> The <CODE>help-flex</CODE> email list is served by GNU. See <A HREF="http://www.gnu.org/">http://www.gnu.org/</A> for details on how to subscribe or search the archives. <H2><A NAME="SEC80" HREF="flex_toc.html#TOC80">Can I include comments in the "rules" section of the file?</A></H2> <P> Yes, just about anywhere you want to. See the manual for the specific syntax. <H2><A NAME="SEC81" HREF="flex_toc.html#TOC81">I get an error about undefined yywrap().</A></H2> <P> You must supply a <CODE>yywrap()</CODE> function of your own, or link to <TT>`libfl.a'</TT> (which provides one), or use <PRE> @verbatim %option noyywrap </PRE> <P> in your source to say you don't want a <CODE>yywrap()</CODE> function. <H2><A NAME="SEC82" HREF="flex_toc.html#TOC82">How can I change the matching pattern at run time?</A></H2> <P> You can't, it's compiled into a static table when flex builds the scanner. <H2><A NAME="SEC83" HREF="flex_toc.html#TOC83">How can I expand macros in the input?</A></H2> <P> The best way to approach this problem is at a higher level, e.g., in the parser. <P> However, you can do this using multiple input buffers. <PRE> @verbatim %% macro/[a-z]+ { /* Saw the macro "macro" followed by extra stuff. */ main_buffer = YY_CURRENT_BUFFER; expansion_buffer = yy_scan_string(expand(yytext)); yy_switch_to_buffer(expansion_buffer); } <<EOF>> { if ( expansion_buffer ) { // We were doing an expansion, return to where // we were. yy_switch_to_buffer(main_buffer); yy_delete_buffer(expansion_buffer); expansion_buffer = 0; } else yyterminate(); } </PRE> <P> You probably will want a stack of expansion buffers to allow nested macros. From the above though hopefully the idea is clear. <H2><A NAME="SEC84" HREF="flex_toc.html#TOC84">How can I build a two-pass scanner?</A></H2> <P> One way to do it is to filter the first pass to a temporary file, then process the temporary file on the second pass. You will probably see a performance hit, do to all the disk I/O. <P> When you need to look ahead far forward like this, it almost always means that the right solution is to build a parse tree of the entire input, then walk it after the parse in order to generate the output. In a sense, this is a two-pass approach, once through the text and once through the parse tree, but the performance hit for the latter is usually an order of magnitude smaller, since everything is already classified, in binary format, and residing in memory. <H2><A NAME="SEC85" HREF="flex_toc.html#TOC85">How do I match any string not matched in the preceding rules?</A></H2> <P> One way to assign precedence, is to place the more specific rules first. If two rules would match the same input (same sequence of characters) then the first rule listed in the <CODE>flex</CODE> input wins. e.g., <PRE> @verbatim %% foo[a-zA-Z_]+ return FOO_ID; bar[a-zA-Z_]+ return BAR_ID; [a-zA-Z_]+ return GENERIC_ID; </PRE> <P> Note that the rule <CODE>[a-zA-Z_]+</CODE> must come *after* the others. It will match the same amount of text as the more specific rules, and in that case the <CODE>flex</CODE> scanner will pick the first rule listed in your scanner as the one to match. <H2><A NAME="SEC86" HREF="flex_toc.html#TOC86">I am trying to port code from AT&T lex that uses yysptr and yysbuf.</A></H2> <P> Those are internal variables pointing into the AT&T scanner's input buffer. I imagine they're being manipulated in user versions of the <CODE>input()</CODE> and <CODE>unput()</CODE> functions. If so, what you need to do is analyze those functions to figure out what they're doing, and then replace <CODE>input()</CODE> with an appropriate definition of <CODE>YY_INPUT</CODE>. You shouldn't need to (and must not) replace <CODE>flex</CODE>'s <CODE>unput()</CODE> function. <H2><A NAME="SEC87" HREF="flex_toc.html#TOC87">Is there a way to make flex treat NULL like a regular character?</A></H2> <P> Yes, <SAMP>`\0'</SAMP> and <SAMP>`\x00'</SAMP> should both do the trick. Perhaps you have an ancient version of <CODE>flex</CODE>. The latest release is version 2.5.23. <H2><A NAME="SEC88" HREF="flex_toc.html#TOC88">Whenever flex can not match the input it says "flex scanner jammed".</A></H2> <P> You need to add a rule that matches the otherwise-unmatched text. e.g., <PRE> @verbatim %option yylineno %% [[a bunch of rules here]] . printf("bad input character '%s' at line %d\n", yytext, yylineno); </PRE> <P> See <CODE>%option default</CODE> for more information. <H2><A NAME="SEC89" HREF="flex_toc.html#TOC89">Why doesn't flex have non-greedy operators like perl does?</A></H2> <P> A DFA can do a non-greedy match by stopping the first time it enters an accepting state, instead of consuming input until it determines that no further matching is possible (a "jam" state). This is actually easier to implement than longest leftmost match (which flex does). <P> But it's also much less useful than longest leftmost match. In general, when you find yourself wishing for non-greedy matching, that's usually a sign that you're trying to make the scanner do some parsing. That's generally the wrong approach, since it lacks the power to do a decent job. Better is to either introduce a separate parser, or to split the scanner into multiple scanners using (exclusive) start conditions. <P> You might have a separate start state once you've seen the <SAMP>`BEGIN'</SAMP>. In that state, you might then have a regex that will match <SAMP>`END'</SAMP> (to kick you out of the state), and perhaps <SAMP>`(.|\n)'</SAMP> to get a single character within the chunk ... <P> This approach also has much better error-reporting properties. <H2><A NAME="SEC90" HREF="flex_toc.html#TOC90">Memory leak - 16386 bytes allocated by malloc.</A></H2> <P> @anchor{faq-memory-leak} <P> UPDATED 2002-07-10: As of <CODE>flex</CODE> version 2.5.9, this leak means that you did not call <CODE>yylex_destroy()</CODE>. If you are using an earlier version of <CODE>flex</CODE>, then read on. <P> The leak is about 16426 bytes. That is, (8192 * 2 + 2) for the read-buffer, and about 40 for <CODE>struct yy_buffer_state</CODE> (depending upon alignment). The leak is in the non-reentrant C scanner only (NOT in the reentrant scanner, NOT in the C++ scanner). Since <CODE>flex</CODE> doesn't know when you are done, the buffer is never freed. <P> However, the leak won't multiply since the buffer is reused no matter how many times you call <CODE>yylex()</CODE>. <P> If you want to reclaim the memory when you are completely done scanning, then you might try this: <PRE> @verbatim /* For non-reentrant C scanner only. */ yy_delete_buffer(yy_current_buffer); yy_init = 1; </PRE> <P> Note: <CODE>yy_init</CODE> is an "internal variable", and hasn't been tested in this situation. It is possible that some other globals may need resetting as well. <H2><A NAME="SEC91" HREF="flex_toc.html#TOC91">How do I track the byte offset for lseek()?</A></H2> <PRE> @verbatim > We thought that it would be possible to have this number through the > evaluation of the following expression: > > seek_position = (no_buffers)*YY_READ_BUF_SIZE + yy_c_buf_p - yy_current_buffer->yy_ch_buf </PRE> <P> While this is the right idea, it has two problems. The first is that it's possible that <CODE>flex</CODE> will request less than <CODE>YY_READ_BUF_SIZE</CODE> during an invocation of <CODE>YY_INPUT</CODE> (or that your input source will return less even though <CODE>YY_READ_BUF_SIZE</CODE> bytes were requested). The second problem is that when refilling its internal buffer, <CODE>flex</CODE> keeps some characters from the previous buffer (because usually it's in the middle of a match, and needs those characters to construct <CODE>yytext</CODE> for the match once it's done). Because of this, <CODE>yy_c_buf_p - yy_current_buffer->yy_ch_buf</CODE> won't be exactly the number of characters already read from the current buffer. <P> An alternative solution is to count the number of characters you've matched since starting to scan. This can be done by using <CODE>YY_USER_ACTION</CODE>. For example, <PRE> @verbatim #define YY_USER_ACTION num_chars += yyleng; </PRE> <P> (You need to be careful to update your bookkeeping if you use <CODE>yymore(</CODE>), <CODE>yyless()</CODE>, <CODE>unput()</CODE>, or <CODE>input()</CODE>.) <H2><A NAME="SEC92" HREF="flex_toc.html#TOC92">How do I use my own I/O classes in a C++ scanner?</A></H2> <P> When the flex C++ scanning class rewrite finally happens, then this sort of thing should become much easier. <P> <A NAME="IDX457"></A> <A NAME="IDX458"></A> <A NAME="IDX459"></A> <A NAME="IDX460"></A> <A NAME="IDX461"></A> <A NAME="IDX462"></A> You can do this by passing the various functions (such as <CODE>LexerInput()</CODE> and <CODE>LexerOutput()</CODE>) nil <CODE>iostream*</CODE>'s, and then dealing with your own I/O classes surreptitiously (i.e., stashing them in special member variables). This works because the only assumption about the lexer regarding what's done with the iostream's is that they're ultimately passed to <CODE>LexerInput()</CODE> and <CODE>LexerOutput</CODE>, which then do whatever is necessary with them. <H2><A NAME="SEC93" HREF="flex_toc.html#TOC93">How do I skip as many chars as possible?</A></H2> <P> How do I skip as many chars as possible -- without interfering with the other patterns? <P> In the example below, we want to skip over characters until we see the phrase "endskip". The following will <EM>NOT</EM> work correctly (do you see why not?) <PRE> @verbatim /* INCORRECT SCANNER */ %x SKIP %% <INITIAL>startskip BEGIN(SKIP); ... <SKIP>"endskip" BEGIN(INITIAL); <SKIP>.* ; </PRE> <P> The problem is that the pattern .* will eat up the word "endskip." The simplest (but slow) fix is: <PRE> @verbatim <SKIP>"endskip" BEGIN(INITIAL); <SKIP>. ; </PRE> <P> The fix involves making the second rule match more, without making it match "endskip" plus something else. So for example: <PRE> @verbatim <SKIP>"endskip" BEGIN(INITIAL); <SKIP>[^e]+ ; <SKIP>. ;/* so you eat up e's, too */ </PRE> <H2><A NAME="SEC94" HREF="flex_toc.html#TOC94">unnamed-faq-33</A></H2> <PRE> @verbatim QUESTION: When was flex born? Vern Paxson took over the Software Tools lex project from Jef Poskanzer in 1982. At that point it was written in Ratfor. Around 1987 or so, Paxson translated it into C, and a legend was born :-). </PRE> <H2><A NAME="SEC95" HREF="flex_toc.html#TOC95">unnamed-faq-42</A></H2> <PRE> @verbatim To: Adoram Rogel <adoram@orna.hybridge.com> Subject: Re: Flex 2.5.2 performance questions In-reply-to: Your message of Wed, 18 Sep 96 11:12:17 EDT. Date: Wed, 18 Sep 96 10:51:02 PDT From: Vern Paxson <vern> [Note, the most recent flex release is 2.5.4, which you can get from ftp.ee.lbl.gov. It has bug fixes over 2.5.2 and 2.5.3.] > 1. Using the pattern > ([Ff](oot)?)?[Nn](ote)?(\.)? > instead of > (((F|f)oot(N|n)ote)|((N|n)ote)|((N|n)\.)|((F|f)(N|n)(\.))) > (in a very complicated flex program) caused the program to slow from > 300K+/min to 100K/min (no other changes were done). These two are not equivalent. For example, the first can match "footnote." but the second can only match "footnote". This is almost certainly the cause in the discrepancy - the slower scanner run is matching more tokens, and/or having to do more backing up. > 2. Which of these two are better: [Ff]oot or (F|f)oot ? From a performance point of view, they're equivalent (modulo presumably minor effects such as memory cache hit rates; and the presence of trailing context, see below). From a space point of view, the first is slightly preferable. > 3. I have a pattern that look like this: > pats {p1}|{p2}|{p3}|...|{p50} (50 patterns ORd) > > running yet another complicated program that includes the following rule: > <snext>{and}/{no4}{bb}{pats} > > gets me to "too complicated - over 32,000 states"... I can't tell from this example whether the trailing context is variable-length or fixed-length (it could be the latter if {and} is fixed-length). If it's variable length, which flex -p will tell you, then this reflects a basic performance problem, and if you can eliminate it by restructuring your scanner, you will see significant improvement. > so I divided {pats} to {pats1}, {pats2},..., {pats5} each consists of about > 10 patterns and changed the rule to be 5 rules. > This did compile, but what is the rule of thumb here ? The rule is to avoid trailing context other than fixed-length, in which for a/b, either the 'a' pattern or the 'b' pattern have a fixed length. Use of the '|' operator automatically makes the pattern variable length, so in this case '[Ff]oot' is preferred to '(F|f)oot'. > 4. I changed a rule that looked like this: > <snext8>{and}{bb}/{ROMAN}[^A-Za-z] { BEGIN... > > to the next 2 rules: > <snext8>{and}{bb}/{ROMAN}[A-Za-z] { ECHO;} > <snext8>{and}{bb}/{ROMAN} { BEGIN... > > Again, I understand the using [^...] will cause a great performance loss Actually, it doesn't cause any sort of performance loss. It's a surprising fact about regular expressions that they always match in linear time regardless of how complex they are. > but are there any specific rules about it ? See the "Performance Considerations" section of the man page, and also the example in MISC/fastwc/. Vern </PRE> <H2><A NAME="SEC96" HREF="flex_toc.html#TOC96">unnamed-faq-43</A></H2> <PRE> @verbatim To: Adoram Rogel <adoram@hybridge.com> Subject: Re: Flex 2.5.2 performance questions In-reply-to: Your message of Thu, 19 Sep 96 10:16:04 EDT. Date: Thu, 19 Sep 96 09:58:00 PDT From: Vern Paxson <vern> > a lot about the backing up problem. > I believe that there lies my biggest problem, and I'll try to improve > it. Since you have variable trailing context, this is a bigger performance problem. Fixing it is usually easier than fixing backing up, which in a complicated scanner (yours seems to fit the bill) can be extremely difficult to do correctly. You also don't mention what flags you are using for your scanner. -f makes a large speed difference, and -Cfe buys you nearly as much speed but the resulting scanner is considerably smaller. > I have an | operator in {and} and in {pats} so both of them are variable > length. -p should have reported this. > Is changing one of them to fixed-length is enough ? Yes. > Is it possible to change the 32,000 states limit ? Yes. I've appended instructions on how. Before you make this change, though, you should think about whether there are ways to fundamentally simplify your scanner - those are certainly preferable! Vern To increase the 32K limit (on a machine with 32 bit integers), you increase the magnitude of the following in flexdef.h: #define JAMSTATE -32766 /* marks a reference to the state that always jams */ #define MAXIMUM_MNS 31999 #define BAD_SUBSCRIPT -32767 #define MAX_SHORT 32700 Adding a 0 or two after each should do the trick. </PRE> <H2><A NAME="SEC97" HREF="flex_toc.html#TOC97">unnamed-faq-44</A></H2> <PRE> @verbatim To: Heeman_Lee@hp.com Subject: Re: flex - multi-byte support? In-reply-to: Your message of Thu, 03 Oct 1996 17:24:04 PDT. Date: Fri, 04 Oct 1996 11:42:18 PDT From: Vern Paxson <vern> > I assume as long as my *.l file defines the > range of expected character code values (in octal format), flex will > scan the file and read multi-byte characters correctly. But I have no > confidence in this assumption. Your lack of confidence is justified - this won't work. Flex has in it a widespread assumption that the input is processed one byte at a time. Fixing this is on the to-do list, but is involved, so it won't happen any time soon. In the interim, the best I can suggest (unless you want to try fixing it yourself) is to write your rules in terms of pairs of bytes, using definitions in the first section: X \xfe\xc2 ... %% foo{X}bar found_foo_fe_c2_bar(); etc. Definitely a pain - sorry about that. By the way, the email address you used for me is ancient, indicating you have a very old version of flex. You can get the most recent, 2.5.4, from ftp.ee.lbl.gov. Vern </PRE> <H2><A NAME="SEC98" HREF="flex_toc.html#TOC98">unnamed-faq-45</A></H2> <PRE> @verbatim To: moleary@primus.com Subject: Re: Flex / Unicode compatibility question In-reply-to: Your message of Tue, 22 Oct 1996 10:15:42 PDT. Date: Tue, 22 Oct 1996 11:06:13 PDT From: Vern Paxson <vern> Unfortunately flex at the moment has a widespread assumption within it that characters are processed 8 bits at a time. I don't see any easy fix for this (other than writing your rules in terms of double characters - a pain). I also don't know of a wider lex, though you might try surfing the Plan 9 stuff because I know it's a Unicode system, and also the PCCT toolkit (try searching say Alta Vista for "Purdue Compiler Construction Toolkit"). Fixing flex to handle wider characters is on the long-term to-do list. But since flex is a strictly spare-time project these days, this probably won't happen for quite a while, unless someone else does it first. Vern </PRE> <H2><A NAME="SEC99" HREF="flex_toc.html#TOC99">unnamed-faq-46</A></H2> <PRE> @verbatim To: Johan Linde <jl@theophys.kth.se> Subject: Re: translation of flex In-reply-to: Your message of Sun, 10 Nov 1996 09:16:36 PST. Date: Mon, 11 Nov 1996 10:33:50 PST From: Vern Paxson <vern> > I'm working for the Swedish team translating GNU program, and I'm currently > working with flex. I have a few questions about some of the messages which > I hope you can answer. All of the things you're wondering about, by the way, concerning flex internals - probably the only person who understands what they mean in English is me! So I wouldn't worry too much about getting them right. That said ... > #: main.c:545 > msgid " %d protos created\n" > > Does proto mean prototype? Yes - prototypes of state compression tables. > #: main.c:539 > msgid " %d/%d (peak %d) template nxt-chk entries created\n" > > Here I'm mainly puzzled by 'nxt-chk'. I guess it means 'next-check'. (?) > However, 'template next-check entries' doesn't make much sense to me. To be > able to find a good translation I need to know a little bit more about it. There is a scheme in the Aho/Sethi/Ullman compiler book for compressing scanner tables. It involves creating two pairs of tables. The first has "base" and "default" entries, the second has "next" and "check" entries. The "base" entry is indexed by the current state and yields an index into the next/check table. The "default" entry gives what to do if the state transition isn't found in next/check. The "next" entry gives the next state to enter, but only if the "check" entry verifies that this entry is correct for the current state. Flex creates templates of series of next/check entries and then encodes differences from these templates as a way to compress the tables. > #: main.c:533 > msgid " %d/%d base-def entries created\n" > > The same problem here for 'base-def'. See above. Vern </PRE> <H2><A NAME="SEC100" HREF="flex_toc.html#TOC100">unnamed-faq-47</A></H2> <PRE> @verbatim To: Xinying Li <xli@npac.syr.edu> Subject: Re: FLEX ? In-reply-to: Your message of Wed, 13 Nov 1996 17:28:38 PST. Date: Wed, 13 Nov 1996 19:51:54 PST From: Vern Paxson <vern> > "unput()" them to input flow, question occurs. If I do this after I scan > a carriage, the variable "yy_current_buffer->yy_at_bol" is changed. That > means the carriage flag has gone. You can control this by calling yy_set_bol(). It's described in the manual. > And if in pre-reading it goes to the end of file, is anything done > to control the end of curren buffer and end of file? No, there's no way to put back an end-of-file. > By the way I am using flex 2.5.2 and using the "-l". The latest release is 2.5.4, by the way. It fixes some bugs in 2.5.2 and 2.5.3. You can get it from ftp.ee.lbl.gov. Vern </PRE> <H2><A NAME="SEC101" HREF="flex_toc.html#TOC101">unnamed-faq-48</A></H2> <PRE> @verbatim To: Alain.ISSARD@st.com Subject: Re: Start condition with FLEX In-reply-to: Your message of Mon, 18 Nov 1996 09:45:02 PST. Date: Mon, 18 Nov 1996 10:41:34 PST From: Vern Paxson <vern> > I am not able to use the start condition scope and to use the | (OR) with > rules having start conditions. The problem is that if you use '|' as a regular expression operator, for example "a|b" meaning "match either 'a' or 'b'", then it must *not* have any blanks around it. If you instead want the special '|' *action* (which from your scanner appears to be the case), which is a way of giving two different rules the same action: foo | bar matched_foo_or_bar(); then '|' *must* be separated from the first rule by whitespace and *must* be followed by a new line. You *cannot* write it as: foo | bar matched_foo_or_bar(); even though you might think you could because yacc supports this syntax. The reason for this unfortunately incompatibility is historical, but it's unlikely to be changed. Your problems with start condition scope are simply due to syntax errors from your use of '|' later confusing flex. Let me know if you still have problems. Vern </PRE> <H2><A NAME="SEC102" HREF="flex_toc.html#TOC102">unnamed-faq-49</A></H2> <PRE> @verbatim To: Gregory Margo <gmargo@newton.vip.best.com> Subject: Re: flex-2.5.3 bug report In-reply-to: Your message of Sat, 23 Nov 1996 16:50:09 PST. Date: Sat, 23 Nov 1996 17:07:32 PST From: Vern Paxson <vern> > Enclosed is a lex file that "real" lex will process, but I cannot get > flex to process it. Could you try it and maybe point me in the right direction? Your problem is that some of the definitions in the scanner use the '/' trailing context operator, and have it enclosed in ()'s. Flex does not allow this operator to be enclosed in ()'s because doing so allows undefined regular expressions such as "(a/b)+". So the solution is to remove the parentheses. Note that you must also be building the scanner with the -l option for AT&T lex compatibility. Without this option, flex automatically encloses the definitions in parentheses. Vern </PRE> <H2><A NAME="SEC103" HREF="flex_toc.html#TOC103">unnamed-faq-50</A></H2> <PRE> @verbatim To: Thomas Hadig <hadig@toots.physik.rwth-aachen.de> Subject: Re: Flex Bug ? In-reply-to: Your message of Tue, 26 Nov 1996 14:35:01 PST. Date: Tue, 26 Nov 1996 11:15:05 PST From: Vern Paxson <vern> > In my lexer code, i have the line : > ^\*.* { } > > Thus all lines starting with an astrix (*) are comment lines. > This does not work ! I can't get this problem to reproduce - it works fine for me. Note though that if what you have is slightly different: COMMENT ^\*.* %% {COMMENT} { } then it won't work, because flex pushes back macro definitions enclosed in ()'s, so the rule becomes (^\*.*) { } and now that the '^' operator is not at the immediate beginning of the line, it's interpreted as just a regular character. You can avoid this behavior by using the "-l" lex-compatibility flag, or "%option lex-compat". Vern </PRE> <H2><A NAME="SEC104" HREF="flex_toc.html#TOC104">unnamed-faq-51</A></H2> <PRE> @verbatim To: Adoram Rogel <adoram@hybridge.com> Subject: Re: Flex 2.5.4 BOF ??? In-reply-to: Your message of Tue, 26 Nov 1996 16:10:41 PST. Date: Wed, 27 Nov 1996 10:56:25 PST From: Vern Paxson <vern> > Organization(s)?/[a-z] > > This matched "Organizations" (looking in debug mode, the trailing s > was matched with trailing context instead of the optional (s) in the > end of the word. That should only happen with lex. Flex can properly match this pattern. (That might be what you're saying, I'm just not sure.) > Is there a way to avoid this dangerous trailing context problem ? Unfortunately, there's no easy way. On the other hand, I don't see why it should be a problem. Lex's matching is clearly wrong, and I'd hope that usually the intent remains the same as expressed with the pattern, so flex's matching will be correct. Vern </PRE> <H2><A NAME="SEC105" HREF="flex_toc.html#TOC105">unnamed-faq-52</A></H2> <PRE> @verbatim To: Cameron MacKinnon <mackin@interlog.com> Subject: Re: Flex documentation bug In-reply-to: Your message of Mon, 02 Dec 1996 00:07:08 PST. Date: Sun, 01 Dec 1996 22:29:39 PST From: Vern Paxson <vern> > I'm not sure how or where to submit bug reports (documentation or > otherwise) for the GNU project stuff ... Well, strictly speaking flex isn't part of the GNU project. They just distribute it because no one's written a decent GPL'd lex replacement. So you should send bugs directly to me. Those sent to the GNU folks sometimes find there way to me, but some may drop between the cracks. > In GNU Info, under the section 'Start Conditions', and also in the man > page (mine's dated April '95) is a nice little snippet showing how to > parse C quoted strings into a buffer, defined to be MAX_STR_CONST in > size. Unfortunately, no overflow checking is ever done ... This is already mentioned in the manual: Finally, here's an example of how to match C-style quoted strings using exclusive start conditions, including expanded escape sequences (but not including checking for a string that's too long): The reason for not doing the overflow checking is that it will needlessly clutter up an example whose main purpose is just to demonstrate how to use flex. The latest release is 2.5.4, by the way, available from ftp.ee.lbl.gov. Vern </PRE> <H2><A NAME="SEC106" HREF="flex_toc.html#TOC106">unnamed-faq-53</A></H2> <PRE> @verbatim To: tsv@cs.UManitoba.CA Subject: Re: Flex (reg).. In-reply-to: Your message of Thu, 06 Mar 1997 23:50:16 PST. Date: Thu, 06 Mar 1997 15:54:19 PST From: Vern Paxson <vern> > [:alpha:] ([:alnum:] | \\_)* If your rule really has embedded blanks as shown above, then it won't work, as the first blank delimits the rule from the action. (It wouldn't even compile ...) You need instead: [:alpha:]([:alnum:]|\\_)* and that should work fine - there's no restriction on what can go inside of ()'s except for the trailing context operator, '/'. Vern </PRE> <H2><A NAME="SEC107" HREF="flex_toc.html#TOC107">unnamed-faq-54</A></H2> <PRE> @verbatim To: "Mike Stolnicki" <mstolnic@ford.com> Subject: Re: FLEX help In-reply-to: Your message of Fri, 30 May 1997 13:33:27 PDT. Date: Fri, 30 May 1997 10:46:35 PDT From: Vern Paxson <vern> > We'd like to add "if-then-else", "while", and "for" statements to our > language ... > We've investigated many possible solutions. The one solution that seems > the most reasonable involves knowing the position of a TOKEN in yyin. I strongly advise you to instead build a parse tree (abstract syntax tree) and loop over that instead. You'll find this has major benefits in keeping your interpreter simple and extensible. That said, the functionality you mention for get_position and set_position have been on the to-do list for a while. As flex is a purely spare-time project for me, no guarantees when this will be added (in particular, it for sure won't be for many months to come). Vern </PRE> <H2><A NAME="SEC108" HREF="flex_toc.html#TOC108">unnamed-faq-55</A></H2> <PRE> @verbatim To: Colin Paul Adams <colin@colina.demon.co.uk> Subject: Re: Flex C++ classes and Bison In-reply-to: Your message of 09 Aug 1997 17:11:41 PDT. Date: Fri, 15 Aug 1997 10:48:19 PDT From: Vern Paxson <vern> > #define YY_DECL int yylex (YYSTYPE *lvalp, struct parser_control > *parm) > > I have been trying to get this to work as a C++ scanner, but it does > not appear to be possible (warning that it matches no declarations in > yyFlexLexer, or something like that). > > Is this supposed to be possible, or is it being worked on (I DID > notice the comment that scanner classes are still experimental, so I'm > not too hopeful)? What you need to do is derive a subclass from yyFlexLexer that provides the above yylex() method, squirrels away lvalp and parm into member variables, and then invokes yyFlexLexer::yylex() to do the regular scanning. Vern </PRE> <H2><A NAME="SEC109" HREF="flex_toc.html#TOC109">unnamed-faq-56</A></H2> <PRE> @verbatim To: Mikael.Latvala@lmf.ericsson.se Subject: Re: Possible mistake in Flex v2.5 document In-reply-to: Your message of Fri, 05 Sep 1997 16:07:24 PDT. Date: Fri, 05 Sep 1997 10:01:54 PDT From: Vern Paxson <vern> > In that example you show how to count comment lines when using > C style /* ... */ comments. My question is, shouldn't you take into > account a scenario where end of a comment marker occurs inside > character or string literals? The scanner certainly needs to also scan character and string literals. However it does that (there's an example in the man page for strings), the lexer will recognize the beginning of the literal before it runs across the embedded "/*". Consequently, it will finish scanning the literal before it even considers the possibility of matching "/*". Example: '([^']*|{ESCAPE_SEQUENCE})' will match all the text between the ''s (inclusive). So the lexer considers this as a token beginning at the first ', and doesn't even attempt to match other tokens inside it. I thinnk this subtlety is not worth putting in the manual, as I suspect it would confuse more people than it would enlighten. Vern </PRE> <H2><A NAME="SEC110" HREF="flex_toc.html#TOC110">unnamed-faq-57</A></H2> <PRE> @verbatim To: "Marty Leisner" <leisner@sdsp.mc.xerox.com> Subject: Re: flex limitations In-reply-to: Your message of Sat, 06 Sep 1997 11:27:21 PDT. Date: Mon, 08 Sep 1997 11:38:08 PDT From: Vern Paxson <vern> > %% > [a-zA-Z]+ /* skip a line */ > { printf("got %s\n", yytext); } > %% What version of flex are you using? If I feed this to 2.5.4, it complains: "bug.l", line 5: EOF encountered inside an action "bug.l", line 5: unrecognized rule "bug.l", line 5: fatal parse error Not the world's greatest error message, but it manages to flag the problem. (With the introduction of start condition scopes, flex can't accommodate an action on a separate line, since it's ambiguous with an indented rule.) You can get 2.5.4 from ftp.ee.lbl.gov. Vern </PRE> <H2><A NAME="SEC111" HREF="flex_toc.html#TOC111">unnamed-faq-58</A></H2> <PRE> @verbatim To: uocarroll@deagostini.co.uk (Ultan O'Carroll) Subject: Re: Flex repositries In-reply-to: Your message of Fri, 12 Sep 1997 15:02:28 PDT. Date: Fri, 12 Sep 1997 10:31:50 PDT From: Vern Paxson <vern> > before I start beavering away I wonder if you know of any > place/libraries for flex > desciption files that might already do this or give me a head start ? Unfortunately, no, I don't. You might try asking on comp.compilers. Vern </PRE> <H2><A NAME="SEC112" HREF="flex_toc.html#TOC112">unnamed-faq-59</A></H2> <PRE> @verbatim To: Adoram Rogel <adoram@hybridge.com> Subject: Re: Conditional compiling in the definitions section In-reply-to: Your message of Thu, 25 Sep 1997 11:22:42 PDT. Date: Thu, 25 Sep 1997 10:56:31 PDT From: Vern Paxson <vern> > I'm trying to combine two large lex files that now differ only in > about 10 lines in the definitions section. > I would like to have something like this: > #ifdef FFF > it \<IT\> > #else > it \<I\> > #endif > > Now, I can't add states for these, as I have already too many states > and the program is very complicated, and I won't be able to handle > 10 or 20 more states. > > Any trick to do this ? You might try using m4, or the C preprocessor plus a sed script to clean up the result (strip out the #line's). Vern </PRE> <H2><A NAME="SEC113" HREF="flex_toc.html#TOC113">unnamed-faq-60</A></H2> <PRE> @verbatim To: Steve Antoch <SteveAn@visio.com> Subject: Re: lex and yacc grammars In-reply-to: Your message of Mon, 17 Nov 1997 15:31:25 PST. Date: Mon, 17 Nov 1997 15:27:01 PST From: Vern Paxson <vern> > Would you happen to know where I can find grammars for lex and yacc? The flex sources have a grammar for (f)lex. Dunno about yacc, Vern </PRE> <H2><A NAME="SEC114" HREF="flex_toc.html#TOC114">unnamed-faq-61</A></H2> <PRE> @verbatim To: Bryan Housel <bryan@drawcomp.com> Subject: Re: Question about Flex v2.5 In-reply-to: Your message of Tue, 11 Nov 1997 21:30:23 PST. Date: Mon, 17 Nov 1997 17:12:21 PST From: Vern Paxson <vern> > It prints one of those "end of buffer.." messages for each character in the > token... This will happen if your LexerInput() function returns only one character at a time, which can happen either if you're scanner is "interactive", or if the streams library on your platform always returns 1 for yyin->gcount(). Solution: override LexerInput() with a version that returns whole buffers. Vern </PRE> <H2><A NAME="SEC115" HREF="flex_toc.html#TOC115">unnamed-faq-62</A></H2> <PRE> @verbatim To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE Subject: Re: Flex maximums In-reply-to: Your message of Mon, 17 Nov 1997 17:16:06 PST. Date: Mon, 17 Nov 1997 17:16:15 PST From: Vern Paxson <vern> > I took a quick look into the flex-sources and altered some #defines in > flexdefs.h: > > #define INITIAL_MNS 64000 > #define MNS_INCREMENT 1024000 > #define MAXIMUM_MNS 64000 The things to fix are to add a couple of zeroes to: #define JAMSTATE -32766 /* marks a reference to the state that always jams */ #define MAXIMUM_MNS 31999 #define BAD_SUBSCRIPT -32767 #define MAX_SHORT 32700 and, if you get complaints about too many rules, make the following change too: #define YY_TRAILING_MASK 0x200000 #define YY_TRAILING_HEAD_MASK 0x400000 - Vern </PRE> <H2><A NAME="SEC116" HREF="flex_toc.html#TOC116">unnamed-faq-63</A></H2> <PRE> @verbatim To: jimmey@lexis-nexis.com (Jimmey Todd) Subject: Re: FLEX question regarding istream vs ifstream In-reply-to: Your message of Mon, 08 Dec 1997 15:54:15 PST. Date: Mon, 15 Dec 1997 13:21:35 PST From: Vern Paxson <vern> > stdin_handle = YY_CURRENT_BUFFER; > ifstream fin( "aFile" ); > yy_switch_to_buffer( yy_create_buffer( fin, YY_BUF_SIZE ) ); > > What I'm wanting to do, is pass the contents of a file thru one set > of rules and then pass stdin thru another set... It works great if, I > don't use the C++ classes. But since everything else that I'm doing is > in C++, I thought I'd be consistent. > > The problem is that 'yy_create_buffer' is expecting an istream* as it's > first argument (as stated in the man page). However, fin is a ifstream > object. Any ideas on what I might be doing wrong? Any help would be > appreciated. Thanks!! You need to pass &fin, to turn it into an ifstream* instead of an ifstream. Then its type will be compatible with the expected istream*, because ifstream is derived from istream. Vern </PRE> <H2><A NAME="SEC117" HREF="flex_toc.html#TOC117">unnamed-faq-64</A></H2> <PRE> @verbatim To: Enda Fadian <fadiane@piercom.ie> Subject: Re: Question related to Flex man page? In-reply-to: Your message of Tue, 16 Dec 1997 15:17:34 PST. Date: Tue, 16 Dec 1997 14:17:09 PST From: Vern Paxson <vern> > Can you explain to me what is ment by a long-jump in relation to flex? Using the longjmp() function while inside yylex() or a routine called by it. > what is the flex activation frame. Just yylex()'s stack frame. > As far as I can see yyrestart will bring me back to the sart of the input > file and using flex++ isnot really an option! No, yyrestart() doesn't imply a rewind, even though its name might sound like it does. It tells the scanner to flush its internal buffers and start reading from the given file at its present location. Vern </PRE> <H2><A NAME="SEC118" HREF="flex_toc.html#TOC118">unnamed-faq-65</A></H2> <PRE> @verbatim To: hassan@larc.info.uqam.ca (Hassan Alaoui) Subject: Re: Need urgent Help In-reply-to: Your message of Sat, 20 Dec 1997 19:38:19 PST. Date: Sun, 21 Dec 1997 21:30:46 PST From: Vern Paxson <vern> > /usr/lib/yaccpar: In function `int yyparse()': > /usr/lib/yaccpar:184: warning: implicit declaration of function `int yylex(...)' > > ld: Undefined symbol > _yylex > _yyparse > _yyin This is a known problem with Solaris C++ (and/or Solaris yacc). I believe the fix is to explicitly insert some 'extern "C"' statements for the corresponding routines/symbols. Vern </PRE> <H2><A NAME="SEC119" HREF="flex_toc.html#TOC119">unnamed-faq-66</A></H2> <PRE> @verbatim To: mc0307@mclink.it Cc: gnu@prep.ai.mit.edu Subject: Re: [mc0307@mclink.it: Help request] In-reply-to: Your message of Fri, 12 Dec 1997 17:57:29 PST. Date: Sun, 21 Dec 1997 22:33:37 PST From: Vern Paxson <vern> > This is my definition for float and integer types: > . . . > NZD [1-9] > ... > I've tested my program on other lex version (on UNIX Sun Solaris an HP > UNIX) and it work well, so I think that my definitions are correct. > There are any differences between Lex and Flex? There are indeed differences, as discussed in the man page. The one you are probably running into is that when flex expands a name definition, it puts parentheses around the expansion, while lex does not. There's an example in the man page of how this can lead to different matching. Flex's behavior complies with the POSIX standard (or at least with the last POSIX draft I saw). Vern </PRE> <H2><A NAME="SEC120" HREF="flex_toc.html#TOC120">unnamed-faq-67</A></H2> <PRE> @verbatim To: hassan@larc.info.uqam.ca (Hassan Alaoui) Subject: Re: Thanks In-reply-to: Your message of Mon, 22 Dec 1997 16:06:35 PST. Date: Mon, 22 Dec 1997 14:35:05 PST From: Vern Paxson <vern> > Thank you very much for your help. I compile and link well with C++ while > declaring 'yylex ...' extern, But a little problem remains. I get a > segmentation default when executing ( I linked with lfl library) while it > works well when using LEX instead of flex. Do you have some ideas about the > reason for this ? The one possible reason for this that comes to mind is if you've defined yytext as "extern char yytext[]" (which is what lex uses) instead of "extern char *yytext" (which is what flex uses). If it's not that, then I'm afraid I don't know what the problem might be. Vern </PRE> <H2><A NAME="SEC121" HREF="flex_toc.html#TOC121">unnamed-faq-68</A></H2> <PRE> @verbatim To: "Bart Niswonger" <NISWONGR@almaden.ibm.com> Subject: Re: flex 2.5: c++ scanners & start conditions In-reply-to: Your message of Tue, 06 Jan 1998 10:34:21 PST. Date: Tue, 06 Jan 1998 19:19:30 PST From: Vern Paxson <vern> > The problem is that when I do this (using %option c++) start > conditions seem to not apply. The BEGIN macro modifies the yy_start variable. For C scanners, this is a static with scope visible through the whole file. For C++ scanners, it's a member variable, so it only has visible scope within a member function. Your lexbegin() routine is not a member function when you build a C++ scanner, so it's not modifying the correct yy_start. The diagnostic that indicates this is that you found you needed to add a declaration of yy_start in order to get your scanner to compile when using C++; instead, the correct fix is to make lexbegin() a member function (by deriving from yyFlexLexer). Vern </PRE> <H2><A NAME="SEC122" HREF="flex_toc.html#TOC122">unnamed-faq-69</A></H2> <PRE> @verbatim To: "Boris Zinin" <boris@ippe.rssi.ru> Subject: Re: current position in flex buffer In-reply-to: Your message of Mon, 12 Jan 1998 18:58:23 PST. Date: Mon, 12 Jan 1998 12:03:15 PST From: Vern Paxson <vern> > The problem is how to determine the current position in flex active > buffer when a rule is matched.... You will need to keep track of this explicitly, such as by redefining YY_USER_ACTION to count the number of characters matched. The latest flex release, by the way, is 2.5.4, available from ftp.ee.lbl.gov. Vern </PRE> <H2><A NAME="SEC123" HREF="flex_toc.html#TOC123">unnamed-faq-70</A></H2> <PRE> @verbatim To: Bik.Dhaliwal@bis.org Subject: Re: Flex question In-reply-to: Your message of Mon, 26 Jan 1998 13:05:35 PST. Date: Tue, 27 Jan 1998 22:41:52 PST From: Vern Paxson <vern> > That requirement involves knowing > the character position at which a particular token was matched > in the lexer. The way you have to do this is by explicitly keeping track of where you are in the file, by counting the number of characters scanned for each token (available in yyleng). It may prove convenient to do this by redefining YY_USER_ACTION, as described in the manual. Vern </PRE> <H2><A NAME="SEC124" HREF="flex_toc.html#TOC124">unnamed-faq-71</A></H2> <PRE> @verbatim To: Vladimir Alexiev <vladimir@cs.ualberta.ca> Subject: Re: flex: how to control start condition from parser? In-reply-to: Your message of Mon, 26 Jan 1998 05:50:16 PST. Date: Tue, 27 Jan 1998 22:45:37 PST From: Vern Paxson <vern> > It seems useful for the parser to be able to tell the lexer about such > context dependencies, because then they don't have to be limited to > local or sequential context. One way to do this is to have the parser call a stub routine that's included in the scanner's .l file, and consequently that has access ot BEGIN. The only ugliness is that the parser can't pass in the state it wants, because those aren't visible - but if you don't have many such states, then using a different set of names doesn't seem like to much of a burden. While generating a .h file like you suggests is certainly cleaner, flex development has come to a virtual stand-still :-(, so a workaround like the above is much more pragmatic than waiting for a new feature. Vern </PRE> <H2><A NAME="SEC125" HREF="flex_toc.html#TOC125">unnamed-faq-72</A></H2> <PRE> @verbatim To: Barbara Denny <denny@3com.com> Subject: Re: freebsd flex bug? In-reply-to: Your message of Fri, 30 Jan 1998 12:00:43 PST. Date: Fri, 30 Jan 1998 12:42:32 PST From: Vern Paxson <vern> > lex.yy.c:1996: parse error before `=' This is the key, identifying this error. (It may help to pinpoint it by using flex -L, so it doesn't generate #line directives in its output.) I will bet you heavy money that you have a start condition name that is also a variable name, or something like that; flex spits out #define's for each start condition name, mapping them to a number, so you can wind up with: %x foo %% ... %% void bar() { int foo = 3; } and the penultimate will turn into "int 1 = 3" after C preprocessing, since flex will put "#define foo 1" in the generated scanner. Vern </PRE> <H2><A NAME="SEC126" HREF="flex_toc.html#TOC126">unnamed-faq-73</A></H2> <PRE> @verbatim To: Maurice Petrie <mpetrie@infoscigroup.com> Subject: Re: Lost flex .l file In-reply-to: Your message of Mon, 02 Feb 1998 14:10:01 PST. Date: Mon, 02 Feb 1998 11:15:12 PST From: Vern Paxson <vern> > I am curious as to > whether there is a simple way to backtrack from the generated source to > reproduce the lost list of tokens we are searching on. In theory, it's straight-forward to go from the DFA representation back to a regular-expression representation - the two are isomorphic. In practice, a huge headache, because you have to unpack all the tables back into a single DFA representation, and then write a program to munch on that and translate it into an RE. Sorry for the less-than-happy news ... Vern </PRE> <H2><A NAME="SEC127" HREF="flex_toc.html#TOC127">unnamed-faq-74</A></H2> <PRE> @verbatim To: jimmey@lexis-nexis.com (Jimmey Todd) Subject: Re: Flex performance question In-reply-to: Your message of Thu, 19 Feb 1998 11:01:17 PST. Date: Thu, 19 Feb 1998 08:48:51 PST From: Vern Paxson <vern> > What I have found, is that the smaller the data chunk, the faster the > program executes. This is the opposite of what I expected. Should this be > happening this way? This is exactly what will happen if your input file has embedded NULs. From the man page: A final note: flex is slow when matching NUL's, particularly when a token contains multiple NUL's. It's best to write rules which match short amounts of text if it's anticipated that the text will often include NUL's. So that's the first thing to look for. Vern </PRE> <H2><A NAME="SEC128" HREF="flex_toc.html#TOC128">unnamed-faq-75</A></H2> <PRE> @verbatim To: jimmey@lexis-nexis.com (Jimmey Todd) Subject: Re: Flex performance question In-reply-to: Your message of Thu, 19 Feb 1998 11:01:17 PST. Date: Thu, 19 Feb 1998 15:42:25 PST From: Vern Paxson <vern> So there are several problems. First, to go fast, you want to match as much text as possible, which your scanners don't in the case that what they're scanning is *not* a <RN> tag. So you want a rule like: [^<]+ Second, C++ scanners are particularly slow if they're interactive, which they are by default. Using -B speeds it up by a factor of 3-4 on my workstation. Third, C++ scanners that use the istream interface are slow, because of how poorly implemented istream's are. I built two versions of the following scanner: %% .*\n .* %% and the C version inhales a 2.5MB file on my workstation in 0.8 seconds. The C++ istream version, using -B, takes 3.8 seconds. Vern </PRE> <H2><A NAME="SEC129" HREF="flex_toc.html#TOC129">unnamed-faq-76</A></H2> <PRE> @verbatim To: "Frescatore, David (CRD, TAD)" <frescatore@exc01crdge.crd.ge.com> Subject: Re: FLEX 2.5 & THE YEAR 2000 In-reply-to: Your message of Wed, 03 Jun 1998 11:26:22 PDT. Date: Wed, 03 Jun 1998 10:22:26 PDT From: Vern Paxson <vern> > I am researching the Y2K problem with General Electric R&D > and need to know if there are any known issues concerning > the above mentioned software and Y2K regardless of version. There shouldn't be, all it ever does with the date is ask the system for it and then print it out. Vern </PRE> <H2><A NAME="SEC130" HREF="flex_toc.html#TOC130">unnamed-faq-77</A></H2> <PRE> @verbatim To: "Hans Dermot Doran" <htd@ibhdoran.com> Subject: Re: flex problem In-reply-to: Your message of Wed, 15 Jul 1998 21:30:13 PDT. Date: Tue, 21 Jul 1998 14:23:34 PDT From: Vern Paxson <vern> > To overcome this, I gets() the stdin into a string and lex the string. The > string is lexed OK except that the end of string isn't lexed properly > (yy_scan_string()), that is the lexer dosn't recognise the end of string. Flex doesn't contain mechanisms for recognizing buffer endpoints. But if you use fgets instead (which you should anyway, to protect against buffer overflows), then the final \n will be preserved in the string, and you can scan that in order to find the end of the string. Vern </PRE> <H2><A NAME="SEC131" HREF="flex_toc.html#TOC131">unnamed-faq-78</A></H2> <PRE> @verbatim To: soumen@almaden.ibm.com Subject: Re: Flex++ 2.5.3 instance member vs. static member In-reply-to: Your message of Mon, 27 Jul 1998 02:10:04 PDT. Date: Tue, 28 Jul 1998 01:10:34 PDT From: Vern Paxson <vern> > %{ > int mylineno = 0; > %} > ws [ \t]+ > alpha [A-Za-z] > dig [0-9] > %% > > Now you'd expect mylineno to be a member of each instance of class > yyFlexLexer, but is this the case? A look at the lex.yy.cc file seems to > indicate otherwise; unless I am missing something the declaration of > mylineno seems to be outside any class scope. > > How will this work if I want to run a multi-threaded application with each > thread creating a FlexLexer instance? Derive your own subclass and make mylineno a member variable of it. Vern </PRE> <H2><A NAME="SEC132" HREF="flex_toc.html#TOC132">unnamed-faq-79</A></H2> <PRE> @verbatim To: Adoram Rogel <adoram@hybridge.com> Subject: Re: More than 32K states change hangs In-reply-to: Your message of Tue, 04 Aug 1998 16:55:39 PDT. Date: Tue, 04 Aug 1998 22:28:45 PDT From: Vern Paxson <vern> > Vern Paxson, > > I followed your advice, posted on Usenet bu you, and emailed to me > personally by you, on how to overcome the 32K states limit. I'm running > on Linux machines. > I took the full source of version 2.5.4 and did the following changes in > flexdef.h: > #define JAMSTATE -327660 > #define MAXIMUM_MNS 319990 > #define BAD_SUBSCRIPT -327670 > #define MAX_SHORT 327000 > > and compiled. > All looked fine, including check and bigcheck, so I installed. Hmmm, you shouldn't increase MAX_SHORT, though looking through my email archives I see that I did indeed recommend doing so. Try setting it back to 32700; that should suffice that you no longer need -Ca. If it still hangs, then the interesting question is - where? > Compiling the same hanged program with a out-of-the-box (RedHat 4.2 > distribution of Linux) > flex 2.5.4 binary works. Since Linux comes with source code, you should diff it against what you have to see what problems they missed. > Should I always compile with the -Ca option now ? even short and simple > filters ? No, definitely not. It's meant to be for those situations where you absolutely must squeeze every last cycle out of your scanner. Vern </PRE> <H2><A NAME="SEC133" HREF="flex_toc.html#TOC133">unnamed-faq-80</A></H2> <PRE> @verbatim To: "Schmackpfeffer, Craig" <Craig.Schmackpfeffer@usa.xerox.com> Subject: Re: flex output for static code portion In-reply-to: Your message of Tue, 11 Aug 1998 11:55:30 PDT. Date: Mon, 17 Aug 1998 23:57:42 PDT From: Vern Paxson <vern> > I would like to use flex under the hood to generate a binary file > containing the data structures that control the parse. This has been on the wish-list for a long time. In principle it's straight-forward - you redirect mkdata() et al's I/O to another file, and modify the skeleton to have a start-up function that slurps these into dynamic arrays. The concerns are (1) the scanner generation code is hairy and full of corner cases, so it's easy to get surprised when going down this path :-( ; and (2) being careful about buffering so that when the tables change you make sure the scanner starts in the correct state and reading at the right point in the input file. > I was wondering if you know of anyone who has used flex in this way. I don't - but it seems like a reasonable project to undertake (unlike numerous other flex tweaks :-). Vern </PRE> <H2><A NAME="SEC134" HREF="flex_toc.html#TOC134">unnamed-faq-81</A></H2> <PRE> @verbatim Received: from 131.173.17.11 (131.173.17.11 [131.173.17.11]) by ee.lbl.gov (8.9.1/8.9.1) with ESMTP id AAA03838 for <vern@ee.lbl.gov>; Thu, 20 Aug 1998 00:47:57 -0700 (PDT) Received: from hal.cl-ki.uni-osnabrueck.de (hal.cl-ki.Uni-Osnabrueck.DE [131.173.141.2]) by deimos.rz.uni-osnabrueck.de (8.8.7/8.8.8) with ESMTP id JAA34694 for <vern@ee.lbl.gov>; Thu, 20 Aug 1998 09:47:55 +0200 Received: (from georg@localhost) by hal.cl-ki.uni-osnabrueck.de (8.6.12/8.6.12) id JAA34834 for vern@ee.lbl.gov; Thu, 20 Aug 1998 09:47:54 +0200 From: Georg Rehm <georg@hal.cl-ki.uni-osnabrueck.de> Message-Id: <199808200747.JAA34834@hal.cl-ki.uni-osnabrueck.de> Subject: "flex scanner push-back overflow" To: vern@ee.lbl.gov Date: Thu, 20 Aug 1998 09:47:54 +0200 (MEST) Reply-To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE X-NoJunk: Do NOT send commercial mail, spam or ads to this address! X-URL: http://www.cl-ki.uni-osnabrueck.de/~georg/ X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hi Vern, Yesterday, I encountered a strange problem: I use the macro processor m4 to include some lengthy lists into a .l file. Following is a flex macro definition that causes some serious pain in my neck: AUTHOR ("A. Boucard / L. Boucard"|"A. Dastarac / M. Levent"|"A.Boucaud / L.Boucaud"|"Abderrahim Lamchichi"|"Achmat Dangor"|"Adeline Toullier"|"Adewale Maja-Pearce"|"Ahmed Ziri"|"Akram Ellyas"|"Alain Bihr"|"Alain Gresh"|"Alain Guillemoles"|"Alain Joxe"|"Alain Morice"|"Alain Renon"|"Alain Zecchini"|"Albert Memmi"|"Alberto Manguel"|"Alex De Waal"|"Alfonso Artico"| [...]) The complete list contains about 10kB. When I try to "flex" this file (on a Solaris 2.6 machine, using a modified flex 2.5.4 (I only increased some of the predefined values in flexdefs.h) I get the error: myflex/flex -8 sentag.tmp.l flex scanner push-back overflow When I remove the slashes in the macro definition everything works fine. As I understand it, the double quotes escape the slash-character so it really means "/" and not "trailing context". Furthermore, I tried to escape the slashes with backslashes, but with no use, the same error message appeared when flexing the code. Do you have an idea what's going on here? Greetings from Germany, Georg -- Georg Rehm georg@cl-ki.uni-osnabrueck.de Institute for Semantic Information Processing, University of Osnabrueck, FRG </PRE> <H2><A NAME="SEC135" HREF="flex_toc.html#TOC135">unnamed-faq-82</A></H2> <PRE> @verbatim To: Georg.Rehm@CL-KI.Uni-Osnabrueck.DE Subject: Re: "flex scanner push-back overflow" In-reply-to: Your message of Thu, 20 Aug 1998 09:47:54 PDT. Date: Thu, 20 Aug 1998 07:05:35 PDT From: Vern Paxson <vern> > myflex/flex -8 sentag.tmp.l > flex scanner push-back overflow Flex itself uses a flex scanner. That scanner is running out of buffer space when it tries to unput() the humongous macro you've defined. When you remove the '/'s, you make it small enough so that it fits in the buffer; removing spaces would do the same thing. The fix is to either rethink how come you're using such a big macro and perhaps there's another/better way to do it; or to rebuild flex's own scan.c with a larger value for #define YY_BUF_SIZE 16384 - Vern </PRE> <H2><A NAME="SEC136" HREF="flex_toc.html#TOC136">unnamed-faq-83</A></H2> <PRE> @verbatim To: Jan Kort <jan@research.techforce.nl> Subject: Re: Flex In-reply-to: Your message of Fri, 04 Sep 1998 12:18:43 +0200. Date: Sat, 05 Sep 1998 00:59:49 PDT From: Vern Paxson <vern> > %% > > "TEST1\n" { fprintf(stderr, "TEST1\n"); yyless(5); } > ^\n { fprintf(stderr, "empty line\n"); } > . { } > \n { fprintf(stderr, "new line\n"); } > > %% > -- input --------------------------------------- > TEST1 > -- output -------------------------------------- > TEST1 > empty line > ------------------------------------------------ IMHO, it's not clear whether or not this is in fact a bug. It depends on whether you view yyless() as backing up in the input stream, or as pushing new characters onto the beginning of the input stream. Flex interprets it as the latter (for implementation convenience, I'll admit), and so considers the newline as in fact matching at the beginning of a line, as after all the last token scanned an entire line and so the scanner is now at the beginning of a new line. I agree that this is counter-intuitive for yyless(), given its functional description (it's less so for unput(), depending on whether you're unput()'ing new text or scanned text). But I don't plan to change it any time soon, as it's a pain to do so. Consequently, you do indeed need to use yy_set_bol() and YY_AT_BOL() to tweak your scanner into the behavior you desire. Sorry for the less-than-completely-satisfactory answer. Vern </PRE> <H2><A NAME="SEC137" HREF="flex_toc.html#TOC137">unnamed-faq-84</A></H2> <PRE> @verbatim To: Patrick Krusenotto <krusenot@mac-info-link.de> Subject: Re: Problems with restarting flex-2.5.2-generated scanner In-reply-to: Your message of Thu, 24 Sep 1998 10:14:07 PDT. Date: Thu, 24 Sep 1998 23:28:43 PDT From: Vern Paxson <vern> > I am using flex-2.5.2 and bison 1.25 for Solaris and I am desperately > trying to make my scanner restart with a new file after my parser stops > with a parse error. When my compiler restarts, the parser always > receives the token after the token (in the old file!) that caused the > parser error. I suspect the problem is that your parser has read ahead in order to attempt to resolve an ambiguity, and when it's restarted it picks up with that token rather than reading a fresh one. If you're using yacc, then the special "error" production can sometimes be used to consume tokens in an attempt to get the parser into a consistent state. Vern </PRE> <H2><A NAME="SEC138" HREF="flex_toc.html#TOC138">unnamed-faq-85</A></H2> <PRE> @verbatim To: Henric Jungheim <junghelh@pe-nelson.com> Subject: Re: flex 2.5.4a In-reply-to: Your message of Tue, 27 Oct 1998 16:41:42 PST. Date: Tue, 27 Oct 1998 16:50:14 PST From: Vern Paxson <vern> > This brings up a feature request: How about a command line > option to specify the filename when reading from stdin? That way one > doesn't need to create a temporary file in order to get the "#line" > directives to make sense. Use -o combined with -t (per the man page description of -o). > P.S., Is there any simple way to use non-blocking IO to parse multiple > streams? Simple, no. One approach might be to return a magic character on EWOULDBLOCK and have a rule .*<magic-character> // put back .*, eat magic character This is off the top of my head, not sure it'll work. Vern </PRE> <H2><A NAME="SEC139" HREF="flex_toc.html#TOC139">unnamed-faq-86</A></H2> <PRE> @verbatim To: "Repko, Billy D" <billy.d.repko@intel.com> Subject: Re: Compiling scanners In-reply-to: Your message of Wed, 13 Jan 1999 10:52:47 PST. Date: Thu, 14 Jan 1999 00:25:30 PST From: Vern Paxson <vern> > It appears that maybe it cannot find the lfl library. The Makefile in the distribution builds it, so you should have it. It's exceedingly trivial, just a main() that calls yylex() and a yyrap() that always returns 1. > %% > \n ++num_lines; ++num_chars; > . ++num_chars; You can't indent your rules like this - that's where the errors are coming from. Flex copies indented text to the output file, it's how you do things like int num_lines_seen = 0; to declare local variables. Vern </PRE> <H2><A NAME="SEC140" HREF="flex_toc.html#TOC140">unnamed-faq-87</A></H2> <PRE> @verbatim To: Erick Branderhorst <Erick.Branderhorst@asml.nl> Subject: Re: flex input buffer In-reply-to: Your message of Tue, 09 Feb 1999 13:53:46 PST. Date: Tue, 09 Feb 1999 21:03:37 PST From: Vern Paxson <vern> > In the flex.skl file the size of the default input buffers is set. Can you > explain why this size is set and why it is such a high number. It's large to optimize performance when scanning large files. You can safely make it a lot lower if needed. Vern </PRE> <H2><A NAME="SEC141" HREF="flex_toc.html#TOC141">unnamed-faq-88</A></H2> <PRE> @verbatim To: "Guido Minnen" <guidomi@cogs.susx.ac.uk> Subject: Re: Flex error message In-reply-to: Your message of Wed, 24 Feb 1999 15:31:46 PST. Date: Thu, 25 Feb 1999 00:11:31 PST From: Vern Paxson <vern> > I'm extending a larger scanner written in Flex and I keep running into > problems. More specifically, I get the error message: > "flex: input rules are too complicated (>= 32000 NFA states)" Increase the definitions in flexdef.h for: #define JAMSTATE -32766 /* marks a reference to the state that always j ams */ #define MAXIMUM_MNS 31999 #define BAD_SUBSCRIPT -32767 recompile everything, and it should all work. Vern </PRE> <H2><A NAME="SEC142" HREF="flex_toc.html#TOC142">unnamed-faq-90</A></H2> <PRE> @verbatim To: "Dmitriy Goldobin" <gold@ems.chel.su> Subject: Re: FLEX trouble In-reply-to: Your message of Mon, 31 May 1999 18:44:49 PDT. Date: Tue, 01 Jun 1999 00:15:07 PDT From: Vern Paxson <vern> > I have a trouble with FLEX. Why rule "/*".*"*/" work properly,=20 > but rule "/*"(.|\n)*"*/" don't work ? The second of these will have to scan the entire input stream (because "(.|\n)*" matches an arbitrary amount of any text) in order to see if it ends with "*/", terminating the comment. That potentially will overflow the input buffer. > More complex rule "/*"([^*]|(\*/[^/]))*"*/ give an error > 'unrecognized rule'. You can't use the '/' operator inside parentheses. It's not clear what "(a/b)*" actually means. > I now use workaround with state <comment>, but single-rule is > better, i think. Single-rule is nice but will always have the problem of either setting restrictions on comments (like not allowing multi-line comments) and/or running the risk of consuming the entire input stream, as noted above. Vern </PRE> <H2><A NAME="SEC143" HREF="flex_toc.html#TOC143">unnamed-faq-91</A></H2> <PRE> @verbatim Received: from mc-qout4.whowhere.com (mc-qout4.whowhere.com [209.185.123.18]) by ee.lbl.gov (8.9.3/8.9.3) with SMTP id IAA05100 for <vern@ee.lbl.gov>; Tue, 15 Jun 1999 08:56:06 -0700 (PDT) Received: from Unknown/Local ([?.?.?.?]) by my-deja.com; Tue Jun 15 08:55:43 1999 To: vern@ee.lbl.gov Date: Tue, 15 Jun 1999 08:55:43 -0700 From: "Aki Niimura" <neko@my-deja.com> Message-ID: <KNONDOHDOBGAEAAA@my-deja.com> Mime-Version: 1.0 Cc: X-Sent-Mail: on Reply-To: X-Mailer: MailCity Service Subject: A question on flex C++ scanner X-Sender-Ip: 12.72.207.61 Organization: My Deja Email (http://www.my-deja.com:80) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Dear Dr. Paxon, I have been using flex for years. It works very well on many projects. Most case, I used it to generate a scanner on C language. However, one project I needed to generate a scanner on C++ lanuage. Thanks to your enhancement, flex did the job. Currently, I'm working on enhancing my previous project. I need to deal with multiple input streams (recursive inclusion) in this scanner (C++). I did similar thing for another scanner (C) as you explained in your documentation. The generated scanner (C++) has necessary methods: - switch_to_buffer(struct yy_buffer_state *b) - yy_create_buffer(istream *is, int sz) - yy_delete_buffer(struct yy_buffer_state *b) However, I couldn't figure out how to access current buffer (yy_current_buffer). yy_current_buffer is a protected member of yyFlexLexer. I can't access it directly. Then, I thought yy_create_buffer() with is = 0 might return current stream buffer. But it seems not as far as I checked the source. (flex 2.5.4) I went through the Web in addition to Flex documentation. However, it hasn't been successful, so far. It is not my intention to bother you, but, can you comment about how to obtain the current stream buffer? Your response would be highly appreciated. Best regards, Aki Niimura --== Sent via Deja.com http://www.deja.com/ ==-- Share what you know. Learn what you don't. </PRE> <H2><A NAME="SEC144" HREF="flex_toc.html#TOC144">unnamed-faq-92</A></H2> <PRE> @verbatim To: neko@my-deja.com Subject: Re: A question on flex C++ scanner In-reply-to: Your message of Tue, 15 Jun 1999 08:55:43 PDT. Date: Tue, 15 Jun 1999 09:04:24 PDT From: Vern Paxson <vern> > However, I couldn't figure out how to access current > buffer (yy_current_buffer). Derive your own subclass from yyFlexLexer. Vern </PRE> <H2><A NAME="SEC145" HREF="flex_toc.html#TOC145">unnamed-faq-93</A></H2> <PRE> @verbatim To: "Stones, Darren" <Darren.Stones@nectech.co.uk> Subject: Re: You're the man to see? In-reply-to: Your message of Wed, 23 Jun 1999 11:10:29 PDT. Date: Wed, 23 Jun 1999 09:01:40 PDT From: Vern Paxson <vern> > I hope you can help me. I am using Flex and Bison to produce an interpreted > language. However all goes well until I try to implement an IF statement or > a WHILE. I cannot get this to work as the parser parses all the conditions > eg. the TRUE and FALSE conditons to check for a rule match. So I cannot > make a decision!! You need to use the parser to build a parse tree (= abstract syntax trwee), and when that's all done you recursively evaluate the tree, binding variables to values at that time. Vern </PRE> <H2><A NAME="SEC146" HREF="flex_toc.html#TOC146">unnamed-faq-94</A></H2> <PRE> @verbatim To: Petr Danecek <petr@ics.cas.cz> Subject: Re: flex - question In-reply-to: Your message of Mon, 28 Jun 1999 19:21:41 PDT. Date: Fri, 02 Jul 1999 16:52:13 PDT From: Vern Paxson <vern> > file, it takes an enormous amount of time. It is funny, because the > source code has only 12 rules!!! I think it looks like an exponencial > growth. Right, that's the problem - some patterns (those with a lot of ambiguity, where yours has because at any given time the scanner can be in the middle of all sorts of combinations of the different rules) blow up exponentially. For your rules, there is an easy fix. Change the ".*" that comes fater the directory name to "[^ ]*". With that in place, the rules are no longer nearly so ambiguous, because then once one of the directories has been matched, no other can be matched (since they all require a leading blank). If that's not an acceptable solution, then you can enter a start state to pick up the .*\n after each directory is matched. Also note that for speed, you'll want to add a ".*" rule at the end, otherwise rules that don't match any of the patterns will be matched very slowly, a character at a time. Vern </PRE> <H2><A NAME="SEC147" HREF="flex_toc.html#TOC147">unnamed-faq-95</A></H2> <PRE> @verbatim To: Tielman Koekemoer <tielman@spi.co.za> Subject: Re: Please help. In-reply-to: Your message of Thu, 08 Jul 1999 13:20:37 PDT. Date: Thu, 08 Jul 1999 08:20:39 PDT From: Vern Paxson <vern> > I was hoping you could help me with my problem. > > I tried compiling (gnu)flex on a Solaris 2.4 machine > but when I ran make (after configure) I got an error. > > -------------------------------------------------------------- > gcc -c -I. -I. -g -O parse.c > ./flex -t -p ./scan.l >scan.c > sh: ./flex: not found > *** Error code 1 > make: Fatal error: Command failed for target `scan.c' > ------------------------------------------------------------- > > What's strange to me is that I'm only > trying to install flex now. I then edited the Makefile to > and changed where it says "FLEX = flex" to "FLEX = lex" > ( lex: the native Solaris one ) but then it complains about > the "-p" option. Is there any way I can compile flex without > using flex or lex? > > Thanks so much for your time. You managed to step on the bootstrap sequence, which first copies initscan.c to scan.c in order to build flex. Try fetching a fresh distribution from ftp.ee.lbl.gov. (Or you can first try removing ".bootstrap" and doing a make again.) Vern </PRE> <H2><A NAME="SEC148" HREF="flex_toc.html#TOC148">unnamed-faq-96</A></H2> <PRE> @verbatim To: Tielman Koekemoer <tielman@spi.co.za> Subject: Re: Please help. In-reply-to: Your message of Fri, 09 Jul 1999 09:16:14 PDT. Date: Fri, 09 Jul 1999 00:27:20 PDT From: Vern Paxson <vern> > First I removed .bootstrap (and ran make) - no luck. I downloaded the > software but I still have the same problem. Is there anything else I > could try. Try: cp initscan.c scan.c touch scan.c make scan.o If this last tries to first build scan.c from scan.l using ./flex, then your "make" is broken, in which case compile scan.c to scan.o by hand. Vern </PRE> <H2><A NAME="SEC149" HREF="flex_toc.html#TOC149">unnamed-faq-97</A></H2> <PRE> @verbatim To: Sumanth Kamenani <skamenan@crl.nmsu.edu> Subject: Re: Error In-reply-to: Your message of Mon, 19 Jul 1999 23:08:41 PDT. Date: Tue, 20 Jul 1999 00:18:26 PDT From: Vern Paxson <vern> > I am getting a compilation error. The error is given as "unknown symbol- yylex". The parser relies on calling yylex(), but you're instead using the C++ scanning class, so you need to supply a yylex() "glue" function that calls an instance scanner of the scanner (e.g., "scanner->yylex()"). Vern </PRE> <H2><A NAME="SEC150" HREF="flex_toc.html#TOC150">unnamed-faq-98</A></H2> <PRE> @verbatim To: daniel@synchrods.synchrods.COM (Daniel Senderowicz) Subject: Re: lex In-reply-to: Your message of Mon, 22 Nov 1999 11:19:04 PST. Date: Tue, 23 Nov 1999 15:54:30 PST From: Vern Paxson <vern> Well, your problem is the switch (yybgin-yysvec-1) { /* witchcraft */ at the beginning of lex rules. "witchcraft" == "non-portable". It's assuming knowledge of the AT&T lex's internal variables. For flex, you can probably do the equivalent using a switch on YYSTATE. Vern </PRE> <H2><A NAME="SEC151" HREF="flex_toc.html#TOC151">unnamed-faq-99</A></H2> <PRE> @verbatim To: archow@hss.hns.com Subject: Re: Regarding distribution of flex and yacc based grammars In-reply-to: Your message of Sun, 19 Dec 1999 17:50:24 +0530. Date: Wed, 22 Dec 1999 01:56:24 PST From: Vern Paxson <vern> > When we provide the customer with an object code distribution, is it > necessary for us to provide source > for the generated C files from flex and bison since they are generated by > flex and bison ? For flex, no. I don't know what the current state of this is for bison. > Also, is there any requrirement for us to neccessarily provide source for > the grammar files which are fed into flex and bison ? Again, for flex, no. See the file "COPYING" in the flex distribution for the legalese. Vern </PRE> <H2><A NAME="SEC152" HREF="flex_toc.html#TOC152">unnamed-faq-100</A></H2> <PRE> @verbatim To: Martin Gallwey <gallweym@hyperion.moe.ul.ie> Subject: Re: Flex, and self referencing rules In-reply-to: Your message of Sun, 20 Feb 2000 01:01:21 PST. Date: Sat, 19 Feb 2000 18:33:16 PST From: Vern Paxson <vern> > However, I do not use unput anywhere. I do use self-referencing > rules like this: > > UnaryExpr ({UnionExpr})|("-"{UnaryExpr}) You can't do this - flex is *not* a parser like yacc (which does indeed allow recursion), it is a scanner that's confined to regular expressions. Vern </PRE> <H2><A NAME="SEC153" HREF="flex_toc.html#TOC153">unnamed-faq-101</A></H2> <PRE> @verbatim To: slg3@lehigh.edu (SAMUEL L. GULDEN) Subject: Re: Flex problem In-reply-to: Your message of Thu, 02 Mar 2000 12:29:04 PST. Date: Thu, 02 Mar 2000 23:00:46 PST From: Vern Paxson <vern> If this is exactly your program: > digit [0-9] > digits {digit}+ > whitespace [ \t\n]+ > > %% > "[" { printf("open_brac\n");} > "]" { printf("close_brac\n");} > "+" { printf("addop\n");} > "*" { printf("multop\n");} > {digits} { printf("NUMBER = %s\n", yytext);} > whitespace ; then the problem is that the last rule needs to be "{whitespace}" ! Vern </PRE> <H1><A NAME="SEC154" HREF="flex_toc.html#TOC154">Appendices</A></H1> <H2><A NAME="SEC155" HREF="flex_toc.html#TOC155">Makefiles and Flex</A></H2> <P> <A NAME="IDX463"></A> <P> In this appendix, we provide tips for writing Makefiles to build your scanners. <P> In a traditional build environment, we say that the <TT>`.c'</TT> files are the sources, and the <TT>`.o'</TT> files are the intermediate files. When using <CODE>flex</CODE>, however, the <TT>`.l'</TT> files are the sources, and the generated <TT>`.c'</TT> files (along with the <TT>`.o'</TT> files) are the intermediate files. This requires you to carefully plan your Makefile. <P> Modern @command{make} programs understand that <TT>`foo.l'</TT> is intended to generate <TT>`lex.yy.c'</TT> or <TT>`foo.c'</TT>, and will behave accordingly<A NAME="DOCF3" HREF="flex_foot.html#FOOT3">(3)</A> and GNU @command{automake} are two such programs that provide implicit rules for flex-generated scanners.}. The following Makefile does not explicitly instruct @command{make} how to build <TT>`foo.c'</TT> from <TT>`foo.l'</TT>. Instead, it relies on the implicit rules of the @command{make} program to build the intermediate file, <TT>`scan.c'</TT>: <P> <A NAME="IDX464"></A> <PRE> @verbatim # Basic Makefile -- relies on implicit rules # Creates "myprogram" from "scan.l" and "myprogram.c" # LEX=flex myprogram: scan.o myprogram.o scan.o: scan.l </PRE> <P> For simple cases, the above may be sufficient. For other cases, you may have to explicitly instruct @command{make} how to build your scanner. The following is an example of a Makefile containing explicit rules: <P> <A NAME="IDX465"></A> <PRE> @verbatim # Basic Makefile -- provides explicit rules # Creates "myprogram" from "scan.l" and "myprogram.c" # LEX=flex myprogram: scan.o myprogram.o $(CC) -o $ $(LDFLAGS) $^ myprogram.o: myprogram.c $(CC) $(CPPFLAGS) $(CFLAGS) -o $ -c $^ scan.o: scan.c $(CC) $(CPPFLAGS) $(CFLAGS) -o $ -c $^ scan.c: scan.l $(LEX) $(LFLAGS) -o $ $^ clean: $(RM) *.o scan.c </PRE> <P> Notice in the above example that <TT>`scan.c'</TT> is in the <CODE>clean</CODE> target. This is because we consider the file <TT>`scan.c'</TT> to be an intermediate file. <P> Finally, we provide a realistic example of a <CODE>flex</CODE> scanner used with a <CODE>bison</CODE> parser<A NAME="DOCF4" HREF="flex_foot.html#FOOT4">(4)</A>. There is a tricky problem we have to deal with. Since a <CODE>flex</CODE> scanner will typically include a header file (e.g., <TT>`y.tab.h'</TT>) generated by the parser, we need to be sure that the header file is generated BEFORE the scanner is compiled. We handle this case in the following example: <PRE> @verbatim # Makefile example -- scanner and parser. # Creates "myprogram" from "scan.l", "parse.y", and "myprogram.c" # LEX = flex YACC = bison -y YFLAGS = -d objects = scan.o parse.o myprogram.o myprogram: $(objects) scan.o: scan.l parse.c parse.o: parse.y myprogram.o: myprogram.c </PRE> <P> In the above example, notice the line, <PRE> @verbatim scan.o: scan.l parse.c </PRE> <P> , which lists the file <TT>`parse.c'</TT> (the generated parser) as a dependency of <TT>`scan.o'</TT>. We want to ensure that the parser is created before the scanner is compiled, and the above line seems to do the trick. Feel free to experiment with your specific implementation of @command{make}. <P> For more details on writing Makefiles, see section `Top' in <CITE>The GNU Make Manual</CITE>. <H2><A NAME="SEC156" HREF="flex_toc.html#TOC156">C Scanners with Bison Parsers</A></H2> <P> <A NAME="IDX466"></A> <A NAME="IDX467"></A> <A NAME="IDX468"></A> <A NAME="IDX469"></A> <A NAME="IDX470"></A> <P> This section describes the <CODE>flex</CODE> features useful when integrating <CODE>flex</CODE> with <CODE>GNU bison</CODE><A NAME="DOCF5" HREF="flex_foot.html#FOOT5">(5)</A>. Skip this section if you are not using <CODE>bison</CODE> with your scanner. Here we discuss only the <CODE>flex</CODE> half of the <CODE>flex</CODE> and <CODE>bison</CODE> pair. We do not discuss <CODE>bison</CODE> in any detail. For more information about generating <CODE>bison</CODE> parsers, see section `Top' in <CITE>the GNU Bison Manual</CITE>. <P> A compatible <CODE>bison</CODE> scanner is generated by declaring <SAMP>`%option bison-bridge'</SAMP> or by supplying <SAMP>`--bison-bridge'</SAMP> when invoking <CODE>flex</CODE> from the command line. This instructs <CODE>flex</CODE> that the macros <CODE>yylval</CODE> and <CODE>yylloc</CODE> may be used. The data types for <CODE>yylval</CODE> and <CODE>yylloc</CODE>, (<CODE>YYSTYPE</CODE> and <CODE>YYLTYPE</CODE>, are typically defined in a header file, included in section 1 of the <CODE>flex</CODE> input file. If <CODE>%option bison-bridge</CODE> is specified, <CODE>flex</CODE> provides support for the functions <CODE>yyget_lval</CODE>, <CODE>yyset_lval</CODE>, <CODE>yyget_lloc</CODE>, and <CODE>yyset_lloc</CODE>, defined below, and the corresponding macros <CODE>yylval</CODE> and <CODE>yylloc</CODE>, for use within actions. <P> <DL> <DT><U>Function:</U> YYSTYPE* <B>yyget_lval</B> <I>( yyscan_t scanner )</I> <DD><A NAME="IDX471"></A> </DL> <DL> <DT><U>Function:</U> YYLTYPE* <B>yyget_lloc</B> <I>( yyscan_t scanner )</I> <DD><A NAME="IDX472"></A> </DL> <P> <DL> <DT><U>Function:</U> void <B>yyset_lval</B> <I>( YYSTYPE* lvalp, yyscan_t scanner )</I> <DD><A NAME="IDX473"></A> </DL> <DL> <DT><U>Function:</U> void <B>yyset_lloc</B> <I>( YYLTYPE* llocp, yyscan_t scanner )</I> <DD><A NAME="IDX474"></A> </DL> <P> Where yyscan_t is defined in the reentrant scanner <A NAME="DOCF6" HREF="flex_foot.html#FOOT6">(6)</A>. Accordingly, the declaration of yylex becomes one of the following: <P> <A NAME="IDX475"></A> <PRE> @verbatim int yylex ( YYSTYPE * lvalp, yyscan_t scanner ); int yylex ( YYSTYPE * lvalp, YYLTYPE * llocp, yyscan_t scanner ); </PRE> <P> Note that the macros <CODE>yylval</CODE> and <CODE>yylloc</CODE> evaluate to pointers. Support for <CODE>yylloc</CODE> is optional in <CODE>bison</CODE>, so it is optional in <CODE>flex</CODE> as well. This support is automatically handled by <CODE>flex</CODE>. Specifically, support for <CODE>yyloc</CODE> is only present in a <CODE>flex</CODE> scanner if the preprocessor symbol <CODE>YYLTYPE</CODE> is defined. The following is an example of a <CODE>flex</CODE> scanner that is compatible with <CODE>bison</CODE>. <P> <A NAME="IDX476"></A> <PRE> @verbatim /* Scanner for "C" assignment statements... sort of. */ %{ #include "y.tab.h" /* Generated by bison. */ %} %option reentrant-bison % [[:digit:]]+ { yylval->num = atoi(yytext); return NUMBER;} [[:alnum:]]+ { yylval->str = strdup(yytext); return STRING;} "="|";" { return yytext[0];} . {} % </PRE> <P> As you can see, there really is no magic here. We just use <CODE>yylval</CODE> as we would any other variable. The data type of <CODE>yylval</CODE> is generated by <CODE>bison</CODE>, and included in the file <TT>`y.tab.h'</TT>. Here is the corresponding <CODE>bison</CODE> parser: <P> <A NAME="IDX477"></A> <PRE> @verbatim /* Parser to convert "C" assignments to lisp. */ %{ /* Pass the argument to yyparse through to yylex. */ #define YYPARSE_PARAM scanner #define YYLEX_PARAM scanner %} %pure_parser %union { int num; char* str; } %token <str> STRING %token <num> NUMBER %% assignment: STRING '=' NUMBER ';' { printf( "(setf %s %d)", $1, $3 ); } ; </PRE> <H1><A NAME="SEC157" HREF="flex_toc.html#TOC157">Indices</A></H1> <H2><A NAME="SEC158" HREF="flex_toc.html#TOC158">Concept Index</A></H2> <P> Jump to: <A HREF="#cindex_$">$</A> - <A HREF="#cindex_%">%</A> - <A HREF="#cindex_<"><</A> - <A HREF="#cindex_[">[</A> - <A HREF="#cindex_^">^</A> - <A HREF="#cindex_a">a</A> - <A HREF="#cindex_b">b</A> - <A HREF="#cindex_c">c</A> - <A HREF="#cindex_d">d</A> - <A HREF="#cindex_e">e</A> - <A HREF="#cindex_f">f</A> - <A HREF="#cindex_g">g</A> - <A HREF="#cindex_h">h</A> - <A HREF="#cindex_i">i</A> - <A HREF="#cindex_k">k</A> - <A HREF="#cindex_l">l</A> - <A HREF="#cindex_m">m</A> - <A HREF="#cindex_n">n</A> - <A HREF="#cindex_o">o</A> - <A HREF="#cindex_p">p</A> - <A HREF="#cindex_r">r</A> - <A HREF="#cindex_s">s</A> - <A HREF="#cindex_t">t</A> - <A HREF="#cindex_u">u</A> - <A HREF="#cindex_v">v</A> - <A HREF="#cindex_w">w</A> - <A HREF="#cindex_y">y</A> <P> <H2><A NAME="cindex_$">$</A></H2> <DIR> <LI><A HREF="flex.html#IDX74">$ as normal character in patterns</A> </DIR> <H2><A NAME="cindex_%">%</A></H2> <DIR> <LI><A HREF="flex.html#IDX93">%array, advantages of</A> <LI><A HREF="flex.html#IDX90">%array, use of</A> <LI><A HREF="flex.html#IDX94">%array, with C++</A> <LI><A HREF="flex.html#IDX124">%pointer, caveat with unput()</A> <LI><A HREF="flex.html#IDX91">%pointer, use of</A> <LI><A HREF="flex.html#IDX24">%{ and %}, in Definitions Section</A> <LI><A HREF="flex.html#IDX99">%{ and %}, in Rules Section</A> </DIR> <H2><A NAME="cindex_<"><</A></H2> <DIR> <LI><A HREF="flex.html#IDX199"><<EOF>>, use of</A> </DIR> <H2><A NAME="cindex_[">[</A></H2> <DIR> <LI><A HREF="flex.html#IDX39">[] in patterns</A> </DIR> <H2><A NAME="cindex_^">^</A></H2> <DIR> <LI><A HREF="flex.html#IDX73">^ as non-special character in patterns</A> </DIR> <H2><A NAME="cindex_a">a</A></H2> <DIR> <LI><A HREF="flex.html#IDX402">accessor functions, use of</A> <LI><A HREF="flex.html#IDX101">actions, embedded C strings</A> <LI><A HREF="flex.html#IDX95">actions, explanation</A> <LI><A HREF="flex.html#IDX209">actions, redefining YY_BREAK</A> <LI><A HREF="flex.html#IDX100">actions, use of { and }</A> <LI><A HREF="flex.html#IDX18">aliases, how to define</A> <LI><A HREF="flex.html#IDX227">arguments, command-line</A> <LI><A HREF="flex.html#IDX213">array, default size for yytext</A> </DIR> <H2><A NAME="cindex_b">b</A></H2> <DIR> <LI><A HREF="flex.html#IDX356">backing up, eliminating</A> <LI><A HREF="flex.html#IDX358">backing up, eliminating by adding error rules</A> <LI><A HREF="flex.html#IDX359">backing up, eliminating with catch-all rule</A> <LI><A HREF="flex.html#IDX355">backing up, example of eliminating</A> <LI><A HREF="flex.html#IDX109">BEGIN</A> <LI><A HREF="flex.html#IDX156">BEGIN, explanation</A> <LI><A HREF="flex.html#IDX54">beginning of line, in patterns</A> <LI><A HREF="flex.html#IDX466">bison, bridging with flex</A> <LI><A HREF="flex.html#IDX477">bison, parser</A> <LI><A HREF="flex.html#IDX476">bison, scanner to be called from bison</A> <LI><A HREF="flex.html#IDX207">BOL, checking the BOL flag</A> <LI><A HREF="flex.html#IDX55">BOL, in patterns</A> <LI><A HREF="flex.html#IDX205">BOL, setting it</A> <LI><A HREF="flex.html#IDX45">braces in patterns</A> <LI><A HREF="flex.html#IDX3">bugs, reporting</A> </DIR> <H2><A NAME="cindex_c">c</A></H2> <DIR> <LI><A HREF="flex.html#IDX26">C code in flex input</A> <LI><A HREF="flex.html#IDX367">C++</A> <LI><A HREF="flex.html#IDX215">C++ and %array</A> <LI><A HREF="flex.html#IDX462">C++ I/O, customizing</A> <LI><A HREF="flex.html#IDX388">C++ scanners, including multiple scanners</A> <LI><A HREF="flex.html#IDX384">C++ scanners, use of</A> <LI><A HREF="flex.html#IDX365">c++, experimental form of scanner class</A> <LI><A HREF="flex.html#IDX385">C++, multiple different scanners</A> <LI><A HREF="flex.html#IDX69">case-insensitive, effect on character classes</A> <LI><A HREF="flex.html#IDX65">character classes in patterns</A> <LI><A HREF="flex.html#IDX40">character classes in patterns, syntax of</A> <LI><A HREF="flex.html#IDX67">character classes, equivalence of</A> <LI><A HREF="flex.html#IDX184">clearing an input buffer</A> <LI><A HREF="flex.html#IDX225">command-line options</A> <LI><A HREF="flex.html#IDX23">comments in flex input</A> <LI><A HREF="flex.html#IDX34">comments in the input</A> <LI><A HREF="flex.html#IDX129">comments, example of discarding</A> <LI><A HREF="flex.html#IDX161">comments, example of scanning C comments</A> <LI><A HREF="flex.html#IDX32">comments, in rules section</A> <LI><A HREF="flex.html#IDX31">comments, syntax of</A> <LI><A HREF="flex.html#IDX33">comments, valid uses of</A> <LI><A HREF="flex.html#IDX103">commments, in actions</A> <LI><A HREF="flex.html#IDX98">compressing whitespace</A> <LI><A HREF="flex.html#IDX52">concatenation, in patterns</A> <LI><A HREF="flex.html#IDX1">copyright of flex</A> <LI><A HREF="flex.html#IDX9">counting characters and lines</A> <LI><A HREF="flex.html#IDX461">customizing I/O in C++ scanners</A> </DIR> <H2><A NAME="cindex_d">d</A></H2> <DIR> <LI><A HREF="flex.html#IDX7">default rule</A>, <A HREF="flex.html#IDX87">default rule</A> <LI><A HREF="flex.html#IDX21">defining pattern aliases</A> <LI><A HREF="flex.html#IDX17">Definitions, in flex input</A> <LI><A HREF="flex.html#IDX96">deleting lines from input</A> <LI><A HREF="flex.html#IDX130">discarding C comments</A> <LI><A HREF="flex.html#IDX2">distributing flex</A> </DIR> <H2><A NAME="cindex_e">e</A></H2> <DIR> <LI><A HREF="flex.html#IDX108">ECHO, explanation</A> <LI><A HREF="flex.html#IDX25">embedding C code in flex input</A> <LI><A HREF="flex.html#IDX60">end of file, in patterns</A> <LI><A HREF="flex.html#IDX70">end of line, in negated character classes</A> <LI><A HREF="flex.html#IDX56">end of line, in patterns</A> <LI><A HREF="flex.html#IDX142">EOF and yyrestart()</A> <LI><A HREF="flex.html#IDX61">EOF in patterns, syntax of</A> <LI><A HREF="flex.html#IDX189">EOF, example using multiple input buffers</A> <LI><A HREF="flex.html#IDX197">EOF, explanation</A> <LI><A HREF="flex.html#IDX127">EOF, pushing back</A> <LI><A HREF="flex.html#IDX71">EOL, in negated character classes</A> <LI><A HREF="flex.html#IDX57">EOL, in patterns</A> <LI><A HREF="flex.html#IDX425">error messages, end of buffer missed</A> <LI><A HREF="flex.html#IDX452">error reporting, diagnostic messages</A> <LI><A HREF="flex.html#IDX382">error reporting, in C++</A> <LI><A HREF="flex.html#IDX357">error rules, to eliminate backing up</A> <LI><A HREF="flex.html#IDX49">escape sequences in patterns, syntax of</A> <LI><A HREF="flex.html#IDX135">exiting with yyterminate()</A> <LI><A HREF="flex.html#IDX366">experimental form of c++ scanner class</A> <LI><A HREF="flex.html#IDX170">extended scope of start conditions</A> </DIR> <H2><A NAME="cindex_f">f</A></H2> <DIR> <LI><A HREF="flex.html#IDX13">file format</A> <LI><A HREF="flex.html#IDX451">file format, serialized tables</A> <LI><A HREF="flex.html#IDX185">flushing an input buffer</A> <LI><A HREF="flex.html#IDX132">flushing the internal buffer</A> <LI><A HREF="flex.html#IDX11">format of flex input</A> <LI><A HREF="flex.html#IDX15">format of input file</A> <LI><A HREF="flex.html#IDX446">freeing tables</A> </DIR> <H2><A NAME="cindex_g">g</A></H2> <DIR> <LI><A HREF="flex.html#IDX166">getting current start state with YY_START</A> </DIR> <H2><A NAME="cindex_h">h</A></H2> <DIR> <LI><A HREF="flex.html#IDX136">halting with yyterminate()</A> <LI><A HREF="flex.html#IDX190">handling include files with multiple input buffers</A> <LI><A HREF="flex.html#IDX387">header files, with C++</A> </DIR> <H2><A NAME="cindex_i">i</A></H2> <DIR> <LI><A HREF="flex.html#IDX386">include files, with C++</A> <LI><A HREF="flex.html#IDX16">input file, Definitions section</A> <LI><A HREF="flex.html#IDX27">input file, Rules Section</A> <LI><A HREF="flex.html#IDX29">input file, user code Section</A> <LI><A HREF="flex.html#IDX131">input(), and C++</A> <LI><A HREF="flex.html#IDX128">input(), explanation</A> <LI><A HREF="flex.html#IDX12">input, format of</A> <LI><A HREF="flex.html#IDX79">input, matching</A> </DIR> <H2><A NAME="cindex_k">k</A></H2> <DIR> <LI><A HREF="flex.html#IDX362">keywords, for performance</A> </DIR> <H2><A NAME="cindex_l">l</A></H2> <DIR> <LI><A HREF="flex.html#IDX424">lex (traditional) and POSIX</A> <LI><A HREF="flex.html#IDX458">LexerInput, overriding</A> <LI><A HREF="flex.html#IDX457">LexerOutput, overriding</A> <LI><A HREF="flex.html#IDX455">limitations of flex</A> <LI><A HREF="flex.html#IDX47">literal text in patterns, syntax of</A> <LI><A HREF="flex.html#IDX444">loading tables at runtime</A> </DIR> <H2><A NAME="cindex_m">m</A></H2> <DIR> <LI><A HREF="flex.html#IDX464">Makefile, example of implicit rules</A> <LI><A HREF="flex.html#IDX465">Makefile, explicit example</A> <LI><A HREF="flex.html#IDX463">Makefile, syntax</A> <LI><A HREF="flex.html#IDX168">matching C-style double-quoted strings</A> <LI><A HREF="flex.html#IDX81">matching, and trailing context</A> <LI><A HREF="flex.html#IDX82">matching, length of</A> <LI><A HREF="flex.html#IDX83">matching, multiple matches</A> <LI><A HREF="flex.html#IDX368">member functions, C++</A> <LI><A HREF="flex.html#IDX431">memory management</A> <LI><A HREF="flex.html#IDX178">memory, allocating input buffers</A> <LI><A HREF="flex.html#IDX397">memory, considerations for reentrant scanners</A> <LI><A HREF="flex.html#IDX182">memory, deleting input buffers</A> <LI><A HREF="flex.html#IDX176">memory, for start condition stacks</A> <LI><A HREF="flex.html#IDX439">memory, serialized tables</A>, <A HREF="flex.html#IDX447">memory, serialized tables</A> <LI><A HREF="flex.html#IDX369">methods, c++</A> <LI><A HREF="flex.html#IDX88">minimal scanner</A> <LI><A HREF="flex.html#IDX177">multiple input streams</A> </DIR> <H2><A NAME="cindex_n">n</A></H2> <DIR> <LI><A HREF="flex.html#IDX427">name definitions, not POSIX</A> <LI><A HREF="flex.html#IDX44">negating ranges in patterns</A> <LI><A HREF="flex.html#IDX58">newline, matching in patterns</A> <LI><A HREF="flex.html#IDX430">non-POSIX features of flex</A> <LI><A HREF="flex.html#IDX50">NULL character in patterns, syntax of</A> </DIR> <H2><A NAME="cindex_o">o</A></H2> <DIR> <LI><A HREF="flex.html#IDX51">octal characters in patterns</A> <LI><A HREF="flex.html#IDX226">options, command-line</A> <LI><A HREF="flex.html#IDX460">overriding LexerInput</A> <LI><A HREF="flex.html#IDX459">overriding LexerOutput</A> <LI><A HREF="flex.html#IDX436">overriding the memory routines</A> </DIR> <H2><A NAME="cindex_p">p</A></H2> <DIR> <LI><A HREF="flex.html#IDX10">Pascal-like language</A> <LI><A HREF="flex.html#IDX20">pattern aliases, defining</A> <LI><A HREF="flex.html#IDX46">pattern aliases, expansion of</A> <LI><A HREF="flex.html#IDX19">pattern aliases, how to define</A> <LI><A HREF="flex.html#IDX22">pattern aliases, use of</A> <LI><A HREF="flex.html#IDX428">patterns and actions on different lines</A> <LI><A HREF="flex.html#IDX68">patterns, character class equivalence</A> <LI><A HREF="flex.html#IDX77">patterns, end of line</A> <LI><A HREF="flex.html#IDX63">patterns, grouping and precedence</A> <LI><A HREF="flex.html#IDX35">patterns, in rules section</A> <LI><A HREF="flex.html#IDX75">patterns, invalid trailing context</A> <LI><A HREF="flex.html#IDX78">patterns, matching</A> <LI><A HREF="flex.html#IDX62">patterns, precedence of operators</A> <LI><A HREF="flex.html#IDX64">patterns, repetitions with grouping</A> <LI><A HREF="flex.html#IDX76">patterns, special characters treated as non-special</A> <LI><A HREF="flex.html#IDX37">patterns, syntax</A>, <A HREF="flex.html#IDX38">patterns, syntax</A> <LI><A HREF="flex.html#IDX353">patterns, tuning for performance</A> <LI><A HREF="flex.html#IDX66">patterns, valid character classes</A> <LI><A HREF="flex.html#IDX361">performance optimization, matching longer tokens</A> <LI><A HREF="flex.html#IDX364">performance optimization, recognizing keywords</A> <LI><A HREF="flex.html#IDX354">performance, backing up</A> <LI><A HREF="flex.html#IDX349">performance, considerations</A> <LI><A HREF="flex.html#IDX363">performance, using keywords</A> <LI><A HREF="flex.html#IDX423">POSIX and lex</A> <LI><A HREF="flex.html#IDX429">POSIX comp;compliance</A> <LI><A HREF="flex.html#IDX41">POSIX, character classes in patterns, syntax of</A> <LI><A HREF="flex.html#IDX107">preprocessor macros, for use in actions</A> <LI><A HREF="flex.html#IDX122">pushing back characters with unput</A> <LI><A HREF="flex.html#IDX119">pushing back characters with yyless</A> <LI><A HREF="flex.html#IDX126">pushing back EOF</A> </DIR> <H2><A NAME="cindex_r">r</A></H2> <DIR> <LI><A HREF="flex.html#IDX42">ranges in patterns</A> <LI><A HREF="flex.html#IDX43">ranges in patterns, negating</A> <LI><A HREF="flex.html#IDX162">recognizing C comments</A> <LI><A HREF="flex.html#IDX390">reentrant scanners, multiple interleaved scanners</A> <LI><A HREF="flex.html#IDX391">reentrant scanners, recursive invocation</A> <LI><A HREF="flex.html#IDX396">reentrant, accessing flex variables</A> <LI><A HREF="flex.html#IDX401">reentrant, accessor functions</A> <LI><A HREF="flex.html#IDX392">reentrant, API explanation</A> <LI><A HREF="flex.html#IDX394">reentrant, calling functions</A> <LI><A HREF="flex.html#IDX393">reentrant, example of</A> <LI><A HREF="flex.html#IDX389">reentrant, explanation</A> <LI><A HREF="flex.html#IDX403">reentrant, extra data</A> <LI><A HREF="flex.html#IDX398">reentrant, initialization</A> <LI><A HREF="flex.html#IDX36">regular expressions, in patterns</A> <LI><A HREF="flex.html#IDX112">REJECT</A> <LI><A HREF="flex.html#IDX113">REJECT, calling multiple times</A> <LI><A HREF="flex.html#IDX111">REJECT, example</A> <LI><A HREF="flex.html#IDX110">REJECT, explanation</A> <LI><A HREF="flex.html#IDX350">REJECT, performance costs</A> <LI><A HREF="flex.html#IDX4">reporting bugs</A> <LI><A HREF="flex.html#IDX426">restarting the scanner</A> <LI><A HREF="flex.html#IDX143">returning from within an action</A> <LI><A HREF="flex.html#IDX8">rules, default</A> <LI><A HREF="flex.html#IDX28">rules, in flex input</A> </DIR> <H2><A NAME="cindex_s">s</A></H2> <DIR> <LI><A HREF="flex.html#IDX5">scanner, definition of</A> <LI><A HREF="flex.html#IDX14">sections of flex input</A> <LI><A HREF="flex.html#IDX438">serialization</A> <LI><A HREF="flex.html#IDX441">serialization of tables</A> <LI><A HREF="flex.html#IDX442">serialized tables, multiple scanners</A> <LI><A HREF="flex.html#IDX171">stacks, routines for manipulating</A> <LI><A HREF="flex.html#IDX169">start condition, applying to multiple patterns</A> <LI><A HREF="flex.html#IDX148">start conditions, basic</A> <LI><A HREF="flex.html#IDX155">start conditions, behavior of default rule</A> <LI><A HREF="flex.html#IDX152">start conditions, exclusive</A> <LI><A HREF="flex.html#IDX147">start conditions, explanation</A> <LI><A HREF="flex.html#IDX160">start conditions, for different interpretations of same input</A> <LI><A HREF="flex.html#IDX59">start conditions, in patterns</A> <LI><A HREF="flex.html#IDX151">start conditions, inclusive</A> <LI><A HREF="flex.html#IDX150">start conditions, inclusive v.s. exclusive</A> <LI><A HREF="flex.html#IDX163">start conditions, integer values</A> <LI><A HREF="flex.html#IDX149">start conditions, multiple</A> <LI><A HREF="flex.html#IDX153">start conditions, special wildcard condition</A> <LI><A HREF="flex.html#IDX172">start conditions, use of a stack</A> <LI><A HREF="flex.html#IDX154">start conditions, use of wildcard condition (<*>)</A> <LI><A HREF="flex.html#IDX159">start conditions, using BEGIN</A> <LI><A HREF="flex.html#IDX141">stdin, default for yyin</A> <LI><A HREF="flex.html#IDX102">strings, in actions</A> <LI><A HREF="flex.html#IDX191">strings, scanning strings instead of files</A> </DIR> <H2><A NAME="cindex_t">t</A></H2> <DIR> <LI><A HREF="flex.html#IDX440">tables, creating serialized</A> <LI><A HREF="flex.html#IDX450">tables, file format</A> <LI><A HREF="flex.html#IDX445">tables, freeing</A> <LI><A HREF="flex.html#IDX443">tables, loading and unloading</A> <LI><A HREF="flex.html#IDX134">terminating with yyterminate()</A> <LI><A HREF="flex.html#IDX84">token</A> <LI><A HREF="flex.html#IDX53">trailing context, in patterns</A> <LI><A HREF="flex.html#IDX72">trailing context, limits of</A> <LI><A HREF="flex.html#IDX80">trailing context, matching</A> <LI><A HREF="flex.html#IDX352">trailing context, performance costs</A> <LI><A HREF="flex.html#IDX360">trailing context, variable length</A> </DIR> <H2><A NAME="cindex_u">u</A></H2> <DIR> <LI><A HREF="flex.html#IDX123">unput() to push back characters</A> <LI><A HREF="flex.html#IDX125">unput(), caveat with %pointer</A> <LI><A HREF="flex.html#IDX121">unput(), explanation</A> <LI><A HREF="flex.html#IDX30">user code, in flex input</A> <LI><A HREF="flex.html#IDX6">username expansion</A> <LI><A HREF="flex.html#IDX164">using integer values of start condition names</A> </DIR> <H2><A NAME="cindex_v">v</A></H2> <DIR> <LI><A HREF="flex.html#IDX48">verbatim text in patterns, syntax of</A> </DIR> <H2><A NAME="cindex_w">w</A></H2> <DIR> <LI><A HREF="flex.html#IDX456">warning, dangerous trailing context</A> <LI><A HREF="flex.html#IDX454">warning, rule cannot be matched</A> <LI><A HREF="flex.html#IDX453">warnings, diagnostic messages</A> <LI><A HREF="flex.html#IDX97">whitespace, compressing, example</A> </DIR> <H2><A NAME="cindex_y">y</A></H2> <DIR> <LI><A HREF="flex.html#IDX224">yacc interface</A> <LI><A HREF="flex.html#IDX222">yacc, interface</A> <LI><A HREF="flex.html#IDX188">YY_CURRENT_BUFFER, and multiple buffers</A> <LI><A HREF="flex.html#IDX408">YY_EXTRA_TYPE, defining your own type</A> <LI><A HREF="flex.html#IDX144">YY_INPUT, overriding</A> <LI><A HREF="flex.html#IDX145">YY_INPUT, overriding the input mechanism</A> <LI><A HREF="flex.html#IDX165">YY_START, example</A> <LI><A HREF="flex.html#IDX201">YY_USER_ACTION to track each time a rule is matched</A> <LI><A HREF="flex.html#IDX432">yyalloc, overriding</A> <LI><A HREF="flex.html#IDX434">yyfree, overriding</A> <LI><A HREF="flex.html#IDX86">yyleng</A> <LI><A HREF="flex.html#IDX105">yyleng, modification of</A> <LI><A HREF="flex.html#IDX120">yyless() to push back characters</A> <LI><A HREF="flex.html#IDX118">yyless(), example</A> <LI><A HREF="flex.html#IDX117">yyless(), explanation</A> <LI><A HREF="flex.html#IDX138">yylex(), in generated scanner</A> <LI><A HREF="flex.html#IDX139">yylex(), overriding</A> <LI><A HREF="flex.html#IDX140">yylex, overriding the prototype</A> <LI><A HREF="flex.html#IDX422">yylineno, in a reentrant scanner</A> <LI><A HREF="flex.html#IDX351">yylineno, performance costs</A> <LI><A HREF="flex.html#IDX116">yymore() to append token to previous token</A> <LI><A HREF="flex.html#IDX115">yymore(), example</A> <LI><A HREF="flex.html#IDX114">yymore(), explanation</A> <LI><A HREF="flex.html#IDX106">yymore, caveat</A> <LI><A HREF="flex.html#IDX433">yyrealloc, overriding</A> <LI><A HREF="flex.html#IDX133">yyterminate(), explanation</A> <LI><A HREF="flex.html#IDX85">yytext</A> <LI><A HREF="flex.html#IDX212">yytext, default array size</A> <LI><A HREF="flex.html#IDX437">yytext, memory considerations</A> <LI><A HREF="flex.html#IDX104">yytext, modification of</A> <LI><A HREF="flex.html#IDX89">yytext, two types of</A> <LI><A HREF="flex.html#IDX146">yywrap(), explanation</A> </DIR> <H2><A NAME="SEC159" HREF="flex_toc.html#TOC159">Index of Functions and Macros</A></H2> <P> This is an index of functions and preprocessor macros that look like functions. For macros that expand to variables or constants, see section <A HREF="flex.html#SEC160">Index of Variables</A>. <P> Jump to: <A HREF="#findex_b">b</A> - <A HREF="#findex_d">d</A> - <A HREF="#findex_l">l</A> - <A HREF="#findex_s">s</A> - <A HREF="#findex_y">y</A> <P> <H2><A NAME="findex_b">b</A></H2> <DIR> <LI><A HREF="flex.html#IDX157">BEGIN</A> </DIR> <H2><A NAME="findex_d">d</A></H2> <DIR> <LI><A HREF="flex.html#IDX374">debug (C++ only)</A> </DIR> <H2><A NAME="findex_l">l</A></H2> <DIR> <LI><A HREF="flex.html#IDX383">LexerError (C++ only)</A> <LI><A HREF="flex.html#IDX380">LexerInput (C++ only)</A> <LI><A HREF="flex.html#IDX381">LexerOutput (C++ only)</A> <LI><A HREF="flex.html#IDX372">lineno (C++ only)</A> </DIR> <H2><A NAME="findex_s">s</A></H2> <DIR> <LI><A HREF="flex.html#IDX373">set_debug (C++ only)</A> <LI><A HREF="flex.html#IDX379">switch_streams (C++ only)</A> </DIR> <H2><A NAME="findex_y">y</A></H2> <DIR> <LI><A HREF="flex.html#IDX208">YY_AT_BOL</A> <LI><A HREF="flex.html#IDX179">yy_create_buffer</A> <LI><A HREF="flex.html#IDX183">yy_delete_buffer</A> <LI><A HREF="flex.html#IDX186">yy_flush_buffer</A> <LI><A HREF="flex.html#IDX187">yy_new_buffer</A> <LI><A HREF="flex.html#IDX198">YY_NEW_FILE (now obsolete)</A> <LI><A HREF="flex.html#IDX174">yy_pop_state</A> <LI><A HREF="flex.html#IDX173">yy_push_state</A> <LI><A HREF="flex.html#IDX195">yy_scan_buffer</A> <LI><A HREF="flex.html#IDX193">yy_scan_bytes</A> <LI><A HREF="flex.html#IDX192">yy_scan_string</A> <LI><A HREF="flex.html#IDX206">yy_set_bol</A> <LI><A HREF="flex.html#IDX204">yy_set_interactive</A> <LI><A HREF="flex.html#IDX181">yy_switch_to_buffer</A> <LI><A HREF="flex.html#IDX175">yy_top_state</A> <LI><A HREF="flex.html#IDX377">yyFlexLexer constructor (C++ only)</A> <LI><A HREF="flex.html#IDX418">yyget_debug</A> <LI><A HREF="flex.html#IDX406">yyget_extra</A>, <A HREF="flex.html#IDX420">yyget_extra</A> <LI><A HREF="flex.html#IDX412">yyget_in</A> <LI><A HREF="flex.html#IDX411">yyget_leng</A> <LI><A HREF="flex.html#IDX414">yyget_lineno</A> <LI><A HREF="flex.html#IDX472">yyget_lloc</A> <LI><A HREF="flex.html#IDX471">yyget_lval</A> <LI><A HREF="flex.html#IDX413">yyget_out</A> <LI><A HREF="flex.html#IDX410">yyget_text</A> <LI><A HREF="flex.html#IDX371">YYLeng (C++ only)</A> <LI><A HREF="flex.html#IDX378">yylex (C++ version)</A> <LI><A HREF="flex.html#IDX475">yylex (reentrant version)</A> <LI><A HREF="flex.html#IDX400">yylex_destroy</A> <LI><A HREF="flex.html#IDX399">yylex_init</A> <LI><A HREF="flex.html#IDX218">yyrestart</A> <LI><A HREF="flex.html#IDX419">yyset_debug</A> <LI><A HREF="flex.html#IDX407">yyset_extra</A>, <A HREF="flex.html#IDX421">yyset_extra</A> <LI><A HREF="flex.html#IDX415">yyset_in</A> <LI><A HREF="flex.html#IDX417">yyset_lineno</A> <LI><A HREF="flex.html#IDX474">yyset_lloc</A> <LI><A HREF="flex.html#IDX473">yyset_lval</A> <LI><A HREF="flex.html#IDX416">yyset_out</A> <LI><A HREF="flex.html#IDX449">yytables_destroy</A> <LI><A HREF="flex.html#IDX448">yytables_fload</A> <LI><A HREF="flex.html#IDX137">yyterminate</A> <LI><A HREF="flex.html#IDX370">YYText (C++ only)</A> </DIR> <H2><A NAME="SEC160" HREF="flex_toc.html#TOC160">Index of Variables</A></H2> <P> This is an index of variables, constants, and preprocessor macros that expand to variables or constants. <P> Jump to: <A HREF="#vindex_i">i</A> - <A HREF="#vindex_y">y</A> <P> <H2><A NAME="vindex_i">i</A></H2> <DIR> <LI><A HREF="flex.html#IDX158">INITIAL</A> </DIR> <H2><A NAME="vindex_y">y</A></H2> <DIR> <LI><A HREF="flex.html#IDX220">YY_CURRENT_BUFFER</A> <LI><A HREF="flex.html#IDX194">YY_END_OF_BUFFER_CHAR</A> <LI><A HREF="flex.html#IDX202">YY_NUM_RULES</A> <LI><A HREF="flex.html#IDX167">YY_START</A>, <A HREF="flex.html#IDX221">YY_START</A> <LI><A HREF="flex.html#IDX404">yyextra</A> <LI><A HREF="flex.html#IDX217">yyin</A> <LI><A HREF="flex.html#IDX216">yyleng</A> <LI><A HREF="flex.html#IDX468">yylloc</A> <LI><A HREF="flex.html#IDX214">YYLMAX</A> <LI><A HREF="flex.html#IDX467">yylval</A> <LI><A HREF="flex.html#IDX223">yylval, with yacc</A> <LI><A HREF="flex.html#IDX219">yyout</A> <LI><A HREF="flex.html#IDX395">yyscanner (reentrant only)</A> <LI><A HREF="flex.html#IDX92">yytext</A>, <A HREF="flex.html#IDX211">yytext</A> </DIR> <H2><A NAME="SEC161" HREF="flex_toc.html#TOC161">Index of Data Types</A></H2> <P> Jump to: <A HREF="#tindex_f">f</A> - <A HREF="#tindex_y">y</A> <P> <H2><A NAME="tindex_f">f</A></H2> <DIR> <LI><A HREF="flex.html#IDX376">FlexLexer (C++ only)</A> </DIR> <H2><A NAME="tindex_y">y</A></H2> <DIR> <LI><A HREF="flex.html#IDX180">YY_BUFFER_STATE</A> <LI><A HREF="flex.html#IDX405">YY_EXTRA_TYPE (reentrant only)</A> <LI><A HREF="flex.html#IDX196">yy_size_t</A> <LI><A HREF="flex.html#IDX375">yyFlexLexer (C++ only)</A> <LI><A HREF="flex.html#IDX469">YYLTYPE</A> <LI><A HREF="flex.html#IDX409">yyscan_t (reentrant only)</A> <LI><A HREF="flex.html#IDX470">YYSTYPE</A> </DIR> <H2><A NAME="SEC162" HREF="flex_toc.html#TOC162">Index of Hooks</A></H2> <P> This is an index of "hooks" that the user may define. These hooks typically correspond to specific locations in the generated scanner, and may be used to insert arbitrary code. <P> Jump to: <A HREF="#hkindex_y">y</A> <P> <H2><A NAME="hkindex_y">y</A></H2> <DIR> <LI><A HREF="flex.html#IDX210">YY_BREAK</A> <LI><A HREF="flex.html#IDX200">YY_USER_ACTION</A> <LI><A HREF="flex.html#IDX203">YY_USER_INIT</A> </DIR> <H2><A NAME="SEC163" HREF="flex_toc.html#TOC163">Index of Scanner Options</A></H2> <P> Jump to: <A HREF="#opindex_-">-</A> - <A HREF="#opindex_7">7</A> - <A HREF="#opindex_8">8</A> - <A HREF="#opindex_a">a</A> - <A HREF="#opindex_b">b</A> - <A HREF="#opindex_c">c</A> - <A HREF="#opindex_d">d</A> - <A HREF="#opindex_e">e</A> - <A HREF="#opindex_f">f</A> - <A HREF="#opindex_h">h</A> - <A HREF="#opindex_i">i</A> - <A HREF="#opindex_l">l</A> - <A HREF="#opindex_m">m</A> - <A HREF="#opindex_n">n</A> - <A HREF="#opindex_o">o</A> - <A HREF="#opindex_p">p</A> - <A HREF="#opindex_r">r</A> - <A HREF="#opindex_s">s</A> - <A HREF="#opindex_t">t</A> - <A HREF="#opindex_v">v</A> - <A HREF="#opindex_w">w</A> - <A HREF="#opindex_y">y</A> <P> <H2><A NAME="opindex_-">-</A></H2> <DIR> <LI><A HREF="flex.html#IDX283">-+</A> <LI><A HREF="flex.html#IDX254">--7bit</A> <LI><A HREF="flex.html#IDX257">--8bit</A> <LI><A HREF="flex.html#IDX301">--align</A> <LI><A HREF="flex.html#IDX261">--always-interactive</A> <LI><A HREF="flex.html#IDX286">--array</A> <LI><A HREF="flex.html#IDX321">--backup</A> <LI><A HREF="flex.html#IDX248">--batch</A> <LI><A HREF="flex.html#IDX275">--bison-bridge</A> <LI><A HREF="flex.html#IDX284">--c++</A> <LI><A HREF="flex.html#IDX242">--case-insensitive</A> <LI><A HREF="flex.html#IDX324">--debug</A> <LI><A HREF="flex.html#IDX259">--default</A> <LI><A HREF="flex.html#IDX304">--ecs</A> <LI><A HREF="flex.html#IDX318">--fast</A> <LI><A HREF="flex.html#IDX315">--full</A> <LI><A HREF="flex.html#IDX228">--header-file</A> <LI><A HREF="flex.html#IDX345">--help</A> <LI><A HREF="flex.html#IDX251">--interactive</A> <LI><A HREF="flex.html#IDX245">--lex-compat</A> <LI><A HREF="flex.html#IDX293">--main</A> <LI><A HREF="flex.html#IDX309">--meta-ecs</A> <LI><A HREF="flex.html#IDX263">--never-interactive</A> <LI><A HREF="flex.html#IDX330">--nodefault</A> <LI><A HREF="flex.html#IDX278">--noline</A> <LI><A HREF="flex.html#IDX295">--nounistd</A> <LI><A HREF="flex.html#IDX336">--nowarn</A> <LI><A HREF="flex.html#IDX231">--outfile</A> <LI><A HREF="flex.html#IDX327">--perf-report</A> <LI><A HREF="flex.html#IDX288">--pointer</A> <LI><A HREF="flex.html#IDX265">--posix</A> <LI><A HREF="flex.html#IDX291">--prefix</A> <LI><A HREF="flex.html#IDX312">--read</A> <LI><A HREF="flex.html#IDX281">--reentrant</A> <LI><A HREF="flex.html#IDX236">--skel</A> <LI><A HREF="flex.html#IDX267">--stack</A> <LI><A HREF="flex.html#IDX269">--stdinit</A> <LI><A HREF="flex.html#IDX234">--stdout</A> <LI><A HREF="flex.html#IDX237">--tables-file</A> <LI><A HREF="flex.html#IDX239">--tables-verify</A> <LI><A HREF="flex.html#IDX333">--trace</A> <LI><A HREF="flex.html#IDX339">--verbose</A> <LI><A HREF="flex.html#IDX348">--version</A> <LI><A HREF="flex.html#IDX341">--warn</A> <LI><A HREF="flex.html#IDX297">--yyclass</A> <LI><A HREF="flex.html#IDX271">--yylineno</A> <LI><A HREF="flex.html#IDX273">--yywrap</A> <LI><A HREF="flex.html#IDX253">-7</A> <LI><A HREF="flex.html#IDX256">-8</A> <LI><A HREF="flex.html#IDX247">-B</A> <LI><A HREF="flex.html#IDX320">-b</A> <LI><A HREF="flex.html#IDX343">-c</A> <LI><A HREF="flex.html#IDX299">-C</A> <LI><A HREF="flex.html#IDX300">-Ca</A> <LI><A HREF="flex.html#IDX303">-Ce</A> <LI><A HREF="flex.html#IDX307">-CF</A> <LI><A HREF="flex.html#IDX306">-Cf</A> <LI><A HREF="flex.html#IDX308">-Cm</A> <LI><A HREF="flex.html#IDX311">-Cr</A> <LI><A HREF="flex.html#IDX323">-d</A> <LI><A HREF="flex.html#IDX314">-f</A> <LI><A HREF="flex.html#IDX317">-F</A> <LI><A HREF="flex.html#IDX344">-h</A> <LI><A HREF="flex.html#IDX250">-I</A> <LI><A HREF="flex.html#IDX241">-i</A> <LI><A HREF="flex.html#IDX277">-L</A> <LI><A HREF="flex.html#IDX244">-l</A> <LI><A HREF="flex.html#IDX346">-n</A> <LI><A HREF="flex.html#IDX230">-o</A> <LI><A HREF="flex.html#IDX326">-p</A> <LI><A HREF="flex.html#IDX290">-P</A> <LI><A HREF="flex.html#IDX280">-R</A> <LI><A HREF="flex.html#IDX329">-s</A> <LI><A HREF="flex.html#IDX233">-t</A> <LI><A HREF="flex.html#IDX332">-T</A> <LI><A HREF="flex.html#IDX347">-V</A> <LI><A HREF="flex.html#IDX338">-v</A> <LI><A HREF="flex.html#IDX335">-w</A> <LI><A HREF="flex.html#IDX264">-X</A> </DIR> <H2><A NAME="opindex_7">7</A></H2> <DIR> <LI><A HREF="flex.html#IDX255">7bit</A> </DIR> <H2><A NAME="opindex_8">8</A></H2> <DIR> <LI><A HREF="flex.html#IDX258">8bit</A> </DIR> <H2><A NAME="opindex_a">a</A></H2> <DIR> <LI><A HREF="flex.html#IDX302">align</A> <LI><A HREF="flex.html#IDX262">always-interactive</A> <LI><A HREF="flex.html#IDX287">array</A> </DIR> <H2><A NAME="opindex_b">b</A></H2> <DIR> <LI><A HREF="flex.html#IDX322">backup</A> <LI><A HREF="flex.html#IDX249">batch</A> <LI><A HREF="flex.html#IDX276">bison-bridge</A> </DIR> <H2><A NAME="opindex_c">c</A></H2> <DIR> <LI><A HREF="flex.html#IDX285">c++</A> <LI><A HREF="flex.html#IDX243">case-insensitive</A> </DIR> <H2><A NAME="opindex_d">d</A></H2> <DIR> <LI><A HREF="flex.html#IDX325">debug</A> <LI><A HREF="flex.html#IDX260">default</A> </DIR> <H2><A NAME="opindex_e">e</A></H2> <DIR> <LI><A HREF="flex.html#IDX305">ecs</A> </DIR> <H2><A NAME="opindex_f">f</A></H2> <DIR> <LI><A HREF="flex.html#IDX319">fast</A> <LI><A HREF="flex.html#IDX316">full</A> </DIR> <H2><A NAME="opindex_h">h</A></H2> <DIR> <LI><A HREF="flex.html#IDX229">header-file</A> </DIR> <H2><A NAME="opindex_i">i</A></H2> <DIR> <LI><A HREF="flex.html#IDX252">interactive</A> </DIR> <H2><A NAME="opindex_l">l</A></H2> <DIR> <LI><A HREF="flex.html#IDX246">lex-compat</A> </DIR> <H2><A NAME="opindex_m">m</A></H2> <DIR> <LI><A HREF="flex.html#IDX294">main</A> <LI><A HREF="flex.html#IDX310">meta-ecs</A> </DIR> <H2><A NAME="opindex_n">n</A></H2> <DIR> <LI><A HREF="flex.html#IDX331">nodefault</A> <LI><A HREF="flex.html#IDX279">noline</A> <LI><A HREF="flex.html#IDX296">nounistd</A> <LI><A HREF="flex.html#IDX337">nowarn</A> <LI><A HREF="flex.html#IDX435">noyyalloc</A> </DIR> <H2><A NAME="opindex_o">o</A></H2> <DIR> <LI><A HREF="flex.html#IDX232">outfile</A> </DIR> <H2><A NAME="opindex_p">p</A></H2> <DIR> <LI><A HREF="flex.html#IDX328">perf-report</A> <LI><A HREF="flex.html#IDX289">pointer</A> <LI><A HREF="flex.html#IDX266">posix</A> <LI><A HREF="flex.html#IDX292">prefix</A> </DIR> <H2><A NAME="opindex_r">r</A></H2> <DIR> <LI><A HREF="flex.html#IDX313">read</A> <LI><A HREF="flex.html#IDX282">reentrant</A> </DIR> <H2><A NAME="opindex_s">s</A></H2> <DIR> <LI><A HREF="flex.html#IDX268">stack</A> <LI><A HREF="flex.html#IDX270">stdinit</A> <LI><A HREF="flex.html#IDX235">stdout</A> </DIR> <H2><A NAME="opindex_t">t</A></H2> <DIR> <LI><A HREF="flex.html#IDX238">tables-file</A> <LI><A HREF="flex.html#IDX240">tables-verify</A> <LI><A HREF="flex.html#IDX334">trace</A> </DIR> <H2><A NAME="opindex_v">v</A></H2> <DIR> <LI><A HREF="flex.html#IDX340">verbose</A> </DIR> <H2><A NAME="opindex_w">w</A></H2> <DIR> <LI><A HREF="flex.html#IDX342">warn</A> </DIR> <H2><A NAME="opindex_y">y</A></H2> <DIR> <LI><A HREF="flex.html#IDX298">yyclass</A> <LI><A HREF="flex.html#IDX272">yylineno</A> <LI><A HREF="flex.html#IDX274">yywrap</A> </DIR> <P><HR><P> This document was generated on 9 November 2002 using <A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A> 1.56k. </BODY> </HTML>