To use goto or not?

后端 未结 9 1270
Happy的楠姐
Happy的楠姐 2020-12-14 17:34

This question may sound cliched, but I am in a situation here.

I am trying to implement a finite state automaton to parse a certain string in C. As I started writin

9条回答
  •  感情败类
    2020-12-14 18:32

    I would use a variable that tracks what state you are in and a switch to handle them:

    fsm_ctx_t ctx = ...;
    state_t state = INITIAL_STATE;
    
    while (state != DONE)
    {
        switch (state)
        {
        case INITIAL_STATE:
        case SOME_STATE:
            state = handle_some_state(ctx)
            break;
    
        case OTHER_STATE:
            state = handle_other_state(ctx);
            break;
        }
    }
    

提交回复
热议问题