Documentation
¶
Overview ¶
Package automata provides data structures and algorithms for working with automata.
In language theory, automata refer to abstract computational models used to define and study formal languages. Automata are mathematical structures that process input strings and determine whether they belong to a specific language.
Index ¶
- Variables
- func NewSymbols(a ...Symbol) set.Set[Symbol]
- type DFA
- func (d *DFA) Accept(s String) bool
- func (d *DFA) Add(s State, a Symbol, next State)
- func (d *DFA) Clone() *DFA
- func (d *DFA) DOT() string
- func (d *DFA) EliminateDeadStates() *DFA
- func (d *DFA) Equal(rhs *DFA) bool
- func (d *DFA) Isomorphic(rhs *DFA) bool
- func (d *DFA) Minimize() *DFA
- func (d *DFA) Next(s State, a Symbol) State
- func (d *DFA) ReindexStates() *DFA
- func (d *DFA) States() []State
- func (d *DFA) String() string
- func (d *DFA) Symbols() []Symbol
- func (d *DFA) ToNFA() *NFA
- func (d *DFA) Transitions() iter.Seq[*Transition[State]]
- type NFA
- func (n *NFA) Accept(s String) bool
- func (n *NFA) Add(s State, a Symbol, next []State)
- func (n *NFA) Clone() *NFA
- func (n *NFA) Concat(ns ...*NFA) *NFA
- func (n *NFA) DOT() string
- func (n *NFA) Equal(rhs *NFA) bool
- func (n *NFA) Isomorphic(rhs *NFA) bool
- func (n *NFA) Next(s State, a Symbol) []State
- func (n *NFA) Star() *NFA
- func (n *NFA) States() []State
- func (n *NFA) String() string
- func (n *NFA) Symbols() []Symbol
- func (n *NFA) ToDFA() *DFA
- func (n *NFA) Transitions() iter.Seq[*Transition[[]State]]
- func (n *NFA) Union(ns ...*NFA) *NFA
- type State
- type States
- type String
- type Symbol
- type Symbols
- type Transition
Constants ¶
This section is empty.
Variables ¶
var ( EqState = generic.NewEqualFunc[State]() CmpState = generic.NewCompareFunc[State]() HashState = hash.HashFuncForInt[State](nil) EqSymbol = generic.NewEqualFunc[Symbol]() CmpSymbol = generic.NewCompareFunc[Symbol]() HashSymbol = hash.HashFuncForInt32[Symbol](nil) )
Functions ¶
Types ¶
type DFA ¶
DFA implements a deterministic finite automaton.
func CombineDFA ¶ added in v0.11.0
CombineDFA merges multiple deterministic finite automata (DFAs) into a single DFA that recognizes the union of the languages accepted by each individual DFA.
The process follows these steps:
- Convert each DFA into a nondeterministic finite automaton (NFA).
- Construct a single NFA that accepts the union of all individual NFAs.
- Convert the unified NFA into a DFA.
- Remove dead states and eliminate transitions leading to them.
- Reassign state indices to maintain a compact representation.
Throughout this process, the method tracks the final states of each original DFA, returning a slice where each index maps to the final states of the corresponding DFA.
Note: This method does not minimize the resulting DFA, as minimization would merge final states, making it impossible to distinguish between the final states of the individual DFAs.
This function is commonly used when constructing a DFA for lexical analysis.
func NewDFA ¶
NewDFA creates a new deterministic finite automaton. Finite automata are recognizers; they simply say yes or no for each possible input string.
func (*DFA) Accept ¶
Accept determines whether or not an input string is recognized (accepted) by the DFA.
func (*DFA) Clone ¶ added in v0.11.0
Clone returns a deep copy of the DFA, ensuring the clone is independent of the original.
func (*DFA) DOT ¶ added in v0.10.0
DOT generates a DOT representation of the transition graph of the DFA.
func (*DFA) EliminateDeadStates ¶ added in v0.11.0
EliminateDeadStates eliminates the dead states and all transitions to them. The subset construction and minimization algorithms sometimes produce a DFA with a single dead state.
Strictly speaking, a DFA must have a transition from every state on every input symbol in its input alphabet. When we construct a DFA to be used in a lexical analyzer, we need to treat the dead state differently. We must know when there is no longer any possibility of recognizing a longer lexeme. Thus, we should always omit transitions to the dead state and eliminate the dead state itself.
func (*DFA) Equal ¶ added in v0.10.3
Equal determines whether or not two DFAs are identical in structure and labeling. Two DFAs are considered equal if they have the same start state, final states, and transitions.
For isomorphic equality, structural equivalence with potentially different state names, use the Isomorphic method.
func (*DFA) Isomorphic ¶ added in v0.8.1
Isomorphic determines whether or not two DFAs are isomorphically the same.
Two DFAs D₁ and D₂ are said to be isomorphic if there exists a bijection f: S(D₁) → S(D₂) between their state sets such that, for every input symbol a, there is a transition from state s to state t on input a in D₁ if and only if there is a transition from state f(s) to state f(t) on input a in D₂.
In simpler terms, the two DFAs have the same structure: one can be transformed into the other by renaming its states and preserving the transitions.
func (*DFA) Minimize ¶ added in v0.4.2
Minimize creates a unique DFA with the minimum number of states.
The minimization algorithm sometimes produces a DFA with one dead state. This state is not accepting and transfers to itself on each input symbol.
We often want to know when there is no longer any possibility of acceptance. If so, we may want to eliminate the dead state and use an automaton that is missing some transitions. This automaton has one fewer state than the minimum-state DFA. Strictly speaking, such an automaton is not a DFA, because of the missing transitions to the dead state.
For more information and details, see "Compilers: Principles, Techniques, and Tools (2nd Edition)".
func (*DFA) Next ¶ added in v0.3.5
Next returns the next state based on the current state s and the input symbol a. If no valid transition exists, it returns an invalid state (-1).
func (*DFA) ReindexStates ¶ added in v0.11.0
ReindexStates reassigns indices to states based on a breadth-first traversal of the DFA, starting from the initial state. This method is typically called after removing unreachable or dead states from the DFA.
func (*DFA) ToNFA ¶ added in v0.4.2
ToNFA constructs a new NFA accepting the same language as the DFA (every DFA is an NFA).
func (*DFA) Transitions ¶ added in v0.11.0
func (d *DFA) Transitions() iter.Seq[*Transition[State]]
Transitions returns an iterator sequence containing all transitions in the DFA.
type NFA ¶
NFA implements a non-deterministic finite automaton.
func NewNFA ¶
NewNFA creates a new non-deterministic finite automaton. Finite automata are recognizers; they simply say yes or no for each possible input string.
func (*NFA) Accept ¶
Accept determines whether or not an input string is recognized (accepted) by the NFA.
func (*NFA) Clone ¶ added in v0.11.0
Clone returns a deep copy of the NFA, ensuring the clone is independent of the original.
func (*NFA) Concat ¶ added in v0.4.3
Concat constructs a new NFA that accepts the concatenation of languages accepted by each individual NFA.
func (*NFA) DOT ¶ added in v0.10.0
DOT generates a DOT representation of the transition graph of the NFA.
func (*NFA) Equal ¶ added in v0.10.3
Equal determines whether or not two NFAs are identical in structure and labeling. Two NFAs are considered equal if they have the same start state, final states, and transitions.
For isomorphic equality, structural equivalence with potentially different state names, use the Isomorphic method.
func (*NFA) Isomorphic ¶ added in v0.8.1
Isomorphic determines whether or not two NFAs are isomorphically the same.
Two NFAs N₁ and N₂ are said to be isomorphic if there exists a bijection f: S(N₁) → S(N₂) between their state sets such that, for every input symbol a, there is a transition from state s to state t on input a in N₁ if and only if there is a transition from state f(s) to state f(t) on input a in N₂.
In simpler terms, the two NFAs have the same structure: one can be transformed into the other by renaming its states and preserving the transitions.
func (*NFA) Next ¶ added in v0.3.5
Next returns the set of next states based on the current state s and the input symbol a. If no valid transition exists, it returns nil
func (*NFA) Star ¶ added in v0.4.4
Star constructs a new NFA that accepts the Kleene star closure of the language accepted by the NFA.
func (*NFA) ToDFA ¶
ToDFA constructs a new DFA accepting the same language as the NFA. It implements the subset construction algorithm.
For more information and details, see "Compilers: Principles, Techniques, and Tools (2nd Edition)".
func (*NFA) Transitions ¶ added in v0.11.0
func (n *NFA) Transitions() iter.Seq[*Transition[[]State]]
Transitions returns an iterator sequence containing all transitions in the NFA.