This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mC.g4
162 lines (134 loc) · 2.81 KB
/
mC.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
grammar mC;
start: statement* EOF;
statement:
ParCurBeg statement* ParCurEnd
| expression Semicolon
| stIf
| stWhile
| stDoWhile
| stFor;
stIf:
If ParRoundBeg expression ParRoundEnd statement (
Else statement
)?;
stWhile: While ParRoundBeg expression ParRoundEnd statement;
stDoWhile:
Do statement While ParRoundBeg expression ParRoundEnd Semicolon;
stFor:
For ParRoundBeg expression? Semicolon expression? Semicolon expression? ParRoundEnd statement;
// operators by precedence
expression:
fncCall
| ParRoundBeg expression ParRoundEnd
// ++ --
| ((OpInc | OpDec) Variable)
// - + ! ~
| ((OpPlus | OpMinus | OpBitNeg | OpNeg) expression)
// * / %
| expression (OpMul | OpDiv | OpMod) expression
// + -
| expression (OpPlus | OpMinus) expression
// << >>
| expression (OpBitShiftLeft | OpBitShiftRight) expression
// == !=
| expression (OpEq | OpEqNot) expression
// < > <= >=
| expression (OpGt | OpGte | OpLt | OpLte) expression
// &
| expression (OpBitAnd) expression
// ^
| expression (OpBitXor) expression
// |
| expression (OpBitOr) expression
// &&
| expression (OpAnd) expression
// ||
| expression (OpOr) expression
| assign
| Boolean
| number
| String
| Char
| Variable;
arguments: (expression (Comma expression)*)?;
fncCall: Variable ParRoundBeg arguments ParRoundEnd;
assign:
Variable OpAssign expression
| Variable opAssignSpecial expression;
opAssignSpecial:
(
OpMul
| OpDiv
| OpMod
| OpPlus
| OpMinus
| OpBitShiftLeft
| OpBitShiftRight
| OpAnd
| OpOr
| OpBitXor
) OpAssign;
Boolean: (True | False);
number: (NumberPrefBin | NumberPrefHex)? Digits+;
/*
* Lexer
*/
Ws: [ ] -> skip;
Nl: [\n\r] -> skip;
CommentMlBeg: '\\*';
CommentMlEnd: '*\\';
CommentSl: '//';
Comment: (CommentMlBeg .*? CommentMlEnd | CommentSl .*? Nl) -> skip;
String: StringDelimiter CharSequence StringDelimiter;
Char: '\'' ('\\' . | .) '\'';
// special symbols
Semicolon: ';';
Colon: ':';
Comma: ',';
// - operators
OpInc: '++';
OpDec: '--';
OpNeg: '!';
OpPlus: '+';
OpMinus: '-';
OpMul: '*';
OpDiv: '/';
OpMod: '%';
OpEq: '==';
OpEqNot: '!=';
OpLt: '<';
OpGt: '>';
OpLte: '<=';
OpGte: '>=';
OpBitShiftLeft: '<<';
OpBitShiftRight: '>>';
OpBitNeg: '~';
OpBitAnd: '&';
OpBitXor: '^';
OpBitOr: '|';
OpAnd: '&&' | 'and' | 'AND';
// todo: priorita ?
OpOr: '||' | 'or' | 'OR';
OpAssign: '=';
// - parenthesis
ParCurBeg: '{';
ParCurEnd: '}';
ParRoundBeg: '(';
ParRoundEnd: ')';
ParSquareBeg: '[';
ParSquareEnd: ']';
// keywords
If: 'if' | 'IF';
Else: 'else' | 'ELSE';
For: 'for' | 'FOR';
While: 'while' | 'WHILE';
Do: 'do' | 'DO';
True: 'true' | 'TRUE';
False: 'false' | 'FALSE';
NumberPrefBin: '0' ('b' | 'B');
NumberPrefHex: '0' ('x' | 'X');
Digits: [0-9]+;
Variable: AZ (AZ | Digits)*;
fragment StringDelimiter: '"';
fragment AZ: [a-zA-Z];
fragment CharSequence: ('\\"' | ~["\r\n])+?;