如何在lex中使用yy_scan_string

aja*_*jai 7 c yacc lex

我想在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)

  • 如果其他人在尝试此操作时遇到未定义符号或其他此类错误:请记住在词法分析器文件中包含 `%option reentrant`。 (2认同)

mar*_*vak 7

这适合我.我在我的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)


Wer*_*sey 3

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