Reputation: 1118
I create an .exe FILE, which can parser an expression, which is generated by lex and yacc. But I do it just get the input from screen, and just return the parser result from screen. I saw some suggestions about using YY_BUFFER_STATE yy_scan_buffer(char *base, yy_size_t size)
, but I still could not find a good way to do it.
Is it possible that I put some headers (which is compiled by lex yacc) to my main program c++, and then I can use yylex() to call it, giving a string as input, and get the return value in the main program? Thanks for your help, I am confused about how to realize it. Thanks.
Upvotes: 0
Views: 508
Reputation: 126175
yy_scan_string
is how you give flex a string as input. You call that first, and then call yylex
and it will use that string as the input to get tokens from rather than stdin
. When you get an EOF from yylex
, it has scanned the entire string. You can then call yy_delete_buffer
on the YY_BUFFER_STATE
returned by yy_scan_string
(to free up memory) and call yy_scan_string
again if you want to scan a new string.
You can use yy_scan_buffer
instead to save a bit of copying, but then you have to set up the buffer properly yourself (basically, it needs to end with two NUL bytes instead of just one).
Unfortunately, there's no standard header file from flex declaring these. So you need to either declare them yourself somewhere (copy the declarations from the flex documentation), or call them in the 3rd section of the .l
file, which is copied verbatim to the end of the lex.y.c
file.
Upvotes: 1