📄 flex.1
字号:
(any previously buffered-up input is lost). Note that calling.B yyrestart()with.I yyinas an argument thus throws away the current input buffer and continuesscanning the same input file..IP -.B FILE *yyoutis the file to which.B ECHOactions are done. It can be reassigned by the user..IP -.B YY_CURRENT_BUFFERreturns a.B YY_BUFFER_STATEhandle to the current buffer..IP -.B YY_STARTreturns an integer value corresponding to the current startcondition. You can subsequently use this value with.B BEGINto return to that start condition..SH INTERFACING WITH YACCOne of the main uses of.I flexis as a companion to the.I yaccparser-generator..I yaccparsers expect to call a routine named.B yylex()to find the next input token. The routine is supposed toreturn the type of the next token as well as putting any associatedvalue in the global.B yylval.To use.I flexwith.I yacc,one specifies the.B \-doption to.I yaccto instruct it to generate the file.B y.tab.hcontaining definitions of all the.B %tokensappearing in the.I yaccinput. This file is then included in the.I flexscanner. For example, if one of the tokens is "TOK_NUMBER",part of the scanner might look like:.nf %{ #include "y.tab.h" %} %% [0-9]+ yylval = atoi( yytext ); return TOK_NUMBER;.fi.SH OPTIONS.I flexhas the following options:.TP.B \-bGenerate backing-up information to.I lex.backup.This is a list of scanner states which require backing upand the input characters on which they do so. By adding rules onecan remove backing-up states. If.I allbacking-up states are eliminated and.B \-Cfor.B \-CFis used, the generated scanner will run faster (see the.B \-pflag). Only users who wish to squeeze every last cycle out of theirscanners need worry about this option. (See the section on PerformanceConsiderations below.).TP.B \-cis a do-nothing, deprecated option included for POSIX compliance..TP.B \-dmakes the generated scanner run in.I debugmode. Whenever a pattern is recognized and the global.B yy_flex_debugis non-zero (which is the default),the scanner will write to.I stderra line of the form:.nf --accepting rule at line 53 ("the matched text").fiThe line number refers to the location of the rule in the filedefining the scanner (i.e., the file that was fed to flex). Messagesare also generated when the scanner backs up, accepts thedefault rule, reaches the end of its input buffer (or encountersa NUL; at this point, the two look the same as far as the scanner's concerned),or reaches an end-of-file..TP.B \-fspecifies.I fast scanner.No table compression is done and stdio is bypassed.The result is large but fast. This option is equivalent to.B \-Cfr(see below)..TP.B \-hgenerates a "help" summary of.I flex'soptions to.I stdout and then exits..B \-?and.B \-\-helpare synonyms for.B \-h..TP.B \-iinstructs.I flexto generate a.I case-insensitivescanner. The case of letters given in the.I flexinput patterns willbe ignored, and tokens in the input will be matched regardless of case. Thematched text given in.I yytextwill have the preserved case (i.e., it will not be folded)..TP.B \-lturns on maximum compatibility with the original AT&T.I leximplementation. Note that this does not mean.I fullcompatibility. Use of this option costs a considerable amount ofperformance, and it cannot be used with the.B \-+, -f, -F, -Cf,or.B -CFoptions. For details on the compatibilities it provides, see the section"Incompatibilities With Lex And POSIX" below. This option also resultsin the name.B YY_FLEX_LEX_COMPATbeing #define'd in the generated scanner..TP.B \-nis another do-nothing, deprecated option included only forPOSIX compliance..TP.B \-pgenerates a performance report to stderr. The reportconsists of comments regarding features of the.I flexinput file which will cause a serious loss of performance in the resultingscanner. If you give the flag twice, you will also get comments regardingfeatures that lead to minor performance losses..IPNote that the use of.B REJECT,.B %option yylineno,and variable trailing context (see the Deficiencies / Bugs section below)entails a substantial performance penalty; use of.I yymore(),the.B ^operator,and the.B \-Iflag entail minor performance penalties..TP.B \-scauses the.I default rule(that unmatched scanner input is echoed to.I stdout)to be suppressed. If the scanner encounters input that does notmatch any of its rules, it aborts with an error. This option isuseful for finding holes in a scanner's rule set..TP.B \-tinstructs.I flexto write the scanner it generates to standard output insteadof.B lex.yy.c..TP.B \-vspecifies that.I flexshould write to.I stderra summary of statistics regarding the scanner it generates.Most of the statistics are meaningless to the casual.I flexuser, but the first line identifies the version of.I flex(same as reported by.B \-V),and the next line the flags used when generating the scanner, includingthose that are on by default..TP.B \-wsuppresses warning messages..TP.B \-Binstructs.I flexto generate a.I batchscanner, the opposite of.I interactivescanners generated by.B \-I(see below). In general, you use.B \-Bwhen you are.I certainthat your scanner will never be used interactively, and you want tosqueeze a.I littlemore performance out of it. If your goal is instead to squeeze out a.I lotmore performance, you should be using the.B \-Cfor.B \-CFoptions (discussed below), which turn on.B \-Bautomatically anyway..TP.B \-Fspecifies that the.ulfastscanner table representation should be used (and stdiobypassed). This representation isabout as fast as the full table representation.B (-f),and for some sets of patterns will be considerably smaller (and forothers, larger). In general, if the pattern set contains both "keywords"and a catch-all, "identifier" rule, such as in the set:.nf "case" return TOK_CASE; "switch" return TOK_SWITCH; ... "default" return TOK_DEFAULT; [a-z]+ return TOK_ID;.fithen you're better off using the full table representation. If onlythe "identifier" rule is present and you then use a hash table or some suchto detect the keywords, you're better off using.B -F..IPThis option is equivalent to.B \-CFr(see below). It cannot be used with.B \-+..TP.B \-Iinstructs.I flexto generate an.I interactivescanner. An interactive scanner is one that only looks ahead to decidewhat token has been matched if it absolutely must. It turns out thatalways looking one extra character ahead, even if the scanner has alreadyseen enough text to disambiguate the current token, is a bit faster thanonly looking ahead when necessary. But scanners that always look aheadgive dreadful interactive performance; for example, when a user typesa newline, it is not recognized as a newline token until they enter.I anothertoken, which often means typing in another whole line..IP.I Flexscanners default to.I interactiveunless you use the.B \-Cfor.B \-CFtable-compression options (see below). That's because if you're lookingfor high-performance you should be using one of these options, so if youdidn't,.I flexassumes you'd rather trade off a bit of run-time performance for intuitiveinteractive behavior. Note also that you.I cannotuse.B \-Iin conjunction with.B \-Cfor.B \-CF.Thus, this option is not really needed; it is on by default for all thosecases in which it is allowed..IPYou can force a scanner to.I notbe interactive by using.B \-B(see above)..TP.B \-Linstructs.I flexnot to generate.B #linedirectives. Without this option,.I flexpeppers the generated scannerwith #line directives so error messages in the actions will be correctlylocated with respect to either the original.I flexinput file (if the errors are due to code in the input file), or.B lex.yy.c(if the errors are.I flex'sfault -- you should report these sorts of errors to the email addressgiven below)..TP.B \-Tmakes.I flexrun in.I tracemode. It will generate a lot of messages to.I stderrconcerningthe form of the input and the resultant non-deterministic and deterministicfinite automata. This option is mostly for use in maintaining.I flex..TP.B \-Vprints the version number to.I stdoutand exits..B \-\-versionis a synonym for.B \-V..TP.B \-7instructs.I flexto generate a 7-bit scanner, i.e., one which can only recognized 7-bitcharacters in its input. The advantage of using.B \-7is that the scanner's tables can be up to half the size of those generatedusing the.B \-8option (see below). The disadvantage is that such scanners often hangor crash if their input contains an 8-bit character..IPNote, however, that unless you generate your scanner using the.B \-Cfor.B \-CFtable compression options, use of.B \-7will save only a small amount of table space, and make your scannerconsiderably less portable..I Flex'sdefault behavior is to generate an 8-bit scanner unless you use the.B \-Cfor.B \-CF,in which case.I flexdefaults to generating 7-bit scanners unless your site was alwaysconfigured to generate 8-bit scanners (as will often be the casewith non-USA sites). You can tell whether flex generated a 7-bitor an 8-bit scanner by inspecting the flag summary in the.B \-voutput as described above..IPNote that if you use.B \-Cfeor.B \-CFe(those table compression options, but also using equivalence classes asdiscussed see below), flex still defaults to generating an 8-bitscanner, since usually with these compression options full 8-bit tablesare not much more expensive than 7-bit tables..TP.B \-8instructs.I flexto generate an 8-bit scanner, i.e., one which can recognize 8-bitcharacters. This flag is only needed for scanners generated using.B \-Cfor.B \-CF,as otherwise flex defaults to generating an 8-bit scanner anyway..IPSee the discussion of.B \-7above for flex's default behavior and the tradeoffs between 7-bitand 8-bit scanners..TP.B \-+specifies that you want flex to generate a C++scanner class. See the section on Generating C++ Scanners below fordetails..TP .B \-C[aefFmr]controls the degree of table compression and, more generally, trade-offsbetween small scanners and fast scanners..IP.B \-Ca("align") instructs flex to trade off larger tables in thegenerated scanner for faster performance because the elements ofthe tables are better aligned for memory access and computation. On someRISC architectures, fetching and manipulating longwords is more efficientthan with smaller-sized units such as shortwords. This option candouble the size of the tables used by your scanner..IP.B \-Cedirects.I flexto construct.I equivalence classes,i.e., sets of characterswhich have identical lexical properties (for example, if the onlyappearance of digits in the.I flexinput is in the character class"[0-9]" then the digits '0', '1', ..., '9' will all be putin the same equivalence class). Equivalence classes usually givedramatic reductions in the final table/object file sizes (typicallya factor of 2-5) and are pretty cheap performance-wise (one arraylook-up per character scanned)..IP.B \-Cfspecifies that the.I fullscanner tables should be generated -.I flexshould not compress thetables by taking advantages of similar transition functions fordifferent states..IP.B \-CFspecifies that the alternate fast scanner representation (describedabove under the.B \-Fflag)should be used. This option cannot be used with.B \-+..IP.B \-Cmdirects.I flexto construct.I meta-equivalence classes,which are sets of equivalence classes (or characters, if equivalenceclasses are not being used) that are commonly used together. Meta-equivalenceclasses are often a big win when using compressed tables, but theyhave a moderate performance impact (one or two "if" tests and onearray look-up per character scanned)..IP.B \-Crcauses the generated scanner to.I bypassuse of the standard I/O library (stdio) for input. Instead of calling.B fread()or.B getc(),the scanner will use the.B read()system call, resulting in a performance gain which varies from systemto system, but in general is probably negligible unless you are also using.B \-Cfor.B \-CF.Using.B \-Crcan cause strange behavior if, for example, you read from.I yyinusing stdio prior to calling the scanner (because the scanner will misswhatever text your previous reads left in the stdio input buffer)..IP.B \-Crhas no effect if you define.B YY_INPUT(see The Generated Scanner abo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -