Compare commits

...

4 Commits
master ... lab1

@ -0,0 +1,50 @@
#!/bin/bash
# 批量测试所有.sy文件的语法解析
test_dir="/home/lingli/nudt-compiler-cpp/test/test_case"
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")"
# 运行解析测试,捕获输出
output=$("$compiler" --emit-parse-tree "$test_file" 2>&1)
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo " ✓ 成功"
((success_count++))
else
echo " ✗ 失败"
echo " 错误信息: $output"
((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

@ -1,8 +1,4 @@
// SysY 子集语法:支持形如
// int main() { int a = 1; int b = 2; return a + b; }
// 的最小返回表达式编译。
// 后续需要自行添加
// SysY 语法扩展支持更多SysY特性
grammar SysY;
@ -10,21 +6,101 @@ grammar SysY;
/* Lexer rules */
/*===-------------------------------------------===*/
// 关键字
INT: 'int';
FLOAT: 'float';
VOID: 'void';
CONST: 'const';
RETURN: 'return';
IF: 'if';
ELSE: 'else';
WHILE: 'while';
BREAK: 'break';
CONTINUE: 'continue';
// 操作符
ASSIGN: '=';
ADD: '+';
SUB: '-';
MUL: '*';
DIV: '/';
MOD: '%';
LT: '<';
LE: '<=';
GT: '>';
GE: '>=';
EQ: '==';
NE: '!=';
// 逻辑操作符
NOT: '!';
AND: '&&';
OR: '||';
// 括号
LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';
LBRACK: '[';
RBRACK: ']';
// 标点
SEMICOLON: ';';
COMMA: ',';
// 标识符和字面量
ID: [a-zA-Z_][a-zA-Z_0-9]*;
ILITERAL: [0-9]+;
ILITERAL
: DECIMAL_LITERAL
| OCTAL_LITERAL
| HEX_LITERAL
;
fragment DECIMAL_LITERAL
: [0-9]+
;
fragment OCTAL_LITERAL
: '0' [0-7]+
;
fragment HEX_LITERAL
: '0' ('x' | 'X') [0-9a-fA-F]+
;
// 浮点字面量
FLITERAL
: (DECIMAL_FLOAT | HEX_FLOAT)
;
fragment DECIMAL_FLOAT
: ((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')
(HEXDIGIT* '.' HEXDIGIT+ | HEXDIGIT+ '.')
(('P' | 'p') ('+' | '-')? DIGIT+)
| '0' ('x' | 'X')
HEXDIGIT+
(('P' | 'p') ('+' | '-')? DIGIT+)
;
fragment DIGIT
: [0-9]
;
fragment HEXDIGIT
: [0-9a-fA-F]
;
// 空白和注释
WS: [ \t\r\n] -> skip;
LINECOMMENT: '//' ~[\r\n]* -> skip;
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
@ -34,33 +110,62 @@ BLOCKCOMMENT: '/*' .*? '*/' -> skip;
/*===-------------------------------------------===*/
compUnit
: funcDef EOF
: (decl | funcDef)* EOF
;
// 声明
decl
: btype varDef SEMICOLON
: constDecl
| varDecl
;
constDecl
: CONST btype constDef (COMMA constDef)* SEMICOLON
;
varDecl
: btype varDef (COMMA varDef)* SEMICOLON
;
btype
: INT
| FLOAT
| VOID
;
constDef
: ID (LBRACK exp RBRACK)* ASSIGN initValue
;
varDef
: lValue (ASSIGN initValue)?
: ID (LBRACK exp RBRACK)* (ASSIGN initValue)?
;
initValue
: exp
| LBRACE (initValue (COMMA initValue)*)? RBRACE
;
// 函数定义
funcDef
: funcType ID LPAREN RPAREN blockStmt
: funcType ID LPAREN (funcFParams)? RPAREN blockStmt
;
funcType
: INT
| FLOAT
| VOID
;
funcFParams
: funcFParam (COMMA funcFParam)*
;
funcFParam
: btype ID (LBRACK (exp)? RBRACK)*
;
// 语句
blockStmt
: LBRACE blockItem* RBRACE
;
@ -71,28 +176,77 @@ blockItem
;
stmt
: returnStmt
: assignStmt
| returnStmt
| blockStmt
| ifStmt
| whileStmt
| breakStmt
| continueStmt
| expStmt
;
expStmt
: exp SEMICOLON
;
assignStmt
: lValue ASSIGN exp SEMICOLON
;
returnStmt
: RETURN exp SEMICOLON
: RETURN (exp)? SEMICOLON
;
exp
: LPAREN exp RPAREN # parenExp
| var # varExp
| number # numberExp
| exp ADD exp # additiveExp
ifStmt
: IF LPAREN exp RPAREN stmt (ELSE stmt)?
;
whileStmt
: WHILE LPAREN exp RPAREN stmt
;
var
: ID
breakStmt
: BREAK SEMICOLON
;
continueStmt
: CONTINUE SEMICOLON
;
// 表达式
lValue
: ID
: 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
;

Loading…
Cancel
Save