Flex and Bison:C 用户定义的类

flex and bison: C++ user-defined class

本文关键字:定义 用户 and Bison Flex      更新时间:2023-10-16

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

预先感谢!

当我用野牛/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,因此东西根本不会被正确地结构或破坏。

如果要玩堆栈上的真实对象,请查看当前的野牛的主分支,您可以在其中运行以下示例,例如以下

%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 for Git访问存储库。