gpt4 book ai didi

c++ - 在 ANTLR4 的 Lexer 中切换流

转载 作者:行者123 更新时间:2023-11-30 04:58:43 24 4
gpt4 key购买 nike

我正在尝试在词法分析器中实现包含功能,以便当它命中“#include“文件名””时,它将切换到该文件的流。我使用如下所示的词法分析器操作使其工作。当我运行它时,它会出现故障。

antlr4::ANTLRInputStream new_source(new_file); // new file is an open ifstream

int pos = _input->index();

filestack.push(std::make_pair(_input,pos)); //my stack to keep track of previous files
reset();
_input= static_cast<antlr4::CharStream*>(&new_source);

我检查了 static_cast<> 是否有效并返回了一个非空指针,并且赋值成功。但是,当它在进入重新编译的 ANLTR 运行时后继续运行时会出现段错误。有什么我想念的吗?

更新:我刚刚重新编译了带有调试标志的 c++ 运行时,现在我看到它在返回 _prevAccept.dfaState->prediction 时在 LexerATNSimulator::failOrAccept 处失败。

此外,这是段错误之前发生的情况:

It exits out of the custom lexer action and the LexerActionExecutor.
It enters LexerATNSimulator::accept.
exits LexerATNSimulator::accept.
Enters LexerATNSimulator::failOrAccept
Segfault

我在切换时重置了词法分析器,这可能与失败有关吗?

最佳答案

仅仅替换输入流的值并不能解决问题。这里和那里有可能导致崩溃的引用。相反,您必须重置 lexer + token 源。它是这样的:

lexer.reset();
lexer.setInputStream(&input); // Not just reset(), which only rewinds the current position.
tokens.setTokenSource(&lexer);

参见 MySQL Workbench code on Github完整的完整代码。

关于 token 来源:词法分析器是 token 来源,您所能做的就是调用.reset()。查看 C++ runtime source有关此功能的详细信息。

关于c++ - 在 ANTLR4 的 Lexer 中切换流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51560147/

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