1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Custom engine for automata
// drawings from asciiflow.com
use crate::regex::{Regex, dfa::DFA};
// Core types
type StateId = usize;
// NFA
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone)]
pub struct NFA {
pub states: Vec<NFAState>,
pub start: StateId,
pub accept: StateId,
}
#[derive(Debug, Clone)]
pub struct NFAState {
// Use Vec for simplicity, HashMap if you need to deduplicate
pub epsilon: Vec<StateId>,
pub transitions: Vec<(char, StateId)>,
}
impl NFAState {
pub fn new() -> Self {
NFAState {
epsilon: Vec::new(),
transitions: Vec::new(),
}
}
pub fn add_epsilon(&mut self, state: StateId) {
self.epsilon.push(state);
}
pub fn add_transition(&mut self, symbol: char, state: StateId) {
self.transitions.push((symbol, state));
}
}
impl NFA {
pub fn new() -> Self {
NFA {
states: Vec::new(),
start: 0,
accept: 0,
}
}
pub fn epsilon_closure(&self, states: Vec<StateId>) -> Vec<StateId> {
let mut closure = states.clone();
let mut stack = states;
while let Some(state) = stack.pop() {
for &next_state in &self.states[state].epsilon {
if !closure.contains(&next_state) {
closure.push(next_state);
stack.push(next_state);
}
}
}
closure
}
pub fn add_state(&mut self) -> StateId {
let id = self.states.len();
self.states.push(NFAState::new());
id
}
pub fn add_epsilon(&mut self, from: StateId, to: StateId) {
self.states[from].add_epsilon(to);
}
pub fn add_transition(&mut self, from: StateId, symbol: char, to: StateId) {
self.states[from].add_transition(symbol, to);
}
pub fn from(r: Regex) -> Self {
let mut nfa = NFA::new();
let start = nfa.add_state();
let accept = nfa.build_from_regex(r, start);
nfa.accept = accept;
nfa
}
pub fn accepts(&self, input: &str) -> bool {
let dfa = DFA::from(self.clone());
dfa.accepts(input)
}
pub fn build_from_regex(&mut self, r: Regex, start: StateId) -> StateId {
// !!
// NFA construction follows Thompson's algorithm
match r {
Regex::Empty => {
// Accepts nothing - create unreachable dead end
self.add_state()
}
Regex::Epsilon => start, // Accepts empty string
Regex::Char(c) => {
let end = self.add_state();
self.add_transition(start, c, end);
end
}
Regex::Union(l, r) => {
/*
┌────────┐
┌►│..LEFT..├─┐
┌─────┬─┘ └────────┘ └─►┌───┐
│START│ │END│
└─────┴┐ ┌─────────┐ ┌►└───┘
└─►┤..RIGHT..├─┘
└─────────┘
*/
let sl = self.build_from_regex(*l, start); // left branch
let sr = self.build_from_regex(*r, start); // right branch
// Unification
let end = self.add_state();
self.add_epsilon(sl, end);
self.add_epsilon(sr, end);
end
}
Regex::Concat(l, r) => {
/*
┌────────┐
┌►│..LEFT..├─┐
┌─────┐ε│ └────────┘ │ε
│START├─┘ ┌─────────┘ ┌─►
└─────┘ │ ┌─────────┐│
└►│..RIGHT..│┘
└─────────┘
*/
let m = self.build_from_regex(*l, start);
// Wire together
self.build_from_regex(*r, m)
}
Regex::Star(r) => {
/*
ε
┌─────┬────►┌──────────┐
│start│ │...LOOP...│
└─────┘◄────└──────────┘
ε
*/
let end = self.build_from_regex(*r, start);
self.add_epsilon(start, end);
self.add_epsilon(end, start);
start
}
Regex::Range(s, l) => {
/*
l
┌───────────┐
│ l-1 │
│ ┌───────┐ │
│ │ l-2 ▼ ▼
┌──┴─┴┬────►┌───┐
│start│... │END│
└──┬─┬┴────►└───┘
│ │ s+2 ▲ ▲
│ └───────┘ │
│ s+1 │
└───────────┘
s
*/
let end = self.add_state();
for code in (s as u32)..=(l as u32) {
if let Some(ch) = char::from_u32(code) {
self.add_transition(start, ch, end);
}
}
end
}
}
}
}
use std::fmt;
impl fmt::Display for NFA {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "NFA with {} states", self.states.len())?;
writeln!(f, "Start: {}, Accept: {}", self.start, self.accept)?;
writeln!(f)?;
for (id, state) in self.states.iter().enumerate() {
write!(f, "State {}", id)?;
if id == self.start {
write!(f, " (START)")?;
}
if id == self.accept {
write!(f, " (ACCEPT)")?;
}
writeln!(f)?;
// Show transitions from this state
if state.transitions.is_empty() && state.epsilon.is_empty() {
writeln!(f, " (no transitions)")?;
} else {
for (symbol, to) in &state.transitions {
writeln!(f, " └─'{}'───> ({})", { *symbol }, to)?;
}
for to in &state.epsilon {
writeln!(f, " └─ε───> ({})", to)?;
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_from_regex() {
let regex = Regex::from_str("(a|b)bb").unwrap();
let automata = NFA::from(regex);
println!("{}", automata);
}
}