feat: 定义 SysY 核心词法规则和基础语法结构
This commit is contained in:
@@ -1,8 +1,4 @@
|
|||||||
// SysY 子集语法:支持形如
|
// SysY 语法:扩展支持更多SysY特性
|
||||||
// int main() { int a = 1; int b = 2; return a + b; }
|
|
||||||
// 的最小返回表达式编译。
|
|
||||||
|
|
||||||
// 后续需要自行添加
|
|
||||||
|
|
||||||
grammar SysY;
|
grammar SysY;
|
||||||
|
|
||||||
@@ -10,43 +6,107 @@ grammar SysY;
|
|||||||
/* Lexer rules */
|
/* Lexer rules */
|
||||||
/*===-------------------------------------------===*/
|
/*===-------------------------------------------===*/
|
||||||
|
|
||||||
|
// 关键字
|
||||||
INT: 'int';
|
INT: 'int';
|
||||||
|
FLOAT: 'float';
|
||||||
|
VOID: 'void';
|
||||||
|
CONST: 'const';
|
||||||
RETURN: 'return';
|
RETURN: 'return';
|
||||||
|
IF: 'if';
|
||||||
|
ELSE: 'else';
|
||||||
|
WHILE: 'while';
|
||||||
|
BREAK: 'break';
|
||||||
|
CONTINUE: 'continue';
|
||||||
|
|
||||||
|
// 操作符
|
||||||
ASSIGN: '=';
|
ASSIGN: '=';
|
||||||
ADD: '+';
|
ADD: '+';
|
||||||
|
SUB: '-';
|
||||||
|
MUL: '*';
|
||||||
|
DIV: '/';
|
||||||
|
MOD: '%';
|
||||||
|
LT: '<';
|
||||||
|
LE: '<=';
|
||||||
|
GT: '>';
|
||||||
|
GE: '>=';
|
||||||
|
EQ: '==';
|
||||||
|
NE: '!=';
|
||||||
|
|
||||||
|
// 逻辑操作符
|
||||||
|
NOT: '!';
|
||||||
|
AND: '&&';
|
||||||
|
OR: '||';
|
||||||
|
|
||||||
|
// 括号
|
||||||
LPAREN: '(';
|
LPAREN: '(';
|
||||||
RPAREN: ')';
|
RPAREN: ')';
|
||||||
LBRACE: '{';
|
LBRACE: '{';
|
||||||
RBRACE: '}';
|
RBRACE: '}';
|
||||||
|
LBRACK: '[';
|
||||||
|
RBRACK: ']';
|
||||||
|
|
||||||
|
// 标点
|
||||||
SEMICOLON: ';';
|
SEMICOLON: ';';
|
||||||
|
COMMA: ',';
|
||||||
|
|
||||||
|
// 标识符和字面量
|
||||||
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
ID: [a-zA-Z_][a-zA-Z_0-9]*;
|
||||||
ILITERAL: [0-9]+;
|
ILITERAL
|
||||||
|
: DECIMAL_LITERAL
|
||||||
|
| OCTAL_LITERAL
|
||||||
|
| HEX_LITERAL
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment DECIMAL_LITERAL
|
||||||
|
: [0-9]+
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment OCTAL_LITERAL
|
||||||
|
: '0' [0-7]+
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment HEX_LITERAL
|
||||||
|
: '0' ('x' | 'X') [0-9a-fA-F]+
|
||||||
|
;
|
||||||
|
|
||||||
|
// 空白和注释
|
||||||
WS: [ \t\r\n] -> skip;
|
WS: [ \t\r\n] -> skip;
|
||||||
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
LINECOMMENT: '//' ~[\r\n]* -> skip;
|
||||||
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
BLOCKCOMMENT: '/*' .*? '*/' -> skip;
|
||||||
|
|
||||||
/*===-------------------------------------------===*/
|
/*===-------------------------------------------===*/
|
||||||
/* Syntax rules */
|
/* Parser rules */
|
||||||
/*===-------------------------------------------===*/
|
/*===-------------------------------------------===*/
|
||||||
|
|
||||||
compUnit
|
compUnit
|
||||||
: funcDef EOF
|
: (decl | funcDef)* EOF
|
||||||
;
|
;
|
||||||
|
|
||||||
decl
|
decl
|
||||||
: btype varDef SEMICOLON
|
: constDecl
|
||||||
|
| varDecl
|
||||||
|
;
|
||||||
|
|
||||||
|
constDecl
|
||||||
|
: CONST btype constDef (COMMA constDef)* SEMICOLON
|
||||||
|
;
|
||||||
|
|
||||||
|
varDecl
|
||||||
|
: btype varDef (COMMA varDef)* SEMICOLON
|
||||||
;
|
;
|
||||||
|
|
||||||
btype
|
btype
|
||||||
: INT
|
: INT
|
||||||
|
| FLOAT
|
||||||
|
| VOID
|
||||||
|
;
|
||||||
|
|
||||||
|
constDef
|
||||||
|
: ID ASSIGN initValue
|
||||||
;
|
;
|
||||||
|
|
||||||
varDef
|
varDef
|
||||||
: lValue (ASSIGN initValue)?
|
: ID (ASSIGN initValue)?
|
||||||
;
|
;
|
||||||
|
|
||||||
initValue
|
initValue
|
||||||
@@ -59,6 +119,8 @@ funcDef
|
|||||||
|
|
||||||
funcType
|
funcType
|
||||||
: INT
|
: INT
|
||||||
|
| FLOAT
|
||||||
|
| VOID
|
||||||
;
|
;
|
||||||
|
|
||||||
blockStmt
|
blockStmt
|
||||||
@@ -75,18 +137,13 @@ stmt
|
|||||||
;
|
;
|
||||||
|
|
||||||
returnStmt
|
returnStmt
|
||||||
: RETURN exp SEMICOLON
|
: RETURN (exp)? SEMICOLON
|
||||||
;
|
;
|
||||||
|
|
||||||
exp
|
exp
|
||||||
: LPAREN exp RPAREN # parenExp
|
: LPAREN exp RPAREN # parenExp
|
||||||
| var # varExp
|
| lValue # lValueExp
|
||||||
| number # numberExp
|
| number # numberExp
|
||||||
| exp ADD exp # additiveExp
|
|
||||||
;
|
|
||||||
|
|
||||||
var
|
|
||||||
: ID
|
|
||||||
;
|
;
|
||||||
|
|
||||||
lValue
|
lValue
|
||||||
|
|||||||
Reference in New Issue
Block a user