问题
Possible Duplicate:
How to make YY_INPUT point to a string rather than stdin in Lex & Yacc (Solaris)
i want to parse from a string rather than a file. i know that v can use yy_scan_string fn to do it.but for me it's not working properly so pls help me
回答1:
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 );
来源:https://stackoverflow.com/questions/1909166/how-to-parse-from-a-string-rather-than-a-file