class 名称未在 flex c++ 扫描器中声明

class name is not declared in flex c++ scanner

我正在尝试使用 flex 生成一个 c++ 扫描器,下面是 flex 和 g++ 打印出的消息。

|| flex -o step1_noinc.cpp --c++ --yyclass=step1_noinc  step1_noinc.lex
|| g++  -c step1_noinc.cpp
step1_noinc.cpp|345 col 21| error: ‘step1_noinc’ has not been declared                                                                                                                                              
||  #define YY_DECL int step1_noinc::yylex()

说class名字还没有申报,请问这是怎么回事?

我尽可能地简化我的 lex 代码,如下所示,以简化调试:

%%

<*>\n {
}

%%

是的,由您来定义class。

来自flex manual

--yyclass=NAME, %option yyclass="NAME"

only applies when generating a C++ scanner (the --c++ option). It informs flex that you have derived NAME as a subclass of yyFlexLexer… (emphasis added)

请注意该句子中 "you" 一词的使用。 Flex 始终定义 yyFlexLexer class(可能会更改 yy 前缀),但它也允许您子 class,以防您在一个项目中有多个不同的类似扫描仪.

除了更改前缀 yy 外,据我所知您不能修改 class flex 生成的名称。