我想在yacc的main函数中解析我给解析器的字符串.我知道这可以通过使用来完成,yy_scan_string但我不知道如何使用它.我搜索了网页和手册页,但我仍然不清楚.请帮我.
Eri*_*ric 16
如果有人需要样本用于重入词法分析器:
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;
}
Run Code Online (Sandbox Code Playgroud)
这适合我.我在我的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;
}
Run Code Online (Sandbox Code Playgroud)