I want to create a read-eval-print loop using flex/bison parser. Trouble is, the flex generated lexer wants input of type FILE* and i would like it to be char*. Is there anyway to do this?
One suggestion has been to create a pipe, feed it the string and open the file descriptor and send to the lexer. This is fairly simple but it feels convoluted and not very platform independent. Is there a better way?
The following routines are available for setting up input buffers for scanning in-memory strings instead of files (as yy_create_buffer does):
YY_BUFFER_STATE yy_scan_string(const char *str)
: scans a NUL-terminated string`YY_BUFFER_STATE yy_scan_bytes(const char *bytes, int len)
: scans len bytes (including possibly NULs) starting at location bytesNote that both of these functions create, return a corresponding YY_BUFFER_STATE handle (which you must delete with yy_delete_buffer() when done with it) so yylex() scan a copy of the string or bytes. This behavior may be desirable since yylex() modifies the contents of the buffer it is scanning).
If you want avoid the copy (and yy_delete_buffer) using:
YY_BUFFER_STATE yy_scan_buffer(char *base, yy_size_t size)
sample main:
int main() { yy_scan_buffer("a test string"); yylex(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With