how to use yy_scan_string in lex

ajai picture ajai · Dec 15, 2009 · Viewed 13.6k times · Source

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.

Answer

Eric picture Eric · Jun 14, 2011

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;
}