Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use yy_scan_string in lex

Tags:

c

yacc

lex

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.

like image 376
ajai Avatar asked Dec 15 '09 14:12

ajai


2 Answers

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;
}
like image 64
Eric Avatar answered Nov 10 '22 22:11

Eric


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;
}
like image 20
markonovak Avatar answered Nov 10 '22 23:11

markonovak