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