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

烂漫一生 提交于 2019-12-07 16:53:55

问题


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:

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:

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. #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;
   }

回答1:


The simplest solution is probably to add a separate function in your grammar file that in turn call 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.



来源:https://stackoverflow.com/questions/12564825/how-to-use-yy-scan-stringconst-char-str-generated-by-lex-yacc-in-a-separ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!