gpt4 book ai didi

c++ - 将 flex 2.5.4a 迁移到 2.6(词法分析器生成器)

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:48 25 4
gpt4 key购买 nike

我有一个使用 flex 生成 cc 代码的文件。当我使用版本 2.5.4a-10 时,代码按预期工作。

如果我使用更新的版本 2.5.37 或更新的版本如 2.6,生成的代码似乎不会分配任何东西。它使用了一些用 nullptr 和崩溃定义的指针。

我认为这些版本之间的语法发生了变化。我发现 Debian/Ubuntu 有一个名为 flex 的包也很奇怪-老话说:

flex is a tool for generating scanners: programs which recognize lexical patterns in text. This is the old 2.5.4a version, which is no longer being developed. You should normally choose flex, unless you have legacy lexer files that do not work with a modern flex.

This product includes software developed by the University of California, Berkeley and its contributors. The upstream source code can be found at http://flex.sourceforge.net/

(编者注:Flex 已移动到 Github 但 v2.5.4a 不在那里。)

我怀疑该版本对其他人来说似乎很重要。回答我的问题:

是否有关于我必须执行哪些操作才能移植该代码以生成一些适用于较新版本的 flex 的 C++ 代码的任何手册或指南?

编辑:这是我取自更大的东西的简单示例:

    int num_lines = 0, num_chars = 0;

%%
\n ++num_lines; ++num_chars;
. ++num_chars;

%%
int main()
{
yy_init=1;

yylex();
printf( "# of lines = %d, # of chars = %d\n",
num_lines, num_chars );
return 0;
}

使用 flex file.l 对其进行调整,并使用 gcc lex.yy.c -lfl 进行构建。现在,如果您使用 2.5.4 版,它就可以工作了。对于更高版本,它可以很好地翻译和编译,但是当您运行该程序时,您将遇到段错误。

最佳答案

我自己发现了问题。变量 yy_init 可以在旧版本中明确设置。在较新的版本中,这是不允许的。我不确定这是否有意,也许有人可以解释为什么会观察到这种行为。我觉得有点奇怪。

如果有人有类似的问题,您可能想看看 yy_init 变量。除此之外我没有任何问题。

关于c++ - 将 flex 2.5.4a 迁移到 2.6(词法分析器生成器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43339702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com