-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mx.g4
217 lines (164 loc) · 4.8 KB
/
Mx.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
grammar Mx;
translationUnit: declarationSeq? EOF;
// Expressions
expression: assignmentExpression (COMMA assignmentExpression)*;
assignmentExpression:
logicalOrExpression (ASSIGN logicalOrExpression)*;
logicalOrExpression:
logicalAndExpression (OR_OR logicalAndExpression)*;
logicalAndExpression:
inclusiveOrExpression (AND_AND inclusiveOrExpression)*;
inclusiveOrExpression:
exclusiveOrExpression (OR exclusiveOrExpression)*;
exclusiveOrExpression: andExpression (CARET andExpression)*;
andExpression: equalityExpression (AND equalityExpression)*;
equalityExpression:
relationalExpression (
op+=(EQUAL | NOT_EQUAL) relationalExpression
)*;
relationalExpression:
shiftExpression (
op+=(LESS | LESS_EQUAL | GREATER | GREATER_EQUAL) shiftExpression
)*;
shiftExpression:
additiveExpression (
op+=(LESS_LESS | GREATER_GREATER) additiveExpression
)*;
additiveExpression:
multiplicativeExpression (
op+=(PLUS | MINUS) multiplicativeExpression
)*;
multiplicativeExpression:
unaryExpression (op+=(STAR | DIV | MOD) unaryExpression)*;
unaryExpression:
postfixExpression
| op=(PLUS_PLUS | MINUS_MINUS | PLUS | MINUS | NOT | TILDE) unaryExpression
| newExpression;
newExpression:
NEW typeSpecifier (
(LEFT_BRACKET expression RIGHT_BRACKET)+ (
LEFT_BRACKET RIGHT_BRACKET
)*
| (LEFT_PAREN RIGHT_PAREN)
)?;
postfixExpression:
primaryExpression
| postfixExpression op=(PLUS_PLUS | MINUS_MINUS)
| postfixExpression LEFT_PAREN expressionList? RIGHT_PAREN
| postfixExpression LEFT_BRACKET expression RIGHT_BRACKET
| postfixExpression DOT idExpression;
expressionList:
assignmentExpression (COMMA assignmentExpression)*;
primaryExpression:
literal
| THIS
| LEFT_PAREN expression RIGHT_PAREN
| idExpression;
idExpression: IDENTIFIER;
literal: boolean_literal | INTEGER_LITERAL | STRING_LITERAL | NULL;
boolean_literal: TRUE | FALSE;
// Statements
statement:
expressionStatement
| compoundStatement
| selectionStatement
| iterationStatement
| jumpStatement
| declarationStatement;
expressionStatement: expression? SEMI;
compoundStatement: LEFT_BRACE statementSeq? RIGHT_BRACE;
statementSeq: statement+;
selectionStatement:
IF LEFT_PAREN expression RIGHT_PAREN ifBody=statement (
ELSE elseBody=statement
)?;
iterationStatement:
WHILE LEFT_PAREN whileCondition=expression RIGHT_PAREN statement
| FOR LEFT_PAREN forInitStatement forCondition=expression? SEMI forIteration=expression? RIGHT_PAREN statement;
forInitStatement: expressionStatement | simpleDeclaration;
jumpStatement: (BREAK | CONTINUE | RETURN expression?) SEMI;
declarationStatement: simpleDeclaration;
// Declarations
declarationSeq: declaration+;
declaration: simpleDeclaration | functionDefinition;
simpleDeclaration: (typeSpecifier initDeclaratorList?)? SEMI;
initDeclaratorList: initDeclarator (COMMA initDeclarator)*;
initDeclarator: declarator initializer?;
declarator: (LEFT_BRACKET RIGHT_BRACKET)* idExpression parametersAndQualifiers?;
parametersAndQualifiers:
LEFT_PAREN parameterDeclarationList? RIGHT_PAREN;
parameterDeclarationList:
parameterDeclaration (COMMA parameterDeclaration)*;
parameterDeclaration: typeSpecifier declarator;
initializer: ASSIGN assignmentExpression;
typeSpecifier: simpleTypeSpecifier | classSpecifier;
simpleTypeSpecifier: IDENTIFIER | INT | BOOL | STRING | VOID;
classSpecifier:
CLASS className=IDENTIFIER LEFT_BRACE declarationSeq? RIGHT_BRACE;
functionDefinition:
simpleTypeSpecifier? declarator compoundStatement;
// reserved names
BOOL: 'bool';
INT: 'int';
STRING: 'string';
NULL: 'null';
VOID: 'void';
TRUE: 'true';
FALSE: 'false';
IF: 'if';
ELSE: 'else';
FOR: 'for';
WHILE: 'while';
BREAK: 'break';
CONTINUE: 'continue';
RETURN: 'return';
NEW: 'new';
CLASS: 'class';
THIS: 'this';
// symbols
LEFT_PAREN: '(';
RIGHT_PAREN: ')';
LEFT_BRACKET: '[';
RIGHT_BRACKET: ']';
LEFT_BRACE: '{';
RIGHT_BRACE: '}';
PLUS: '+';
MINUS: '-';
STAR: '*';
DIV: '/';
MOD: '%';
CARET: '^';
AND: '&';
OR: '|';
TILDE: '~';
NOT: '!';
LESS_LESS: '<<';
GREATER_GREATER: '>>';
ASSIGN: '=';
LESS: '<';
GREATER: '>';
EQUAL: '==';
NOT_EQUAL: '!=';
LESS_EQUAL: '<=';
GREATER_EQUAL: '>=';
AND_AND: '&&';
OR_OR: '||';
PLUS_PLUS: '++';
MINUS_MINUS: '--';
COMMA: ',';
DOT: '.';
SEMI: ';';
// TOKENS
IDENTIFIER: LETTER (NONDIGIT | DIGIT)*;
INTEGER_LITERAL: DECIMAL_LITERAL;
STRING_LITERAL: '"' (~["\\\n\r] | '\\' ["\\nr])* '"';
// encoding
fragment DIGIT: [0-9];
fragment NONZERO_DIGIT: [1-9];
DECIMAL_LITERAL: NONZERO_DIGIT DIGIT* | DIGIT;
fragment LETTER: [a-zA-Z];
fragment NONDIGIT: [a-zA-Z_];
WHITESPACE: [ \t]+ -> skip;
NEWLINE: ('\r' '\n'? | '\n') -> skip;
BLOCK_COMMENT: '/*' .*? '*/' -> skip;
LINE_COMMENT: '//' ~[\r\n]* -> skip;