Add DFA#nil_transition_states

This commit is contained in:
Josh Holtrop 2021-05-20 17:08:34 -04:00
parent c77c81bf25
commit f38a7456e9
3 changed files with 31 additions and 1 deletions

View File

@ -1,3 +1,5 @@
require "erb"
require "set"
require_relative "imbecile/cli"
require_relative "imbecile/grammar"
require_relative "imbecile/regex"
@ -5,7 +7,6 @@ require_relative "imbecile/regex/dfa"
require_relative "imbecile/regex/nfa"
require_relative "imbecile/regex/unit"
require_relative "imbecile/version"
require "erb"
module Imbecile

View File

@ -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

View File

@ -6,6 +6,7 @@ module Imbecile
class State
attr_accessor :accepts
attr_reader :transitions
def initialize
@transitions = []