forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
874 B
74 lines
874 B
// SysY 子集语法:支持形如
|
|
// int main() { int a = 1; int b = 2; return a + b; }
|
|
// 的最小返回表达式编译。
|
|
|
|
// 后续需要自行添加
|
|
grammar SysY;
|
|
|
|
compUnit
|
|
: funcDef EOF
|
|
;
|
|
|
|
funcDef
|
|
: Int Main L_PAREN R_PAREN block
|
|
;
|
|
|
|
block
|
|
: L_BRACE stmt* R_BRACE
|
|
;
|
|
|
|
stmt
|
|
: varDecl
|
|
| returnStmt
|
|
;
|
|
|
|
varDecl
|
|
: Int Ident (Assign exp)? Semi
|
|
;
|
|
|
|
returnStmt
|
|
: Return exp Semi
|
|
;
|
|
|
|
exp
|
|
: addExp
|
|
;
|
|
|
|
addExp
|
|
: primary (AddOp primary)*
|
|
;
|
|
|
|
primary
|
|
: Number
|
|
| Ident
|
|
| L_PAREN exp R_PAREN
|
|
;
|
|
|
|
Int : 'int';
|
|
Return : 'return';
|
|
Main : 'main';
|
|
|
|
AddOp : '+';
|
|
Assign : '=';
|
|
Semi : ';';
|
|
L_PAREN : '(';
|
|
R_PAREN : ')';
|
|
L_BRACE : '{';
|
|
R_BRACE : '}';
|
|
|
|
Ident
|
|
: [a-zA-Z_][a-zA-Z_0-9]*
|
|
;
|
|
|
|
Number
|
|
: [0-9]+
|
|
;
|
|
|
|
WS
|
|
: [ \t\r\n]+ -> skip
|
|
;
|
|
|
|
COMMENT
|
|
: '//' ~[\r\n]* -> skip
|
|
;
|