Remo D. said:
I just reiterate the suggestion I gave in that SO thread: define these
simple macros:
#define FSM
#define STATE(x) s_##x :
#define NEXTSTATE(x) goto s_##x
and map your diagram 1-to-1 with:
FSM {
STATE(x) {
...
NEXTSTATE(y);
}
STATE(y) {
...
if (x == 0)
NEXTSTATE(y);
else
NEXTSTATE(x);
}
}
I don't think these macros are "working hard enough" to justify their
existence. STATE(x) is slightly more obscure than STATE_x: which is
understandable and also obviously a label. NEXTSTATE(x); is not
much better than goto STATE_x;.
I do have a slightly more complex implementation of those macro in
clibutl to allow "sub state",
And there it might pay off. In the above, I don't think it does.
I'd also point out the using labels rather than, say, a big switch
looses the possibility of storing the states in a table or any other
structure (some C dialects like gcc can do a computed goto, but not
standard C). Sometimes the best way to find the target state is with
a table rather than an 'if'.
What I really like of this approach is that it retains intact the
"shape" of the diagram one usually draw when designing a state
machine, the other approaches I've seen, tend to hide the diagram
structure using tables or loops.
This is a big win when the process is documented as a state machine
but it can be a problem to unravel what a state machine is really
doing if the process is either not documented or is described by some
other means. I mention this just as a cautionary note: state machines
can be clear for the author but hard to decode for the reader!