-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_Antlr.g
More file actions
281 lines (247 loc) · 7.79 KB
/
Project_Antlr.g
File metadata and controls
281 lines (247 loc) · 7.79 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
grammar Project_Antlr;
@members{
GrammarFunctions g = new GrammarFunctions();
Stack<Boolean> compStacks [] = null;
boolean variableCheck = true;
}
prog : (stmt)+
;
stmt : assignment | element_assignment | list_addition | slicing | list_comprehension |
print
;
assignment : ID {g.initList($ID.text);} '=' '[' value_list[$ID.text] ']'
;
element_assignment : ID '[' INT ']' '=' value {int i = new Integer($INT.text).intValue(); g.update($ID.text,i,$value.v);}
;
list_addition : list0 = ID '=' list1 = ID {
ArrayList<Object> AL = new ArrayList<Object>();
for (Object o: g.lists.get($list1.text)){
AL.add(o);
}
}
('+' list2 = ID {
for (Object o: g.lists.get($list2.text)){
AL.add(o);
}
})*
{g.lists.put($list0.text,AL);}
;
slicing : id1 = ID '=' id2 = ID '[' (start = INT)? ':' (end = INT)? ']' {
int s = 0;
int e = g.lists.get($id2.text).size();
if ($start.text != null){
s = Integer.parseInt($start.text);
}
if ($end.text != null){
e = Integer.parseInt($end.text);
}
ArrayList<Object> al = new ArrayList<Object>();
int i = s;
while (i<e){
al.add(g.lists.get($id2.text).get(i));
i+=1;
}
g.lists.put($id1.text,al);
}
;
list_comprehension : destinationList = ID '=' '[' 'for' variable = ID 'in' sourceList = ID {int n = g.lists.get($sourceList.text).size(); compStacks = new Stack[n]; for (int i = 0; i < n; i++) compStacks[i] = new Stack<>();}'if' logical_statement[$sourceList.text,$variable.text] ']'
{
ArrayList<Object> sourcelist = g.lists.get($sourceList.text);
ArrayList<Object> a = new ArrayList<>();
if(variableCheck){
for (int i = 0; i<n;i++){
if (compStacks[i].pop()){
a.add(sourcelist.get(i));
}
}
}
g.lists.put($destinationList.text,a);
}
;
value_list [String listID] : v1 = value {g.addToList($listID,$v1.v);} (',' v2 = value {g.addToList($listID,$v2.v);})*
|
;
value returns [Object v] : INT {$v = new Integer($INT.text);}
| STRING {$v = new String($STRING.text);}
| ID {$v = new String($ID.text);}
;
print : 'print' '(' ID ')' {g.printList($ID.text);}
;
logical_statement[String listname, String var] : logical_statement1[$listname,$var] ('or' logical_statement1[$listname,$var]{
int n = compStacks.length;
for (int i = 0;i<n; i++){
boolean first = compStacks[i].pop();
boolean second = compStacks[i].pop();
boolean result = first || second;
compStacks[i].push(result);
}
})*
;
logical_statement1[String listname, String var] : logical_statement2[$listname,$var] ('and' logical_statement2[$listname,$var]{
int n = compStacks.length;
for (int i = 0;i<n; i++){
boolean first = compStacks[i].pop();
boolean second = compStacks[i].pop();
boolean result = first && second;
compStacks[i].push(result);
}
})*
;
logical_statement2[String listname, String var] : (notOperation = 'not')? term[$listname, $var] {
if($notOperation.text!=null){
int n = compStacks.length;
for (int i = 0; i<n; i++){
boolean temp = compStacks[i].pop();
temp = !temp;
compStacks[i].push(temp);
}
}
}
;
term[String listname, String var] :'(' ID logical_operator INT ')' {
if ($var.equals($ID.text)){
ArrayList<Object> arr = g.lists.get($listname);
int n = arr.size();
int arg = Integer.parseInt($INT.text);
if($logical_operator.str.equals("<")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))<arg);
}
}
else if($logical_operator.text.equals(">")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))>arg);
}
}
else if($logical_operator.text.equals(">=")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))>=arg);
}
}
else if($logical_operator.text.equals("<=")){
for (int i = 0;i<n;i++){
int cast = (int)(arr.get(i));
boolean temp = cast<=arg;
System.out.println("hello");
compStacks[i].push(temp);
}
}
else if($logical_operator.text.equals("==")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))==arg);
}
}
else if($logical_operator.text.equals("!=")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))!=arg);
}
}
variableCheck = true;
}
else{
variableCheck = false;
System.out.println("Unknown variable. Did you mean "+ $var + "?");
}
}
|'(' INT logical_operator ID ')' {
if ($var.equals($ID.text)){
ArrayList<Object> arr = g.lists.get($listname);
int n = arr.size();
int arg = Integer.parseInt($INT.text);
if ($logical_operator.text.equals("<")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))>arg);
}
}
else if($logical_operator.text.equals(">")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))<arg);
}
}
else if($logical_operator.text.equals(">=")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))<=arg);
}
}
else if($logical_operator.text.equals("<=")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))>=arg);
}
}
else if($logical_operator.text.equals("==")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))==arg);
}
}
else if($logical_operator.text.equals("!=")){
for (int i = 0;i<n;i++){
compStacks[i].push((int)(arr.get(i))!=arg);
}
}
variableCheck = true;
}
else{
variableCheck = false;
System.out.println("Unknown variable. Did you mean "+ $var + "?");
}
}
|'(' id1 = ID logical_operator id2 = ID ')'{
if (($var.equals($id1.text)) && ($var.equals($id2.text))){
int n = compStacks.length;
if ($logical_operator.text.equals("==") | $logical_operator.text.equals("<=") | $logical_operator.text.equals(">=")){
for (int i = 0; i<n; i++){
compStacks[i].push(true);
}
}
else{
for (int i = 0; i<n; i++){
compStacks[i].push(false);
}
}
variableCheck = true;
}
else{
variableCheck = false;
System.out.println("Unknown variable. Did you mean "+ $var + "?");
}
}
|'(' int1 = INT logical_operator int2 = INT ')'{
int n = compStacks.length;
int first = Integer.parseInt($int1.text);
int second = Integer.parseInt($int2.text);
boolean answer = true;
if ($logical_operator.text.equals("<")){
answer = first<second;
}
else if($logical_operator.text.equals(">")){
answer = first>second;
}
else if($logical_operator.text.equals(">=")){
answer = first>=second;
}
else if($logical_operator.text.equals("<=")){
answer = first<=second;
}
else if($logical_operator.text.equals("==")){
answer = first == second;
}
else if($logical_operator.text.equals("!=")){
answer = first!=second;
}
for (int i = 0; i<n; i++){
compStacks[i].push(answer);
}
}
|'('logical_statement[$listname,$var] ')'
;
logical_operator returns [String str] : '<' { $str = new String("<"); }
| '>' { $str = new String(">"); }
|'>=' { $str = new String(">="); }
|'<=' { $str = new String("<="); }
|'==' { $str = new String("=="); }
|'!=' { $str = new String("!="); }
;
INT:('-')?('0'..'9')+;
ID:('a'..'z'|'A'..'Z'|'\_')('a'..'z'|'A'..'Z'|'0'..'9'|'\_')*;
STRING:'\''('a'..'z'|'A'..'Z'|'0'..'9')* '\'';
WS:(' '|'\t'|'\n'|'\r')+{skip();};