Need a metavalue singleton kinda thingy as a reserved key...

holger krekel pyth at devel.trillke.net
Sun Aug 11 16:35:47 EDT 2002


Noah wrote:
> 
> Oh, so you want a motivation for doing this?

yes, thanks.

> I'm creating a generic Finite State Machine class.
> As part of the input to the FSM you specify a map from
>     (input_symbol, state)  -->  (action, next_state)
> The input_symbol can be any object or value you want.

this somewhat implies that there is *no* metasymbol. 
Anything you define may be used and thus is not 'meta' anymore.

> I want a convenient way for the user to specify
> special symbols that the FSM treats differently
> than regular symbols. For example, some types of
> special symbols might be "MATCH_ANY", "MATCH_ALWAYS", 
> "LAMBDA", "DEFAULT_MATCH". These meta-symbols are
> special in that the FSM will check for a match on them
> before or after other possible transitions. 

> I suppose I could specify the special meta-transitions 
> using a separate method so that there is no ambiguity, but 
> my initial goal was to limit the number of methods.
> 

Yes, this seems to require a different method than registering
direct transitions.  You may want something like

    def addRule(state=None, pre=None, default=None):
        """ pre    function which is called with (input_symbol, state)
                   before any lookup takes place. this functions either
                   returns None or a new state. When returning
                   None remaining Rules are "tried".  Note that 
                   the functions are only called for the given
                   state (or with all states if state is None)

            default same as 'pre' but it is only called if no 
                   direct transition is available. 
        """

This way you can register multiple rules in the desired order.
Note that you could add a special rule that handles the type 
of transitions you defined earlier.  It becomes a special case :-)

> And I admit this is a bit pedantic anyway since I have never
> seen anybody use an FSM to match anything except characters.

But allowing classes and such makes sense to me. 

> Originally I used None as a meta-symbol for "ANY", but then 
> I came to a situation where I actually wanted to match None
> Later I came up with more than one meta symbol, but
> I had only one None :-) ...

:-) And you have to distinguish 'before' and 'default'-semantics,
anyway.  One may want to register a new default handler or 
intercept transitions (e.g. for logging purposes or event generation). 

> Since I could not cleanly do what I wanted I thought
> that maybe I was looking at this the wrong way.

Maybe i am too :-)

    holger




More information about the Python-list mailing list