简体   繁体   English

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

[英]how to use `yy_scan_string(const char *str)` (generated by lex yacc ) in a separated file

I want to use YY_BUFFER_STATE yy_scan_string(const char *str) and other functions like yyparse() in my main.cpp , I did things: 我想在我的main.cpp使用YY_BUFFER_STATE yy_scan_string(const char *str)yyparse()等其他函数,我做了一些事情:

extern "C"{
extern YY_BUFFER_STATE yy_scan_string(const char *str);
}

But there is a error error: YY_BUFFER_STATE' does not name a type`, then I did: 但是有一个错误error: YY_BUFFER_STATE'没有命名类型`,然后我做了:

extern yy_buffer_state;
typedef yy_buffer_state *YY_BUFFER_STATE;
extern int yyparse();
extern YY_BUFFER_STATE yy_scan_buffer(char *, size_t);

But the same problem also, how to do it, thanks, really appreciate your help!! 但同样的问题,怎么做,谢谢,真的很感谢你的帮助!

Here is the main.cpp file. 这是main.cpp文件。 #include "main.h" #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;
   }

The simplest solution is probably to add a separate function in your grammar file that in turn call yy_scan_string . 最简单的解决方案可能是在语法文件中添加一个单独的函数,该函数又调用yy_scan_string

/* Stuff... */

%%

/* Grammar */

%%

void scan_string(const char* str)
{
    yy_switch_to_buffer(yy_scan_string(str));
}

Then call scan_string from your code. 然后从您的代码中调用scan_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM