如何在lex中使用yy_scan_string

我想在yacc的main函数中解析我给解析器的字符串。 我知道这可以通过使用yy_scan_string来完成,但我不知道如何使用它。 我搜索了网页和手册页,但我仍然不清楚。 请帮我。

如果有人需要样本用于重入词法分析器:

 int main(void) { yyscan_t scanner; YY_BUFFER_STATE buf; yylex_init(&scanner); buf = yy_scan_string("replace me with the string youd like to scan", scanner); yylex(scanner); yy_delete_buffer(buf, scanner); yylex_destroy(scanner); return 0; } 

这对我有用。 我在我的Bison文件的子例程部分(即第三部分)中有这个代码:

 struct eq_tree_node *parse_equation(char *str_input) { struct eq_tree_node *result; yy_scan_string(str_input); yyparse(); /* to avoid leakage */ yylex_destroy(); /* disregard this. it is the function that I defined to get the result of the parsing. */ result = symtab_get_parse_result(); return result; } 

我总是向想要学习lex / yacc(或flex / bison)的人推荐这个页面

这对我有用…使用yy_scan_string()

 int main(int argc, char **argv) { char Command[509]; int ReturnVal; char input[40] = "This is my input string"; /*Copy string into new buffer and Switch buffers*/ yy_scan_string (input); /*Analyze the string*/ yylex(); /*Delete the new buffer*/ yy_delete_buffer(YY_CURRENT_BUFFER); } 

我在这里找到了自己的榜样。 可能对你有用:

http://osdir.com/ml/lex.flex.windows/2003-04/msg00008.html