1

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

4

1 回答 1

5

我最近自己解决了这个问题。关于该主题的 flex 文档还有一些不足之处。

我马上看到两件事可能会让你绊倒。首先,请注意您的字符串需要以双 NULL 终止。也就是说,您需要采用常规的 NULL 终止字符串并在其末尾添加另一个 NULL 终止符。这个事实隐藏在 flex 文档中,我也花了一段时间才找到。

其次,您停止了对“yy_switch_to_buffer”的调用。这在文档中也不是特别清楚。如果您将代码更改为这样的内容,它应该可以工作。

// 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 );
于 2009-12-15T19:41:39.753 回答