This commit is contained in:
2026-03-23 20:01:25 +08:00
parent 995e159f6f
commit 6fc0c89072
2 changed files with 18 additions and 12 deletions

View File

@@ -2,7 +2,7 @@
# 批量测试所有.sy文件的语法解析 # 批量测试所有.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" compiler="/home/lingli/nudt-compiler-cpp/build/bin/compiler"
if [ ! -f "$compiler" ]; then if [ ! -f "$compiler" ]; then
@@ -21,14 +21,16 @@ echo "="
for test_file in $(find "$test_dir" -name "*.sy" | sort); do for test_file in $(find "$test_dir" -name "*.sy" | sort); do
echo "测试: $(basename "$test_file")" 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 " ✓ 成功" echo " ✓ 成功"
((success_count++)) ((success_count++))
else else
echo " ✗ 失败" echo " ✗ 失败"
echo " 错误信息: $output"
((failed_count++)) ((failed_count++))
failed_tests+=($(basename "$test_file")) failed_tests+=($(basename "$test_file"))
fi fi

View File

@@ -75,17 +75,19 @@ FLITERAL
; ;
fragment DECIMAL_FLOAT fragment DECIMAL_FLOAT
: (('+' | '-')? (DIGIT+ '.' DIGIT* | '.' DIGIT+ | DIGIT+) : ((DIGIT+ '.' DIGIT* | '.' DIGIT+)
(('E' | 'e') ('+' | '-')? DIGIT+)?) (('E' | 'e') ('+' | '-')? DIGIT+)?)
| (('+' | '-')? '0' [0-7]+ '.' [0-7]* | ((DIGIT+ '.' DIGIT* | '.' DIGIT+ | DIGIT+)
(('E' | 'e') ('+' | '-')? DIGIT+)?) (('E' | 'e') ('+' | '-')? DIGIT+))
| ('0' [0-7]+ '.' [0-7]*
(('E' | 'e') ('+' | '-')? DIGIT+)?)
; ;
fragment HEX_FLOAT fragment HEX_FLOAT
: ('+' | '-')? '0' ('x' | 'X') : '0' ('x' | 'X')
(HEXDIGIT* '.' HEXDIGIT+ | HEXDIGIT+ '.') (HEXDIGIT* '.' HEXDIGIT+ | HEXDIGIT+ '.')
(('P' | 'p') ('+' | '-')? DIGIT+) (('P' | 'p') ('+' | '-')? DIGIT+)
| ('+' | '-')? '0' ('x' | 'X') | '0' ('x' | 'X')
HEXDIGIT+ HEXDIGIT+
(('P' | 'p') ('+' | '-')? DIGIT+) (('P' | 'p') ('+' | '-')? DIGIT+)
; ;
@@ -104,13 +106,14 @@ LINECOMMENT: '//' ~[\r\n]* -> skip;
BLOCKCOMMENT: '/*' .*? '*/' -> skip; BLOCKCOMMENT: '/*' .*? '*/' -> skip;
/*===-------------------------------------------===*/ /*===-------------------------------------------===*/
/* Parser rules */ /* Syntax rules */
/*===-------------------------------------------===*/ /*===-------------------------------------------===*/
compUnit compUnit
: (decl | funcDef)* EOF : (decl | funcDef)* EOF
; ;
// 声明
decl decl
: constDecl : constDecl
| varDecl | varDecl
@@ -143,6 +146,7 @@ initValue
| LBRACE (initValue (COMMA initValue)*)? RBRACE | LBRACE (initValue (COMMA initValue)*)? RBRACE
; ;
// 函数定义
funcDef funcDef
: funcType ID LPAREN (funcFParams)? RPAREN blockStmt : funcType ID LPAREN (funcFParams)? RPAREN blockStmt
; ;
@@ -161,6 +165,7 @@ funcFParam
: btype ID (LBRACK (exp)? RBRACK)* : btype ID (LBRACK (exp)? RBRACK)*
; ;
// 语句
blockStmt blockStmt
: LBRACE blockItem* RBRACE : LBRACE blockItem* RBRACE
; ;
@@ -245,4 +250,3 @@ number
: ILITERAL : ILITERAL
| FLITERAL | FLITERAL
; ;