-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFA.java
More file actions
41 lines (37 loc) · 1.35 KB
/
Copy pathDFA.java
File metadata and controls
41 lines (37 loc) · 1.35 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class DFA {
private String[] action;
private SymbolTable<Character, Integer>[] next;
public DFA(String filename) {
In in = new In(filename);
int n = in.readInt();
String alphabet = in.readString();
action = new String[n];
next = (SymbolTable<Character, Integer>[]) new SymbolTable[n];
for (int state = 0; state < n; state++) {
action[state] = in.readString();
next[state] = new SymbolTable<Character, Integer>();
for (int i = 0; i < alphabet.length(); i++) {
next[state].put(alphabet.charAt(i), in.readInt());
}
}
}
public String simulate(String input) {
int state = 0;
for (int i = 0; i < input.length(); i++) {
state = next[state].get(input.charAt(i));
}
return action[state];
}
public static void main(String[] args) {
DFA dfa = new DFA(args[0]);
while (StdIn.hasNextLine()) {
String input = StdIn.readLine();
System.out.println(dfa.simulate(input));
}
}
}