问题
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