String input to flex lexer

前端 未结 8 586
梦如初夏
梦如初夏 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 04:59

    here is a small example for using bison / flex as a parser inside your cpp code for parsing string and changing a string value according to it (few parts of the code were removed so there might be irrelevant parts there.) parser.y :

    %{
    #include "parser.h"
    #include "lex.h"
    #include  
    #include 
    #include  
    #include 
    #include 
    using namespace std;
     int yyerror(yyscan_t scanner, string result, const char *s){  
        (void)scanner;
        std::cout << "yyerror : " << *s << " - " << s << std::endl;
        return 1;
      }
        %}
    
    %code requires{
    #define YY_TYPEDEF_YY_SCANNER_T 
    typedef void * yyscan_t;
    #define YYERROR_VERBOSE 0
    #define YYMAXDEPTH 65536*1024 
    #include  
    #include 
    #include  
    #include 
    #include 
    }
    %output "parser.cpp"
    %defines "parser.h"
    %define api.pure full
    %lex-param{ yyscan_t scanner }
    %parse-param{ yyscan_t scanner } {std::string & result}
    
    %union {
      std::string *  sval;
    }
    
    %token TOKEN_ID TOKEN_ERROR TOKEN_OB TOKEN_CB TOKEN_AND TOKEN_XOR TOKEN_OR TOKEN_NOT
    %type   TOKEN_ID expression unary_expression binary_expression
    %left BINARY_PRIO
    %left UNARY_PRIO
    %%
    
    top:
    expression {result = *$1;}
    ;
    expression:
    TOKEN_ID  {$$=$1; }
    | TOKEN_OB expression TOKEN_CB  {$$=$2;}
    | binary_expression  {$$=$1;}
    | unary_expression  {$$=$1;}
    ;
    
    unary_expression:
     TOKEN_NOT expression %prec UNARY_PRIO {result =  " (NOT " + *$2 + " ) " ; $$ = &result;}
    ;
    binary_expression:
    expression expression  %prec BINARY_PRIO {result = " ( " + *$1+ " AND " + *$2 + " ) "; $$ = &result;}
    | expression TOKEN_AND expression %prec BINARY_PRIO {result = " ( " + *$1+ " AND " + *$3 + " ) "; $$ = &result;} 
    | expression TOKEN_OR expression %prec BINARY_PRIO {result = " ( " + *$1 + " OR " + *$3 + " ) "; $$ = &result;} 
    | expression TOKEN_XOR expression %prec BINARY_PRIO {result = " ( " + *$1 + " XOR " + *$3 + " ) "; $$ = &result;} 
    ;
    
    %%
    
    lexer.l : 
    
    %{
    #include 
    #include "parser.h"
    
    %}
    %option outfile="lex.cpp" header-file="lex.h"
    %option noyywrap never-interactive
    %option reentrant
    %option bison-bridge
    
    %top{
    /* This code goes at the "top" of the generated file. */
    #include 
    }
    
    id        ([a-zA-Z][a-zA-Z0-9]*)+
    white     [ \t\r]
    newline   [\n]
    
    %%
    {id}                    {    
        yylval->sval = new std::string(yytext);
        return TOKEN_ID;
    }
    "(" {return TOKEN_OB;}
    ")" {return TOKEN_CB;}
    "*" {return TOKEN_AND;}
    "^" {return TOKEN_XOR;}
    "+" {return TOKEN_OR;}
    "!" {return TOKEN_NOT;}
    
    {white};  // ignore white spaces
    {newline};
    . {
    return TOKEN_ERROR;
    }
    
    %%
    
    usage : 
    void parse(std::string& function) {
      string result = "";
      yyscan_t scanner;
      yylex_init_extra(NULL, &scanner);
      YY_BUFFER_STATE state = yy_scan_string(function.c_str() , scanner);
      yyparse(scanner,result);
      yy_delete_buffer(state, scanner);
      yylex_destroy(scanner);
      function = " " + result + " ";  
    }
    
    makefile:
    parser.h parser.cpp: parser.y
        @ /usr/local/bison/2.7.91/bin/bison -y -d parser.y
    
    
    lex.h lex.cpp: lexer.l
        @ /usr/local/flex/2.5.39/bin/flex lexer.l
    
    clean:
        - \rm -f *.o parser.h parser.cpp lex.h lex.cpp
    

提交回复
热议问题