可以处理生成加法的IR

This commit is contained in:
jing
2025-12-28 18:44:48 +08:00
parent 77bee889d7
commit e941cced9b
42 changed files with 919 additions and 158 deletions

View File

@@ -1,5 +1,34 @@
// 前端解析驱动:
// - 读取源代码
// - 调用 ANTLR 生成的 lexer/parser 得到 parse tree
// - 对外提供“可用的解析入口”(语法正确性由测试保证)
// 调用 ANTLR 生成的 Lexer/Parser返回 parse tree。
#include "frontend/AntlrDriver.h"
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "SysYLexer.h"
#include "SysYParser.h"
#include "antlr4-runtime.h"
AntlrResult ParseFileWithAntlr(const std::string& path) {
std::ifstream fin(path);
if (!fin.is_open()) {
throw std::runtime_error("无法打开输入文件: " + path);
}
std::ostringstream ss;
ss << fin.rdbuf();
auto input = std::make_unique<antlr4::ANTLRInputStream>(ss.str());
auto lexer = std::make_unique<SysYLexer>(input.get());
auto tokens = std::make_unique<antlr4::CommonTokenStream>(lexer.get());
auto parser = std::make_unique<SysYParser>(tokens.get());
parser->removeErrorListeners();
auto tree = parser->compUnit();
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;
}