String input to flex lexer

前端 未结 8 563
梦如初夏
梦如初夏 2020-11-29 04:42

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 an

相关标签:
8条回答
  • 2020-11-29 05:02

    There's this funny code in libmatheval:

    /* Redefine macro to redirect scanner input from string instead of
     * standard input.  */
    #define YY_INPUT( buffer, result, max_size ) \
    { result = input_from_string (buffer, max_size); }
    
    0 讨论(0)
  • 2020-11-29 05:07

    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 bytes

    Note 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();
    }
    
    0 讨论(0)
提交回复
热议问题