-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFallbackDfa.java
More file actions
182 lines (147 loc) · 3.73 KB
/
FallbackDfa.java
File metadata and controls
182 lines (147 loc) · 3.73 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
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
package csen1002.main.task3;
import java.util.ArrayList;
import java.util.Stack;
/**
* Write your info here
*
* @name
* @id
* @labNumber
*/
public class FallbackDfa {
/**
* Constructs a Fallback DFA
*
* @param fdfa A formatted string representation of the Fallback DFA. The string
* representation follows the one in the task description
*/
private int[] states;
private String[] alpha;
ArrayList<Transition> transitions;
private int initial;
private int[] acptStates;
Stack<Integer> stateStack = new Stack<>();
public FallbackDfa(String fdfa) {
// TODO Auto-generated constructor stub
String []parts=fdfa.split("#",5);
String[] state = parts[0].split(";");
alpha=parts[1].split(";");
String [] tran=parts[2].split(";");
initial=Integer.parseInt(parts[3]);
String [] accepted=parts[4].split(";");
states=new int[state.length];
for(int i=0;i<state.length;i++)
{
states[i]=Integer.parseInt(state[i]);
}
transitions=new ArrayList<>();
for(int i=0;i<tran.length;i++)
{
String[] t=tran[i].split(",");
int currState=Integer.parseInt(t[0]);
char symbol=t[1].charAt(0);
int nxtState=Integer.parseInt(t[2]);
transitions.add(new Transition(currState,symbol,nxtState));
}
acptStates=new int[accepted.length];
for(int i=0;i<accepted.length;i++)
{
acptStates[i]= Integer.parseInt(accepted[i]);
}
}
static class Transition{
int currState;
char symbol;
int nxtState;
public Transition(int currState,char symbol, int nxtState) {
this.currState=currState;
this.symbol=symbol;
this.nxtState=nxtState;
}
public int getCurrState() {
return currState;
}
public char getSymbol() {
return symbol;
}
public int getNxtState() {
return nxtState;
}
}
/**
* @param input The string to simulate by the FDFA.
*
* @return Returns a formatted string representation of the list of tokens. The
* string representation follows the one in the task description
*/
public String run(String input) {
// TODO Auto-generated method stub
int curr=initial;
stateStack.push(curr);
int i=0;
int k =0;
int r = 0;
String out="";
String ans = "";
while(r<input.length())
{
for(int y = k ; y < input.length();y++)
{
char with=input.charAt(y);
for(int j=0;j<transitions.size();j++)
{
Transition tr= transitions.get(j);
if(tr.getCurrState()==curr && tr.getSymbol()==with)
{
curr =tr.getNxtState();
stateStack.push(curr);
break;
}
}
}
String noAcc ="";
i = input.length();
boolean flag = false;
while(true)
{
curr=stateStack.pop();
if(i == input.length())
{
noAcc = input.substring(k, i) + "," + curr +";" ;
}
for(int q=0;q<acptStates.length;q++)
{
if(curr==acptStates[q])
{
out=input.substring(k, i) + "," + curr +";" ;
ans += out;
stateStack.clear();
curr = initial;
stateStack.push(curr);
k = i;
r = k;
flag = true;
}
}
if(flag)
break;
if(stateStack.isEmpty())
{
ans += noAcc;
stateStack.clear();
curr = initial;
stateStack.push(curr);
k = input.length();
r = input.length();
break;
}
i=i-1;
}
}
return ans.substring(0, ans.length()-1);
}
public static void main(String[] args) {
FallbackDfa f = new FallbackDfa("0;1;2;3#a;b#0,a,0;0,b,1;1,a,2;1,b,1;2,a,0;2,b,3;3,a,3;3,b,3#0#1;2");
System.out.println(f.run("baababb"));
}
}