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) Clone() *DFA
- func (d *DFA) DOT() string
- func (d *DFA) EliminateDeadStates() *DFA
- func (d *DFA) Equal(rhs *DFA) bool
- func (d *DFA) Final() []State
- func (d *DFA) Isomorphic(rhs *DFA) bool
- func (d *DFA) Minimize() *DFA
- func (d *DFA) ReindexStates() *DFA
- func (d *DFA) Runner() *DFARunner
- func (d *DFA) Start() State
- func (d *DFA) States() []State
- func (d *DFA) String() string
- func (d *DFA) Symbols() []disc.Range[Symbol]
- func (d *DFA) ToNFA() *NFA
- func (d *DFA) Transitions() iter.Seq2[State, iter.Seq2[[]disc.Range[Symbol], State]]
- func (d *DFA) TransitionsFrom(s State) iter.Seq2[[]disc.Range[Symbol], State]
- func (d *DFA) Union(ds ...*DFA) (*DFA, [][]State)
- type DFABuilder
- type DFARunner
- type NFA
- 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) Final() []State
- func (n *NFA) Isomorphic(rhs *NFA) bool
- func (n *NFA) Runner() *NFARunner
- func (n *NFA) Star() *NFA
- func (n *NFA) Start() State
- func (n *NFA) States() []State
- func (n *NFA) String() string
- func (n *NFA) Symbols() []disc.Range[Symbol]
- func (n *NFA) ToDFA() *DFA
- func (n *NFA) Transitions() iter.Seq2[State, iter.Seq2[[]disc.Range[Symbol], []State]]
- func (n *NFA) TransitionsFrom(s State) iter.Seq2[[]disc.Range[Symbol], []State]
- func (n *NFA) Union(ns ...*NFA) *NFA
- type NFABuilder
- type NFARunner
- type State
- type States
- type String
- type Symbol
- type Symbols
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) EqStates = func(a, b States) bool { if a == nil && b == nil { return true } if a == nil || b == nil { return false } return a.Equal(b) } CmpStates = func(a, b States) int { if a == nil && b == nil { return 0 } else if a == nil { return -1 } else if b == nil { return 1 } lhs := generic.Collect1(a.All()) rhs := generic.Collect1(b.All()) for i := 0; i < len(lhs) && i < len(rhs); i++ { if c := CmpState(lhs[i], rhs[i]); c != 0 { return c } } return len(lhs) - len(rhs) } EqSymbols = func(a, b Symbols) bool { if a == nil && b == nil { return true } if a == nil || b == nil { return false } return a.Equal(b) } )
Functions ¶
Types ¶
type DFA ¶
type DFA struct {
// contains filtered or unexported fields
}
DFA represents a deterministic finite automaton.
A DFA is defined by a 5-tuple (Q, Σ, δ, q₀, F) where:
- Q is a finite set of states.
- Σ is a finite set of input symbols (alphabet).
- δ: Q × Σ → Q is the transition function.
- q₀ ∈ Q is the initial (start) state.
- F ⊆ Q is the set of accepting (final) states.
This model is meant to be an immutable representation of deterministic finite automata. Algorithms that transform or optimize a DFA must construct and return a new DFA.
func UnionDFA ¶ added in v0.12.1
UnionDFA constructs a DFA that recognizes the union of the languages accepted by the provided DFAs.
The process involves:
- Converting each DFA to an NFA.
- Building a single NFA that accepts the union of all input NFAs.
- Converting the unified NFA to a DFA.
- Removing dead states and transitions to them.
- Reindexing states to maintain a compact representation.
The returned DFA accepts any string accepted by at least one input DFA. The second return value maps each input DFA to its corresponding final states in the resulting DFA.
Note: The resulting DFA is not minimized, so final states remain distinguishable for each input DFA. This is useful for applications like lexical analysis, where tracking the origin of acceptance matters.
func (*DFA) DOT ¶ added in v0.10.0
DOT generates a DOT representation of the DFA transition graph for visualization.
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) 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.
This is a very expensive operation as graph isomorphism problem is an NP (non-deterministic polynomial time) problem.
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) 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) Runner ¶ added in v0.12.0
Runner constructs a new DFARunner for simulating (running) the DFA on input symbols.
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
Transitions returns all transitions in the DFA.
func (*DFA) TransitionsFrom ¶ added in v0.12.0
TransitionsFrom returns all transitions from the given state in the DFA.
func (*DFA) Union ¶ added in v0.12.0
Union constructs a DFA that recognizes the union of the languages accepted by the provided DFAs.
The process involves:
- Converting each DFA to an NFA.
- Building a single NFA that accepts the union of all input NFAs.
- Converting the unified NFA to a DFA.
- Removing dead states and transitions to them.
- Reindexing states to maintain a compact representation.
The returned DFA accepts any string accepted by at least one input DFA. The second return value maps each input DFA to its corresponding final states in the resulting DFA.
Note: The resulting DFA is not minimized, so final states remain distinguishable for each input DFA. This is useful for applications like lexical analysis, where tracking the origin of acceptance matters.
type DFABuilder ¶ added in v0.12.0
type DFABuilder struct {
// contains filtered or unexported fields
}
DFABuilder implements the Builder design pattern for constructing DFA instances.
The Builder pattern separates the construction of a DFA from its representation. This approach ensures the resulting DFA is immutable and optimized for simulation (running).
func NewDFABuilder ¶ added in v0.12.0
func NewDFABuilder() *DFABuilder
NewDFABuilder creates a new DFA builder instance.
func (*DFABuilder) AddTransition ¶ added in v0.12.0
func (b *DFABuilder) AddTransition(s State, start, end Symbol, next State) *DFABuilder
AddTransition adds transitions from state s to state next on all input symbols in the range [start, end].
func (*DFABuilder) Build ¶ added in v0.12.0
func (b *DFABuilder) Build() *DFA
Build constructs the DFA based on the configurations provided to the builder.
This method reduces the size of the alphabet by computing the equivalence classes of input symbols based on the transition function. The resulting DFA will recognize the same language, but with a minimized alphabet optimized for faster simulation and smaller memory footprint.
Formally, given a DFA = (Q, Σ, δ, q₀, F), we want compute a partition of Σ into k equivalence classes such that
P = {C₁, C₂, ..., Cₖ} where each Cᵢ ⊆ Σ and ∪ Cᵢ = Σ
∀ a, b ∈ Cᵢ, ∀ q ∈ Q: δ(q, a) = δ(q, b)
Informally, two symbols are equivalent if they lead to the same next state from any given state (every state treats them the same).
func (*DFABuilder) SetFinal ¶ added in v0.12.0
func (b *DFABuilder) SetFinal(f []State) *DFABuilder
SetFinal sets the final (accepting) states of the DFA.
func (*DFABuilder) SetStart ¶ added in v0.12.0
func (b *DFABuilder) SetStart(s State) *DFABuilder
SetStart sets the start state of the DFA.
type DFARunner ¶ added in v0.12.0
type DFARunner struct {
// contains filtered or unexported fields
}
DFARunner is used for simulating (running) a DFA on input symbols. It is immutable and optimized for fast execution.
type NFA ¶
type NFA struct {
// contains filtered or unexported fields
}
NFA represents a non-deterministic finite automaton.
A NFA is defined by a 5-tuple (Q, Σ, δ, q₀, F) where:
- Q is a finite set of states.
- Σ is a finite set of input symbols (alphabet).
- δ: Q × Σ → P(Q) is the transition function.
- q₀ ∈ Q is the initial (start) state.
- F ⊆ Q is the set of accepting (final) states.
This model is meant to be an immutable representation of non-deterministic finite automata. Algorithms that transform or optimize a NFA must construct and return a new NFA.
func ConcatNFA ¶ added in v0.12.1
ConcatNFA constructs a new NFA that accepts the concatenation of languages accepted by each individual NFA.
func UnionNFA ¶ added in v0.12.1
UnionNFA constructs a new NFA that accepts the union of languages accepted by each individual NFA.
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 NFA transition graph for visualization.
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.
This is a very expensive operation as graph isomorphism problem is an NP (non-deterministic polynomial time) problem.
func (*NFA) Runner ¶ added in v0.12.0
Runner constructs a new NFARunner for simulating (running) the NFA on input symbols.
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.
func (*NFA) Transitions ¶ added in v0.11.0
Transitions returns all transitions in the NFA.
func (*NFA) TransitionsFrom ¶ added in v0.12.0
TransitionsFrom returns all transitions from the given state in the NFA.
type NFABuilder ¶ added in v0.12.0
type NFABuilder struct {
// contains filtered or unexported fields
}
NFABuilder implements the Builder design pattern for constructing NFA instances.
The Builder pattern separates the construction of an NFA from its representation, This approach ensures the resulting NFA is immutable and optimized for simulation (running).
func NewNFABuilder ¶ added in v0.12.0
func NewNFABuilder() *NFABuilder
NewNFABuilder creates a new NFA builder instance.
func (*NFABuilder) AddTransition ¶ added in v0.12.0
func (b *NFABuilder) AddTransition(s State, start, end Symbol, next []State) *NFABuilder
AddTransition adds transitions from state s to states next on all input symbols in the range [start, end].
func (*NFABuilder) Build ¶ added in v0.12.0
func (b *NFABuilder) Build() *NFA
Build constructs the NFA based on the configurations provided to the builder.
This method reduces the size of the alphabet by computing the equivalence classes of input symbols based on the transition function. The resulting NFA will recognize the same language, but with a minimized alphabet optimized for faster simulation and smaller memory footprint.
Formally, given a NFA = (Q, Σ, δ, q₀, F), we want compute a partition of Σ into k equivalence classes such that
P = {C₁, C₂, ..., Cₖ} where each Cᵢ ⊆ Σ and ∪ Cᵢ = Σ
∀ a, b ∈ Cᵢ, ∀ q ∈ Q: δ(q, a) = δ(q, b)
Informally, two symbols are equivalent if they lead to the same next state from any given state (every state treats them the same).
func (*NFABuilder) SetFinal ¶ added in v0.12.0
func (b *NFABuilder) SetFinal(f []State) *NFABuilder
SetFinal sets the final (accepting) states of the NFA.
func (*NFABuilder) SetStart ¶ added in v0.12.0
func (b *NFABuilder) SetStart(s State) *NFABuilder
SetStart sets the start state of the NFA.
type NFARunner ¶ added in v0.12.0
type NFARunner struct {
// contains filtered or unexported fields
}
NFARunner is used for simulating (running) a NFA on input symbols. It is immutable and optimized for fast execution.