// 调用前端解析流程,返回语法树。 #include "frontend/AntlrDriver.h" #include #include #include #include #include "SysYLexer.h" #include "SysYParser.h" #include "antlr4-runtime.h" namespace { class ParseErrorListener : public antlr4::BaseErrorListener { public: void syntaxError(antlr4::Recognizer* /*recognizer*/, antlr4::Token* /*offendingSymbol*/, size_t line, size_t charPositionInLine, const std::string& msg, std::exception_ptr /*e*/) override { throw std::runtime_error("[parse] 暂不支持的语法/词法 @" + std::to_string(line) + ":" + std::to_string(charPositionInLine) + " - " + msg); } }; } // namespace AntlrResult ParseFileWithAntlr(const std::string& path) { std::ifstream fin(path); if (!fin.is_open()) { throw std::runtime_error("[parse] 无法打开输入文件: " + path); } std::ostringstream ss; ss << fin.rdbuf(); auto input = std::make_unique(ss.str()); auto lexer = std::make_unique(input.get()); auto tokens = std::make_unique(lexer.get()); auto parser = std::make_unique(tokens.get()); ParseErrorListener error_listener; lexer->removeErrorListeners(); lexer->addErrorListener(&error_listener); parser->removeErrorListeners(); parser->addErrorListener(&error_listener); parser->setErrorHandler(std::make_shared()); antlr4::tree::ParseTree* tree = nullptr; try { tree = parser->compUnit(); } catch (const std::exception& ex) { const std::string msg = ex.what(); if (!msg.empty()) { throw std::runtime_error("[parse] 暂不支持的语法/词法 - " + msg); } if (auto* tok = parser->getCurrentToken()) { throw std::runtime_error("[parse] 暂不支持的语法/词法 @" + std::to_string(tok->getLine()) + ":" + std::to_string(tok->getCharPositionInLine()) + " near token '" + tok->getText() + "'"); } throw std::runtime_error("[parse] 暂不支持的语法/词法"); } AntlrResult result; result.input = std::move(input); result.lexer = std::move(lexer); result.tokens = std::move(tokens); result.parser = std::move(parser); result.tree = tree; return result; }