SMO - State Machine Objects State Machine Diagram
STATES
TRANSITIONS
EVENTS
ACTIONS
Each circle is a STATE in the machine.
Each line going from one state to another or to the same state is a STATE TRANSITION
Each text item near each transition is an EVENT/ACTION pair. Sometimes the event is only listed and the action description is left for assumption or more detailed documentation.
The whole state machine is driven by events. Events in this example are characters coming in from some input stream, such as a file. For each event-transition your stare machine code can call any number of functions or methods within object oriented programming languages.
The above example is that of a simple programming language. It shows how a state machine can be used to process simple variable definitions and the beginning of processing keywords in program statements. Note that the "First Char Keyword" state and subsequent "Second Char Keyword" state, and so on, have a limit, and the limit is equal to the maximum size of any given keyword in the language syntax definition. When you consider syntax such as: IF <expression> THEN <statement> END IF, then it becomes necessary to use MACRO STATES. These are states that are transitioned to many times in the machine, and thus are cloned to provide ease of flow. An example is the EXPRESSION state. In the transition from IF to EXPRESSION, the state machine must analyze character by character the input stream to determine if there is a valid expression, and if valid, perform whatever actions are necessary for processing the expression. Looking at the state EXPRESSION in this way, we see that it is not just a state, it is a state machine within a state machine. Expanding the EXPRESSION state would reveal more states within it.
More to come
How can you code such a diagram?
First you need to start with some master control code, that is nothing but a switch statement that handles transitions into all the possible states of your machine. Then each state needs to have a piece of code allocated for it. Basically, the master control code of the machine is the state machine engine. It simply calls the code for each state based on the current state to transition to. We call this the "GOTO STATE" or "NEXT STATE". The code in each state ends with a return statement, that when returned to the state machine engine code, determines the next state to goto, to transition to.
#define S_START 1
#define S_VAR_NAME_DEF 2
#define S_VAR_DATA_DEF 3
etc.
int next_state = START;
do {
switch(next_state) {
CASE S_START:
next_state = Start.Run();
break;
CASE S_VAR_NAME_DEF:
next_state = VarNameDef.Run();
break;
CASE S_VAR_DATA_DEF:
next_state = VarDataDef.Run();
break;
CASE S_CHAR_1_KEY:
next_state = Char1Key.Run();
break;
CASE S_CHAR_2_KEY:
next_state = Char2Key.Run();
break;
CASE S_KEYWORD_IF:
next_state = Keyword_if.Run();
break;
CASE S_EXPRESSION:
next_state = Expression.Run(); // Expression is another state machine, not just a single state.
break;
} // switch
} while (machine.status == 1);
int Start::Run() // C++ style implementation of a class method called Run
{
int ns = S_START; // default next state to goto in state mahcine engine
char c; // Character input
Start.GetInput(&c) // got get some input from the main input source.
switch(c) { // Based on that input, setup
the next state to goto and perform associated actions.
CASE 13:
// CR do nothing, goto same state.
break;
CASE 10:
// LF do something, goto same state. Eats leading CR/LF
ns = S_START;
break;
CASE 'i':
// The first character of IF keyword
CASE 'd':
// The first character of DO keyword
ns = S_CHAR_1_KEY;
break;
Etc...
} // switch
return(ns);
}
©1997 KG Computer Services