首页 > 解决方案 > Flex Bison 可重入 C++ 解析器:yyscanner 未声明的标识符

问题描述

我正在尝试使用带有 flex 和 bison 的 C++ 创建一个可重入解析器。我也在使用驱动程序类Driver。我在'yyscanner' : undeclared identifierlex 方面遇到了错误。我认为它与 flex 中的 reentrant 选项有关,这很奇怪,因为我会假设它driver会被使用,因为我声明yylexyylex(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

标签: c++bisonflex-lexer

解决方案


如果你生成一个可重入扫描器,你必须定义它的原型来包含一个名为yyscannertype的参数yyscan_t。您需要使用该参数调用扫描仪。您不能替换为yyscan_t参数定义的某些类型,因为您的类型不包括 flex 生成的扫描仪使用的数据成员。而且您必须使用该名称yyscanner,因为这就是 Flex 生成的代码引用数据成员的方式。

如果你想要一个yylex只需要一个参数的“官方”方法,那就是将你的数据成员(或指向它们的指针)放入yyscan_t. 但只使用两个参数可能更容易。


推荐阅读