class Kleene::NFA
def add_transition(token, from_state, to_state)
def add_transition(token, from_state, to_state) # # make sure states EITHER have a single outbound epsilon transition OR non-epsilon outbound transitions; they can't have both # if token == NFATransition::Epsilon # # make sure from_state doesn't have any outbound non-epsilon transitions # raise "Error: Non-epsilon transitions are already present on #{from_state.to_s}! States may EITHER have a single outbound epsilon transision OR have outbound non-epsilon transitions, but not both." if transitions_from(from_state).any? {|t| !t.epsilon? } # else # # make sure from_state doesn't have any outbound epsilon transition # raise "Error: Epsilon transitions are already present on #{from_state.to_s}! States may EITHER have a single outbound epsilon transision OR have outbound non-epsilon transitions, but not both." if transitions_from(from_state).any?(&:epsilon?) # end @alphabet << token # alphabet is a set, so there will be no duplications @states << from_state @states << to_state new_transition = NFATransition.new(token, from_state, to_state) char_transition_map = @transitions[from_state] ||= Hash.new set_of_transisions = char_transition_map[token] ||= Set.new set_of_transisions << new_transition new_transition end