Bison:将联合语义类型与C++解析器一起使用
Bison: using the Union semantic type with a C++ parser
我一直在尝试在Bison中设置一个小解析器,但是当我尝试构建它时,我得到:
stone.tab.cc:在成员函数'virtual int yy::StoneParser::p arse()'中: stone.tab.cc:507:81:错误:从类型为"yy::StoneParser::semantic_type*"的右值初始化类型为"StoneDriver&"的非常量引用无效 yyla.type = yytranslate_ (yylex (&yyla.value, &yyla.location, driver));
我怀疑原因是将联合定义的语义类型与野牛生成的C++编译器一起使用时存在问题,但我似乎无法解决它......
如何生成具有"联合"语义定义的有效C++野牛解析器?
石.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"; }
石匠.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
石匠.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_namelocation_type
是必需的,因为 stone.yy 具有 %位置 定义。StoneDriver&
是来自 stone.yy 的解析上下文, 定义为%param { StoneDriver& driver }
感谢您的帮助!
错误消息提供了问题所在,尽管奇怪的是它侧重于第一个参数中的类型不匹配yylex
而不是您为yylex
提供的原型只有一个参数而调用有三个参数的事实。叮当更清楚:
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;
^
bison info 文件第 10.1.5.1 节描述了来自C++扫描程序的yylex
调用约定,具有union
语义类型:
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.
这实际上与 C API 中纯解析器使用的调用约定相同:要yylex
的前两个参数(如果启用了位置,就像在程序中一样)是语义值对象的地址和位置对象的地址;第三个和后续参数是%param
指定的参数(如果存在)。
所以你应该根据上面的原型来修复YY_DECL
的定义。
您还必须更改 flex 操作以将yylval
(或任何您称为第一个参数的内容)视为指针而不是联合,这主要意味着将.
更改为->
。
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 在将 new 与指针一起使用时,创建数组的指定长度