第二章 flex输入输出结构

对于一个词法分析程序,一般读取文件或者终端

一个默认lex程序大致看上去像这样

YY_BUFFER_STATE bp;
extern FILE* yyin;
  ... whatever the program does before the first call to the scanner
if(!yyin) yyin = stdin; default input is stdin
bp = yy_create_buffer(yyin,YY_BUF_SIZE );   YY_BUF_SIZE defined by flex, typically 16K yy_switch_to_buffer(bp); tell it to use the buffer we just made
yylex(); or yyparse() or whatever calls the scanner

1. yy_create_buffer(yyin,YY_BUF_SIZE )

创建一个缓冲区

2. yy_switch_to_buffer(bp);

让lex从缓冲区读取输入

flex输入管理的三个层次

  • 设置yyin来读取所需文件
  • 创建并使用YY_BUFFER_STATE输入缓冲区
  • 重新定义YY_INPUT
原文地址:https://www.cnblogs.com/xzpp/p/4638119.html