如何在单独的文件中使用`yy_scan_string(const char *str)`(由 lex yacc 生成)

CJA*_*LEE 4 c++ redirect yacc lex bison

我想在 my 中使用YY_BUFFER_STATE yy_scan_string(const char *str)和其他功能,我做了以下事情:yyparse()main.cpp

extern "C"{
extern YY_BUFFER_STATE yy_scan_string(const char *str);
}
Run Code Online (Sandbox Code Playgroud)

但是有一个错误error:YY_BUFFER_STATE' does not name a type`,然后我做了:

extern yy_buffer_state;
typedef yy_buffer_state *YY_BUFFER_STATE;
extern int yyparse();
extern YY_BUFFER_STATE yy_scan_buffer(char *, size_t);
Run Code Online (Sandbox Code Playgroud)

但是同样的问题,怎么办,谢谢,非常感谢您的帮助!!

这是 main.cpp 文件。#include "main.h"

 #include <string.h>
 extern "C"{void scan_string(const char* str);}
 int yyparse();
 void test::getvalue(int& var)
 {
    if (var!=0)
        std::cout<<"True"<<std::endl;
    else
        std::cout<<"False"<<std::endl;
  }

  int main(){
     std::string str="T+F";
     //how to send str as an Input to parse?

     yyparse();
     return 0;
   }
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 5

最简单的解决方案可能是在语法文件中添加一个单独的函数,然后调用yy_scan_string.

/* Stuff... */

%%

/* Grammar */

%%

void scan_string(const char* str)
{
    yy_switch_to_buffer(yy_scan_string(str));
}
Run Code Online (Sandbox Code Playgroud)

然后scan_string从您的代码中调用。