Method: Kleene::DSL#append!

Defined in:
lib/kleene/dsl.rb

#append!(a, b) ⇒ Object

Destructively append b onto a Appending produces a machine that matches all the strings in machine a followed by all the strings in machine b. This differs from ‘seq` in that the composite machine’s final states are the union of machine a’s final states and machine b’s final states.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kleene/dsl.rb', line 127

def append!(a, b)
  a.alphabet = a.alphabet | b.alphabet

  # add an epsilon transition from each final state of machine a to the start state of maachine b.
  a.final_states.each do |final_state|
    a.add_transition(NFATransition::Epsilon, final_state, b.start_state)
  end

  # add all of machine b's transitions to machine a
  b.all_transitions.each {|transition| a.add_transition(transition.token, transition.from, transition.to) }
  # a.final_states = a.final_states | b.final_states
  a.update_final_states

  a
end