-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
234 lines (216 loc) · 11.1 KB
/
Main.java
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
// Theory of Computation Second Project
// Instructor: Dr. Hosein Rahmani
// Written by: Danial Bazmandeh & Alireza Haqani
// Number of lines: About 1814 (+473)
package Project2;
import javafx.application.Application;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Main {
static String start;
static boolean hasSpace;
static File greibachFile = new File("src/Project2/log/Greibach.txt");
static File chomskyFile = new File("src/Project2/log/Chomsky.txt");
static File deleteTrashFile = new File("src/Project2/log/Delete Trash.txt");
static Scanner reader;
static FileWriter writer;
static boolean isGreibachCalculated = false, isChomskyCalculated = false, isDeleteTrashCalculated = false;
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("*** WELCOME ***");
System.out.println("Please enter the User Interface you want to continue with:");
System.out.println("1. Terminal");
System.out.println("2. GUI");
int selectUI = Integer.parseInt(input.nextLine());
if (selectUI == 1) {
int productionNums;
System.out.println("Enter Grammar Production Rules Number: ");
productionNums = input.nextInt();
System.out.println("Enter Grammar Productions: ");
String[] allProductionRules = new String[productionNums];
input.nextLine();
for (int line = 0; line < productionNums; line++) {
allProductionRules[line] = input.nextLine();
}
//region Example:
// <START> -> <SUBJECT><DISTANCE><VERB><DISTANCE><OBJECTS>
// <SUBJECT> -> i|we
// <DISTANCE> -> " "
// <VERB> -> eat|drink|go
// <OBJECTS> -> food|water|lambda
//endregion
ArrayList<String> variables = new ArrayList<>();
ArrayList<String> terminals = new ArrayList<>();
Map<String, Set<ArrayList<String>>> productions = new LinkedHashMap<>();
//split into 2 parts
GrammarParse(allProductionRules, variables, terminals, productions, start);
/*
for (String productionRules: allProductionRules){
String[] parts = productionRules.split(" -> ");
//find variables
//variables = [<START>, <SUBJECT>, <DISTANCE>, <VERB>, <OBJECTS>]
String variable = parts[0].substring(1, parts[0].length()-1);
variables.add(variable);
if (allProductionRules[0].equals(productionRules))
start = variable;
//find terminals
//construct productions
String[] productionRulesParts = parts[1].split("\\|");
productions.put(variable, new LinkedHashSet<>());
for (String productionRule : productionRulesParts) {
ArrayList<String> productionRuleParameters = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(productionRule, "<>");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
productionRuleParameters.add(token);
if (Character.isLowerCase(token.charAt(0)) || token.equals(" "))
terminals.add(token);
}
productions.get(variable).add(productionRuleParameters);
}
}
*/
if (terminals.contains(" "))
hasSpace = true;
Grammar grammar = new Grammar(variables, terminals, start, productions);
int command = 0;
while (command != 8) {
System.out.println();
System.out.println("Select Command Number: ");
System.out.println("1. IsChomskyForm");
System.out.println("2. IsGreibachNormalForm");
System.out.println("3. IsDeleteTrash");
System.out.println("4. ChangeToGreibachForm");
System.out.println("5. ChangeToChomskyForm");
System.out.println("6. DeleteTrash");
System.out.println("7. IsGenerateByGrammar");
System.out.println("8. Exit");
command = input.nextInt();
String word;
switch (command) {
case 1:
System.out.println();
System.out.println("1. IsChomskyForm");
String isChomskeyForm = grammar.isChomskyForm() ? "Yes!" : "No!";
System.out.println("Input Grammar 'IsChomskyForm': " + isChomskeyForm);
break;
case 2:
System.out.println();
System.out.println("2. IsGreibachNormalForm");
String isGreibachForm = grammar.isGreibachNormalForm() ? "Yes!" : "No!";
System.out.println("Input Grammar 'IsGreibachNormalForm': " + isGreibachForm);
break;
case 3:
System.out.println();
System.out.println("3. IsDeleteTrash");
String isDeleteTrash = grammar.isDeleteTrash() ? "Yes!" : "No!";
System.out.println("Input Grammar 'IsDeleteTrash': " + isDeleteTrash);
break;
case 4:
System.out.println();
System.out.println("4. ChangeToGreibachForm");
System.out.println("=====Change to GNF Has Successfully Done!!!=====");
System.out.println("New Grammar: ");
if (!isGreibachCalculated) {
grammar.ChangeToGreibachForm();
writer = new FileWriter("src/Project2/log/Greibach.txt");
writer.write(grammar.printGrammar());
writer.close();
System.out.println(grammar.printGrammar());
isGreibachCalculated = true;
} else {
reader = new Scanner(greibachFile);
while (reader.hasNextLine())
System.out.println(reader.nextLine());
}
break;
case 5:
System.out.println();
System.out.println("5. ChangeToChomskyForm");
System.out.println("=====Change to CNF Has Successfully Done!!!=====");
System.out.println("New Grammar: ");
if (!isChomskyCalculated) {
grammar.ChangeToChomskyForm();
writer = new FileWriter("src/Project2/log/Chomsky.txt");
writer.write(grammar.printGrammar());
writer.close();
System.out.println(grammar.printGrammar());
isChomskyCalculated = true;
} else {
reader = new Scanner(chomskyFile);
while (reader.hasNextLine())
System.out.println(reader.nextLine());
}
break;
case 6:
System.out.println();
System.out.println("6. DeleteTrash");
System.out.println("=====Deleting Trash Has Successfully Done!!!=====");
System.out.println("New Grammar: ");
if (!isDeleteTrashCalculated) {
grammar.DeleteTrash();
writer = new FileWriter("src/Project2/log/Delete Trash.txt");
writer.write(grammar.printGrammar());
writer.close();
System.out.println(grammar.printGrammar());
isDeleteTrashCalculated = true;
} else {
reader = new Scanner(deleteTrashFile);
while (reader.hasNextLine())
System.out.println(reader.nextLine());
}
break;
case 7:
System.out.println();
System.out.println("7. IsGenerateByGrammar");
System.out.println("Please enter the word you want to check:");
input.nextLine();
word = input.nextLine();
String isAccepted = grammar.IsGenerateByGrammar(word) ? "Accepted!" : "Rejected!";
System.out.println(word + ": " + isAccepted);
break;
default:
if (command != 8)
System.out.println("Command Not Found! \n\t Enter Command Again: ");
break;
}
}
System.out.println("8. Exit");
System.out.println("*** GoodBye! ***");
System.exit(0);
} else if (selectUI == 2)
Application.launch(MainPage.class);
else
System.out.println("Input Error!");
}
public static void GrammarParse(String[] allProductionRules, ArrayList<String> variables, ArrayList<String> terminals, Map<String, Set<ArrayList<String>>> productions, String start) {
for (String productionRules: allProductionRules){
String[] parts = productionRules.split(" -> ");
//find variables
//variables = [<START>, <SUBJECT>, <DISTANCE>, <VERB>, <OBJECTS>]
String variable = parts[0].substring(1, parts[0].length()-1);
variables.add(variable);
if (allProductionRules[0].equals(productionRules))
Main.start = variable;
//find terminals
//construct productions
String[] productionRulesParts = parts[1].split("\\|");
productions.put(variable, new LinkedHashSet<>());
for (String productionRule : productionRulesParts) {
ArrayList<String> productionRuleParameters = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(productionRule, "<>");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
productionRuleParameters.add(token);
if (Character.isLowerCase(token.charAt(0)) || token.equals(" ") ||
Character.isDigit(token.charAt(0)) || !Character.isLetter(token.charAt(0)))
if (!terminals.contains(token))
terminals.add(token);
}
productions.get(variable).add(productionRuleParameters);
}
}
}
}