Ejemplo n.º 1
0
class Node:
    """A state of an NFA or DFA."""

    def __init__(self):
        # Preinitialise the list of empty transitions, because
        # the nfa-to-dfa algorithm needs it
        #self.transitions = {'':[]}
        self.transitions = TransitionMap()
        self.action = None # Action
        self.number = 0 # for debug output
        self.action_priority = LOWEST_PRIORITY
        self.epsilon_closure = None # used by nfa_to_dfa()

    def destroy(self):
        #print "Destroying", self ###
        self.transitions = None
        self.action = None
        self.epsilon_closure = None

    def add_transition(self, event, new_state):
        self.transitions.add(event, new_state)

    def link_to(self, state):
        """Add an epsilon-move from this state to another state."""
        self.add_transition('', state)

    def set_action(self, action, priority):
        """Make this an accepting state with the given action. If
        there is already an action, choose the action with highest
        priority."""
        if priority > self.action_priority:
            self.action = action
            self.action_priority = priority

    def get_action(self):
        return self.action

    def get_action_priority(self):
        return self.action_priority

    def is_accepting(self):
        return self.action is not None

    def __str__(self):
        return "State %d" % self.number

    def dump(self, file):
        # Header
        file.write("   State %d:\n" % self.number)
        # Transitions
        #self.dump_transitions(file)
        self.transitions.dump(file)
        # Action
        action = self.action
        priority = self.action_priority
        if action is not None:
            file.write("      %s [priority %d]\n" % (action, priority))
Ejemplo n.º 2
0
 def __init__(self):
     # Preinitialise the list of empty transitions, because
     # the nfa-to-dfa algorithm needs it
     #self.transitions = {'':[]}
     self.transitions = TransitionMap()
     self.action = None # Action
     self.number = 0 # for debug output
     self.action_priority = LOWEST_PRIORITY
     self.epsilon_closure = None # used by nfa_to_dfa()