Lines Matching refs:fsm
3 """This module implements a Finite State Machine (FSM). In addition to state
4 this FSM also maintains a user defined "memory". So this FSM can be used as a
5 Push-down Automata (PDA) since a PDA is a FSM + memory.
7 The following describes how the FSM works, but you will probably also need to
8 see the example function to understand how the FSM is used in practice.
10 You define an FSM by building tables of transitions. For a given input symbol
12 the next state will be. The FSM has a table of transitions that associate:
18 to the transition table. The FSM also has a table of transitions that
24 FSM also has one default transition that is not associated with any specific
28 When an action function is called it is passed a reference to the FSM. The
29 action function may then access attributes of the FSM such as input_symbol,
31 want to pass along to the action functions. It is not used by the FSM itself.
35 input_symbol to process. The FSM will search the table of transitions that
43 If the FSM cannot find a match for (input_symbol, current_state) it will then
55 For the case where the FSM did not match either of the previous two cases the
56 FSM will try to use the default transition. If the default transition is
63 current_state then the FSM will raise an exception. This may be desirable, but
71 """This is the FSM Exception class."""
79 class FSM:
81 """This is a Finite State Machine (FSM).
86 """This creates the FSM. You set the initial state here. The "memory"
88 functions. It is not used by the FSM. For parsing you would typically
167 next_state if the FSM cannot find the input symbol and the current
168 state in the transition list and if the FSM cannot find the
180 This does not modify the FSM state, so calling this method has no side
213 cause the FSM to change state and call an action. This method calls
236 # The following is an example that demonstrates the use of the FSM class to
256 def BeginBuildNumber (fsm):
257 fsm.memory.append (fsm.input_symbol)
259 def BuildNumber (fsm):
260 s = fsm.memory.pop ()
261 s = s + fsm.input_symbol
262 fsm.memory.append (s)
264 def EndBuildNumber (fsm):
265 s = fsm.memory.pop ()
266 fsm.memory.append (int(s))
268 def DoOperator (fsm):
269 ar = fsm.memory.pop()
270 al = fsm.memory.pop()
271 if fsm.input_symbol == '+':
272 fsm.memory.append (al + ar)
273 elif fsm.input_symbol == '-':
274 fsm.memory.append (al - ar)
275 elif fsm.input_symbol == '*':
276 fsm.memory.append (al * ar)
277 elif fsm.input_symbol == '/':
278 fsm.memory.append (al / ar)
280 def DoEqual (fsm):
281 print str(fsm.memory.pop())
283 def Error (fsm):
285 print str(fsm.input_symbol)
289 """This is where the example starts and the FSM state transitions are
293 f = FSM ('INIT', []) # "memory" will be used as a stack.
314 parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), usage=globals()['__doc__'], version='$Id: FSM.py 490 2007-12-07 15:46:24Z noah $')