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

mirror
zjx 3 weeks ago
parent 3832d65537
commit 702ed9c1fd

@ -1,68 +1,70 @@
// SysY 子集语法:支持形如
// int main() { int a = 1; int b = 2; return a + b; }
// 的最小返回表达式编译。
// 后续需要自行添加
grammar SysY;
/*===-------------------------------------------===*/
/* Lexer rules */
/*===-------------------------------------------===*/
INT: 'int';
RETURN: 'return';
ASSIGN: '=';
ADD: '+';
// ======================
// Parser Rules
// ======================
LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';
SEMICOLON: ';';
compUnit
: (decl | funcDef)+
;
ID: [a-zA-Z_][a-zA-Z_0-9]*;
ILITERAL: [0-9]+;
decl
: constDecl
| varDecl
;
WS: [ \t\r\n] -> skip;
LINECOMMENT: '//' ~[\r\n]* -> skip;
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
constDecl
: 'const' bType constDef (',' constDef)* ';'
;
/*===-------------------------------------------===*/
/* Syntax rules */
/*===-------------------------------------------===*/
bType
: 'int'
| 'float'
;
compUnit
: funcDef EOF
constDef
: Ident ('[' constExp ']')* '=' constInitVal
;
decl
: btype varDef SEMICOLON
constInitVal
: constExp
| '{' (constInitVal (',' constInitVal)*)? '}'
;
btype
: INT
varDecl
: bType varDef (',' varDef)* ';'
;
varDef
: lValue (ASSIGN initValue)?
: Ident ('[' constExp ']')*
| Ident ('[' constExp ']')* '=' initVal
;
initValue
initVal
: exp
| '{' (initVal (',' initVal)*)? '}'
;
funcDef
: funcType ID LPAREN RPAREN blockStmt
: funcType Ident '(' funcFParams? ')' block
;
funcType
: INT
: 'void'
| 'int'
| 'float'
;
blockStmt
: LBRACE blockItem* RBRACE
funcFParams
: funcFParam (',' funcFParam)*
;
funcFParam
: bType Ident ('[' ']' ('[' exp ']')*)?
;
block
: '{' blockItem* '}'
;
blockItem
@ -71,28 +73,129 @@ blockItem
;
stmt
: returnStmt
: lVal '=' exp ';'
| exp? ';'
| block
| 'if' '(' cond ')' stmt ('else' stmt)?
| 'while' '(' cond ')' stmt
| 'break' ';'
| 'continue' ';'
| 'return' exp? ';'
;
returnStmt
: RETURN exp SEMICOLON
exp
: addExp
;
exp
: LPAREN exp RPAREN # parenExp
| var # varExp
| number # numberExp
| exp ADD exp # additiveExp
cond
: lOrExp
;
var
: ID
lVal
: Ident ('[' exp ']')*
;
lValue
: ID
primaryExp
: '(' exp ')'
| lVal
| 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