Add DFA#nil_transition_states
This commit is contained in:
1 parent
c77c81bf25
commit
f38a7456e9
3 files changed
+31
-1
No files matched your search
@@ -4,6 +4,34 @@ module Imbecile
|
||||
class DFA
|
||||
|
||||
def initialize(nfas)
|
||||
start_nfa = NFA.new
|
||||
nfas.each do |nfa|
|
||||
start_nfa.start_state.add_transition(nil, nfa.start_state)
|
||||
end
|
||||
nil_transition_states = nil_transition_states(start_nfa.start_state)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Determine the set of states that can be reached by nil transitions
|
||||
# from the given state.
|
||||
#
|
||||
# @return [Set<NFA::State>]
|
||||
# Set of states.
|
||||
def nil_transition_states(state)
|
||||
states = Set[state]
|
||||
analyze_state = lambda do |state|
|
||||
state.transitions.each do |range, dest_state|
|
||||
if range.nil?
|
||||
unless states.include?(dest_state)
|
||||
states << dest_state
|
||||
analyze_state[dest_state]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
analyze_state[state]
|
||||
states
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ module Imbecile
|
||||
class State
|
||||
|
||||
attr_accessor :accepts
|
||||
attr_reader :transitions
|
||||
|
||||
def initialize
|
||||
@transitions = []
|
||||
|
||||
Reference in new issue
Block a user