-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcGrammar.g4
44 lines (42 loc) · 895 Bytes
/
calcGrammar.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
grammar calcGrammar;
EQUAL: '=';
NLINE: '\n';
INT: [0-9]+;
DOUBLE: [0-9]+ '.' [0-9]+
| '.'[0-9]+;
ADD: '+';
MINUS: '-';
MULT: '*';
DIV: '/';
ID: [a-z_A-Z_0-9]+;
WSPACE: [ \t\r] -> skip;
LPAR: '(';
RPAR: ')';
EXP: '^';
start:
ID EQUAL NLINE #Assign
| addSub NLINE? EOF #Calculate
;
addSub:
addSub ADD multDiv #Add
| addSub MINUS multDiv #Subtract
| multDiv #ToMultiplyorDivide
;
multDiv:
multDiv MULT pow #Multiply
| multDiv DIV pow #Divide
| pow #Exponent
;
pow:
negative (EXP pow)? #RaisetoPower
;
negative:
MINUS negative #TimesNegativeOne
| type #ToDataType
;
type:
INT #Integer
| DOUBLE #Double
| ID #Variable
| '(' addSub ')' #Parenthesis
;