103. class Simulator
def initialize(dfa)
@dfa = dfa
end
def simulate(symbols)
state = 0
until symbols.empty?
state = @dfa.move(state, symbols.shift)
end
state
end
end
117. FSM Simulation
class Simulator
def initialize(nfa)
@nfa = nfa
end
def simulate(symbols)
states = @nfa.nil_closure([0])
until symbols.empty?
next_s = @nfa.move(states, symbols.shift)
states = @nfa.nil_closure(next_s)
end
states
end
end
118. Move function
class NFA
...
def move(states, symbol)
states.map { |s| @table[s][symbol] }.flatten
end
end
119. irb> sim = Simulator.new(NFA.new)
=> #<Simulator:0x007faa188a5f88 ...>
irb> sim.simulate %w{ a }
=> [6]
irb> sim.simulate %w{ b }
=> [6]
irb> sim.simulate %w{ b b }
=> []
irb> sim.simulate %w{ c }
=> []
120. Time: O(r ⨉ x)
r = operators.length, x = string.length