feat(grammar): 完善 SysY.g4 语法定义支持 Lab1 要求

- 扩展 SysY 语法定义以支持更多合法程序
- 完善词法和语法规则,增强解析能力
- 确保符合 SysY 语言规范
Oliveira 3 weeks ago
parent be143f5326
commit 3e4165c4bb

@ -1,31 +1,61 @@
// SysY 子集语法:支持形如
// int main() { int a = 1; int b = 2; return a + b; }
// 的最小返回表达式编译。
// 后续需要自行添加
grammar SysY;
/*===-------------------------------------------===*/
/* Lexer rules */
/*===-------------------------------------------===*/
CONST: 'const';
INT: 'int';
FLOAT: 'float';
VOID: 'void';
IF: 'if';
ELSE: 'else';
WHILE: 'while';
BREAK: 'break';
CONTINUE: 'continue';
RETURN: 'return';
ASSIGN: '=';
ADD: '+';
SUB: '-';
MUL: '*';
DIV: '/';
MOD: '%';
EQ: '==';
NEQ: '!=';
LT: '<';
GT: '>';
LE: '<=';
GE: '>=';
NOT: '!';
AND: '&&';
OR: '||';
LPAREN: '(';
RPAREN: ')';
LBRACK: '[';
RBRACK: ']';
LBRACE: '{';
RBRACE: '}';
COMMA: ',';
SEMICOLON: ';';
ID: [a-zA-Z_][a-zA-Z_0-9]*;
ILITERAL: [0-9]+;
WS: [ \t\r\n] -> skip;
ILITERAL
: '0' | [1-9][0-9]* // Decimal
| '0' [0-7]+ // Octal
| ('0x' | '0X') [0-9a-fA-F]+ // Hex
;
FLITERAL
: ([0-9]* '.' [0-9]+ | [0-9]+ '.') ([eE] [+-]? [0-9]+)?
| [0-9]+ [eE] [+-]? [0-9]+
| ('0x'|'0X') ([0-9a-fA-F]* '.' [0-9a-fA-F]+ | [0-9a-fA-F]+ '.') ([pP] [+-]? [0-9]+)?
| ('0x'|'0X') [0-9a-fA-F]+ [pP] [+-]? [0-9]+
;
WS: [ \t\r\n]+ -> skip;
LINECOMMENT: '//' ~[\r\n]* -> skip;
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
@ -34,31 +64,62 @@ BLOCKCOMMENT: '/*' .*? '*/' -> skip;
/*===-------------------------------------------===*/
compUnit
: funcDef EOF
: (decl | funcDef)+ EOF
;
decl
: btype varDef SEMICOLON
: constDecl
| varDecl
;
constDecl
: CONST btype constDef (COMMA constDef)* SEMICOLON
;
btype
: INT
| FLOAT
;
constDef
: ID (LBRACK constExp RBRACK)* ASSIGN constInitVal
;
constInitVal
: constExp
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
;
varDecl
: btype varDef (COMMA varDef)* SEMICOLON
;
varDef
: lValue (ASSIGN initValue)?
: ID (LBRACK constExp RBRACK)*
| ID (LBRACK constExp RBRACK)* ASSIGN initValue
;
initValue
: exp
| LBRACE (initValue (COMMA initValue)*)? RBRACE
;
funcDef
: funcType ID LPAREN RPAREN blockStmt
: funcType ID LPAREN funcFParams? RPAREN blockStmt
;
funcType
: INT
: VOID
| INT
| FLOAT
;
funcFParams
: funcFParam (COMMA funcFParam)*
;
funcFParam
: btype ID (LBRACK RBRACK (LBRACK exp RBRACK)*)?
;
blockStmt
@ -71,28 +132,53 @@ blockItem
;
stmt
: returnStmt
;
returnStmt
: RETURN exp SEMICOLON
: lValue ASSIGN exp SEMICOLON # assignStmt
| exp? SEMICOLON # exprStmt
| blockStmt # blockStmtStmt
| IF LPAREN cond RPAREN stmt (ELSE stmt)? # ifStmt
| WHILE LPAREN cond RPAREN stmt # whileStmt
| BREAK SEMICOLON # breakStmt
| CONTINUE SEMICOLON # continueStmt
| RETURN exp? SEMICOLON # returnStmt
;
exp
: LPAREN exp RPAREN # parenExp
| var # varExp
| number # numberExp
| exp ADD exp # additiveExp
: LPAREN exp RPAREN # parenExp
| lValue # lvalExp
| number # numberExp
| ID LPAREN funcRParams? RPAREN # callExp
| unaryOp exp # unaryExp
| exp (MUL | DIV | MOD) exp # mulExp
| exp (ADD | SUB) exp # addExp
| exp (LT | GT | LE | GE) exp # relExp
| exp (EQ | NEQ) exp # eqExp
| exp AND exp # lAndExp
| exp OR exp # lOrExp
;
var
: ID
cond
: exp
;
lValue
: ID
: ID (LBRACK exp RBRACK)*
;
number
: ILITERAL
: ILITERAL # intConst
| FLITERAL # floatConst
;
unaryOp
: ADD
| SUB
| NOT
;
funcRParams
: exp (COMMA exp)*
;
constExp
: exp
;

Loading…
Cancel
Save