Flex Bison Reentrant C++ 解析器:yyscanner 未声明的标识符

问题描述

我正在尝试使用带有 flex 和 bison 的 C++ 创建一个可重入的解析器。我也在使用驱动程序类 Driver。我在 lex 方面收到错误 'yyscanner' : undeclared identifier。我认为这与 flex 中的可重入选项有关,这很奇怪,因为我认为 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"
}

...

词法分析器.l


%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

解决方法

如果生成可重入扫描器,则必须定义其原型以包含类型为 g++ main.cpp -llapacke -llapack -lblas 的名为 yyscanner 的参数。您需要使用该参数调用扫描仪。您不能替换为 yyscan_t 参数定义的某些类型,因为您的类型不包括 flex 生成的扫描器使用的数据成员。并且您必须使用名称 yyscan_t,因为 Flex 生成的代码就是这样引用数据成员的。

如果您想要一个只需要一个参数的 yyscanner,那么“官方”的方法是将您的数据成员(或指向它们的指针)放入 {{1} 中的“额外数据” }}。但是只使用两个参数可能更容易。