c++ - Flex Bison 可重入 C++ 解析器:yyscanner 未声明的标识符
问题描述
我正在尝试使用带有 flex 和 bison 的 C++ 创建一个可重入解析器。我也在使用驱动程序类Driver
。我在'yyscanner' : undeclared identifier
lex 方面遇到了错误。我认为它与 flex 中的 reentrant 选项有关,这很奇怪,因为我会假设它driver
会被使用,因为我声明yylex
为yylex(Driver& driver)
. driver.h
我在下面的代码中做错了什么以获得此错误消息吗?任何帮助将非常感激。
解析器.y
%require "3.2"
%define parse.error verbose
%locations
%param { Driver& driver }
%language "c++"
%define api.value.type variant
%code requires {
class Driver;
}
%code {
#include "driver.h"
}
...
词法分析器
%option noyywrap noinput nounput
%option nodefault
%option nounistd
%option reentrant
%{
#include <iostream>
#include <string>
#include "driver.h"
#include "parser.tab.h"
%}
%%
%{
yy::location& loc = driver.location;
%}
...
驱动程序.h
#ifndef DRIVER_HH
#include "parser.tab.h"
#define YY_DECL \
int yylex(Driver& driver)
YY_DECL;
class Driver
{
public:
Driver() {}
yy::location location;
};
#endif // ! DRIVER_HH
解决方案
如果你生成一个可重入扫描器,你必须定义它的原型来包含一个名为yyscanner
type的参数yyscan_t
。您需要使用该参数调用扫描仪。您不能替换为yyscan_t
参数定义的某些类型,因为您的类型不包括 flex 生成的扫描仪使用的数据成员。而且您必须使用该名称yyscanner
,因为这就是 Flex 生成的代码引用数据成员的方式。
如果你想要一个yylex
只需要一个参数的“官方”方法,那就是将你的数据成员(或指向它们的指针)放入yyscan_t
. 但只使用两个参数可能更容易。
推荐阅读
- arrays - 如何使用 VBA 逐行读取文本文件并放入 excel 工作表单元格?读取线不工作
- docusignapi - DocuSign 属性 ApplyAnchorTabs 是什么意思
- angular - 以角度形式显示数据
- javascript - Openlayers自定义控件事件监听器获取地图
- tarantool - Tarantool 模块安装失败搜索清单
- c# - 合并包含列表的字典的最有效方法是什么
- javascript - 语句在查询中不起作用是否打印正确的语句
- angular - 在调用之前动态生成 Angular 方法名称
- lighttpd - 如何将 lightpd 配置为仅阻止外部 IP 而不是 localhost 的 http 连接
- php - PhpStorm 缺少对受保护属性的属性类型声明