-
Notifications
You must be signed in to change notification settings - Fork 2
/
FiniteAutomata.java
262 lines (240 loc) · 8.28 KB
/
FiniteAutomata.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import java.util.ArrayList;
/*Big god class that holds all the FA related information*/
public class FiniteAutomata
{
State Initial;
State Integer;
State Equals;
State Plus;
State Times;
State Semi;
State Identifier;
State Decimal;
State Lparen;
State Rparen;
State Quote;
State Float;
State String;
State PlusPlus;
State PlusEquals;
State TimesEquals;
State ClosingQuote;
ArrayList<State> states;
Condition white;
Condition alpha;
Condition digit;
Condition lparen;
Condition rparen;
Condition equals;
Condition plus;
Condition times;
Condition semi;
Condition quote;
Condition decimal;
Condition all;
ArrayList<Condition> conditions;
ArrayList<Condition> integerRelated;
ArrayList<Condition> equalsRelated;
ArrayList<Condition> plusRelated;
ArrayList<Condition> timesRelated;
ArrayList<Condition> semiRelated;
ArrayList<Condition> toIdentifierRelated;
ArrayList<Condition> identifierRelated;
ArrayList<Condition> decimalRelated;
ArrayList<Condition> lparenRelated;
ArrayList<Condition> rparenRelated;
ArrayList<Condition> quoteRelated;
ArrayList<Condition> toFloatRelated;
ArrayList<Condition> floatRelated;
ArrayList<Condition> stringRelated;
ArrayList<Condition> plusplusRelated;
ArrayList<Condition> plusequalsRelated;
ArrayList<Condition> timesequalsRelated;
Transition FromInitialtoInteger;
Transition FromInitialtoEquals;
Transition FromInitialtoPlus;
Transition FromInitialtoTimes;
Transition FromInitialtoSemi;
Transition FromInitialtoIdentifier;
Transition FromInitialtoDecimal;
Transition FromInitialtoLparen;
Transition FromInitialtoRparen;
Transition FromInitialtoQuote;
Transition FromIntegertoInteger;
Transition FromIntegertoFloat;
Transition FromPlustoPlusPlus;
Transition FromPlustoPlusEquals;
Transition FromTimestoTimesEquals;
Transition FromIdentifiertoIdentifier;
Transition FromDecimaltoFloat;
Transition FromQuotetoString;
Transition FromQuotetoClosingQuote;
Transition FromFloattoFloat;
Transition FromStringtoClosingQuote;
Transition FromStringtoString;
ArrayList<Transition> transitions;
public FiniteAutomata()
{
Initial = new State("initial");
Integer = new State("integer",true);
Equals = new State("equals",true);
Plus = new State("plus",true);
Times = new State("times",true);
Semi = new State("semicolon",true);
Identifier = new State("identifier",true);
Decimal = new State("decimal");
Lparen = new State("lparen",true);
Rparen = new State("rparen",true);
Quote = new State("quote");
Float = new State("float",true);
String = new State("string");
PlusPlus = new State("plusplus",true);
PlusEquals = new State("plusequals",true);
TimesEquals = new State("timesequals",true);
ClosingQuote = new State("string",true);
states = new ArrayList<State>();
states.add(Initial);
states.add(Integer);
states.add(Equals);
states.add(Plus);
states.add(Times);
states.add(Semi);
states.add(Identifier);
states.add(Decimal);
states.add(Lparen);
states.add(Rparen);
states.add(Quote);
states.add(Float);
states.add(String);
states.add(PlusPlus);
states.add(PlusEquals);
states.add(TimesEquals);
states.add(ClosingQuote);
white = new Condition("[\n\\ \t\b\012]");//White space characters
alpha = new Condition("[a-z]|[A-Z]");//all alphabet characters
digit = new Condition("[0-9]");//all digits 0-9
lparen = new Condition("[(]"); // (
rparen = new Condition("[)]"); // (
equals = new Condition("[=]");// =
plus = new Condition("[+]");// +
times = new Condition("[*]");// *
semi = new Condition("[;]");// ;
quote = new Condition("[\"]");// "
decimal = new Condition("[\\.]");// .(period)
all = new Condition("[^\"]");// all except quotation marks
conditions = new ArrayList<Condition>();
conditions.add(white);
conditions.add(alpha);
conditions.add(digit);
conditions.add(lparen);
conditions.add(rparen);
conditions.add(equals);
conditions.add(plus);
conditions.add(times);
conditions.add(semi);
conditions.add(quote);
conditions.add(decimal);
integerRelated = new ArrayList<Condition>();
equalsRelated = new ArrayList<Condition>();
plusRelated = new ArrayList<Condition>();
timesRelated = new ArrayList<Condition>();
semiRelated = new ArrayList<Condition>();
toIdentifierRelated = new ArrayList<Condition>();
identifierRelated = new ArrayList<Condition>();
decimalRelated = new ArrayList<Condition>();
lparenRelated = new ArrayList<Condition>();
rparenRelated = new ArrayList<Condition>();
quoteRelated = new ArrayList<Condition>();
toFloatRelated = new ArrayList<Condition>();
floatRelated = new ArrayList<Condition>();
stringRelated = new ArrayList<Condition>();
plusplusRelated= new ArrayList<Condition>();
plusequalsRelated= new ArrayList<Condition>();
timesequalsRelated= new ArrayList<Condition>();
integerRelated.add(digit);
equalsRelated.add(equals);
plusRelated.add(plus);
timesRelated.add(times);
semiRelated.add(semi);
toIdentifierRelated.add(alpha);
identifierRelated.add(digit);
identifierRelated.add(alpha);
decimalRelated.add(decimal);
lparenRelated.add(lparen);
rparenRelated.add(rparen);
quoteRelated.add(quote);
toFloatRelated.add(decimal);
floatRelated.add(digit);
stringRelated.add(white);
stringRelated.add(alpha);
stringRelated.add(digit);
stringRelated.add(lparen);
stringRelated.add(rparen);
stringRelated.add(equals);
stringRelated.add(plus);
stringRelated.add(times);
stringRelated.add(semi);
stringRelated.add(quote);
stringRelated.add(decimal);
stringRelated.add(all);
/* initialize the transition table */
FromInitialtoInteger = new Transition(Initial, integerRelated, Integer);
FromInitialtoEquals = new Transition(Initial, equalsRelated, Equals);
FromInitialtoPlus = new Transition(Initial, plusRelated, Plus);
FromInitialtoTimes = new Transition(Initial, timesRelated, Times);
FromInitialtoSemi = new Transition(Initial, semiRelated, Semi);
FromInitialtoIdentifier = new Transition(Initial, toIdentifierRelated, Identifier);
FromInitialtoDecimal = new Transition(Initial, decimalRelated, Decimal);
FromInitialtoLparen = new Transition(Initial, lparenRelated, Lparen);
FromInitialtoRparen = new Transition(Initial, rparenRelated, Rparen);
FromInitialtoQuote = new Transition(Initial, quoteRelated, Quote);
FromIntegertoInteger = new Transition(Integer, integerRelated, Integer);
FromIntegertoFloat = new Transition(Integer, toFloatRelated, Float);
FromPlustoPlusPlus = new Transition(Plus, plusRelated, PlusPlus);
FromPlustoPlusEquals = new Transition(Plus, equalsRelated, PlusEquals);
FromTimestoTimesEquals = new Transition(Times, equalsRelated, TimesEquals);
FromIdentifiertoIdentifier = new Transition(Identifier, identifierRelated, Identifier);
FromDecimaltoFloat = new Transition(Decimal, floatRelated, Float);
FromQuotetoString = new Transition(Quote, stringRelated, String);
FromQuotetoClosingQuote = new Transition(Quote, quoteRelated, ClosingQuote);
FromFloattoFloat = new Transition(Float, floatRelated, Float);
FromStringtoClosingQuote = new Transition(String, quoteRelated, ClosingQuote);
FromStringtoString = new Transition(String, stringRelated, String);
transitions = new ArrayList<Transition>();
transitions.add(FromInitialtoInteger);
transitions.add(FromInitialtoEquals);
transitions.add(FromInitialtoPlus);
transitions.add(FromInitialtoTimes);
transitions.add(FromInitialtoSemi);
transitions.add(FromInitialtoIdentifier);
transitions.add(FromInitialtoDecimal);
transitions.add(FromInitialtoLparen);
transitions.add(FromInitialtoRparen);
transitions.add(FromInitialtoQuote);
transitions.add(FromIntegertoInteger);
transitions.add(FromIntegertoFloat);
transitions.add(FromPlustoPlusPlus);
transitions.add(FromPlustoPlusEquals);
transitions.add(FromTimestoTimesEquals);
transitions.add(FromIdentifiertoIdentifier);
transitions.add(FromDecimaltoFloat);
transitions.add(FromQuotetoString);
transitions.add(FromQuotetoClosingQuote);
transitions.add(FromFloattoFloat);
transitions.add(FromStringtoClosingQuote);
transitions.add(FromStringtoString);
/*END initialize the transition table */
}
ArrayList<State> getStateList()
{
return states;
}
ArrayList<Condition> getConditionList()
{
return conditions;
}
ArrayList<Transition> getTransitionList()
{
return transitions;
}
}