[VC MFC C++ flex bison] flex 特别action
2008-02-01 18:07
363 查看
`ECHO' copies yytext to the scanner's output.
Without the
(The first three rules share the fourth's action since they use the special '|' action.)
`yymore()' tells the scanner that the next time it matches a rule, the corresponding token should be appended onto the current value of
First "mega-" is matched and echoed to the output. Then "kludge" is matched, but the previous "mega-" is still hanging around at the beginning of
`yyless(n)' returns all but the first n characters of the current token back to the input stream, where they will be rescanned when the scanner looks for the next match.
An argument of 0 to
`unput(c)' puts the character
Note that since each `unput()' puts the given character back at the beginning of the input stream, pushing back strings must be done back-to-front. An important potential problem when using `unput()' is that if you are using `%pointer' (the default), a call to `unput()' destroys the contents of
`input()' reads the next character from the input stream. For example, the following is one way to eat up C comments:
(Note that if the scanner is compiled using `C++', then `input()' is instead referred to as `yyinput()', in order to avoid a name clash with the `C++' stream by the name of
YY_FLUSH_BUFFER 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
`yyterminate()' 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, `yyterminate()' is also called when an end-of-file is encountered. It is a macro and may be redefined.
BEGINfollowed by the name of a start condition places the scanner in the corresponding start condition (see below).
REJECTdirects 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 "How the Input is Matched", and
yytextand
yylengset up appropriately. It may either be one which matched as much text as the originally chosen rule but came later in the
flexinput file, or one which matched less text. For example, the following will both count the words in the input and call the routine special() whenever "frob" is seen:
int word_count = 0; %% frob special(); REJECT; [^ /t/n]+ ++word_count;
Without the
REJECT, any "frob"'s in the input would not be counted as words, since the scanner normally executes only one action per token. Multiple
REJECT'sare allowed, each one finding the next best choice to the currently active rule. For example, when the following scanner scans the token "abcd", it will write "abcdabcaba" to the output:
%% a | ab | abc | abcd ECHO; REJECT; .|/n /* eat up any unmatched character */
(The first three rules share the fourth's action since they use the special '|' action.)
REJECTis a particularly expensive feature in terms of scanner performance; if it is used in any of the scanner's actions it will slow down all of the scanner's matching. Furthermore,
REJECTcannot be used with the `-Cf' or `-CF' options (see below). Note also that unlike the other special actions,
REJECTis a branch; code immediately following it in the action will not be executed.
`yymore()' tells the scanner that the next time it matches a rule, the corresponding token should be appended onto the current value of
yytextrather than replacing it. For example, given the input "mega-kludge" the following will write "mega-mega-kludge" to the output:
%% mega- ECHO; yymore(); kludge ECHO;
First "mega-" is matched and echoed to the output. Then "kludge" is matched, but the previous "mega-" is still hanging around at the beginning of
yytextso the `ECHO' for the "kludge" rule will actually write "mega-kludge".
`yyless(n)' returns all but the first n characters of the current token back to the input stream, where they will be rescanned when the scanner looks for the next match.
yytextand
yylengare adjusted appropriately (e.g.,
yylengwill now be equal to n ). For example, on the input "foobar" the following will write out "foobarbar":
%% foobar ECHO; yyless(3); [a-z]+ ECHO;
An argument of 0 to
yylesswill cause the entire current input string to be scanned again. Unless you've changed how the scanner will subsequently process its input (using
BEGIN, for example), this will result in an endless loop. Note that
yylessis a macro and can only be used in the flex input file, not from other source files.
`unput(c)' puts the character
cback 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.
{ 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 ); }
Note that since each `unput()' puts the given character back at the beginning of the input stream, pushing back strings must be done back-to-front. An important potential problem when using `unput()' is that if you are using `%pointer' (the default), a call to `unput()' destroys the contents of
yytext, 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 `unput()' (as in the above example), you must either first copy it elsewhere, or build your scanner using `%array' instead (see How The Input Is Matched). Finally, note that you cannot put back
EOFto attempt to mark the input stream with an end-of-file.
`input()' reads the next character from the input stream. For example, the following is one way to eat up C comments:
%% "/*" { 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; } } }
(Note that if the scanner is compiled using `C++', then `input()' is instead referred to as `yyinput()', in order to avoid a name clash with the `C++' stream by the name of
input.)
YY_FLUSH_BUFFER 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
YY_INPUT(see The Generated Scanner, below). This action is a special case of the more general `yy_flush_buffer()' function, described below in the section Multiple Input Buffers.
`yyterminate()' 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, `yyterminate()' is also called when an end-of-file is encountered. It is a macro and may be redefined.
相关文章推荐
- [VC MFC C++ flex bison] flex rules(规则)
- [VC MFC C++ flex bison] flex format
- [VC MFC C++ flex bison] flex example(Pascal-like language scanner)
- [C++ VC MFC]巧妙程序自删除 仅供参考 我测试了一下不能删除自己
- [C++ VC MFC]巧妙程序自删除
- C/C++/VC++/MFC怎样通过代码运行Dos窗口或以管理员身份运行DOS窗口,并执行一条指令或命令参数
- Flex&Bison Tips(2010-08-27) 如何在VC中更好的断点调试flex&bison词法和语法分析器
- C++/VC/MFC字符串/文件加解密
- C++ 常用代码001 IsValidFileName VC/MFC 判断有效文件名
- VC++/MFC/C/C++ 资料下载汇总
- 将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
- [C++ VC MFC] Windows 的定时机制
- vc 热键、组合键的用法 MFC c++ hotkey WM_HOTKEY
- C/C++/mfc/vc++中怎样通过代码让你做的应用程序以管理员的身份安装执行
- 命令行下的C++程序转换成VC的MFC程序需要注意的问题
- [C++ MFC VC] 虚函数表指针的位置
- C/C++/VC MFC----char & int---study
- C++/VC/MFC获得GUID
- 保存画面为图片 当前MFC保存该程序为图片 c++ vc
- VC/C++/MFC 获取IE浏览器的滚动条位置