class Kleene::DFA

Defined in:

dfa.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(start_state : Kleene::State, alphabet : Set(Char) = DEFAULT_ALPHABET, transitions : Hash(Kleene::State, Hash(Char, Kleene::DFATransition)) = Hash(State, Hash(Char, DFATransition)).new, dfa_state_to_nfa_state_sets : Hash(Kleene::State, Set(Kleene::State)) = Hash(State, Set(State)).new, transition_callbacks = nil, origin_nfa : Nil | Kleene::NFA = nil) #

[View source]

Instance Method Detail

def accept? #

[View source]
def add_transition(token, from_state, to_state) #

[View source]
def all_transitions : Array(DFATransition) #

[View source]
def alphabet : Set(Char) #

[View source]
def alphabet=(alphabet : Set(Char)) #

[View source]
def clear_error_states #

[View source]
def current_state : State #

[View source]
def current_state=(current_state : State) #

[View source]
def deep_clone #

transition callbacks are not copied beacuse it is assumed that the state transition callbacks may be stateful and reference structures or states that only exist in self, but not the cloned copy.


[View source]
def dfa_state_to_nfa_state_sets : Hash(State, Set(State)) #

[View source]
def dfa_state_to_nfa_state_sets=(dfa_state_to_nfa_state_sets : Hash(State, Set(State))) #

[View source]
def error? #

[View source]
def error_states #

[View source]
def final_states : Set(State) #

[View source]
def final_states=(final_states : Set(State)) #

[View source]
def handle_token!(input_token, token_index) #

accept an input token and transition to the next state in the state machine


[View source]
def match?(input : String) #

[View source]
def matches(input) #

Returns an array of matches found anywhere in the input string


[View source]
def matches_at_offset(input, input_start_offset) #

Returns an array of matches found in the input string, each of which begins at the offset input_start_offset


[View source]
def minimize! #

This is an implementation of the "Reducing a DFA to a Minimal DFA" algorithm presented here: http://web.cecs.pdx.edu/~harry/compilers/slides/LexicalPart4.pdf This implements Hopcroft's algorithm as presented on page 142 of the first edition of the dragon book.


[View source]
def next_state(from_state, input_token, token_index) #

if the DFA is currently in a final state, then we look up the associated NFA states that were also final, and return them def accepting_nfa_states : Set(State) if accept? dfa_state_to_nfa_state_sets[@current_state].select(&.final?).to_set else Set(State).new end end this function transitions from state to state on an input token


[View source]
def nfa_state_to_dfa_state_sets : Hash(State, Set(State)) #

[View source]
def nfa_state_to_dfa_state_sets=(nfa_state_to_dfa_state_sets : Hash(State, Set(State))) #

[View source]
def on_transition(transition, &blk : DFATransitionCallback) #

[View source]
def on_transition_to(state, &blk : DFATransitionCallback) #

[View source]
def origin_nfa #

[View source]
def reachable_states(start_state) #

Returns a set of State objects which are reachable through any transition path from the DFA's start_state.


[View source]
def regex_pattern #

[View source]
def reset_current_state #

[View source]
def set_regex_pattern(pattern : Nil | String) #

[View source]
def shallow_clone #

[View source]
def start_state : State #

[View source]
def start_state=(start_state : State) #

[View source]
def states : Set(State) #

[View source]
def states=(states : Set(State)) #

[View source]
def to_s(verbose = false) #
Description copied from class Object

Returns a nicely readable and concise string representation of this object, typically intended for users.

This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.

Also see #inspect.


[View source]
def transition_callbacks : Hash(DFATransition, DFATransitionCallback) #

[View source]
def transition_callbacks=(transition_callbacks : Hash(DFATransition, DFATransitionCallback)) #

[View source]
def transition_callbacks_per_destination_state : Hash(State, DFATransitionCallback) #

[View source]
def transition_callbacks_per_destination_state=(transition_callbacks_per_destination_state : Hash(State, DFATransitionCallback)) #

[View source]
def transitions : Hash(State, Hash(Char, DFATransition)) #

[View source]
def transitions=(transitions : Hash(State, Hash(Char, DFATransition))) #

[View source]
def update_final_states #

[View source]