-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDA.java
234 lines (212 loc) · 9.36 KB
/
PDA.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
package Project2;
import javafx.application.Application;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class PDA {
static Grammar PDAGrammar;
//Start/State/Final/Productions
public static void main(String[] args) {
File file = new File("src/Project2/PDA.txt");
try {
//region CreateGrammar
Scanner reader = new Scanner(file);
int productionNums;
productionNums = Integer.parseInt(reader.nextLine());
String[] allProductionRules = new String[productionNums];
for (int line = 0; line < productionNums; line++)
allProductionRules[line] = reader.nextLine();
ArrayList<String> variables = new ArrayList<>();
ArrayList<String> terminals = new ArrayList<>();
Map<String, Set<ArrayList<String>>> productions = new LinkedHashMap<>();
String start = "EXPRESSION";
Main.GrammarParse(allProductionRules, variables, terminals, productions, start);
PDAGrammar = new Grammar(variables, terminals, start, productions);
//endregion
System.out.println("Show Schematic? 1. Yes 2. No (Enter Expression)");
reader = new Scanner(System.in);
int commandNum = reader.nextInt();
switch (commandNum) {
case 1:
Application.launch(ChematicPDA.class, args);
break;
case 2:
System.out.println("Enter Expression: ");
break;
default:
System.out.println("Input Error!");
break;
}
reader.nextLine();
String expression = reader.nextLine();
expression = expression.trim();
String isAccepted = PDAGrammar.IsGenerateByGrammar(expression) ? "Accepted" : "Rejected";
System.out.println(isAccepted);
// isAccepted = "Accepted";
if (isAccepted.equals("Accepted"))
System.out.println(Calculate(expression));
//System.out.println(PDAGrammar.printGrammar());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static String Calculate(String expression) {
// region RPN(Reverse Polish Notation)
// convert infix to postfix
ArrayList<String> postfix = new ArrayList<>();
String note = InfixToPostfix(expression, postfix) ? "Result: " : "ERROR!";
String result = note;
if (note.equals("Result: ")) {
// using RPN(Reversed Polish Notation) to calculate
ArrayList<String> Ops = new ArrayList<>(Arrays.asList("+", "-", "*", "/", "^", "(", ")"));
ArrayList<String> Functions = new ArrayList<>(Arrays.asList("Sin", "Cos", "Tan", "Cot", "Sqrt", "Log", "Ln", "Floor", "Dec"));
Stack<String> stack = new Stack<>();
stack.push(postfix.get(0));
stack.push(postfix.get(1));
for (String s : postfix) {
if (Ops.contains(s) || Functions.contains(s)) {
switch (s) {
case "+":
stack.push(String.valueOf(Double.parseDouble(stack.pop()) + Double.parseDouble(stack.pop())));
break;
case "-":
stack.push(String.valueOf(Double.parseDouble(stack.pop()) - Double.parseDouble(stack.pop())));
break;
case "*":
stack.push(String.valueOf(Double.parseDouble(stack.pop()) * Double.parseDouble(stack.pop())));
break;
case "/":
stack.push(String.valueOf(Double.parseDouble(stack.pop()) / Double.parseDouble(stack.pop())));
break;
case "^":
stack.push(String.valueOf(Math.pow(Double.parseDouble(stack.pop()), Double.parseDouble(stack.pop()))));
break;
case "Cos":
stack.push(String.valueOf(Math.cos(Double.parseDouble(stack.pop()))));
break;
case "Sin":
stack.push(String.valueOf(Math.sin(Double.parseDouble(stack.pop()))));
break;
case "Tan":
stack.push(String.valueOf(Math.tan(Double.parseDouble(stack.pop()))));
break;
case "Cot":
stack.push(String.valueOf(1 / Math.tan(Double.parseDouble(stack.pop()))));
break;
case "Sqrt":
stack.push(String.valueOf(Math.sqrt(Double.parseDouble(stack.pop()))));
break;
case "Log":
stack.push(String.valueOf(Math.log10(Double.parseDouble(stack.pop()))));
break;
case "Ln":
stack.push(String.valueOf(Math.log(Double.parseDouble(stack.pop()))));
break;
case "Floor":
stack.push(String.valueOf(Math.floor(Double.parseDouble(stack.pop()))));
break;
case "Dec":
double number = Double.parseDouble(stack.pop());
stack.push(String.valueOf(number - Math.floor(number)));
break;
}
} else
stack.push(s);
}
result += stack.peek();
//endregion
}
return result;
}
private static int Precendence(String op){
switch (op){
case "+":
case "-":
return 1;
case "*":
case "/":
return 2;
case "^":
return 3;
case "Sin":
case "Cos":
case "Tan":
case "Cot":
case "Sqrt":
case "Log":
case "Ln":
case "Floor":
case "Dec":
return 4;
}
return -1;
}
private static boolean InfixToPostfix(String expression, ArrayList<String> postfix) {
Stack<String> stack = new Stack<>();
StringTokenizer tokenizer = new StringTokenizer(expression, "(+-*/^)");
ArrayList<String> Ops = new ArrayList<>(Arrays.asList("+", "-", "*", "/", "^", "(", ")"));
ArrayList<String> Functions = new ArrayList<>(Arrays.asList("Sin", "Cos", "Tan", "Cot", "Sqrt", "Log", "Ln", "Floor", "Dec"));
//region Replacements
//region Ops
expression = expression.replace("+", " + ");
expression = expression.replace("-", " - ");
expression = expression.replace("*", " * ");
expression = expression.replace("/", " / ");
expression = expression.replace("^", " ^ ");
expression = expression.replace("(", "( ");
expression = expression.replace(")", " )");
//endregion
//region Functions
//region Trigonometric
expression = expression.replace("Sin", "Sin ");
expression = expression.replace("Cos", "Cos ");
expression = expression.replace("Tan", "Tan ");
expression = expression.replace("Cot", "Cot ");
//endregion
//region Exponential
expression = expression.replace("Sqrt", "Sqrt ");
expression = expression.replace("Log", "Log ");
expression = expression.replace("Ln", "Ln ");
//endregion
//region [],{}
expression = expression.replace("Floor", "Floor ");
expression = expression.replace("Dec", "Dec ");
//endregion
//endregion
//region ScientificNumbers
expression = expression.replace("e", "2.718281828459045");
expression = expression.replace("pi", "3.141592653589793");
//endregion
//endregion
String[] tokens = expression.split(" ");
for (String token : tokens){
if (!Ops.contains(token) && !Functions.contains(token))
postfix.add(token);
else if (token.equals("("))
stack.push(token);
else if (token.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("("))
postfix.add(stack.pop());
if (!stack.isEmpty() && !stack.peek().equals("("))
return false;
else
stack.pop();
if (Functions.contains(stack.peek()))
postfix.add(stack.pop());
} else {
while (!stack.isEmpty() && Precendence(token) <= Precendence(stack.peek())) {
if (stack.peek().equals("("))
return false;
postfix.add(stack.pop());
}
stack.push(token);
}
}
while (!stack.isEmpty()) {
if (stack.peek().equals("("))
return false;
postfix.add(stack.pop());
}
return true;
}
}