【问题标题】:Compiling multiple lexers with flex gives redefinition errors使用 flex 编译多个词法分析器会导致重新定义错误
【发布时间】:2015-07-02 12:35:44
【问题描述】:

我有两个词法分析器 - shell.l 和 javascript.l,分别带有前缀(%option 前缀)shell 和 javascript(shell.l 中的 %option prefix="shell" 和 javascript.l 中的 %option prefix="javascript" )。

我从另一个文件 (main_file.c) 依次调用词法分析器:

somefunc(){
.....
shelllex();

......
javascriptlex();
}

为了调用这些,我在 main_file.c 中包含了这两个词法分析器的头文件:

#include <.....>
#include "lex.shell.h"
#include "lex.javascript.h" 

而且,当我将 flex 文件编译为:

flex --header-file=lex.shell.h shell.l
flex --header-file=lex.javascript.h javascript.l
gcc -o lang lex.shell.c lex.javascript.c main_file.c -lfl

当我编译 main_file.c 时,我得到如下重定义错误:

在 code_detector.c:16:0 包含的文件中:

lex.javascript.h:227:29: error: redefinition of ‘yy_nxt’
 static yyconst flex_int16_t yy_nxt[][128] =
                             ^
In file included from code_detector.c:15:0:
lex.shell.h:227:29: note: previous definition of ‘yy_nxt’ was here
 static yyconst flex_int16_t yy_nxt[][128] =

我浏览了其他几篇 SO 帖子,但没有找到太多帮助。 对于解决这些问题,我将不胜感激!

谢谢!

【问题讨论】:

  • 你用的是什么版本的 flex?

标签: c gcc flex-lexer


【解决方案1】:

显然,如果存在%option full,则存在导致扫描仪转换表yy_nxt 错误地写入头文件的错误。这应该在最新版本的 flex (2.5.39) 中得到修复。

如果您不想升级您的 flex 版本,一个简单的解决方法是避免使用%option full。您可能会发现速度损失无法衡量。

【讨论】:

    猜你喜欢
    • 2020-09-11
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 2020-02-21
    • 2013-04-18
    相关资源
    最近更新 更多