how to use yy_scan_string in lex

2019-01-11 20:57发布

I want to parse a string which I give to the parser in the main function of yacc . I know that this could be done by using yy_scan_string but I don't know how to use it. I searched the web and the man pages but it is still not clear to me. Please help me.

标签: c yacc lex
5条回答
爷、活的狠高调
2楼-- · 2019-01-11 21:04

This works for me. I have this code in the subroutines section (i.e. the third section) of my Bison file:

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;
}
查看更多
Root(大扎)
3楼-- · 2019-01-11 21:08

I've found and example here to myself. May it can be usefull for you:

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

查看更多
聊天终结者
4楼-- · 2019-01-11 21:09

I always recommend this page to people who want to learn lex/yacc (or flex/bison)

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-11 21:13

This worked for me ... use 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);
}
查看更多
Luminary・发光体
6楼-- · 2019-01-11 21:23

In case anyone needs the sample for a re-entrant lexer:

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;
}
查看更多
登录 后发表回答