1. 30
    1. 6

      i have used the following style for state-machine implementation to great advantage. for larger more complicated cases i have found that HSM or hierarchical-state-machines are also pretty useful toolkit to have around.

      1. 4

        Searching for HSMs, I stumbled across this description of behavior trees, which are new to me: https://web.stanford.edu/class/cs123/lectures/CS123_lec08_HFSM_BT.pdf. Neat construction.

        1. 6

          Would recommend this pre-print book for a thorough discussion of behaviour trees.

          Edit: And this old paper from Harel (PDF) for hierarchical state charts.

          1. 1

            yes, i am familiar with harel’s paper, and have used that as a basis for design & development of hsm’s used @day-job. thanks for the book link on behavior trees. looks pretty interesting.

            as a side note, for parsing, specifically network protocols where things are strictly (a very loose definition of strict fwiw) specified through rfc’s etc., i have relied mostly on ragel. would love it hear your (and other folks as well!) experiences/wisdom in this regard as well.

    2. 2

      I inevitably end up with a state machine when implementing serial protocols on micrcontrollers. The mainloop code looks like

      while (serial buffer isn't empty) {
          /* (maybe with some limit on how many characters we consume
              per trip through the loop) */
          serial_state_machine(next ch from the serial buffer);

      The state machine has states like “expecting start-of-message”, “expecting data”, “just saw an escape character”, and “expecting the checksum”, and transitions like “if we’re expecting start-of-message and we got something that isn’t the start-of-message character, stay in state and ignore”, “if we’re expecting data, copy a char into the buffer and feed it to the checksum algorithm”, “if we read the checksum and it doesn’t match, increment an error counter and go back to expecting start-of-message”, and “if we read the checksum and it matches, send the message buffer to a handler function”.