如何使yyparse函数从C字符串读取其输入?使用Bison从char *字符串解析
试图yy_scan_buffer(使用 “FP” 的前缀,而不是 “YY”):
extern YY_BUFFER_STATE fs_scan_string (const char *str);
struct fs *parse(char *s)
{
fp_scan_buffer(s);
int r = fpparse();
return r == 0 ? AST : NULL;
}
但是:
h1.cpp:27: error: ‘YY_BUFFER_STATE’ does not name a type
h1.cpp: In function ‘fs* parse(char*)’:
h1.cpp:32: error: ‘fp_scan_buffer’ was not declared in this scope
使用yy_delete_buffer,同样的结果尝试:在未声明这个范围。
fp.y
%{
#include "ft.h"
#include <map>
#include <iostream>
int fplex();
int fperror(char *p);
int fperror(char *p) { }
using namespace std;
struct fs *AST;
bool fpworking = true;
%}
%union {
struct fs *f;
struct ts *t;
std::list<struct ts *> *tl;
std::string *s;
}
%token END_OF_FILE
MORE TOKEN HERE...
%%
s : formula '\n' { AST = $1; fpworking = true; YYACCEPT; }
MORE RULE HERE...
FP.L
%{
#include <iostream>
#include <list>
using namespace std;
#include "fp.tab.h"
%}
%option noyywrap
%%
[a-z][a-zA-Z0-9]* { fplval.s = new std::string(fptext); return (TERM_ID); }
MORE PATTERN HERE...
h1.cpp
#include <iostream>
#include <list>
#include <string>
#include <map>
#include <stdlib.h>
#include <fstream>
#include "ft.h"
int fpparse();
int signparse();
extern bool fpworking;
extern struct fs *AST;
int main(int argc, char **argv)
{
MORE CODE HERE...
}
请注意正确的标记。 Flex用于Adobe/Apache UI框架。 Flex-lexer用于词法分析器。 – JeffryHouser
问题出在您显示的代码与您未显示的代码之间的交互中。请构建一个完整的,自包含的,精简的.y文件,演示相同的问题。 – zwol