docs(solution): 补充语法树保存选项与 Lab1 运行说明

dev
brkstar 2 weeks ago
parent f54091c010
commit dcaada11bc

@ -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 所需的“语法分析与语法树构建”。以下能力仍属于后续实验范围:

@ -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 程序

@ -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. 常用附加命令

@ -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

Loading…
Cancel
Save