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
|
#![allow(dead_code, unused_variables, unused_mut)]
#[derive(Debug)]
struct State {
transitions: Vec<(Trans, usize)>, // (input symbol, destination state index)
}
#[derive(Debug)]
struct NFA {
graph: Vec<State>,
initial_state: usize,
accepting_states: Vec<usize>,
}
impl NFA {
fn new(graph: Vec<State>, initial_state: usize, accepting_states: Vec<usize>) -> Self {
NFA {
graph,
initial_state,
accepting_states,
}
}
fn simulate(&self, input: &str) -> bool {
let mut current_states = vec![self.initial_state];
for c in input.chars() {
let mut next_states = vec![];
for state in ¤t_states {
for (trans, dest_state) in &self.graph[*state].transitions {
// println!("char: {c}, trans: {trans:?}, dest_state: {dest_state:?}");
match trans {
Trans::Symbol(symbol) if *symbol == c => {
next_states.push(*dest_state);
}
Trans::Epsilon => {
next_states.push(*dest_state);
}
_ => {}
}
}
}
current_states = next_states;
}
// println!("states: {:?}", current_states);
current_states
.iter()
.any(|&state| self.accepting_states.contains(&state))
}
}
#[derive(PartialEq, Clone, Debug)]
enum Trans {
Symbol(char),
Epsilon,
}
struct Regex {
regex: &'static str,
}
impl Regex {
fn new(input: &'static str) -> Self {
Regex { regex: input }
}
fn compile_nfa(&self) -> NFA {
let mut graph: Vec<State> = vec![State {
transitions: vec![],
}];
let initial_state = 0;
let mut accepting_states = vec![];
let mut current_state = initial_state;
let mut prev_state: Option<usize> = None;
let input: Vec<char> = self.regex.chars().collect();
for (i, &c) in input.iter().enumerate() {
match c {
'*' => {}
'+' => todo!(),
'.' => {
let next_state = graph.len();
println!("{next_state}");
graph.push(State {
transitions: vec![],
});
graph[next_state]
.transitions
.push((Trans::Epsilon, next_state));
current_state = next_state;
}
'|' => {
let before = input[i - 1];
let after = input[i + 1];
let next_state = graph.len();
graph.push(State {
transitions: vec![],
});
graph[current_state]
.transitions
.push((Trans::Symbol(c), next_state));
current_state = next_state;
}
_ => {
// let after = input[i + 1];
// if after != '|' {
let next_state = graph.len();
graph.push(State {
transitions: vec![],
});
graph.push(State {
transitions: vec![],
});
graph[current_state]
.transitions
.push((Trans::Symbol(c), next_state + 1));
graph[next_state]
.transitions
.push((Trans::Epsilon, next_state + 1));
current_state = next_state + 1;
// }
}
}
}
accepting_states.push(current_state);
NFA::new(graph, initial_state, accepting_states)
}
}
fn main() {
let regex = Regex::new("a.bc");
let nfa = regex.compile_nfa();
println!("NFA: {:#?}", nfa);
let input = vec!["a", "abcc", "abc", "abbc", "abbbc", "abbbbc", "ac", "ab"];
println!("Regex: {:#?}", regex.regex);
for inp in input {
println!("{} -> {:?}", inp, nfa.simulate(inp)); // Should match
}
}
|