Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would model the state machine explicitly:</p> <pre><code>def StateMachine(): state = 'A' while True: print(state) input = (yield) if state == 'A': if input == 'a': state = 'B' elif input == 'b': state = 'C' else: break elif state == 'B': if input == 'a': state = 'C' elif input == 'b': state = 'A' else: break elif state == 'C': if input == 'a': state = 'A' elif input == 'b': state = 'B' else: break else: break </code></pre> <p>This should translate very neatly to C++ using nested <code>switch</code> statements or a state transition table.</p> <p>If you prefer an implicit model, you need a way to handle the cycles in your state machine. In C or C++, this would probably end up being <code>goto</code>. I wouldn't recommend this approach, but if you're more comfortable with it than explicit state, here's what it might look like:</p> <pre><code>#include &lt;stdio.h&gt; #define start(state) switch(state) { case 0:; #define finish default:;} #define yield(state, value) do { state = __LINE__; return (value); case __LINE__:; } while(0) struct coroutine { int state; }; int run(struct coroutine *c, char input) { start(c-&gt;state) { A: printf("Entered state A\n"); yield(c-&gt;state, 1); if(input == 'a') goto B; if(input == 'b') goto C; B: printf("Entered state B\n"); yield(c-&gt;state, 1); if(input == 'a') goto C; if(input == 'b') goto A; C: printf("Entered state C\n"); yield(c-&gt;state, 1); if(input == 'a') goto A; if(input == 'b') goto B; } finish; return 0; } int main(void) { int a; struct coroutine c = {0}; while((a = getchar()) != EOF &amp;&amp; run(&amp;c, a)); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload