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

被刻印的时光 ゝ 提交于 2019-12-13 04:46:30

问题


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

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.


回答1:


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

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.

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 );
}



回答2:


You can also use yy_scan_buffer(char*,size_t)



来源:https://stackoverflow.com/questions/5845652/is-there-working-example-of-flex-bison-with-input-from-string-not-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!