我试图在词法分析器中实现一个include功能,以便当它命中“#include“filename””时,它将切换到该文件的流。我使用下面显示的lexer Action 使它工作。当我运行它时,它会出现故障。antlr4::ANTLRInputStream new_source(new_file); // new file is an open ifstreamint pos = _input->index();filestack.push(std::make_pair(_input,pos)); //my stack to keep track of previous filesreset();_input= static_cast<antlr4::CharStream*>(&new_source);我检查了static_cast 是否工作并且返回了非null指针,并且赋值成功。但是,当它继续运行后,进入重新编译的ANLTR运行时后就会出现段错误。有什么我想念的吗?更新:我只是使用调试标志重新编译了c++运行时,现在我发现它在LexerATNSimulator::failOrAccept失败时返回_prevAccept.dfaState-> prediction。另外,这是在段错误发生之前发生的事情:It exits out of the custom lexer action and the LexerActionExecutor.It enters LexerATNSimulator::accept.exits LexerATNSimulator::accept.Enters LexerATNSimulator::failOrAcceptSegfault切换时我正在重置词法分析器,这与故障有关吗? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 仅替换输入流的值不会削减它。到处都有可能导致崩溃的引用。相反,您必须重置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中查找此函数的详细信息。 (adsbygoogle = window.adsbygoogle || []).push({});