📄 flex.1
字号:
or the '$' operator). The start condition, '^', and "<<EOF>>" patternscan only occur at the beginning of a pattern, and, as well as with '/' and '$',cannot be grouped inside parentheses. The following are all illegal:.nf foo/bar$ foo|(bar$) foo|^bar <sc1>foo<sc2>bar.fi.SH SUMMARY OF SPECIAL ACTIONSIn addition to arbitrary C code, the following can appear in actions:.IP -.B ECHOcopies yytext to the scanner's output..IP -.B BEGINfollowed by the name of a start condition places the scanner in thecorresponding start condition..IP -.B REJECTdirects the scanner to proceed on to the "second best" rule which matched theinput (or a prefix of the input)..B yytextand.B yylengare set up appropriately. Note that.B REJECTis a particularly expensive feature in terms scanner performance;if it is used in.I anyof the scanner's actions it will slow down.I allof the scanner's matching. Furthermore,.B REJECTcannot be used with the.B \-for.B \-Foptions..IPNote also that unlike the other special actions,.B REJECTis a.I branch;code immediately following it in the action will.I notbe executed..IP -.B yymore()tells the scanner that the next time it matches a rule, the correspondingtoken should be.I appendedonto the current value of.B yytextrather than replacing it..IP -.B yyless(n)returns all but the first.I ncharacters of the current token back to the input stream, where theywill be rescanned when the scanner looks for the next match..B yytextand.B yylengare adjusted appropriately (e.g.,.B yylengwill now be equal to.I n)..IP -.B unput(c)puts the character.I cback onto the input stream. It will be the next character scanned..IP -.B input()reads the next character from the input stream (this routine is called.B yyinput()if the scanner is compiled using.B C++)..IP -.B yyterminate()can be used in lieu of a return statement in an action. It terminatesthe scanner and returns a 0 to the scanner's caller, indicating "all done"..IPBy default,.B yyterminate()is also called when an end-of-file is encountered. It is a macro andmay be redefined..IP -.B YY_NEW_FILEis an action available only in <<EOF>> rules. It means "Okay, I'veset up a new input file, continue scanning". It is no longer required;you can just assign.I yyinto point to a new file in the <<EOF>> action..IP -.B yy_create_buffer( file, size )takes a.I FILEpointer and an integer.I size.It returns a YY_BUFFER_STATEhandle to a new input buffer large enough to accomodate.I sizecharacters and associated with the given file. When in doubt, use.B YY_BUF_SIZEfor the size..IP -.B yy_switch_to_buffer( new_buffer )switches the scanner's processing to scan for tokens fromthe given buffer, which must be a YY_BUFFER_STATE..IP -.B yy_delete_buffer( buffer )deletes the given buffer..SH VALUES AVAILABLE TO THE USER.IP -.B char *yytextholds the text of the current token. It may be modified but not lengthened(you cannot append characters to the end). Modifying the last charactermay affect the activity of rules anchored using '^' during the next scan;see.B lexdoc(1)for details..IPIf the special directive.B %arrayappears in the first section of the scanner description, then.B yytextis instead declared.B char yytext[YYLMAX],where.B YYLMAXis a macro definition that you can redefine in the first sectionif you don't like the default value (generally 8KB). Using.B %arrayresults in somewhat slower scanners, but the value of.B yytextbecomes immune to calls to.I input()and.I unput(),which potentially destroy its value when.B yytextis a character pointer. The opposite of.B %arrayis.B %pointer,which is the default..IP -.B int yylengholds the length of the current token..IP -.B FILE *yyinis the file which by default.I lexreads from. It may be redefined but doing so only makes sense beforescanning begins or after an EOF has been encountered. Changing it inthe midst of scanning will have unexpected results since.I lexbuffers its input; use.B yyrestart()instead.Once scanning terminates because an end-of-filehas been seen,.Byou can assign.I yyinat the new input file and then call the scanner again to continue scanning..IP -.B void yyrestart( FILE *new_file )may be called to point.I yyinat the new input file. The switch-over to the new file is immediate(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 MACROS AND FUNCTIONS YOU CAN REDEFINE.IP -.B YY_DECLcontrols how the scanning routine is declared.By default, it is "int yylex()", or, if prototypes are beingused, "int yylex(void)". This definition may be changed by redefiningthe "YY_DECL" macro. Note thatif you give arguments to the scanning routine using aK&R-style/non-prototyped function declaration, you must terminatethe definition with a semi-colon (;)..IP -The nature of how the scannergets its input can be controlled by redefining the.B YY_INPUTmacro.YY_INPUT's calling sequence is "YY_INPUT(buf,result,max_size)". Itsaction is to place up to.I max_sizecharacters in the character array.I bufand return in the integer variable.I resulteither thenumber of characters read or the constant YY_NULL (0 on Unix systems)to indicate EOF. The default YY_INPUT reads from theglobal file-pointer "yyin".A sample redefinition of YY_INPUT (in the definitionssection of the input file):.nf %{ #undef YY_INPUT #define YY_INPUT(buf,result,max_size) \\ { \\ int c = getchar(); \\ result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \\ } %}.fi.IP -When the scanner receives an end-of-file indication from YY_INPUT,it then checks the function.B yywrap()function. If.B yywrap()returns false (zero), then it is assumed that thefunction has gone ahead and set up.I yyinto point to another input file, and scanning continues. If it returnstrue (non-zero), then the scanner terminates, returning 0 to itscaller..IPThe default.B yywrap()always returns 1..IP -YY_USER_ACTIONcan be redefined to provide an actionwhich is always executed prior to the matched rule's action..IP -The macro.B YY_USER_INITmay be redefined to provide an action which is always executed beforethe first scan..IP -In the generated scanner, the actions are all gathered in one largeswitch statement and separated using.B YY_BREAK,which may be redefined. By default, it is simply a "break", to separateeach rule's action from the following rule's..SH FILES.TP.B \-lllibrary with which scanners may be linked to obtain default versionsof.I yywrap()and.I main()..TP.I lex.yy.cgenerated scanner (called.I lexyy.con some systems)..TP.I lex.backupbacking-up information for.B \-bflag (called.I lex.bckon some systems)..SH "SEE ALSO".PPlexdoc(1), yacc(1), sed(1), awk(1)..PPM. E. Lesk and E. Schmidt,.I LEX \- Lexical Analyzer Generator.SH DIAGNOSTICS.PP.I reject_used_but_not_detected undefinedor.PP.I yymore_used_but_not_detected undefined -These errors can occur at compile time. They indicate that thescanner uses.B REJECTor.B yymore()but that.I lexfailed to notice the fact, meaning that.I lexscanned the first two sections looking for occurrences of these actionsand failed to find any, but somehow you snuck some in (via a #includefile, for example). Make an explicit reference to the action in your.I lexinput file. (Note that previously.I lexsupported a.B %used/%unusedmechanism for dealing with this problem; this feature is still supportedbut now deprecated, and will go away soon unless the author hears frompeople who can argue compellingly that they need it.).PP.I lex scanner jammed -a scanner compiled with.B \-shas encountered an input string which wasn't matched byany of its rules..PP.I warning, rule cannot be matchedindicates that the given rulecannot be matched because it follows other rules that willalways match the same text as it. See.I lexdoc(1)for an example..PP.I warning,.B \-s.Ioption given but default rule can be matchedmeans that it is possible (perhaps only in a particular start condition)that the default rule (match any single character) is the only onethat will match a particular input. Since.PP.I scanner input buffer overflowed -a scanner rule matched more text than the available dynamic memory..PP.I token too large, exceeds YYLMAX -your scanner uses.B %arrayand one of its rules matched a string longer than the.B YYLMAXconstant (8K bytes by default). You can increase the value by#define'ing.B YYLMAXin the definitions section of your.I lexinput..PP.I scanner requires \-8 flag to.I use the character 'x' -Your scanner specification includes recognizing the 8-bit character.I 'x'and you did not specify the \-8 flag, and your scanner defaulted to 7-bitbecause you used the.B \-Cfor.B \-CFtable compression options..PP.I lex scanner push-back overflow -you used.B unput()to push back so much text that the scanner's buffer could not holdboth the pushed-back text and the current token in.B yytext.Ideally the scanner should dynamically resize the buffer in this case, but atpresent it does not..PP.Iinput buffer overflow, can't enlarge buffer because scanner uses REJECT -the scanner was working on matching an extremely large token and neededto expand the input buffer. This doesn't work with scanners that use.BREJECT..PP.Ifatal lex scanner internal error--end of buffer missed -This can occur in an scanner which is reentered after a long-jumphas jumped out (or over) the scanner's activation frame. Beforereentering the scanner, use:.nf yyrestart( yyin );.fi.SH AUTHORVern Paxson, with the help of many ideas and much inspiration fromVan Jacobson. Original version by Jef Poskanzer..PPSee lexdoc(1) for additional credits and the address to send comments to..SH DEFICIENCIES / BUGS.PPSome trailing contextpatterns cannot be properly matched and generatewarning messages ("dangerous trailing context"). These arepatterns where the ending of thefirst part of the rule matches the beginning of the secondpart, such as "zx*/xy*", where the 'x*' matches the 'x' atthe beginning of the trailing context. (Note that the POSIX draftstates that the text matched by such patterns is undefined.).PPFor some trailing context rules, parts which are actually fixed-length arenot recognized as such, leading to the abovementioned performance loss.In particular, parts using '|' or {n} (such as "foo{3}") are alwaysconsidered variable-length..PPCombining trailing context with the special '|' action can result in.I fixedtrailing context being turned into the more expensive.I variabletrailing context. For example, in the following:.nf %% abc | xyz/def.fi.PPUse of.B unput()or.B input()invalidates yytext and yyleng, unless the.B %arraydirectiveor the.B \-loption has been used..PPUse of unput() to push back more text than was matched canresult in the pushed-back text matching a beginning-of-line ('^')rule even though it didn't come at the beginning of the line(though this is rare!)..PPPattern-matching of NUL's is substantially slower than matching othercharacters..PPDynamic resizing of the input buffer is slow, as it entails rescanningall the text matched so far by the current (generally huge) token..PP.I lexdoes not generate correct #line directives for code internalto the scanner; thus, bugs in its skeleton fileyield bogus line numbers..PPDue to both buffering of input and read-ahead, you cannot intermixcalls to <stdio.h> routines, such as, for example,.B getchar(),with.I lexrules and expect it to work. Call.B input()instead..PPThe total table entries listed by the.B \-vflag excludes the number of table entries needed to determinewhat rule has been matched. The number of entries is equalto the number of DFA states if the scanner does not use.B REJECT,and somewhat greater than the number of states if it does..PP.B REJECTcannot be used with the.B \-for.B \-Foptions..PPThe.I lexinternal algorithms need documentation.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -