语法/前端解析,构建AST
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
20
src/frontend/AntlrDriver.h
Normal file
20
src/frontend/AntlrDriver.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// 包装 ANTLR4,提供简易的解析入口。
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "SysYLexer.h"
|
||||
#include "SysYParser.h"
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
struct AntlrResult {
|
||||
std::unique_ptr<antlr4::ANTLRInputStream> input;
|
||||
std::unique_ptr<SysYLexer> lexer;
|
||||
std::unique_ptr<antlr4::CommonTokenStream> tokens;
|
||||
std::unique_ptr<SysYParser> parser;
|
||||
antlr4::tree::ParseTree* tree = nullptr; // owned by parser
|
||||
};
|
||||
|
||||
// 解析指定文件,发生错误时抛出 std::runtime_error。
|
||||
AntlrResult ParseFileWithAntlr(const std::string& path);
|
||||
@@ -1,4 +1,114 @@
|
||||
// AST 构建:
|
||||
// - 将 ANTLR parse tree 转换为 AST(对应 src/ast/*)
|
||||
// - 在 AST 节点上保留必要的定位信息(可选,用于调试/日志)
|
||||
// 将 parse tree 转换为 AST。
|
||||
#include "frontend/AstBuilder.h"
|
||||
|
||||
#include <any>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "SysYBaseVisitor.h"
|
||||
#include "SysYParser.h"
|
||||
#include "ast/AstNodes.h"
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using ast::BinaryExpr;
|
||||
using ast::BinaryOp;
|
||||
using ast::Block;
|
||||
using ast::CompUnit;
|
||||
using ast::FuncDef;
|
||||
using ast::NumberExpr;
|
||||
using ast::ReturnStmt;
|
||||
using ast::VarDecl;
|
||||
using ast::VarExpr;
|
||||
|
||||
template <typename T>
|
||||
T Take(std::any&& value) {
|
||||
if (auto* ptr = std::any_cast<T>(&value)) {
|
||||
return std::move(*ptr);
|
||||
}
|
||||
throw std::runtime_error("AST 构建失败:类型不匹配");
|
||||
}
|
||||
|
||||
class Builder : public SysYBaseVisitor {
|
||||
public:
|
||||
std::any visitCompUnit(SysYParser::CompUnitContext* ctx) override {
|
||||
auto func = Take<std::shared_ptr<FuncDef>>(visit(ctx->funcDef()));
|
||||
return std::make_shared<CompUnit>(std::move(func));
|
||||
}
|
||||
|
||||
std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override {
|
||||
auto body = Take<std::shared_ptr<Block>>(visit(ctx->block()));
|
||||
return std::make_shared<FuncDef>("main", std::move(body));
|
||||
}
|
||||
|
||||
std::any visitBlock(SysYParser::BlockContext* ctx) override {
|
||||
auto block = std::make_shared<Block>();
|
||||
for (auto stmtCtx : ctx->stmt()) {
|
||||
if (stmtCtx->varDecl()) {
|
||||
block->varDecls.emplace_back(
|
||||
Take<std::shared_ptr<VarDecl>>(visit(stmtCtx->varDecl())));
|
||||
} else if (stmtCtx->returnStmt()) {
|
||||
block->stmts.emplace_back(
|
||||
Take<std::shared_ptr<ReturnStmt>>(visit(stmtCtx->returnStmt())));
|
||||
}
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
std::any visitVarDecl(SysYParser::VarDeclContext* ctx) override {
|
||||
std::shared_ptr<ast::Expr> init;
|
||||
if (ctx->exp()) {
|
||||
init = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
|
||||
}
|
||||
return std::make_shared<VarDecl>(ctx->Ident()->getText(), std::move(init));
|
||||
}
|
||||
|
||||
std::any visitReturnStmt(SysYParser::ReturnStmtContext* ctx) override {
|
||||
auto expr = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
|
||||
return std::make_shared<ReturnStmt>(std::move(expr));
|
||||
}
|
||||
|
||||
std::any visitExp(SysYParser::ExpContext* ctx) override {
|
||||
return visit(ctx->addExp());
|
||||
}
|
||||
|
||||
std::any visitAddExp(SysYParser::AddExpContext* ctx) override {
|
||||
auto node = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(0)));
|
||||
for (size_t i = 1; i < ctx->primary().size(); ++i) {
|
||||
auto rhs = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(i)));
|
||||
auto opToken = ctx->AddOp(i - 1);
|
||||
BinaryOp op = BinaryOp::Add;
|
||||
if (opToken->getText() == "-") op = BinaryOp::Sub;
|
||||
node = std::make_shared<BinaryExpr>(op, std::move(node), std::move(rhs));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
std::any visitPrimary(SysYParser::PrimaryContext* ctx) override {
|
||||
if (ctx->Number()) {
|
||||
std::shared_ptr<ast::Expr> expr =
|
||||
std::make_shared<NumberExpr>(std::stoi(ctx->Number()->getText()));
|
||||
return expr;
|
||||
}
|
||||
if (ctx->Ident()) {
|
||||
std::shared_ptr<ast::Expr> expr =
|
||||
std::make_shared<VarExpr>(ctx->Ident()->getText());
|
||||
return expr;
|
||||
}
|
||||
return visit(ctx->exp());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree) {
|
||||
if (!tree) {
|
||||
throw std::runtime_error("parse tree 为空");
|
||||
}
|
||||
Builder visitor;
|
||||
auto result = visitor.visit(tree);
|
||||
return Take<std::shared_ptr<ast::CompUnit>>(std::move(result));
|
||||
}
|
||||
|
||||
16
src/frontend/AstBuilder.h
Normal file
16
src/frontend/AstBuilder.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// 将 ANTLR parse tree 转换为内部 AST。
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace antlr4 {
|
||||
namespace tree {
|
||||
class ParseTree;
|
||||
}
|
||||
} // namespace antlr4
|
||||
|
||||
namespace ast {
|
||||
struct CompUnit;
|
||||
}
|
||||
|
||||
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree);
|
||||
Reference in New Issue
Block a user