0

我不明白如何将 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

提前致谢!

4

2 回答 2

2

当我用 bison/g++ 编译你的代码时,我得到了错误:

parser.y:16:15: error: member ‘none_type YYSTYPE::none_value’ with constructor not allowed in union
parser.y:16:15: error: member ‘none_type YYSTYPE::none_value’ with destructor not allowed in union
parser.y:16:15: note: unrestricted unions only available with -std=c++0x or -std=gnu++0x

它可以准确地告诉您问题所在——您不能将非 POD 类型放在联合中,因为编译器无法判断要调用哪个 ctor/dtor。请注意您可以在 C++ 11 中执行此操作的注释,但这并没有真正的帮助,因为在这种情况下,它不会自动为您调用 ctor/dtor,因此不会正确地构造或销毁东西。

于 2012-10-24T22:20:46.273 回答
2

如果您想尝试将真实对象放入堆栈中,请查看 Bison 当前的 master 分支,您可以在其中运行以下示例

%token <::std::string> TEXT;
%token <int> NUMBER;
%token END_OF_FILE 0;
%type <::std::string> item;
%type <::std::list<std::string>> list;
%printer { yyoutput << $$; } <int> <::std::string> <::std::list<std::string>>;

%%

result:
  list  { std::cout << $1 << std::endl; }
;

list:
  /* nothing */ { /* Generates an empty string list */ }
| list item     { std::swap ($$, $1); $$.push_back ($2); }
;

item:
  TEXT          { std::swap ($$, $1); }
| NUMBER        { $$ = string_cast ($1); }
;
%%

// The yylex function providing subsequent tokens:
// TEXT         "I have three numbers for you:"
// NUMBER       1
// NUMBER       2
// NUMBER       3
// TEXT         " and that's all!"
// END_OF_FILE

static
yy::parser::symbol_type
yylex ()
{
  static int stage = -1;
  ++stage;
  yy::parser::location_type loc(0, stage + 1, stage + 1);
  switch (stage)
  {
    case 0:
      return yy::parser::make_TEXT ("I have three numbers for you.", loc);
    case 1:
    case 2:
    case 3:
      return yy::parser::make_NUMBER (stage, loc);
    case 4:
      return yy::parser::make_TEXT ("And that's all!", loc);
    default:
      return yy::parser::make_END_OF_FILE (loc);
  }
}

获得此功能的可能用户的意见将是最有用的。例如在 gnu.org 上的 help-bison。请参阅https://savannah.gnu.org/git/?group=bison以获取对存储库的 Git 访问权限。

于 2012-10-25T07:54:22.183 回答