how to parse from a string rather than a file [duplicate]

纵然是瞬间 提交于 2019-11-29 12:59:50

I fought through this problem myself very recently. The flex documentation on the subject leaves a bit to be desired.

I see two things right off the bat that might be tripping you up. First, note that your string needs to be double NULL terminated. That is, you need to take a regular, NULL terminated string and add ANOTHER NULL terminator at the end of it. That fact is buried in the flex documentation, and it took me a while to find as well.

Second, you've left off a call to "yy_switch_to_buffer". This is also not particularly clear from the documentation. If you change your code to something like this, it should work.

// add the second NULL terminator
int len = strlen(my_string);
char *temp = new char[ len + 2 ];
strcpy( temp, my_string );
temp[ len + 1 ] = 0; // The first NULL terminator is added by strcpy

YY_BUFFER_STATE my_string_buffer = yy_scan_string(temp); 
yy_switch_to_buffer( my_string_buffer ); // switch flex to the buffer we just created
yyparse(); 
yy_delete_buffer(my_string_buffer );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!