-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFA.java
194 lines (152 loc) · 5.55 KB
/
NFA.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JC
*/
public class NFA{
public final static char epsilon = '~';
private LinkedList<State> graph;
private static int id = 0; // used to keep track of the state numbers. Static so that when NFA's are merged in concatenation, the states still have unique ids.
public NFA()
{
this.graph = new LinkedList<State>();
//this.graph = null;
}
private void setGraph(LinkedList<State> graph)
{
this.graph = graph;
}
public LinkedList<State> getGraph()
{
return this.graph;
}
private State createState()
{
State state = new State(id++);
return state;
}
private void mergeWith(NFA nfaToMergeWith)
{
LinkedList<State> result = new LinkedList<>();
result.addAll(this.graph);
result.addAll(nfaToMergeWith.getGraph());
this.setGraph(result);
}
public void print(){
if(graph == null) {System.out.println("NFA has not been created!");}
else
{
System.out.println("Printing out GraphViz compatible state list");
for(State state : graph)
{
state.print();
}
}
}
// generates the NFA
public void generate(String postfixRegex){
Stack<NFA> NFAStack = new Stack<>();
// make sure generate always uses empty stacks
NFAStack.clear();
for(int i = 0; i < postfixRegex.length(); ++i)
{
Parser.Operator op = Parser.getOperator(postfixRegex.charAt(i));
if(Parser.inAlpha(postfixRegex.charAt(i)))
{
NFA basic = this.createBasicNFA(postfixRegex.charAt(i));
NFAStack.push(basic);
//System.out.println("Creating Basic NFA: " + regex.charAt(i));
}
else if(op == Parser.Operator.KLEENE)
{
NFA nfa = this.createRepetition(NFAStack.pop());
NFAStack.push(nfa);
}
else if(op == Parser.Operator.CONCATENATION)
{
NFA rightSide = NFAStack.pop();
NFA leftSide = NFAStack.pop();
NFA nfa = this.createConcat(leftSide, rightSide);
NFAStack.push(nfa);
}
else if(op == Parser.Operator.ALTERNATION){
NFA rightSide = NFAStack.pop();
NFA leftSide = NFAStack.pop();
NFA nfa = this.createAltern(leftSide, rightSide);
NFAStack.push(nfa);
}
}
if(NFAStack.size() > 1) throw new RuntimeException("The regular expression resulted in more than one nondeterministic finite automaton.");
else if(NFAStack.size() < 1) throw new RuntimeException("The regular expression could not resolve to a nondeterministic finite automaton.");
setGraph(NFAStack.pop().getGraph());
this.getGraph().getLast().setAcceptingState(true);
print();
}
// create concatenation
public NFA createConcat(NFA lNFA, NFA rNFA){
State linkedFromState = lNFA.getGraph().getLast();
State linkedToState = rNFA.getGraph().getFirst();
linkedFromState.addTransition(epsilon, linkedToState);
lNFA.mergeWith(rNFA);
return lNFA;
}
// create alternation
public NFA createAltern(NFA lNFA, NFA rNFA){
State firstStateOfLeft = lNFA.getGraph().getFirst();
State firstStateOfRight = rNFA.getGraph().getFirst();
State lastStateOfLeft = lNFA.getGraph().getLast();
State lastStateOfRight = rNFA.getGraph().getLast();
State start = this.createState();
State end = this.createState();
start.addTransition(epsilon, firstStateOfLeft);
start.addTransition(epsilon, firstStateOfRight);
lastStateOfLeft.addTransition(epsilon, end);
lastStateOfRight.addTransition(epsilon, end);
lNFA.mergeWith(rNFA);
lNFA.getGraph().addFirst(start);
lNFA.getGraph().addLast(end);
return lNFA;
}
// create repetition
public NFA createRepetition(NFA nfa){
State start = this.createState();
State end = this.createState();
// add epsilon transition from start to end
start.addTransition(epsilon, end);
// add epsilon transition from start to first state on stack
start.addTransition(epsilon, nfa.getGraph().getFirst());
// add epsilon from last state of NFA on stack to first state of NFA
// on stack
nfa.getGraph().getLast().addTransition(epsilon, nfa.getGraph().getFirst());
// add epsilon transition from last state of NFA on stack to last state
nfa.getGraph().getLast().addTransition(epsilon, end);
// add the new states to the NFA that was on the stack
nfa.getGraph().addFirst(start);
nfa.getGraph().addLast(end);
// return the altered NFA
return nfa;
}
// This function should create a base NFA for single characters and push on
// on the NFA stack
public NFA createBasicNFA(char transition)
{
State s0 = this.createState();
State s1 = this.createState();
s0.addTransition(transition, s1);
NFA nfa = new NFA();
// add states to new NFA
nfa.getGraph().addLast(s0);
nfa.getGraph().addLast(s1);
return nfa;
}
}