SysY.g4文件修改(变量命名修改,注释改为中文)

tangttangtang 2 weeks ago
parent 96dda8642a
commit 472f059af7

@ -19,8 +19,8 @@ public class SysYLexer extends Lexer {
CONST=1, INT=2, FLOAT=3, VOID=4, IF=5, ELSE=6, WHILE=7, BREAK=8, CONTINUE=9,
RETURN=10, ADD=11, SUB=12, MUL=13, DIV=14, MOD=15, ASSIGN=16, EQ=17, NE=18,
LT=19, LE=20, GT=21, GE=22, NOT=23, AND=24, OR=25, LPAREN=26, RPAREN=27,
LBRACK=28, RBRACK=29, LBRACE=30, RBRACE=31, COMMA=32, SEMI=33, IDENT=34,
ILITERAL=35, FLITERAL=36, WS=37, LINE_COMMENT=38, BLOCK_COMMENT=39;
LBRACK=28, RBRACK=29, LBRACE=30, RBRACE=31, COMMA=32, SEMI=33, Ident=34,
IntConst=35, FloatConst=36, WS=37, LINE_COMMENT=38, BLOCK_COMMENT=39;
public static String[] channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
};
@ -34,10 +34,10 @@ public class SysYLexer extends Lexer {
"CONST", "INT", "FLOAT", "VOID", "IF", "ELSE", "WHILE", "BREAK", "CONTINUE",
"RETURN", "ADD", "SUB", "MUL", "DIV", "MOD", "ASSIGN", "EQ", "NE", "LT",
"LE", "GT", "GE", "NOT", "AND", "OR", "LPAREN", "RPAREN", "LBRACK", "RBRACK",
"LBRACE", "RBRACE", "COMMA", "SEMI", "IDENT", "DIGIT", "NON_ZERO_DIGIT",
"OCT_DIGIT", "HEX_DIGIT", "DEC_INTEGER", "OCT_INTEGER", "HEX_INTEGER",
"DEC_FRACTION", "DEC_EXPONENT", "DEC_FLOAT", "HEX_FRACTION", "BIN_EXPONENT",
"HEX_FLOAT", "ILITERAL", "FLITERAL", "WS", "LINE_COMMENT", "BLOCK_COMMENT"
"LBRACE", "RBRACE", "COMMA", "SEMI", "Ident", "Digit", "NonzeroDigit",
"OctDigit", "HexDigit", "DecInteger", "OctInteger", "HexInteger", "DecFraction",
"DecExponent", "DecFloat", "HexFraction", "BinExponent", "HexFloat",
"IntConst", "FloatConst", "WS", "LINE_COMMENT", "BLOCK_COMMENT"
};
}
public static final String[] ruleNames = makeRuleNames();
@ -56,8 +56,8 @@ public class SysYLexer extends Lexer {
null, "CONST", "INT", "FLOAT", "VOID", "IF", "ELSE", "WHILE", "BREAK",
"CONTINUE", "RETURN", "ADD", "SUB", "MUL", "DIV", "MOD", "ASSIGN", "EQ",
"NE", "LT", "LE", "GT", "GE", "NOT", "AND", "OR", "LPAREN", "RPAREN",
"LBRACK", "RBRACK", "LBRACE", "RBRACE", "COMMA", "SEMI", "IDENT", "ILITERAL",
"FLITERAL", "WS", "LINE_COMMENT", "BLOCK_COMMENT"
"LBRACK", "RBRACK", "LBRACE", "RBRACE", "COMMA", "SEMI", "Ident", "IntConst",
"FloatConst", "WS", "LINE_COMMENT", "BLOCK_COMMENT"
};
}
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
grammar SysY;
// ========================= Parser Rules =========================
// ===================================== 语法规则(Parser Rules =====================================
compUnit: (decl | funcDef)+ EOF;
@ -10,7 +10,7 @@ constDecl: CONST bType constDef (COMMA constDef)* SEMI;
bType: INT | FLOAT;
constDef: IDENT (LBRACK constExp RBRACK)* ASSIGN constInitVal;
constDef: Ident (LBRACK constExp RBRACK)* ASSIGN constInitVal;
constInitVal:
constExp
@ -18,19 +18,21 @@ constInitVal:
varDecl: bType varDef (COMMA varDef)* SEMI;
varDef: IDENT (LBRACK constExp RBRACK)* (ASSIGN initVal)?;
varDef:
Ident (LBRACK constExp RBRACK)*
| Ident (LBRACK constExp RBRACK)* ASSIGN initVal;
initVal: exp | LBRACE (initVal (COMMA initVal)*)? RBRACE;
funcDef: funcType IDENT LPAREN funcFParams? RPAREN block;
funcDef: funcType Ident LPAREN funcFParams? RPAREN block;
funcType: VOID | INT | FLOAT;
funcFParams: funcFParam (COMMA funcFParam)*;
funcFParam:
bType IDENT
| bType IDENT LBRACK RBRACK (LBRACK exp RBRACK)*;
bType Ident
| bType Ident LBRACK RBRACK (LBRACK exp RBRACK)*;
block: LBRACE blockItem* RBRACE;
@ -50,15 +52,15 @@ exp: addExp;
cond: lOrExp;
lVal: IDENT (LBRACK exp RBRACK)*;
lVal: Ident (LBRACK exp RBRACK)*;
primaryExp: LPAREN exp RPAREN | lVal | number;
number: ILITERAL | FLITERAL;
number: IntConst | FloatConst;
unaryExp:
primaryExp # primaryUnaryExp
| IDENT LPAREN funcRParams? RPAREN # callUnaryExp
| Ident LPAREN funcRParams? RPAREN # callUnaryExp
| unaryOp unaryExp # opUnaryExp;
unaryOp: ADD | SUB | NOT;
@ -87,9 +89,9 @@ lOrExp: lAndExp # andLOrExp | lOrExp OR lAndExp # binaryLOrExp;
constExp: addExp;
// ========================= Lexer Rules =========================
// ===================================== 词法规则(Lexer Rules =====================================
// keywords
// ---------- 关键字 ----------
CONST: 'const';
INT: 'int';
FLOAT: 'float';
@ -101,7 +103,7 @@ BREAK: 'break';
CONTINUE: 'continue';
RETURN: 'return';
// operators
// ---------- 运算符 ----------
ADD: '+';
SUB: '-';
MUL: '*';
@ -121,7 +123,7 @@ NOT: '!';
AND: '&&';
OR: '||';
// punctuations
// ---------- 界符 ----------
LPAREN: '(';
RPAREN: ')';
LBRACK: '[';
@ -131,50 +133,67 @@ RBRACE: '}';
COMMA: ',';
SEMI: ';';
// identifiers
IDENT: [a-zA-Z_] [a-zA-Z_0-9]*;
// ---------- 标识符 ---------- 与文档中的 Ident 对应
Ident: [a-zA-Z_] [a-zA-Z_0-9]*;
// integer literals
fragment DIGIT: [0-9];
// ===================================== 数值常量相关片段规则 =====================================
fragment NON_ZERO_DIGIT: [1-9];
// 十进制数字
fragment Digit: [0-9];
fragment OCT_DIGIT: [0-7];
// 非零十进制数字
fragment NonzeroDigit: [1-9];
fragment HEX_DIGIT: [0-9a-fA-F];
// 八进制数字
fragment OctDigit: [0-7];
fragment DEC_INTEGER: NON_ZERO_DIGIT DIGIT*;
// 十六进制数字
fragment HexDigit: [0-9a-fA-F];
fragment OCT_INTEGER: '0' OCT_DIGIT*;
// 十进制整数:非零开头,后接若干十进制数字
fragment DecInteger: NonzeroDigit Digit*;
fragment HEX_INTEGER: '0' [xX] HEX_DIGIT+;
// 八进制整数:以 0 开头
fragment OctInteger: '0' OctDigit*;
// float literals
fragment DEC_FRACTION: DIGIT+ '.' DIGIT* | '.' DIGIT+;
// 十六进制整数:以 0x 或 0X 开头
fragment HexInteger: '0' [xX] HexDigit+;
fragment DEC_EXPONENT: [eE] [+\-]? DIGIT+;
// 十进制小数部分
fragment DecFraction: Digit+ '.' Digit* | '.' Digit+;
fragment DEC_FLOAT:
DEC_FRACTION DEC_EXPONENT?
| DEC_INTEGER DEC_EXPONENT;
// 十进制指数部分
fragment DecExponent: [eE] [+\-]? Digit+;
fragment HEX_FRACTION:
HEX_DIGIT* '.' HEX_DIGIT+
| HEX_DIGIT+ '.';
// 十进制浮点数
fragment DecFloat:
DecFraction DecExponent?
| DecInteger DecExponent;
fragment BIN_EXPONENT: [pP] [+\-]? DIGIT+;
// 十六进制小数部分
fragment HexFraction: HexDigit* '.' HexDigit+ | HexDigit+ '.';
fragment HEX_FLOAT:
'0' [xX] HEX_FRACTION BIN_EXPONENT
| HEX_INTEGER BIN_EXPONENT;
// 十六进制浮点数的二进制指数部分
fragment BinExponent: [pP] [+\-]? Digit+;
ILITERAL: DEC_INTEGER | OCT_INTEGER | HEX_INTEGER;
// 十六进制浮点数
fragment HexFloat:
'0' [xX] HexFraction BinExponent
| HexInteger BinExponent;
FLITERAL: DEC_FLOAT | HEX_FLOAT;
// ---------- 整数字面量 ---------- 与文档中的 IntConst 对应
IntConst: DecInteger | OctInteger | HexInteger;
// comments and whitespace
// ---------- 浮点数字面量 ---------- 与文档中的 FloatConst 对应
FloatConst: DecFloat | HexFloat;
// ===================================== 空白符与注释 =====================================
// 空白字符直接跳过
WS: [ \t\r\n]+ -> skip;
// 单行注释:从 // 到行末
LINE_COMMENT: '//' ~[\r\n]* -> skip;
// 多行注释:从 /* 到 */
BLOCK_COMMENT: '/*' .*? '*/' -> skip;
Loading…
Cancel
Save