I process a bitstream serially. I.e. I go over it, block by block, and infer some information. It all looks a lot like a state machine. For inststance, only in case of some block appearing 3 times in a row I print results. Then I wait for all similar blocks to pass, and print results only if something changes.

This is problematic when different scenarios must be handled. I find myself copy-pasting code (the horror !!). It's terrible, but I see no clean solution. For some cases, a block must appear 3 times in a row, for some 2 times in a row. For some results should be printed, for some not. Argh...

I did some web-sniffing and found "Hierarchical state machines". I wonder if this can (somehow) help me.

Writing bad code when there seems to be no other way is depressing :-(