forked from katef/libfsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase-code.c
55 lines (45 loc) · 964 Bytes
/
phase-code.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
int
fsm_main(int (*fsm_getc)(void *opaque), void *opaque)
{
int c;
enum {
S0, S1, S2, S3, S4
} state;
state = S0;
while (c = fsm_getc(opaque), c != EOF) {
switch (state) {
case S0: /* start */
switch ((unsigned char) c) {
case 'a': state = S1; break;
default: return TOK_UNKNOWN;
}
break;
case S1: /* e.g. "a" */
switch ((unsigned char) c) {
case 'b': state = S2; break;
default: return TOK_UNKNOWN;
}
break;
case S2: /* e.g. "ab" */
switch ((unsigned char) c) {
case 'b': break;
case 'c': state = S4; break;
default: state = S3; break;
}
break;
case S3: /* e.g. "aba" */
return TOK_UNKNOWN;
case S4: /* e.g. "abc" */
state = S3; break;
default:
; /* unreached */
}
}
/* end states */
switch (state) {
case S2: return 0x1; /* "^ab+c?.?$" */
case S3: return 0x1; /* "^ab+c?.?$" */
case S4: return 0x1; /* "^ab+c?.?$" */
default: return -1; /* unexpected EOT */
}
}