diff --git a/scripts/run_all_tests.sh b/scripts/run_all_tests.sh new file mode 100644 index 0000000..ad67323 --- /dev/null +++ b/scripts/run_all_tests.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# 批量测试所有.sy文件的语法解析 + +test_dir="/home/lingli/nudt-compiler-cpp/test/test_case/functional" +compiler="/home/lingli/nudt-compiler-cpp/build/bin/compiler" + +if [ ! -f "$compiler" ]; then + echo "错误:编译器不存在,请先构建项目" + exit 1 +fi + +success_count=0 +failed_count=0 +failed_tests=() + +echo "开始测试所有.sy文件的语法解析..." +echo "=" + +# 获取所有.sy文件并排序 +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 + + if [ $? -eq 0 ]; then + echo " ✓ 成功" + ((success_count++)) + else + echo " ✗ 失败" + ((failed_count++)) + failed_tests+=($(basename "$test_file")) + fi +done + +echo "=" +echo "测试完成!" +echo "总测试数: $((success_count + failed_count))" +echo "成功: $success_count" +echo "失败: $failed_count" + +if [ $failed_count -gt 0 ]; then + echo "失败的测试用例:" + for test in "${failed_tests[@]}"; do + echo " - $test" + done +fi diff --git a/src/antlr4/SysY.g4 b/src/antlr4/SysY.g4 index 5115f15..d36eafa 100644 --- a/src/antlr4/SysY.g4 +++ b/src/antlr4/SysY.g4 @@ -171,23 +171,76 @@ blockItem ; stmt - : returnStmt + : assignStmt + | returnStmt + | blockStmt + | ifStmt + | whileStmt + | breakStmt + | continueStmt + | expStmt + ; + +expStmt + : exp SEMICOLON + ; + +assignStmt + : lValue ASSIGN exp SEMICOLON ; returnStmt : RETURN (exp)? SEMICOLON ; -exp - : LPAREN exp RPAREN # parenExp - | lValue # lValueExp - | number # numberExp +ifStmt + : IF LPAREN exp RPAREN stmt (ELSE stmt)? + ; + +whileStmt + : WHILE LPAREN exp RPAREN stmt ; +breakStmt + : BREAK SEMICOLON + ; + +continueStmt + : CONTINUE SEMICOLON + ; + +// 表达式 lValue : ID (LBRACK exp RBRACK)* ; +exp + : LPAREN exp RPAREN # parenExp + | lValue # lValueExp + | number # numberExp + | ID LPAREN (funcRParams)? RPAREN # funcCallExp + | NOT exp # notExp + | ADD exp # unaryAddExp + | SUB exp # unarySubExp + | exp MUL exp # mulExp + | exp DIV exp # divExp + | exp MOD exp # modExp + | exp ADD exp # addExp + | exp SUB exp # subExp + | exp LT exp # ltExp + | exp LE exp # leExp + | exp GT exp # gtExp + | exp GE exp # geExp + | exp EQ exp # eqExp + | exp NE exp # neExp + | exp AND exp # andExp + | exp OR exp # orExp + ; + +funcRParams + : exp (COMMA exp)* + ; + number : ILITERAL | FLITERAL