From 6fc0c89072ae7ea1a85e76a8a5e26eace54c91b4 Mon Sep 17 00:00:00 2001 From: yanglijia <929772356@qq.com> Date: Mon, 23 Mar 2026 20:01:25 +0800 Subject: [PATCH] lab1 --- scripts/run_all_tests.sh | 10 ++++++---- src/antlr4/SysY.g4 | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/scripts/run_all_tests.sh b/scripts/run_all_tests.sh index ad67323..25b7604 100644 --- a/scripts/run_all_tests.sh +++ b/scripts/run_all_tests.sh @@ -2,7 +2,7 @@ # 批量测试所有.sy文件的语法解析 -test_dir="/home/lingli/nudt-compiler-cpp/test/test_case/functional" +test_dir="/home/lingli/nudt-compiler-cpp/test/test_case" compiler="/home/lingli/nudt-compiler-cpp/build/bin/compiler" if [ ! -f "$compiler" ]; then @@ -21,14 +21,16 @@ echo "=" for test_file in $(find "$test_dir" -name "*.sy" | sort); do echo "测试: $(basename "$test_file")" - # 运行解析测试,将输出重定向到/dev/null - "$compiler" --emit-parse-tree "$test_file" > /dev/null 2>&1 + # 运行解析测试,捕获输出 + output=$("$compiler" --emit-parse-tree "$test_file" 2>&1) + exit_code=$? - if [ $? -eq 0 ]; then + if [ $exit_code -eq 0 ]; then echo " ✓ 成功" ((success_count++)) else echo " ✗ 失败" + echo " 错误信息: $output" ((failed_count++)) failed_tests+=($(basename "$test_file")) fi diff --git a/src/antlr4/SysY.g4 b/src/antlr4/SysY.g4 index d36eafa..b9713ea 100644 --- a/src/antlr4/SysY.g4 +++ b/src/antlr4/SysY.g4 @@ -75,17 +75,19 @@ FLITERAL ; fragment DECIMAL_FLOAT - : (('+' | '-')? (DIGIT+ '.' DIGIT* | '.' DIGIT+ | DIGIT+) - (('E' | 'e') ('+' | '-')? DIGIT+)?) - | (('+' | '-')? '0' [0-7]+ '.' [0-7]* - (('E' | 'e') ('+' | '-')? DIGIT+)?) + : ((DIGIT+ '.' DIGIT* | '.' DIGIT+) + (('E' | 'e') ('+' | '-')? DIGIT+)?) + | ((DIGIT+ '.' DIGIT* | '.' DIGIT+ | DIGIT+) + (('E' | 'e') ('+' | '-')? DIGIT+)) + | ('0' [0-7]+ '.' [0-7]* + (('E' | 'e') ('+' | '-')? DIGIT+)?) ; fragment HEX_FLOAT - : ('+' | '-')? '0' ('x' | 'X') + : '0' ('x' | 'X') (HEXDIGIT* '.' HEXDIGIT+ | HEXDIGIT+ '.') (('P' | 'p') ('+' | '-')? DIGIT+) - | ('+' | '-')? '0' ('x' | 'X') + | '0' ('x' | 'X') HEXDIGIT+ (('P' | 'p') ('+' | '-')? DIGIT+) ; @@ -104,13 +106,14 @@ LINECOMMENT: '//' ~[\r\n]* -> skip; BLOCKCOMMENT: '/*' .*? '*/' -> skip; /*===-------------------------------------------===*/ -/* Parser rules */ +/* Syntax rules */ /*===-------------------------------------------===*/ compUnit : (decl | funcDef)* EOF ; +// 声明 decl : constDecl | varDecl @@ -143,6 +146,7 @@ initValue | LBRACE (initValue (COMMA initValue)*)? RBRACE ; +// 函数定义 funcDef : funcType ID LPAREN (funcFParams)? RPAREN blockStmt ; @@ -161,6 +165,7 @@ funcFParam : btype ID (LBRACK (exp)? RBRACK)* ; +// 语句 blockStmt : LBRACE blockItem* RBRACE ; @@ -245,4 +250,3 @@ number : ILITERAL | FLITERAL ; -