feat(antlr4):语法树构建的相关代码修改

zjx 2 months ago
parent 3832d65537
commit 702ed9c1fd

@ -1,68 +1,70 @@
// SysY 子集语法:支持形如
// int main() { int a = 1; int b = 2; return a + b; }
// 的最小返回表达式编译。
// 后续需要自行添加
grammar SysY; grammar SysY;
/*===-------------------------------------------===*/ // ======================
/* Lexer rules */ // Parser Rules
/*===-------------------------------------------===*/ // ======================
INT: 'int';
RETURN: 'return';
ASSIGN: '=';
ADD: '+';
LPAREN: '('; compUnit
RPAREN: ')'; : (decl | funcDef)+
LBRACE: '{'; ;
RBRACE: '}';
SEMICOLON: ';';
ID: [a-zA-Z_][a-zA-Z_0-9]*; decl
ILITERAL: [0-9]+; : constDecl
| varDecl
;
WS: [ \t\r\n] -> skip; constDecl
LINECOMMENT: '//' ~[\r\n]* -> skip; : 'const' bType constDef (',' constDef)* ';'
BLOCKCOMMENT: '/*' .*? '*/' -> skip; ;
/*===-------------------------------------------===*/ bType
/* Syntax rules */ : 'int'
/*===-------------------------------------------===*/ | 'float'
;
compUnit constDef
: funcDef EOF : Ident ('[' constExp ']')* '=' constInitVal
; ;
decl constInitVal
: btype varDef SEMICOLON : constExp
| '{' (constInitVal (',' constInitVal)*)? '}'
; ;
btype varDecl
: INT : bType varDef (',' varDef)* ';'
; ;
varDef varDef
: lValue (ASSIGN initValue)? : Ident ('[' constExp ']')*
| Ident ('[' constExp ']')* '=' initVal
; ;
initValue initVal
: exp : exp
| '{' (initVal (',' initVal)*)? '}'
; ;
funcDef funcDef
: funcType ID LPAREN RPAREN blockStmt : funcType Ident '(' funcFParams? ')' block
; ;
funcType funcType
: INT : 'void'
| 'int'
| 'float'
; ;
blockStmt funcFParams
: LBRACE blockItem* RBRACE : funcFParam (',' funcFParam)*
;
funcFParam
: bType Ident ('[' ']' ('[' exp ']')*)?
;
block
: '{' blockItem* '}'
; ;
blockItem blockItem
@ -71,28 +73,129 @@ blockItem
; ;
stmt stmt
: returnStmt : lVal '=' exp ';'
| exp? ';'
| block
| 'if' '(' cond ')' stmt ('else' stmt)?
| 'while' '(' cond ')' stmt
| 'break' ';'
| 'continue' ';'
| 'return' exp? ';'
; ;
returnStmt exp
: RETURN exp SEMICOLON : addExp
; ;
exp cond
: LPAREN exp RPAREN # parenExp : lOrExp
| var # varExp
| number # numberExp
| exp ADD exp # additiveExp
; ;
var lVal
: ID : Ident ('[' exp ']')*
; ;
lValue primaryExp
: ID : '(' exp ')'
| lVal
| number
; ;
number number
: ILITERAL : FloatConst
| IntConst
;
unaryExp
: primaryExp
| Ident '(' funcRParams? ')'
| unaryOp unaryExp
;
unaryOp
: '+'
| '-'
| '!'
; ;
funcRParams
: exp (',' exp)*
;
mulExp
: unaryExp (('*' | '/' | '%') unaryExp)*
;
addExp
: mulExp (('+' | '-') mulExp)*
;
relExp
: addExp (('<' | '>' | '<=' | '>=') addExp)*
;
eqExp
: relExp (('==' | '!=') relExp)*
;
lAndExp
: eqExp ('&&' eqExp)*
;
lOrExp
: lAndExp ('||' lAndExp)*
;
constExp
: addExp
;
// ======================
// Lexer Rules
// ======================
fragment DIGIT : [0-9] ;
fragment HEXDIGIT : [0-9a-fA-F] ;
fragment EXP : [eE][+-]? DIGIT+ ;
fragment PEXP : [pP][+-]? DIGIT+ ;
// Float含 hex float
FloatConst
: ('0x' | '0X')
(
HEXDIGIT+ '.' HEXDIGIT*
| '.' HEXDIGIT+
| HEXDIGIT+
)
PEXP
| '.' DIGIT+ EXP?
| DIGIT+ '.' DIGIT* EXP?
| DIGIT+ EXP
;
// Int完整三种
IntConst
: '0'
| [1-9][0-9]* // decimal
| '0'[0-7]+ // octal
| ('0x' | '0X')[0-9a-fA-F]+ // hex
;
// ---------- 标识符 ----------
Ident
: [a-zA-Z_][a-zA-Z0-9_]*
;
// ---------- 空白 ----------
WS
: [ \t\r\n]+ -> skip
;
// ---------- 注释 ----------
LINE_COMMENT
: '//' ~[\r\n]* -> skip
;
BLOCK_COMMENT
: '/*' .*? '*/' -> skip
;
Loading…
Cancel
Save