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
89 """This is the FSM Exception class."""
97 class FSM:
99 """This is a Finite State Machine (FSM).
104 """This creates the FSM. You set the initial state here. The "memory"
106 functions. It is not used by the FSM. For parsing you would typically
185 next_state if the FSM cannot find the input symbol and the current
186 state in the transition list and if the FSM cannot find the
198 This does not modify the FSM state, so calling this method has no side
231 cause the FSM to change state and call an action. This method calls
254 # The following is an example that demonstrates the use of the FSM class to
274 def BeginBuildNumber (fsm):
275 fsm.memory.append (fsm.input_symbol)
277 def BuildNumber (fsm):
278 s = fsm.memory.pop ()
279 s = s + fsm.input_symbol
280 fsm.memory.append (s)
282 def EndBuildNumber (fsm):
283 s = fsm.memory.pop ()
284 fsm.memory.append (int(s))
286 def DoOperator (fsm):
287 ar = fsm.memory.pop()
288 al = fsm.memory.pop()
289 if fsm.input_symbol == '+':
290 fsm.memory.append (al + ar)
291 elif fsm.input_symbol == '-':
292 fsm.memory.append (al - ar)
293 elif fsm.input_symbol == '*':
294 fsm.memory.append (al * ar)
295 elif fsm.input_symbol == '/':
296 fsm.memory.append (al / ar)
298 def DoEqual (fsm):
299 print str(fsm.memory.pop())
301 def Error (fsm):
303 print str(fsm.input_symbol)
307 """This is where the example starts and the FSM state transitions are
311 f = FSM ('INIT', []) # "memory" will be used as a stack.
332 parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), usage=globals()['__doc__'], version='$Id: FSM.py 533 2012-10-20 02:19:33Z noah $')