-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachine.c
278 lines (204 loc) · 5.67 KB
/
statemachine.c
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "statemachine.h"
#include "panic.h"
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
static char EPSILON_TRANSITION = 0;
enum { DEBUG = 0 };
static struct transition * get_transition(struct state_machine *sm, int state, char token)
{
int i;
struct transition *transition;
for (i = 0; i < sm->t_used; ++i) {
transition = *(sm->transitions + i);
if (transition->source_state != state)
continue;
if (transition->token == token)
return transition;
}
return NULL;
}
void sm_initialize(struct state_machine *sm)
{
bzero(sm, sizeof (*sm));
sm->t_capacity = 16;
}
void sm_add_transition(struct state_machine *sm, struct transition *t)
{
if (sm->transitions == NULL) {
sm->transitions = (struct transition **) calloc(sm->t_capacity, sizeof (t));
bzero(sm->transitions, sm->t_capacity * sizeof (t));
}
if (sm->t_used == (sm->t_capacity - 2)) {
sm->t_capacity <<= 1;
struct transition **ptr = (struct transition **)
realloc(sm->transitions, sm->t_capacity * sizeof (t));
if (ptr == NULL)
panic("Not enough memory.");
sm->transitions = ptr;
}
sm->transitions[sm->t_used] = t;
sm->t_used++;
}
void sm_free_transitions(struct state_machine *sm)
{
int i;
for (i = 0; i < sm->t_used; ++i)
free(*(sm->transitions + i));
free(sm->transitions);
free(sm->input);
sm->t_used = 0;
sm->t_capacity = 16;
}
void sm_free(struct state_machine *sm)
{
sm_free_transitions(sm);
free(sm->fstates);
free(sm);
}
struct state_machine * sm_new(void)
{
struct state_machine *sm;
sm = malloc(sizeof *sm);
sm_initialize(sm);
return sm;
}
void sm_set_nstates(struct state_machine *sm, int n)
{
sm->nstates = n;
}
void sm_set_istate(struct state_machine *sm, int i)
{
assert(i < sm->nstates);
sm->istate = i;
}
void sm_set_fstates(struct state_machine *sm, int *fstates, int count)
{
int i;
size_t alloc_size = count * sizeof(int *);
for (i = 0; i < count; ++i) {
if (fstates[i] >= sm->nstates) {
printf("Final state %d is out of range, keeping current final states\n", fstates[i]);
return;
}
}
if (sm->fstates != NULL)
free(sm->fstates);
sm->fstates = malloc(alloc_size);
sm->fs_count = count;
memcpy(sm->fstates, fstates, alloc_size);
}
struct transition * sm_transition_new(char token, int source_state, int target_state)
{
struct transition *t;
t = (struct transition *) malloc(sizeof *t);
t->token = token;
t->source_state = source_state;
t->target_state = target_state;
t->callback = NULL;
return t;
}
struct transition * sm_epsilon_transition_new(int source_state, int target_state)
{
return sm_transition_new(EPSILON_TRANSITION, source_state, target_state);
}
void sm_transition_set_cb(struct transition *t, transition_callback cb)
{
t->callback = cb;
}
void sm_set_input_buffer(struct state_machine *sm, const char *buffer)
{
if (sm->input != NULL)
free(sm->input);
sm->input = strdup(buffer);
}
inline int sm_has_state(struct state_machine *sm, int state)
{
return (state < sm->nstates);
}
int sm_is_final_state(struct state_machine *sm, int state)
{
int i;
for (i = 0; i < sm->fs_count; ++i) {
if (*(sm->fstates + i) == state)
return 1;
}
return 0;
}
static struct transition * find_transition(struct state_machine *sm,
int state, char token)
{
struct transition *t;
t = get_transition(sm, state, token);
if (t != NULL)
return t;
return get_transition(sm, state, EPSILON_TRANSITION);
}
int sm_exec(struct state_machine *sm)
{
char *ptr;
char ch;
int current_state;
struct transition *t;
ptr = sm->input;
current_state = sm->istate;
while (*ptr) {
if (DEBUG)
printf("current state: %d; input = '%c'\n", current_state, *ptr);
t = find_transition(sm, current_state, *ptr);
if (t == NULL) {
printf("Invalid input\n");
return -1;
}
if (t->callback)
t->callback(*ptr);
current_state = t->target_state;
if (t->token != EPSILON_TRANSITION)
++ptr;
}
/* input is fully consumed, check for a final
* epsilon transition
*/
t = get_transition(sm, current_state, EPSILON_TRANSITION);
if (t != NULL) {
if (t->callback)
t->callback((char) 0);
current_state = t->target_state;
}
if (DEBUG)
printf("current state: %d;\n", current_state);
if (sm_is_final_state(sm, current_state)) {
printf("Input accepted\n");
return 0;
}
printf("Invalid input\n");
return -1;
}
void add_transition(struct state_machine *sm, int sstate, int tstate,
char ch, transition_callback callback)
{
struct transition *t;
t = sm_transition_new(ch, sstate, tstate);
sm_transition_set_cb(t, callback);
sm_add_transition(sm, t);
}
void add_range_transition(struct state_machine *sm, int sstate, int tstate,
char start, char end, transition_callback callback)
{
char ch;
assert(start < end);
for (ch = start; ch <= end; ++ch)
add_transition(sm, sstate, tstate, ch, callback);
}
void add_epsilon_transition(struct state_machine *sm,
int source_state, int target_state, transition_callback callback)
{
struct transition *t;
t = sm_epsilon_transition_new(source_state, target_state);
sm_transition_set_cb(t, callback);
sm_add_transition(sm, t);
}