forked from NUDT-compiler/nudt-compiler-cpp
Compare commits
No commits in common. 'hyz' and 'master' have entirely different histories.
@ -1,98 +0,0 @@
|
|||||||
// SysY 子集语法:支持形如
|
|
||||||
// int main() { int a = 1; int b = 2; return a + b; }
|
|
||||||
// 的最小返回表达式编译。
|
|
||||||
|
|
||||||
// 后续需要自行添加
|
|
||||||
|
|
||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
|
|
||||||
INT: 'int';
|
|
||||||
RETURN: 'return';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
/* Syntax rules */
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: funcDef EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
decl
|
|
||||||
: btype varDef SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: lValue (ASSIGN initValue)?
|
|
||||||
;
|
|
||||||
|
|
||||||
initValue
|
|
||||||
: exp
|
|
||||||
;
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: returnStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
returnStmt
|
|
||||||
: RETURN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
exp
|
|
||||||
: LPAREN exp RPAREN # parenExp
|
|
||||||
| var # varExp
|
|
||||||
| number # numberExp
|
|
||||||
| exp ADD exp # additiveExp
|
|
||||||
;
|
|
||||||
|
|
||||||
var
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
lValue
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
;
|
|
||||||
@ -1,98 +0,0 @@
|
|||||||
// SysY 子集语法:支持形如
|
|
||||||
// int main() { int a = 1; int b = 2; return a + b; }
|
|
||||||
// 的最小返回表达式编译。
|
|
||||||
|
|
||||||
// 后续需要自行添加
|
|
||||||
|
|
||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
|
|
||||||
INT: 'int';
|
|
||||||
RETURN: 'return';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
/* Syntax rules */
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: funcDef EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
decl
|
|
||||||
: btype varDef SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: lValue (ASSIGN initValue)?
|
|
||||||
;
|
|
||||||
|
|
||||||
initValue
|
|
||||||
: exp
|
|
||||||
;
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: returnStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
returnStmt
|
|
||||||
: RETURN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
exp
|
|
||||||
: LPAREN exp RPAREN # parenExp
|
|
||||||
| var # varExp
|
|
||||||
| number # numberExp
|
|
||||||
| exp ADD exp # additiveExp
|
|
||||||
;
|
|
||||||
|
|
||||||
var
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
lValue
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
;
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
int main() {
|
|
||||||
int a = 1, b = 2;
|
|
||||||
int c;
|
|
||||||
c = a + b * 3;
|
|
||||||
|
|
||||||
if (c > 5) {
|
|
||||||
c = c - 1;
|
|
||||||
} else {
|
|
||||||
c = c + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (c < 10) {
|
|
||||||
c = c + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
@ -1,98 +0,0 @@
|
|||||||
// SysY 子集语法:支持形如
|
|
||||||
// int main() { int a = 1; int b = 2; return a + b; }
|
|
||||||
// 的最小返回表达式编译。
|
|
||||||
|
|
||||||
// 后续需要自行添加
|
|
||||||
|
|
||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
|
|
||||||
INT: 'int';
|
|
||||||
RETURN: 'return';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
/* Syntax rules */
|
|
||||||
/*===-------------------------------------------===*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: funcDef EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
decl
|
|
||||||
: btype varDef SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: lValue (ASSIGN initValue)?
|
|
||||||
;
|
|
||||||
|
|
||||||
initValue
|
|
||||||
: exp
|
|
||||||
;
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: returnStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
returnStmt
|
|
||||||
: RETURN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
exp
|
|
||||||
: LPAREN exp RPAREN # parenExp
|
|
||||||
| var # varExp
|
|
||||||
| number # numberExp
|
|
||||||
| exp ADD exp # additiveExp
|
|
||||||
;
|
|
||||||
|
|
||||||
var
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
lValue
|
|
||||||
: ID
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
;
|
|
||||||
@ -1,187 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
INT: 'int';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Syntax rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: btype varDefList SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
varDefList
|
|
||||||
: varDef (COMMA varDef)*
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID (LBRACK number RBRACK)? (ASSIGN initValue)?
|
|
||||||
;
|
|
||||||
|
|
||||||
initValue
|
|
||||||
: exp
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParams
|
|
||||||
: funcParam (COMMA funcParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParam
|
|
||||||
: btype ID
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: returnStmt
|
|
||||||
| assignStmt
|
|
||||||
| expStmt
|
|
||||||
| blockStmt
|
|
||||||
| ifStmt
|
|
||||||
| whileStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
returnStmt
|
|
||||||
: RETURN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
assignStmt
|
|
||||||
: lValue ASSIGN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
expStmt
|
|
||||||
: exp? SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
ifStmt
|
|
||||||
: IF LPAREN exp RPAREN stmt (ELSE stmt)?
|
|
||||||
;
|
|
||||||
|
|
||||||
whileStmt
|
|
||||||
: WHILE LPAREN exp RPAREN stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: logicalExp
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 逻辑(先简化成关系表达式) */
|
|
||||||
logicalExp
|
|
||||||
: relationalExp
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 比较 */
|
|
||||||
relationalExp
|
|
||||||
: additiveExp ( (LT | GT | LE | GE | EQ | NEQ) additiveExp )*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 加减 */
|
|
||||||
additiveExp
|
|
||||||
: multiplicativeExp ( (ADD | SUB) multiplicativeExp )*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 乘除 */
|
|
||||||
multiplicativeExp
|
|
||||||
: unaryExp ( (MUL | DIV) unaryExp )*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 一元 */
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| ADD unaryExp
|
|
||||||
| SUB unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 基本表达式 */
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| number
|
|
||||||
| lValue
|
|
||||||
| funcCall
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 函数调用 */
|
|
||||||
funcCall
|
|
||||||
: ID LPAREN (exp (COMMA exp)*)? RPAREN
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 基础 ===== */
|
|
||||||
|
|
||||||
lValue
|
|
||||||
: ID (LBRACK exp RBRACK)?
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
;
|
|
||||||
@ -1,229 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
CONST: 'const';
|
|
||||||
INT: 'int';
|
|
||||||
FLOAT: 'float';
|
|
||||||
VOID: 'void';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
BREAK: 'break';
|
|
||||||
CONTINUE: 'continue';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
MOD: '%';
|
|
||||||
NOT: '!';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
AND: '&&';
|
|
||||||
OR: '||';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
|
|
||||||
/* 浮点字面量要放在整数字面量前面 */
|
|
||||||
FLOATLITERAL
|
|
||||||
: [0-9]+ '.' [0-9]* ([eE] [+\-]? [0-9]+)?
|
|
||||||
| '.' [0-9]+ ([eE] [+\-]? [0-9]+)?
|
|
||||||
| [0-9]+ [eE] [+\-]? [0-9]+
|
|
||||||
;
|
|
||||||
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Parser rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: constDecl
|
|
||||||
| varDecl
|
|
||||||
;
|
|
||||||
|
|
||||||
constDecl
|
|
||||||
: CONST btype constDef (COMMA constDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
varDecl
|
|
||||||
: btype varDef (COMMA varDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
;
|
|
||||||
|
|
||||||
constDef
|
|
||||||
: ID constExpArrayDims ASSIGN constInitVal
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID arrayDims? (ASSIGN initVal)?
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 变量定义时数组维度一般要求是 exp */
|
|
||||||
arrayDims
|
|
||||||
: (LBRACK exp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
/* const 定义也支持多维 */
|
|
||||||
constExpArrayDims
|
|
||||||
: (LBRACK constExp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
constInitVal
|
|
||||||
: constExp
|
|
||||||
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
initVal
|
|
||||||
: exp
|
|
||||||
| LBRACE (initVal (COMMA initVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcFParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
| VOID
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParams
|
|
||||||
: funcFParam (COMMA funcFParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParam
|
|
||||||
: btype ID
|
|
||||||
| btype ID LBRACK RBRACK (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: lVal ASSIGN exp SEMICOLON # assignStatement
|
|
||||||
| exp? SEMICOLON # expStatement
|
|
||||||
| blockStmt # blockStatement
|
|
||||||
| IF LPAREN cond RPAREN stmt (ELSE stmt)? # ifStatement
|
|
||||||
| WHILE LPAREN cond RPAREN stmt # whileStatement
|
|
||||||
| BREAK SEMICOLON # breakStatement
|
|
||||||
| CONTINUE SEMICOLON # continueStatement
|
|
||||||
| RETURN exp? SEMICOLON # returnStatement
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
|
|
||||||
cond
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
lVal
|
|
||||||
: ID (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| lVal
|
|
||||||
| number
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
| FLOATLITERAL
|
|
||||||
;
|
|
||||||
|
|
||||||
funcRParams
|
|
||||||
: exp (COMMA exp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| ID LPAREN funcRParams? RPAREN
|
|
||||||
| unaryOp unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryOp
|
|
||||||
: ADD
|
|
||||||
| SUB
|
|
||||||
| NOT
|
|
||||||
;
|
|
||||||
|
|
||||||
mulExp
|
|
||||||
: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
addExp
|
|
||||||
: mulExp ((ADD | SUB) mulExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
relExp
|
|
||||||
: addExp ((LT | GT | LE | GE) addExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
eqExp
|
|
||||||
: relExp ((EQ | NEQ) relExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lAndExp
|
|
||||||
: eqExp (AND eqExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lOrExp
|
|
||||||
: lAndExp (OR lAndExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constExp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
@ -1,229 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
CONST: 'const';
|
|
||||||
INT: 'int';
|
|
||||||
FLOAT: 'float';
|
|
||||||
VOID: 'void';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
BREAK: 'break';
|
|
||||||
CONTINUE: 'continue';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
MOD: '%';
|
|
||||||
NOT: '!';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
AND: '&&';
|
|
||||||
OR: '||';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
|
|
||||||
/* 浮点字面量要放在整数字面量前面 */
|
|
||||||
FLOATLITERAL
|
|
||||||
: [0-9]+ '.' [0-9]* ([eE] [+\-]? [0-9]+)?
|
|
||||||
| '.' [0-9]+ ([eE] [+\-]? [0-9]+)?
|
|
||||||
| [0-9]+ [eE] [+\-]? [0-9]+
|
|
||||||
;
|
|
||||||
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Parser rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: constDecl
|
|
||||||
| varDecl
|
|
||||||
;
|
|
||||||
|
|
||||||
constDecl
|
|
||||||
: CONST btype constDef (COMMA constDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
varDecl
|
|
||||||
: btype varDef (COMMA varDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
;
|
|
||||||
|
|
||||||
constDef
|
|
||||||
: ID constExpArrayDims ASSIGN constInitVal
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID arrayDims? (ASSIGN initVal)?
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 变量定义时数组维度一般要求是 exp */
|
|
||||||
arrayDims
|
|
||||||
: (LBRACK exp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
/* const 定义也支持多维 */
|
|
||||||
constExpArrayDims
|
|
||||||
: (LBRACK constExp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
constInitVal
|
|
||||||
: constExp
|
|
||||||
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
initVal
|
|
||||||
: exp
|
|
||||||
| LBRACE (initVal (COMMA initVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcFParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
| VOID
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParams
|
|
||||||
: funcFParam (COMMA funcFParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParam
|
|
||||||
: btype ID
|
|
||||||
| btype ID LBRACK RBRACK (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: lVal ASSIGN exp SEMICOLON # assignStatement
|
|
||||||
| exp? SEMICOLON # expStatement
|
|
||||||
| blockStmt # blockStatement
|
|
||||||
| IF LPAREN cond RPAREN stmt (ELSE stmt)? # ifStatement
|
|
||||||
| WHILE LPAREN cond RPAREN stmt # whileStatement
|
|
||||||
| BREAK SEMICOLON # breakStatement
|
|
||||||
| CONTINUE SEMICOLON # continueStatement
|
|
||||||
| RETURN exp? SEMICOLON # returnStatement
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
|
|
||||||
cond
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
lVal
|
|
||||||
: ID (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| lVal
|
|
||||||
| number
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
| FLOATLITERAL
|
|
||||||
;
|
|
||||||
|
|
||||||
funcRParams
|
|
||||||
: exp (COMMA exp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| ID LPAREN funcRParams? RPAREN
|
|
||||||
| unaryOp unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryOp
|
|
||||||
: ADD
|
|
||||||
| SUB
|
|
||||||
| NOT
|
|
||||||
;
|
|
||||||
|
|
||||||
mulExp
|
|
||||||
: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
addExp
|
|
||||||
: mulExp ((ADD | SUB) mulExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
relExp
|
|
||||||
: addExp ((LT | GT | LE | GE) addExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
eqExp
|
|
||||||
: relExp ((EQ | NEQ) relExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lAndExp
|
|
||||||
: eqExp (AND eqExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lOrExp
|
|
||||||
: lAndExp (OR lAndExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constExp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
@ -1,229 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
CONST: 'const';
|
|
||||||
INT: 'int';
|
|
||||||
FLOAT: 'float';
|
|
||||||
VOID: 'void';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
BREAK: 'break';
|
|
||||||
CONTINUE: 'continue';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
MOD: '%';
|
|
||||||
NOT: '!';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
AND: '&&';
|
|
||||||
OR: '||';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
|
|
||||||
/* 浮点字面量要放在整数字面量前面 */
|
|
||||||
FLOATLITERAL
|
|
||||||
: [0-9]+ '.' [0-9]* ([eE] [+\-]? [0-9]+)?
|
|
||||||
| '.' [0-9]+ ([eE] [+\-]? [0-9]+)?
|
|
||||||
| [0-9]+ [eE] [+\-]? [0-9]+
|
|
||||||
;
|
|
||||||
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Parser rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: constDecl
|
|
||||||
| varDecl
|
|
||||||
;
|
|
||||||
|
|
||||||
constDecl
|
|
||||||
: CONST btype constDef (COMMA constDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
varDecl
|
|
||||||
: btype varDef (COMMA varDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
;
|
|
||||||
|
|
||||||
constDef
|
|
||||||
: ID constExpArrayDims ASSIGN constInitVal
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID arrayDims? (ASSIGN initVal)?
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 变量定义时数组维度一般要求是 exp */
|
|
||||||
arrayDims
|
|
||||||
: (LBRACK exp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
/* const 定义也支持多维 */
|
|
||||||
constExpArrayDims
|
|
||||||
: (LBRACK constExp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
constInitVal
|
|
||||||
: constExp
|
|
||||||
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
initVal
|
|
||||||
: exp
|
|
||||||
| LBRACE (initVal (COMMA initVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcFParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
| VOID
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParams
|
|
||||||
: funcFParam (COMMA funcFParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParam
|
|
||||||
: btype ID
|
|
||||||
| btype ID LBRACK RBRACK (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: lVal ASSIGN exp SEMICOLON # assignStatement
|
|
||||||
| exp? SEMICOLON # expStatement
|
|
||||||
| blockStmt # blockStatement
|
|
||||||
| IF LPAREN cond RPAREN stmt (ELSE stmt)? # ifStatement
|
|
||||||
| WHILE LPAREN cond RPAREN stmt # whileStatement
|
|
||||||
| BREAK SEMICOLON # breakStatement
|
|
||||||
| CONTINUE SEMICOLON # continueStatement
|
|
||||||
| RETURN exp? SEMICOLON # returnStatement
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
|
|
||||||
cond
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
lVal
|
|
||||||
: ID (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| lVal
|
|
||||||
| number
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
| FLOATLITERAL
|
|
||||||
;
|
|
||||||
|
|
||||||
funcRParams
|
|
||||||
: exp (COMMA exp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| ID LPAREN funcRParams? RPAREN
|
|
||||||
| unaryOp unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryOp
|
|
||||||
: ADD
|
|
||||||
| SUB
|
|
||||||
| NOT
|
|
||||||
;
|
|
||||||
|
|
||||||
mulExp
|
|
||||||
: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
addExp
|
|
||||||
: mulExp ((ADD | SUB) mulExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
relExp
|
|
||||||
: addExp ((LT | GT | LE | GE) addExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
eqExp
|
|
||||||
: relExp ((EQ | NEQ) relExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lAndExp
|
|
||||||
: eqExp (AND eqExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lOrExp
|
|
||||||
: lAndExp (OR lAndExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constExp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
@ -1,229 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
CONST: 'const';
|
|
||||||
INT: 'int';
|
|
||||||
FLOAT: 'float';
|
|
||||||
VOID: 'void';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
BREAK: 'break';
|
|
||||||
CONTINUE: 'continue';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
MOD: '%';
|
|
||||||
NOT: '!';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
AND: '&&';
|
|
||||||
OR: '||';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
|
|
||||||
/* 浮点字面量要放在整数字面量前面 */
|
|
||||||
FLOATLITERAL
|
|
||||||
: [0-9]+ '.' [0-9]* ([eE] [+\-]? [0-9]+)?
|
|
||||||
| '.' [0-9]+ ([eE] [+\-]? [0-9]+)?
|
|
||||||
| [0-9]+ [eE] [+\-]? [0-9]+
|
|
||||||
;
|
|
||||||
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Parser rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: constDecl
|
|
||||||
| varDecl
|
|
||||||
;
|
|
||||||
|
|
||||||
constDecl
|
|
||||||
: CONST btype constDef (COMMA constDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
varDecl
|
|
||||||
: btype varDef (COMMA varDef)* SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
;
|
|
||||||
|
|
||||||
constDef
|
|
||||||
: ID constExpArrayDims ASSIGN constInitVal
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID arrayDims? (ASSIGN initVal)?
|
|
||||||
;
|
|
||||||
|
|
||||||
/* 变量定义时数组维度一般要求是 exp */
|
|
||||||
arrayDims
|
|
||||||
: (LBRACK exp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
/* const 定义也支持多维 */
|
|
||||||
constExpArrayDims
|
|
||||||
: (LBRACK constExp RBRACK)+
|
|
||||||
;
|
|
||||||
|
|
||||||
constInitVal
|
|
||||||
: constExp
|
|
||||||
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
initVal
|
|
||||||
: exp
|
|
||||||
| LBRACE (initVal (COMMA initVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcFParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
| VOID
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParams
|
|
||||||
: funcFParam (COMMA funcFParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcFParam
|
|
||||||
: btype ID
|
|
||||||
| btype ID LBRACK RBRACK (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: lVal ASSIGN exp SEMICOLON # assignStatement
|
|
||||||
| exp? SEMICOLON # expStatement
|
|
||||||
| blockStmt # blockStatement
|
|
||||||
| IF LPAREN cond RPAREN stmt (ELSE stmt)? # ifStatement
|
|
||||||
| WHILE LPAREN cond RPAREN stmt # whileStatement
|
|
||||||
| BREAK SEMICOLON # breakStatement
|
|
||||||
| CONTINUE SEMICOLON # continueStatement
|
|
||||||
| RETURN exp? SEMICOLON # returnStatement
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
|
|
||||||
cond
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
lVal
|
|
||||||
: ID (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| lVal
|
|
||||||
| number
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
| FLOATLITERAL
|
|
||||||
;
|
|
||||||
|
|
||||||
funcRParams
|
|
||||||
: exp (COMMA exp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| ID LPAREN funcRParams? RPAREN
|
|
||||||
| unaryOp unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryOp
|
|
||||||
: ADD
|
|
||||||
| SUB
|
|
||||||
| NOT
|
|
||||||
;
|
|
||||||
|
|
||||||
mulExp
|
|
||||||
: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
addExp
|
|
||||||
: mulExp ((ADD | SUB) mulExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
relExp
|
|
||||||
: addExp ((LT | GT | LE | GE) addExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
eqExp
|
|
||||||
: relExp ((EQ | NEQ) relExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lAndExp
|
|
||||||
: eqExp (AND eqExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lOrExp
|
|
||||||
: lAndExp (OR lAndExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constExp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
@ -1,260 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
CONST: 'const';
|
|
||||||
INT: 'int';
|
|
||||||
FLOAT: 'float';
|
|
||||||
VOID: 'void';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
BREAK: 'break';
|
|
||||||
CONTINUE: 'continue';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
MOD: '%';
|
|
||||||
NOT: '!';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
AND: '&&';
|
|
||||||
OR: '||';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
|
|
||||||
/* 浮点字面量放在整数字面量前 */
|
|
||||||
FLOATLITERAL
|
|
||||||
: [0-9]+ '.' [0-9]* ([eE] [+\-]? [0-9]+)?
|
|
||||||
| '.' [0-9]+ ([eE] [+\-]? [0-9]+)?
|
|
||||||
| [0-9]+ [eE] [+\-]? [0-9]+
|
|
||||||
;
|
|
||||||
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Parser rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: constDecl
|
|
||||||
| varDecl
|
|
||||||
;
|
|
||||||
|
|
||||||
constDecl
|
|
||||||
: CONST btype constDefList SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
varDecl
|
|
||||||
: btype varDefList SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
;
|
|
||||||
|
|
||||||
constDefList
|
|
||||||
: constDef (COMMA constDef)*
|
|
||||||
;
|
|
||||||
|
|
||||||
varDefList
|
|
||||||
: varDef (COMMA varDef)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constDef
|
|
||||||
: ID (LBRACK constExp RBRACK)+ ASSIGN constInitVal
|
|
||||||
| ID ASSIGN constInitVal
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID (LBRACK exp RBRACK)* (ASSIGN initVal)?
|
|
||||||
;
|
|
||||||
|
|
||||||
constInitVal
|
|
||||||
: constExp
|
|
||||||
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
initVal
|
|
||||||
: exp
|
|
||||||
| LBRACE (initVal (COMMA initVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
| VOID
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParams
|
|
||||||
: funcParam (COMMA funcParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParam
|
|
||||||
: btype ID
|
|
||||||
| btype ID LBRACK RBRACK (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: returnStmt
|
|
||||||
| assignStmt
|
|
||||||
| expStmt
|
|
||||||
| blockStmt
|
|
||||||
| ifStmt
|
|
||||||
| whileStmt
|
|
||||||
| breakStmt
|
|
||||||
| continueStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
returnStmt
|
|
||||||
: RETURN exp? SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
assignStmt
|
|
||||||
: lValue ASSIGN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
expStmt
|
|
||||||
: exp? SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
ifStmt
|
|
||||||
: IF LPAREN cond RPAREN stmt (ELSE stmt)?
|
|
||||||
;
|
|
||||||
|
|
||||||
whileStmt
|
|
||||||
: WHILE LPAREN cond RPAREN stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
breakStmt
|
|
||||||
: BREAK SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
continueStmt
|
|
||||||
: CONTINUE SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
cond
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
lValue
|
|
||||||
: ID (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| number
|
|
||||||
| lValue
|
|
||||||
;
|
|
||||||
|
|
||||||
funcCall
|
|
||||||
: ID LPAREN funcRParams? RPAREN
|
|
||||||
;
|
|
||||||
|
|
||||||
funcRParams
|
|
||||||
: exp (COMMA exp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| funcCall
|
|
||||||
| unaryOp unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryOp
|
|
||||||
: ADD
|
|
||||||
| SUB
|
|
||||||
| NOT
|
|
||||||
;
|
|
||||||
|
|
||||||
mulExp
|
|
||||||
: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
addExp
|
|
||||||
: mulExp ((ADD | SUB) mulExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
relExp
|
|
||||||
: addExp ((LT | GT | LE | GE) addExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
eqExp
|
|
||||||
: relExp ((EQ | NEQ) relExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lAndExp
|
|
||||||
: eqExp (AND eqExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lOrExp
|
|
||||||
: lAndExp (OR lAndExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constExp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
| FLOATLITERAL
|
|
||||||
;
|
|
||||||
@ -1,260 +0,0 @@
|
|||||||
grammar SysY;
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Lexer rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
CONST: 'const';
|
|
||||||
INT: 'int';
|
|
||||||
FLOAT: 'float';
|
|
||||||
VOID: 'void';
|
|
||||||
RETURN: 'return';
|
|
||||||
IF: 'if';
|
|
||||||
ELSE: 'else';
|
|
||||||
WHILE: 'while';
|
|
||||||
BREAK: 'break';
|
|
||||||
CONTINUE: 'continue';
|
|
||||||
|
|
||||||
ASSIGN: '=';
|
|
||||||
ADD: '+';
|
|
||||||
SUB: '-';
|
|
||||||
MUL: '*';
|
|
||||||
DIV: '/';
|
|
||||||
MOD: '%';
|
|
||||||
NOT: '!';
|
|
||||||
|
|
||||||
LT: '<';
|
|
||||||
GT: '>';
|
|
||||||
LE: '<=';
|
|
||||||
GE: '>=';
|
|
||||||
EQ: '==';
|
|
||||||
NEQ: '!=';
|
|
||||||
|
|
||||||
AND: '&&';
|
|
||||||
OR: '||';
|
|
||||||
|
|
||||||
LPAREN: '(';
|
|
||||||
RPAREN: ')';
|
|
||||||
LBRACE: '{';
|
|
||||||
RBRACE: '}';
|
|
||||||
LBRACK: '[';
|
|
||||||
RBRACK: ']';
|
|
||||||
SEMICOLON: ';';
|
|
||||||
COMMA: ',';
|
|
||||||
|
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
|
||||||
|
|
||||||
/* 浮点字面量放在整数字面量前 */
|
|
||||||
FLOATLITERAL
|
|
||||||
: [0-9]+ '.' [0-9]* ([eE] [+\-]? [0-9]+)?
|
|
||||||
| '.' [0-9]+ ([eE] [+\-]? [0-9]+)?
|
|
||||||
| [0-9]+ [eE] [+\-]? [0-9]+
|
|
||||||
;
|
|
||||||
|
|
||||||
ILITERAL: [0-9]+;
|
|
||||||
|
|
||||||
WS: [ \t\r\n] -> skip;
|
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
|
||||||
|
|
||||||
|
|
||||||
/*====================*/
|
|
||||||
/* Parser rules */
|
|
||||||
/*====================*/
|
|
||||||
|
|
||||||
compUnit
|
|
||||||
: (decl | funcDef)* EOF
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 声明 ===== */
|
|
||||||
|
|
||||||
decl
|
|
||||||
: constDecl
|
|
||||||
| varDecl
|
|
||||||
;
|
|
||||||
|
|
||||||
constDecl
|
|
||||||
: CONST btype constDefList SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
varDecl
|
|
||||||
: btype varDefList SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
btype
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
;
|
|
||||||
|
|
||||||
constDefList
|
|
||||||
: constDef (COMMA constDef)*
|
|
||||||
;
|
|
||||||
|
|
||||||
varDefList
|
|
||||||
: varDef (COMMA varDef)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constDef
|
|
||||||
: ID (LBRACK constExp RBRACK)+ ASSIGN constInitVal
|
|
||||||
| ID ASSIGN constInitVal
|
|
||||||
;
|
|
||||||
|
|
||||||
varDef
|
|
||||||
: ID (LBRACK exp RBRACK)* (ASSIGN initVal)?
|
|
||||||
;
|
|
||||||
|
|
||||||
constInitVal
|
|
||||||
: constExp
|
|
||||||
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
initVal
|
|
||||||
: exp
|
|
||||||
| LBRACE (initVal (COMMA initVal)*)? RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 函数 ===== */
|
|
||||||
|
|
||||||
funcDef
|
|
||||||
: funcType ID LPAREN funcParams? RPAREN blockStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
funcType
|
|
||||||
: INT
|
|
||||||
| FLOAT
|
|
||||||
| VOID
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParams
|
|
||||||
: funcParam (COMMA funcParam)*
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParam
|
|
||||||
: btype ID
|
|
||||||
| btype ID LBRACK RBRACK (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句块 ===== */
|
|
||||||
|
|
||||||
blockStmt
|
|
||||||
: LBRACE blockItem* RBRACE
|
|
||||||
;
|
|
||||||
|
|
||||||
blockItem
|
|
||||||
: decl
|
|
||||||
| stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 语句 ===== */
|
|
||||||
|
|
||||||
stmt
|
|
||||||
: returnStmt
|
|
||||||
| assignStmt
|
|
||||||
| expStmt
|
|
||||||
| blockStmt
|
|
||||||
| ifStmt
|
|
||||||
| whileStmt
|
|
||||||
| breakStmt
|
|
||||||
| continueStmt
|
|
||||||
;
|
|
||||||
|
|
||||||
returnStmt
|
|
||||||
: RETURN exp? SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
assignStmt
|
|
||||||
: lValue ASSIGN exp SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
expStmt
|
|
||||||
: exp? SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
ifStmt
|
|
||||||
: IF LPAREN cond RPAREN stmt (ELSE stmt)?
|
|
||||||
;
|
|
||||||
|
|
||||||
whileStmt
|
|
||||||
: WHILE LPAREN cond RPAREN stmt
|
|
||||||
;
|
|
||||||
|
|
||||||
breakStmt
|
|
||||||
: BREAK SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
continueStmt
|
|
||||||
: CONTINUE SEMICOLON
|
|
||||||
;
|
|
||||||
|
|
||||||
/* ===== 表达式 ===== */
|
|
||||||
|
|
||||||
exp
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
cond
|
|
||||||
: lOrExp
|
|
||||||
;
|
|
||||||
|
|
||||||
lValue
|
|
||||||
: ID (LBRACK exp RBRACK)*
|
|
||||||
;
|
|
||||||
|
|
||||||
primaryExp
|
|
||||||
: LPAREN exp RPAREN
|
|
||||||
| number
|
|
||||||
| lValue
|
|
||||||
;
|
|
||||||
|
|
||||||
funcCall
|
|
||||||
: ID LPAREN funcRParams? RPAREN
|
|
||||||
;
|
|
||||||
|
|
||||||
funcRParams
|
|
||||||
: exp (COMMA exp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryExp
|
|
||||||
: primaryExp
|
|
||||||
| funcCall
|
|
||||||
| unaryOp unaryExp
|
|
||||||
;
|
|
||||||
|
|
||||||
unaryOp
|
|
||||||
: ADD
|
|
||||||
| SUB
|
|
||||||
| NOT
|
|
||||||
;
|
|
||||||
|
|
||||||
mulExp
|
|
||||||
: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
addExp
|
|
||||||
: mulExp ((ADD | SUB) mulExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
relExp
|
|
||||||
: addExp ((LT | GT | LE | GE) addExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
eqExp
|
|
||||||
: relExp ((EQ | NEQ) relExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lAndExp
|
|
||||||
: eqExp (AND eqExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
lOrExp
|
|
||||||
: lAndExp (OR lAndExp)*
|
|
||||||
;
|
|
||||||
|
|
||||||
constExp
|
|
||||||
: addExp
|
|
||||||
;
|
|
||||||
|
|
||||||
number
|
|
||||||
: ILITERAL
|
|
||||||
| FLOATLITERAL
|
|
||||||
;
|
|
||||||
Loading…
Reference in new issue