NFA.hpp
A Non-deterministic Finite Automata simulator.
To build a standard NFA, use NFA. If you want to have more symbols or more stop states, use tNFA<S,T> where S is the number of symbols and T is the type used for stop. (defaults are 128 for ASCII-128 and uint8_t respectively.)
The constructor can take as parameters the number of states and the id of the start state (both default to 0)
Note
Status: BETA
Note
DFA’s use SetTransition(), but NFA’s use AddTransition. This distinction is intentional since in a DFA a second SetTransition with the same start state and symbol will override first, while in an NFA a second AddTransition will always add a new option.
Typedefs
-
using NFA_State = tNFA_State<128, uint8_t>
NFA_State is the most standard tNFA_State setup.
-
template<size_t S = 128, typename STOP_TYPE = uint8_t>
class tNFA - #include <NFA.hpp>
A dynamic NFA class, for easily building non-determanistic finite automata.
Public Functions
-
inline tNFA(size_t num_states = 1, size_t start_state = 0)
-
inline ~tNFA()
-
inline size_t GetSize() const
Return the current number of states.
-
inline const std::set<size_t> &GetStart() const
Return start state and all others reachable through empty transitions.
-
inline std::set<size_t> GetNext(size_t sym, size_t from_id = 0) const
Return the states reachable from the current state given the provided symbol.
-
inline std::set<size_t> GetNext(size_t sym, const std::set<size_t> from_set) const
return the states reachable from the current set of states given the provided symbol.
-
inline bool HasFreeTransitions(size_t id) const
Does the provided state have free transitions?
-
inline bool HasSymTransitions(size_t id) const
Does the provided state have symbol-transitions?
-
inline opts_t GetSymbolOptions(const std::set<size_t> &test_set) const
Return an BitSet indicating the symbols available from the provided set of states.
-
inline void Resize(size_t new_size)
Change the number of available states.
-
inline size_t AddNewState()
Add a new state into the NFA and return its id.
-
inline void AddTransitionSymbol(size_t from, size_t to, size_t sym)
Add a transition between states ‘from’ and ‘to’ that can be taken with the provided symbol.
-
inline void AddTransition(size_t from, size_t to, const char sym)
Add a transition between states ‘from’ and ‘to’ that can be taken with a char symbol.
-
inline void AddTransition(size_t from, size_t to, const std::string &sym_set)
Add a transition between states ‘from’ and ‘to’ that can be taken with the provided symbols.
-
inline void AddTransition(size_t from, size_t to, const char *sym_set)
Add a transition between states ‘from’ and ‘to’ that can be taken with the provided symbols.
-
inline void AddTransition(size_t from, size_t to, const BitSet<NUM_SYMBOLS> &sym_set)
Add a transition between states ‘from’ and ‘to’ that can be taken with the provided symbols.
-
inline void AddFreeTransition(size_t from, size_t to)
Create a free transition between ‘from’ and ‘to’.
-
template<typename T = stop_t>
inline void SetStop(size_t state, T stop_val = 1) Set the specified state to be a stop state (with an optional stop value.)
-
inline bool IsStart(size_t state) const
Test if NFA begins at provided state (may have free transitions to other states)
-
inline bool IsStop(size_t state) const
Test if this state is a legal endpoint for the NFA.
-
inline bool IsEmpty(size_t state) const
Test if this state has only empty transitions from it, and not stop state.
-
inline void Merge(const tNFA<NUM_SYMBOLS, STOP_TYPE> &nfa2)
Merge another NFA into this one.
-
inline void Print() const
Print information about this NFA (for debugging)
-
inline void PrintFreeMoves()
Identify free moves in NFA (for debugging)
Private Members
-
size_t start
Main start state (others might be reached for free.)
-
inline tNFA(size_t num_states = 1, size_t start_state = 0)
-
template<size_t NUM_SYMBOLS = 128, typename STOP_TYPE = uint8_t>
class tNFA_State - #include <NFA.hpp>
Information about the current full state (i.e., set of legal states) of an NFA.
Public Functions
-
inline tNFA_State(const tNFA<NUM_SYMBOLS, STOP_TYPE> &_nfa)
-
inline ~tNFA_State()
-
inline const tNFA<NUM_SYMBOLS, STOP_TYPE> &GetNFA() const
Get the NFA associated with this state.
-
inline bool IsActive() const
Are there currently any legal NFA states?
-
inline bool IsStop() const
Can we legally stop in any of the current states?
-
inline bool HasState(size_t id)
Is a particular NFA state currently included?
-
inline size_t GetSize()
How many states are currently included?
-
inline void Reset()
Change current states to start + free transitions from start.
-
inline void Next(size_t sym)
Update states given a new input symbol.
-
inline void Next(const std::string &sym_set)
Update states given a new series of input symbols (as a string)
-
inline void Print()
Print out current information about this NFA State (for debugging)
-
inline tNFA_State(const tNFA<NUM_SYMBOLS, STOP_TYPE> &_nfa)