-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJASEP.java
More file actions
68 lines (63 loc) · 2.45 KB
/
JASEP.java
File metadata and controls
68 lines (63 loc) · 2.45 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
import java.util.*;
class JASEP {
private ArrayList<String> alphabet = new ArrayList<String>(); //our alphabet
private ArrayList<String> undefinedSymbolLog = new ArrayList<String>(); //currently unused... maybe you'll develop some ideas :)
private String[] collection = {"beg", "a", "begin", "b", "c", "d"}; //All of your symbols go here as shown
private String message = "undefined: "; //Message to be shown if error
public JASEP(String[] args) { //Simple constructor method where the alphabet is set
orderCollection();
for (int i = 0; i < this.collection.length; i++) {
this.alphabet.add(this.collection[i]);
}
if (parsingSuccessful(args[0])) {
System.out.println("No errors found");
} else {
System.out.println(message); //Whatever error is printed out here (can be changed without worries)
}
}
public JASEP(String[] args, String[] collection) { //Advanced constructor where alphabet (collection of string values) can be added
this.collection = collection;
orderCollection();
for (int i = 0; i < this.collection.length; i++) {
this.alphabet.add(this.collection[i]);
}
if (parsingSuccessful(args[0])) {
System.out.println("No errors found");
} else {
System.out.println(message);
}
}
public static void main(String[] args) {
String[] testCollection = {"a", "lol", "v", "bc"}; //
new JASEP(args, testCollection);
}
boolean parsingSuccessful(String arg0) { //Heart of the Parser, only edit if you know what you're doing!
for (int i = 0; i < this.alphabet.size(); i++) {
if (arg0.contains(this.alphabet.get(i))) {
arg0 = arg0.replaceAll(this.alphabet.get(i), "");
}
if (arg0.length() == 0) return true;
}
if (arg0.length() > 0) {
for (int i = 0; i < arg0.length(); i++) {
message = message+arg0.substring(i, i+1)+" "; //Here you can influence the way undefined symbols are listed
}
}
return false;
}
void orderCollection() { //prepares the symbol collection for the parsing process as the parser needs symbols to be ordered their by length, descending
String longestElement = null;
String switchBuffer = "";
for (int i = 0; i < this.collection.length; i++) {
longestElement = this.collection[i];
for (int j = i; j < this.collection.length; j++) {
if (longestElement.length() < this.collection[j].length()) {
switchBuffer = longestElement;
longestElement = this.collection[j];
this.collection[j] = switchBuffer;
this.collection[i] = longestElement;
}
}
}
}
}