-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParser.jj
More file actions
598 lines (478 loc) · 16.3 KB
/
Parser.jj
File metadata and controls
598 lines (478 loc) · 16.3 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Our language is case insensitive
options {
IGNORE_CASE=true;
}
PARSER_BEGIN(Parser)
// Needed for the array list class
import java.util.*;
public class Parser {
// A class to represent a location and the things around it
static class Location {
// Name of the place
private String currentName;
// The areas around it
public ArrayList north = new ArrayList();
public ArrayList south = new ArrayList();
public ArrayList east = new ArrayList();
public ArrayList west = new ArrayList();
public String name;
// Set currentName in the constructor
Location (String name) {
currentName = name.toLowerCase();
}
// Return current name, important for finding out which place this class represents
public String getName() {
return currentName.toLowerCase();
}
}
// Global array list for storing places
public static ArrayList places = new ArrayList();
// Global array list for storing location objects
public static ArrayList relatedPlaces = new ArrayList();
public static void main(String[] args) throws ParseException, TokenMgrError {
// Create a new parser and feed it the standard input
Parser parser = new Parser(System.in);
try {
parser.parse();
}
catch(TokenMgrError e) {
System.err.println("[LEXING ERROR] Invalid character");
System.exit(0);
}
catch(ParseException e) {
System.err.println("[PARSING EXCEPTION] Invalid Input");
System.exit(0);
}
}
}
PARSER_END(Parser)
// Things we want to ignore (white space)
SKIP:
{
" " |
"\n" |
"\r" |
"\t"
}
// The tokens for our language
TOKEN :
{
<SEE: "I see"> |
<THE: "the"> |
<AND: "and"> |
<IS: "is"> |
<ARE: "are"> |
<NORTH: "north"> |
<SOUTH: "south"> |
<EAST: "east"> |
<WEST: "west"> |
<OF: "of"> |
<FULLSTOP: "."> |
<SEMICOLON: ";"> |
<COMMA: ","> |
// Cover all possible formats of numbers
<NUMBER : ((["0"-"9"])+ | (["0"-"9"])+ "." (["0"-"9"])+ | (["0"-"9"])+ ".")+ > |
<UNIT: "m" | "km" | "miles"> |
// A word can be any character of the alphabet (upper or lower case) or any numbers
<WORD: (["a"-"z", "A"-"Z"])+> |
// An escaped word must start with a [ be followed by any character of the alphabet (upper or lowercase) or any numbers and may contain
// white space and then end with a ]
<ESCAPEDWORD: "[" (["a"-"z", "A"-"Z", " "])+ "]" >
}
void parse():
{
}
{
// File ends with EOF
start() <EOF>
}
// Recognises a list of places or a list of places relative to each other
void start() throws ParseException:
{
}
{
( // Either were entering a list of things we see, or a list of things we see with respect to some other thing
(spotted() | direction())
// Each line must end with either a . or ;
try {
(<FULLSTOP> | <SEMICOLON>)
}
// If a line didn't end that way alert the user to an invalid terminator and quit
catch (ParseException e) {
System.err.println("[LEXING ERROR] Invalid terminator");
System.exit(0);
}
)* // You can have as many lines of this input as you like
}
// Recognises a list of places you can see
void spotted() throws ParseException:
{
}
{
try {
// This bit has to always be at the start
( <SEE> <THE> place() )
// Followed by a list of any number of items
list()
}
catch(ParseException e) {
System.err.println("[PARSING ERROR] Locations can only be declared once");
System.exit(0);
}
}
// Recognises a list of places seperated by either a "," or "and"
void list() throws ParseException:
{
}
{
try {
(
// We need a list seperator and then "the" followed by the place name
(<COMMA> | <AND>)
<THE> place()
)* // We can have as many items in the list as we want
}
// If a list doesn't meet that standard throw an error informing the user their list is in the incorrect format
catch (ParseException e) {
System.err.println("[PARSING ERROR] Invalid list format ");
System.exit(0);
}
}
// Recognises a place name, either in escaped or unescaped format and adds it to an array list of locations
void place() throws ParseException:
{
String x = "";
Token y = null;
}
{
(
( // A word any number of times e.g. Theme park
y = <WORD>
{ x += " " + y.image; }
)+
|
(
// Or a single escaped word, e.g. [Theme Park]
y = <ESCAPEDWORD>
{ x += " " + y.image; }
)
)
// Now we have a string representing the place add it to the array list.
{
// If the place is already in the array list throw a parse exception as we only allow a place to be declared once
if (places.contains(x.toLowerCase())) {
throw new ParseException();
}
else {
// Add the place to our array list of places
places.add(x.toLowerCase());
// Create a new place object and add it to our array list of place objects
Location z = new Location(x.toLowerCase());
relatedPlaces.add(z);
}
}
}
// Recognises any one of the cardinal points
String compass() throws ParseException:
{
Token x;
String y = "";
}
{ // Attempt to recognise a cardinal point
try {
(
x = <NORTH>
{ y = x.image; }
)
|
(
x = <SOUTH>
{ y = x.image; }
)
|
(
x = <EAST>
{ y = x.image; }
)
|
(
x = <WEST>
{ y = x.image; }
)
}
// If there were none throw an error and quit
catch (ParseException e) {
System.err.println("[PARSING ERROR] Invalid direction");
System.exit(0);
}
{return y.toLowerCase();}
}
// Recognises a list of places with respect to to some other places
void direction() throws ParseException:
{
// Will hold the first place on the left hand side of a scentence
String place1 = "";
// Will hold the first place on the right hand side of a scentence
String place2 = "";
// Will hold the cardinal point between places
String direction = "";
// Will hold the list of places on the left hand side of a scentence
ArrayList list1 = null;
// Will hold the list of places on the right hand side of a scentence
ArrayList list2 = null;
}
{
// Every line must start with the place
<THE>
try {
// Check the first place in the list is already declared
place1 = existingPlace()
}
catch (ParseException e) {
System.err.println("[PARSE EXCEPTION] Undeclared location");
System.exit(0);
}
(
// Followed by is for a single place
<IS> |
try {
(
// Or a list of places seperated by either a "," or "and"
( list1 = existingList() )
// The list of places must be followed by "are" due to the rules of english
<ARE>
)
}
// If the format for a list of places was not met throw an error
catch (ParseException e) {
System.err.println("[PARSING ERROR] Bad Grammar");
System.exit(0);
}
)
// We may have a distance being specified
(<NUMBER> <UNIT>)?
// Now we need a cardinal point followed by of the location and potentially a list of other locations
direction = compass()
<OF> <THE>
try {
// Check the first place on the right hand side of the list is already declared
place2 = existingPlace()
}
catch (ParseException e) {
System.err.println("[PARSING ERROR] Undeclared Location");
System.exit(0);
}
// Check to see if we have a list of places on the right
list2 = existingList()
// Now check the relations are valid
try{
validateRelations(list1, list2, place1, place2, direction )
}
catch (ParseException e) {
System.err.println("[SEMANTIC ERROR] Bad relation");
System.exit(0);
}
}
// Checks realtions are semantically valid
void validateRelations(ArrayList list1, ArrayList list2, String place1, String place2, String direction):
{
}
{
{
// The basic easy part
validLocations(place1, place2, direction);
// Now if we had a list of places on the left we need to make sure they are all valid relations
if(list1 != null) {
// For every place in it set it direction of place2
for(int i=0; i<list1.size(); i++) {
validLocations((String)list1.get(i), place2, direction);
}
}
// If we had a list of places on the right hand side check they are all valid relations
if(list2 != null ) {
// For every place in it set it direction of place2
for(int i=0; i<list2.size(); i++) {
validLocations(place1, (String)list2.get(i), direction);
}
}
// Now if we had a list of places on the right and the left we need to make sure we they are all valid locations
if(list2 != null && list1 != null) {
// for every element on the left
for(int i=0; i<list1.size(); i++) {
// Set it <direction of> every element on the right
for(int j=0; j<list2.size(); j++) {
validLocations((String)list1.get(i), (String)list2.get(j), direction);
}
}
}
}
}
// Checks that the positioning of two places is valid
void validLocations(String p1, String p2, String direction):
{
}
{
{
// Check the two places are not the same as this is invalid
if(p1.equals(p2) ) {
throw new ParseException();
}
// Get the objects for these places from the array list
Location l1 = getPlace(p1.toLowerCase());
Location l2 = getPlace(p2.toLowerCase());
direction = direction.toLowerCase();
if(direction.equals("north")) {
// If l1 is north of l2 then l2 can not be north of l1 so check this
if( l2.south.contains(l1) ) {
throw new ParseException();
}
// Else everything is fine so add the places to the array lists
l2.north.add(l1);
l1.south.add(l2);
// Deal with transitive relations
// Every element that is north of l1 must be north of l2
for(int i=0; i<l1.north.size(); i++) {
l2.north.add(l1.north.get(i));
}
// Every element that is south of l2 must be south of l1
for(int i=0; i<l2.south.size(); i++) {
l1.south.add(l2.south.get(i));
}
}
if(direction.equals("south")) {
// If l1 is south of l2 then l2 can not be south of l1 so check this
if( l2.north.contains(l1) ) {
throw new ParseException();
}
// Else everything is fine so add the places to the array lists
l2.south.add(l1);
l1.north.add(l2);
// Deal with transitive relations
// Every element that is south of l1 must be south of l2
for(int i=0; i<l1.south.size(); i++) {
l2.south.add(l1.south.get(i));
}
// Every element that is north of l2 must be north of l1
for(int i=0; i<l2.north.size(); i++) {
l1.north.add(l2.north.get(i));
}
}
if(direction.equals("east")) {
// If l1 is east of l2 then l2 can not be east of l1 so check this
if( l2.west.contains(l1) ) {
throw new ParseException();
}
// Else everything is fine so add the places to the array lists
l2.east.add(l1);
l1.west.add(l2);
// Deal with transitive relations
// Every element that is east of l1 must be east of l2
for(int i=0; i<l1.east.size(); i++) {
l2.east.add(l1.east.get(i));
}
// Every element that is west of l2 must be west of l1
for(int i=0; i<l2.west.size(); i++) {
l1.west.add(l2.west.get(i));
}
}
if(direction.equals("west")) {
// If l1 is west of l2 then l2 can not be west of l1 so check this
if( l2.east.contains(l1) ) {
throw new ParseException();
}
// Else everything is fine so add the places to the array lists
l2.west.add(l1);
l1.east.add(l2);
// Deal with transitive relations
// Every element that is west of l1 must be west of l2
for(int i=0; i<l1.west.size(); i++) {
l2.west.add(l1.west.get(i));
}
// Every element that is east of l2 must be east of l1
for(int i=0; i<l2.east.size(); i++) {
l1.east.add(l2.east.get(i));
}
}
}
}
// Returns the place represent by string x from the array list
Location getPlace(String x):
{
}
{
{
for(int i=0; i<relatedPlaces.size(); i++) {
Location check = (Location) relatedPlaces.get(i);
if(check.getName().equals(x) ){
return check;
}
}
}
}
// Checks if a place name is already in the array list
String existingPlace():
{
String x = "";
Token y = null;
}
{
(
(
// A word any number of times e.g. Theme park
y = <WORD>
{ x += " " + y.image; }
)+
|
( // Or a single escaped word, e.g. [Theme Park]
y = <ESCAPEDWORD>
{ x += " " + y.image; }
)
)
// Now we have the location string check its in the array list.
{
if (!places.contains(x.toLowerCase())) {
throw new ParseException();
}
return x.toLowerCase();
}
}
// Checks if a list of places are already in the array list
ArrayList existingList() throws ParseException:
{
String x = "";
ArrayList list = new ArrayList();
}
{
try {
(
// We need a list seperator and then "the" followed by the place name
(<COMMA> | <AND>)
<THE>
// Check the place in the list is already in the array list
try {
x = existingPlace()
}
catch (ParseException e) {
System.err.println("[PARSING ERROR] Undeclared Location");
System.exit(0);
}
// Add the place to our list of places
{list.add(x);}
)* // We can have as many items in the list as we want
}
// If a list doesn't meet that standard throw an error informing the user their list is in the incorrect format
catch (ParseException e) {
System.err.println("[PARSING ERROR] Invalid list format");
System.exit(0);
}
{
// If we had a list of places then return it
if( list.size() > 0 ) {
return list;
}
// If not return null so our check in the for loop doesn't fail
else {
return null;
}
}
}