Bison:将Union语义类型与C ++解析器

时间:2017-04-04 22:42:15

标签: c++ bison

我一直在尝试在Bison中设置一个小解析器,但是当我尝试构建它时,我得到了:

  

stone.tab.cc:在成员函数'virtual int yy :: StoneParser :: parse()'中:   stone.tab.cc:507:81:错误:从'yy :: StoneParser :: semantic_type *'类型的右值开始,无效初始化'StoneDriver&'类型的非const引用         yyla.type = yytranslate_(yylex(& yyla.value,& yyla.location,driver));

我怀疑原因是使用联盟定义的语义类型与野牛生成的C ++编译器存在问题,但我似乎无法解决它......

如何使用'union'语义定义生成有效的C ++ bison解析器?

stone.yy:

%skeleton "lalr1.cc"
%require "3.0.4"

%defines
%define parser_class_name {StoneParser}

%code requires
{
# include <string>
class StoneDriver;
}

%param { StoneDriver& driver }

%locations

%code
{
# include "StoneDriver.hpp"
}

%union
{
   int intVal;
   char* stringVal;
}

%token <stringVal> KEY_ENUM
%token <stringVal> KEY_CLASS

%token K_TEST
%token T_ID

%type <stringVal> class
%type <stringVal> enum

%%

input: toplevel_declarations

toplevel_declarations: toplevel_declarations toplevel_declaration
    | %empty

toplevel_declaration: class
    | enum

class: KEY_CLASS { $$ = "test"; }

enum: KEY_ENUM { $$ = "test"; }

StoneDriver.hpp

#ifndef STONEDRIVER_INCLUDED_HPP
#define STONEDRIVER_INCLUDED_HPP
#include <string>
#include <map>
#include "stone.tab.hh"

class StoneDriver;

//Tell Flex the lexer's prototype ...
#define YY_DECL \
  yy::StoneParser::symbol_type yylex (StoneDriver& driver)
// ... and declare it for the parser's sake.
YY_DECL;

//Scans and parses Stone
class StoneDriver
{
   public:
      //Constructor
      StoneDriver();

      //Destructor
      virtual ~StoneDriver();

  std::map<std::string, int> variables;

  int result;
  // Handling the scanner.
  void scan_begin ();
  void scan_end ();
  bool trace_scanning;
  // Run the parser on file F.
  // Return 0 on success.
  int parse (const std::string& f);
  // The name of the file being parsed.
  // Used later to pass the file name to the location tracker.
  std::string file;
  // Whether parser traces should be generated.
  bool trace_parsing;
  // Error handling.
  void error (const yy::location& l, const std::string& m);
  void error (const std::string& m);
};//StoneDriver

#endif

StoneDriver.cpp:

#include "StoneDriver.hpp"
#include "stone.tab.hh"

StoneDriver::StoneDriver()
  : trace_scanning (false), trace_parsing (false)
{
   variables["one"] = 1;
   variables["two"] = 2;
}//constructor

StoneDriver::~StoneDriver()
{
}//destructor

int StoneDriver::parse(const std::string &f)
{
   file = f;
   scan_begin();
   yy::StoneParser parser(*this);

   #if YYDEBUG
      parser.set_debug_level(trace_parsing);
   #endif

   int res = parser.parse();
   scan_end();

   return res;
}//parse

void StoneDriver::error(const yy::location& l, const std::string& m)
{
  std::cerr << l << ": " << m << std::endl;
}//error

void StoneDriver::error(const std::string& m)
{
  std::cerr << m << std::endl;
}//error

解: Rici表明YY_DECL的方法签名应该这样定义:

int yylex (semantic_type* YYLVAL, location_type* YYLLOC, TYPE1 ARG1, ...)

就我而言,神奇的界限是:

#define YY_DECL int yylex( yy::StoneParser::semantic_type* const lval, yy::StoneParser::location_type* location, StoneDriver& driver )
  • yy::是我的Bison文件中的命名空间(在我的情况下是stone.yy)
  • StoneParser是来自stone.yy
  • 的已定义的parser_class_name
  • location_type是必需的,因为stone.yy有%位置 定义
  • StoneDriver&是来自stone.yy的解析上下文, 定义为%param { StoneDriver& driver }

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

错误消息提供了一个有问题的明确指示,尽管它奇怪地关注yylex的第一个参数中的类型不匹配,而不是您为{{1}提供的原型这一事实。只有一个参数,而调用有三个参数。 Clang更清楚:

yylex

bison信息文件的第10.1.5.1节描述了来自C ++扫描程序的stone.tab.cc:474:39: error: no matching function for call to 'yylex' yyla.type = yytranslate_ (yylex (&yyla.value, &yyla.location, driver)); ^~~~~ ./stone_driver.hpp:14:1: note: candidate function not viable: requires single argument 'driver', but 3 arguments were provided YY_DECL; ^ 的调用约定,其语义类型为yylex

union

这实际上与C API中的纯解析器使用的调用约定相同:The interface is as follows. -- Method on parser: int yylex (semantic_type* YYLVAL, location_type* YYLLOC, TYPE1 ARG1, ...) -- Method on parser: int yylex (semantic_type* YYLVAL, TYPE1 ARG1, ...) Return the next token. Its type is the return value, its semantic value and location (if enabled) being YYLVAL and YYLLOC. Invocations of ‘%lex-param {TYPE1 ARG1}’ yield additional arguments. 的前两个参数(如果启用了位置,就像程序中的情况一样)是地址语义值对象和位置对象的地址;第三个和后续参数是yylex指定的参数(如果存在)。

因此,您应该根据上述原型修复%param的定义。

您还必须更改您的flex操作以将YY_DECL(或任何您称为第一个参数的内容)视为指针而不是联合,这主要意味着将yylval更改为{{1} }。