-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseGrammar.g4
executable file
·128 lines (106 loc) · 3.6 KB
/
BaseGrammar.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
grammar BaseGrammar;
import BaseLexerRules;
@parser::header {
import java.util.Map;
import java.util.HashMap;
}
@parser::members {static protected Map<String,BGSymbol> symbolTable = new HashMap<>();
}
// Instructions may or may not be separated by '\n' character
// Accepts empty lines
main: statList EOF;
statList: (stat)*;
stat:
loop
|if_else
|instruction
;
// Value
value returns[vartype typ,String unit,String nr]:
num=(INT|REAL) pow? NAME #valueUnit
| '!' num=(INT|REAL) pow? NAME #valueUnitNeg
|num=(INT|REAL) pow? #valueS
| '!' num=(INT|REAL) pow? #valueSNeg
;
// General intruction
instruction returns[String varName]:
// Variable declaration
varType NAME #varDec
// Print/Read variable
| print #instPrint
// Value atribution to variable
// (This also accepts values that are not the result of an operation)
|(varType)? NAME '=' operation #assignment
// Operation without storing result or (most common) variable increment/decrement
| operation #soloOp
| deincrement #instDeincr
| convValue #instConv
;
print: 'Print' '(' NAME ')';
/* --------------------
* CONDITIONALS SECTION
* --------------------
*/
if_else:
'if' '(' (cc=condition|bc=booleanCondition) ')' (ifA=ifArg) ('else' (elseA=ifArg))?;
ifArg:
'{'statList'}' #ifStatList
|stat? #ifStat
;
/* -------------
* LOOPS SECTION
* -------------
*/
loop:
// FOR LOOP
'for' '(' var=NAME ';' min=INT ';' max=INT ')' '{' statList '}' #loopFor
// WHILE LOOP
| 'while' '(' (cc=condition|bc=booleanCondition) ')' '{' statList '}' #loopWhile
// DO-WHILE LOOP
| 'do' '{' statList '}' 'while' '(' (cc=condition|bc=booleanCondition) ')' #loopDoWhile
;
/* ------------------
* OPERATIONS SECTION
* ------------------
*/
// Operations
operation returns [vartype ty,String varGen] :
'(' n=operation ')' #par
| left=operation NUMERIC_OPERATOR right=operation #op
| NAME #assignVar
| value #val
;
deincrement returns [vartype ty]:
NAME '++' #increment
| NAME '--' #decrement
;
// Conditions
booleanCondition returns[String varGen]:
left=booleanCondition BOOLEAN_OPERATOR right=booleanCondition #boolCondOp
|NOT condition #boolNotCond
|condition #boolCond
;
condition returns[String varGen]:
left=conditionE CONDITIONAL_OPERATOR right=conditionE #compare
|conditionE #soloCond
;
conditionE returns [vartype type,String varGen]:
value #condiEValue
|NAME #condiEVar
;
convValue: src=value '€' dest=NAME;
// Variable Types
varType:
'simpVar' #simple
| 'unitVar' #unit
;
// Equivalent to "*10^"
pow: 'e^' (min='!')? exp=(INT|REAL);
BOOLEAN_OPERATOR:
'and'
| 'or'
;
NOT: 'not';
NAME: [a-zA-Z] [a-zA-Z_0-9]*;
WS: [ \t\r\n]+ -> skip;
COMMENT: '->' ~[\r\n]* -> skip;