From dcaada11bc081fe4575c38047d185c36fce5d640 Mon Sep 17 00:00:00 2001 From: brkstar <1576878717@qq.com> Date: Sun, 22 Mar 2026 14:43:17 +0800 Subject: [PATCH] =?UTF-8?q?docs(solution):=20=E8=A1=A5=E5=85=85=E8=AF=AD?= =?UTF-8?q?=E6=B3=95=E6=A0=91=E4=BF=9D=E5=AD=98=E9=80=89=E9=A1=B9=E4=B8=8E?= =?UTF-8?q?=20Lab1=20=E8=BF=90=E8=A1=8C=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution/Lab1-修改记录.md | 18 ++++++++++++- solution/Lab1-设计方案.md | 7 +++++ solution/RUN.md | 16 ++++++++++++ solution/run_lab1_batch.sh | 48 +++++++++++++++++++++++++++++++---- 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/solution/Lab1-修改记录.md b/solution/Lab1-修改记录.md index ac3cb6a..525b2a0 100644 --- a/solution/Lab1-修改记录.md +++ b/solution/Lab1-修改记录.md @@ -14,6 +14,8 @@ - `src/irgen/IRGenExp.cpp` - `solution/Lab1-设计方案.md` - `solution/Lab1-修改记录.md` +- `solution/RUN.md` +- `solution/run_lab1_batch.sh` ## 2. 文法扩展 @@ -110,7 +112,21 @@ cmake --build build -j 4 - `test/test_case` 下全部 `.sy` 用例在 `--emit-parse-tree` 模式下通过 -## 8. 已知边界 +## 8. 批量脚本增强 + +补充更新了 `solution/run_lab1_batch.sh`: + +- 新增可选参数 `--save-tree` +- 启用后,会在仓库根目录下创建 `test_tree/` +- 并按照 `functional/`、`performance/` 的目录结构保存每个样例对应的语法树 + +同时同步更新了: + +- `solution/RUN.md` +- `solution/Lab1-设计方案.md` +- `solution/Lab1-修改记录.md` + +## 9. 已知边界 当前提交完成的是 Lab1 所需的“语法分析与语法树构建”。以下能力仍属于后续实验范围: diff --git a/solution/Lab1-设计方案.md b/solution/Lab1-设计方案.md index 0847345..9d9f54f 100644 --- a/solution/Lab1-设计方案.md +++ b/solution/Lab1-设计方案.md @@ -170,6 +170,13 @@ 1. 使用代表性样例检查语法树结构。 2. 批量遍历 `test/test_case/functional/*.sy` 与 `test/test_case/performance/*.sy`,执行 `./build/bin/compiler --emit-parse-tree`。 +另外补充一个批量自动化脚本 `solution/run_lab1_batch.sh`,用于统一执行: + +- ANTLR 文件重新生成 +- CMake 配置与编译 +- 所有 `.sy` 用例的语法树回归 +- 在需要时通过 `--save-tree` 将语法树保存到 `test_tree/` + 验证目标是: - 文法能接受测试目录中的 SysY 程序 diff --git a/solution/RUN.md b/solution/RUN.md index 8a86738..fea7a5e 100644 --- a/solution/RUN.md +++ b/solution/RUN.md @@ -68,6 +68,12 @@ cmake --build build -j "$(nproc)" ./solution/run_lab1_batch.sh ``` +如果希望在批量测试时把每个样例的语法树保存到 `test_tree/` 目录,可以加可选项: + +```bash +./solution/run_lab1_batch.sh --save-tree +``` + 该脚本会自动完成: 1. 重新生成 `build/generated/antlr4` 下的 ANTLR 文件 @@ -76,6 +82,16 @@ cmake --build build -j "$(nproc)" 4. 批量测试 `test/test_case/functional/*.sy` 5. 批量测试 `test/test_case/performance/*.sy` +若使用 `--save-tree`,还会额外: + +6. 在仓库根目录下创建 `test_tree/` +7. 将语法树按测试集目录结构保存,例如: + +```bash +test_tree/functional/simple_add.tree +test_tree/performance/fft0.tree +``` + 若某个用例失败,脚本会打印失败用例名并返回非零退出码。 ## 6. 常用附加命令 diff --git a/solution/run_lab1_batch.sh b/solution/run_lab1_batch.sh index e97e36d..3a70875 100755 --- a/solution/run_lab1_batch.sh +++ b/solution/run_lab1_batch.sh @@ -8,6 +8,22 @@ ANTLR_DIR="$BUILD_DIR/generated/antlr4" JAR_PATH="$ROOT_DIR/third_party/antlr-4.13.2-complete.jar" GRAMMAR_PATH="$ROOT_DIR/src/antlr4/SysY.g4" COMPILER="$BUILD_DIR/bin/compiler" +SAVE_TREE=false +TREE_DIR="$ROOT_DIR/test_tree" + +while [[ $# -gt 0 ]]; do + case "$1" in + --save-tree) + SAVE_TREE=true + ;; + *) + echo "Unknown option: $1" >&2 + echo "Usage: $0 [--save-tree]" >&2 + exit 1 + ;; + esac + shift +done echo "[1/4] Generating ANTLR sources..." mkdir -p "$ANTLR_DIR" @@ -27,13 +43,35 @@ cmake --build "$BUILD_DIR" -j "$(nproc)" echo "[4/4] Running parse-tree tests..." failed=0 +if [[ "$SAVE_TREE" == true ]]; then + rm -rf "$TREE_DIR" + mkdir -p "$TREE_DIR" +fi + for case_file in "$ROOT_DIR"/test/test_case/functional/*.sy "$ROOT_DIR"/test/test_case/performance/*.sy; do - if ! "$COMPILER" --emit-parse-tree "$case_file" >/dev/null 2>/tmp/lab1_parse.err; then - echo "FAIL: $case_file" - cat /tmp/lab1_parse.err - failed=1 + if [[ "$SAVE_TREE" == true ]]; then + rel_path="${case_file#"$ROOT_DIR"/test/test_case/}" + rel_dir="$(dirname "$rel_path")" + stem="$(basename "${case_file%.sy}")" + out_dir="$TREE_DIR/$rel_dir" + out_file="$out_dir/$stem.tree" + mkdir -p "$out_dir" + if ! "$COMPILER" --emit-parse-tree "$case_file" >"$out_file" 2>/tmp/lab1_parse.err; then + echo "FAIL: $case_file" + cat /tmp/lab1_parse.err + rm -f "$out_file" + failed=1 + else + echo "PASS: $case_file -> $out_file" + fi else - echo "PASS: $case_file" + if ! "$COMPILER" --emit-parse-tree "$case_file" >/dev/null 2>/tmp/lab1_parse.err; then + echo "FAIL: $case_file" + cat /tmp/lab1_parse.err + failed=1 + else + echo "PASS: $case_file" + fi fi done