I don't know of a general way of merging FSAs like this, but here's an approach for your specific problem.
Make two copies of your original FSA, call them 'Even' and 'Odd'. The invariant is that whenever you're in a state of one of the sub-FSAs, the parity is as indicated by its name. To achieve this, every 0
transition must jump to the corresponding node in the other FSA; 1
transitions continue in the same FSA.
The start state is in Even. The corresponding node in Odd is unreachable, and can be deleted.
All of your original accepting states are no longer valid, as you haven't verified the parity bit yet. In Odd, each of those states needs a 1
transition to the new accepting state. In Even, a 0
from a former accepting state needs to be an accepting state; all of those transitions exist already, so their target (node #1) becomes the new accepting state.
As for why your regexp solution didn't work, consider the first subgroup within it: (00100|00011|010)
. That includes subpatterns with both even and odd numbers of 0
s; you have no way to tell which one matched, so you've ALREADY permanently lost track of what the final parity will be. You'd have to somehow arrange for each individual part of the overall regex to match only things with the same parity - I'm not entirely sure that this is even possible in a traditional regex. (If I was solving this using a modern computer implementation of regular expressions, I'd use a lookahead assertion to verify the overall parity as an entirely separate process from verifying the concatenation of your three patterns.)
EDIT: Illustrated!
We start by making two copies of your original FSA, labelled for even or odd parity. You could also think of this as making a copy of the nodes in your 2nd FSA for each node in your first FSA.
The start state is in Even, since you initially have zero 0s. State o0 is unreachable, and will be omitted in further diagrams. The labels aren't valid yet, since every time there's a 0
transition, the parity changes - those transitions need to go to the other copy of the FSA. I couldn't get graphviz to produce anything that's even slightly comprehensible with all of these crossovers, so I'm going to have to drop the boxes around the even/odd states. Here's the diagram with crossovers applied:
This FSA matches exactly the same sentences your original one did, except that you always know whether you have odd or even parity at each state. The accepting states are still the same as the original ones: to handle your final parity bit, we need to add one final transition from each of those states to a new accepting state. From all three Odd accepting states, there is a 1
transition to the new state labelled "o". From all four Even accepting states, the final parity bit would be a 0
transition - which happens to correspond to the existing "o1" state. Here is the final diagram, with the updated accepting states:
