int fsm_main(const char *s) { const char *p; enum { S0, S1, S2 } state; state = S0; for (p = s; *p != '\0'; p++) { switch (state) { case S0: /* start */ switch ((unsigned char) *p) { case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': state = S1; break; case '1': state = S2; break; default: return -1; /* leaf */ } break; case S1: /* e.g. "2" */ return -1; /* leaf */ case S2: /* e.g. "1" */ switch ((unsigned char) *p) { case '0': case '1': case '2': state = S1; break; default: return -1; /* leaf */ } break; default: ; /* unreached */ } } /* end states */ switch (state) { case S1: return 0x1; /* "(1|2|3|4|5|6|7|8|9|10|11|12)" */ case S2: return 0x1; /* "(1|2|3|4|5|6|7|8|9|10|11|12)" */ default: return -1; /* unexpected EOT */ } }