Exemplo n.º 1
0
class Node:
    """A state of an NFA or DFA."""
    transitions = None       # TransitionMap
    action = None            # Action
    action_priority = None   # integer
    number = 0               # for debug output
    epsilon_closure = None   # used by nfa_to_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_priority = LOWEST_PRIORITY

    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 merge_actions(self, other_state):
#		"""Merge actions of other state into this state according
#    to their priorities."""
#		action = other_state.get_action()
#		priority = other_state.get_action_priority()
#		self.set_action(action, priority)

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

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

    def dump(self, file):
        import string
        # 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))
Exemplo n.º 2
0
class Node:
    """A state of an NFA or DFA."""
    transitions = None  # TransitionMap
    action = None  # Action
    action_priority = None  # integer
    number = 0  # for debug output
    epsilon_closure = None  # used by nfa_to_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_priority = LOWEST_PRIORITY

    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 merge_actions(self, other_state):
#		"""Merge actions of other state into this state according
#    to their priorities."""
#		action = other_state.get_action()
#		priority = other_state.get_action_priority()
#		self.set_action(action, priority)

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

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

    def dump(self, file):
        import string
        # 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))