设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> 其他开发语言 >> 为flex提供多输入缓冲!Multiple input buffers
精品推荐
其他开发语言点击TOP10
·数字小键盘指法练习
·用C语言编通讯录程序(初学者级别的)
·Modem 常用AT指令集
·单片机模拟I2C总线及24C02(I2C EEPROM)读写实例(源代码)
·C++经典电子书下载
·Thinking in C++ 简体中文第二版
·debug和release的区别
·error LNK2001: unresolved external symbol __ftol2 错误解决
·C库函数手册
·一个简单的C语言编译器
编程开发点击TOP10
·数字小键盘指法练习
·ASP.NET 程序中常用的三十三种代码
·用C语言编通讯录程序(初学者级别的)
·我写的Java学生成绩管理系统源代码
·CHK文件恢复工具
·Modem 常用AT指令集
·java笔试题
·异常java.sql.SQLException: Io exception:The Network Adapter could not establish connection
·单片机模拟I2C总线及24C02(I2C EEPROM)读写实例(源代码)
·C++经典电子书下载
精选专题

为flex提供多输入缓冲!Multiple input buffers

作者: 来源:网络文章 时间:2005-12-17 1:02:20

为flex提供多输入缓冲!Multiple input buffers(1)  CSdn.net/Emoticons/regular_smile.gif">

Some scanners (sUCh as those which support "include" files) require reading from several input streams. As flex scanners do a large amount of buffering, one cannot control where the next input will be read from by simply writing a YY_INPUT which is sensitive to the scanning context. YY_INPUT 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 "include" which requires switching the input source.

To negotiate these sorts of problems, flex provides a mechanism for creating and switching between multiple input buffers. An input buffer is created by using:

YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )

which takes a FILE pointer and a size and creates a buffer associated with the given file and large enough to hold size characters (when in doubt, use YY_BUF_SIZE for the size). It returns a YY_BUFFER_STATE handle, which may then be passed to other routines (see below). The YY_BUFFER_STATE type is a pointer to an opaque struct yy_buffer_state structure, so you may safely initialize YY_BUFFER_STATE variables to ((YY_BUFFER_STATE) 0) 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 FILE pointer in the call to yy_create_buffer is only used as the value of yyin seen by YY_INPUT; if you redefine YY_INPUT so it no longer uses yyin, then you can safely pass a nil FILE pointer to yy_create_buffer. You select a particular buffer to scan from using:

void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )

switches the scanner's input buffer so subsequent tokens will come from new_buffer. Note that yy_switch_to_buffer() may be used by yywrap() to set things up for continued scanning, instead of opening a new file and pointing yyin at it. Note also that switching input sources via either yy_switch_to_buffer() or yywrap() does not change the start condition.

void yy_delete_buffer( YY_BUFFER_STATE buffer )

is used to reclaim the storage associated with a buffer. You can also clear the current contents of a buffer using:

void yy_flush_buffer( YY_BUFFER_STATE buffer )

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 YY_INPUT.

yy_new_buffer() is an alias for yy_create_buffer(), provided for compatibility with the C++ use of new and delete for creating and destroying dynamic objects.

Finally, the YY_CURRENT_BUFFER macro returns a YY_BUFFER_STATE handle to the current buffer.

Here is an example of using these features for writing a scanner which eXPands include files (the <<EOF>> feature is discussed below):

/* the "incl" state is used for picking up the name* of an include file*/%x incl %{#define MAX_INCLUDE_DEPTH 10YY_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] );} }

Three 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 YY_BUFFER_STATE handle (which you should delete with yy_delete_buffer() when done with it). They also switch to the new buffer using yy_switch_to_buffer(), so the next call to yylex() will start scanning the string.

yy_scan_string(const char *str)
scans a NUL-terminated string.
共2页 9 7 [1] [28 :>

为flex提供多输入缓冲!Multiple input buffers 相关文章:
为flex提供多输入缓冲!Multiple input buffers 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com