-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNfaStruct.java
102 lines (89 loc) · 2.42 KB
/
NfaStruct.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
import java.util.*;
public class NfaStruct{
private NfaNode start;
private NfaNode current;
private NfaNode[] finalStates = new NfaNode[20];
private int finalCount;
private Hashtable allNodes = new Hashtable<String, NfaNode>(10,10); //Node identifier is the key, the node object is the value
private int nodeCount;
/* Creates an NfaStruct with the input node as the start state */
public NfaStruct(NfaNode node){
start = node;
current = node;
if(node!=null){
nodeCount = 1;
}
else{
nodeCount = 0;
}
}
/* Creates an empty NfaStruct */
public NfaStruct(){
this(null);
}
public NfaNode makeTransition(String key){
if(current!=null){
if(current.isTransition(key)){
return current.getTransition(key);
}
}
return null;
}
public boolean addNode(NfaNode node, String transition){
if(node==null){
return false;
}
if(node.isFinal()){
finalStates[finalCount] = node;
finalCount++;
}
allNodes.put(node.getIdentifier(), node);
if(start==null){
start = node;
current = node;
nodeCount += 1;
return true;
}
else{
nodeCount += 1;
return current.addTransition(transition, node); //Just add a node that is connected to the current node
}
}
public NfaNode getStart(){
return start;
}
public NfaNode[] getFinal(){
return finalStates;
}
public int getNodeCount(){
return nodeCount;
}
/* Method that changes the current node, returns false if no node has the identifier */
public boolean setCurrent(String identifier){
if(allNodes.containsKey(identifier)){
current = (NfaNode)allNodes.get(identifier);
return true;
}
return false;
}
/* Gets the epsilon closures of the current node */
public NfaNode[] getEpsilonClosure(){
if(current!=null){
return current.getEpsilonTransition();
}
else{
return null; //null if node is empty
}
}
public void printNfa(NfaNode init){
//Print out the initial node and its transitions
NfaNode temp = init;
System.out.printf("%s:\t", temp.getIdentifier());
Hashtable tempTrans = temp.getAllTransitions();
System.out.println(tempTrans.toString());
//Recurse through all of the transitions from the init node
for(Enumeration<NfaNode> nodeEnum = tempTrans.elements(); nodeEnum.hasMoreElements();){
printNfa(nodeEnum.nextElement());
}
}
}