是否有使用字符串而不是文件输入的flex + bison的工作示例? [英] Is there working example of flex + bison with input from string, not file?

查看:142
本文介绍了是否有使用字符串而不是文件输入的flex + bison的工作示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用字符串而不是文件输入的flex + bison(需要野牛)的工作示例?

Is there working example of flex + bison (bison is necessary) with input from string, not file?

我尝试使用YY_BUFFER_STATE ...函数而不是yyin并收到错误柔性扫描仪推回溢出". Flex将InputString [1]更改为0.SO上的其他几个答案无济于事-实际代码会有用得多.

I have tried to use YY_BUFFER_STATE ... functions instead of yyin and got error "flex scanner push-back overflow". Flex changes InputString[1] to 0. Several other answers on SO are of little help - actual code will be much more useful.

推荐答案

描述了扫描内存区域的方法 Flex手册中的.

The way to scan memory region is described in the Flex manual.

Flex修改yy_scan_buffer给定的缓冲区.如果需要避免被修改,请yy_scan_bytesyy_scan_string 合适.

Flex modifies the buffer given by yy_scan_buffer. If you need to avoid to be modified, yy_scan_bytes or yy_scan_string will be appropriate.

扫描文件或内存区域是否独立于解析器(野牛).

Whether scanning a file or memory region is independent of the parser (bison).

如果您的词法分析器失败,我建议甚至检查它是否也失败 从文件读取时.

If your lexer fails, I'd recommend checking whether it fails too even when reading from a file.

供您参考,以下弹性代码在我的环境中打印abcd.

For your information, the following flex code prints ab and cd in my environment.

%%

[a-z]+  puts( yytext );
.
\n

%%

int yywrap( void ) { return 1; }

int main() {
  yy_scan_string("ab cd");
  yylex();
  yy_delete_buffer( YY_CURRENT_BUFFER );
}

这篇关于是否有使用字符串而不是文件输入的flex + bison的工作示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆