我不明白如何将 C++ 用户定义的类嵌入到bison
解析器中。这是我所拥有的(只是一些必要的部分;如果您需要,我可以发布所有代码)。
scanner.l
%{
#include "parser.tab.h"
#include "types.h"
#include <iostream>
#include <string>
#define YY_DECL extern "C" int yylex()
using namespace std;
int chars = 0;
int words = 0;
int lines = 0;
extern "C" {
int yylex(void);
} /* extern "C" */
%}
%%
"none" {
yylval.none_value = none_type();
return NONE;
} /* none type */
{DIGIT_BIN}|{DIGIT_OCT}|{DIGIT_DEC}|{DIGIT_HEX} {
yylval.int_value = atoi(yytext);
return INT;
} /* int type */
parser.y
%{
#include "types.h"
#include <iostream>
using namespace std;
void yyerror(const char *error) {
cerr << error << endl;
} /* error handler */
extern "C" {
int yylex(void);
int yyparse(void);
int yywrap() { return 1; }
} /* extern "C" */
%}
%union {
none_type none_value; /* HERE IS WHAT I WANT */
int int_value;
} /* union */
%token <none_value> NONE
%token <int_value> INT
types.h
#include <iostream>
class none_type {
public:
none_type(void);
~none_type();
}; /* none_type */
如您所见,这里的代码并不完整,但应该足以描述我想要的内容。我对默认 C++ 类型所做的一切都运行良好;我可以实现自己的课程吗?
编译器返回这样的错误:
parser.y:20:3: error: 'none_value' does not name a type
In file included from scanner.l:3:0:
parser.y:20:3: error: 'none_value' does not name a type
scanner.l: In function 'int yylex()':
scanner.l:54:32: error: cannot convert 'none_type' to 'int' in assignment
make: *** [caesar] Error 1
提前致谢!