|
| 1 | +use std::collections::{BTreeMap, BTreeSet}; |
| 2 | + |
| 3 | +use crate::nfa::{NFAState, Transition, collect_epsilon_closure}; |
| 4 | + |
| 5 | +pub const INVALID_STATE: usize = !0; |
| 6 | + |
| 7 | +/// Represents a state in a Deterministic Finite Automaton specialized for lexical analysis. |
| 8 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 9 | +pub struct DFAState { |
| 10 | + /// Transition table mapping byte values (0-255) to destination state indices. |
| 11 | + /// Invalid transitions are marked as !0 (usize::MAX). |
| 12 | + pub transitions: [usize; 256], |
| 13 | + |
| 14 | + /// ID of the lexical rule that accepts this state. |
| 15 | + /// Lower values indicate higher priority rules (those appearing earlier in the flex definition). |
| 16 | + /// When multiple rules can match the same input, the rule with the lowest ID is selected. |
| 17 | + pub accept_lexer_rule_id: Option<u32>, |
| 18 | +} |
| 19 | + |
| 20 | +pub struct DFA { |
| 21 | + pub states: Vec<DFAState>, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'a> DFA { |
| 25 | + pub fn match_bytes(&self, bs: &[u8]) -> Option<u32> { |
| 26 | + let mut state = 0; |
| 27 | + |
| 28 | + // The initial state may be an accepting state, as with patterns like '.*' |
| 29 | + let mut accepted = self.states[state].accept_lexer_rule_id; |
| 30 | + |
| 31 | + for &b in bs { |
| 32 | + let next_state = self.states[state].transitions[b as usize]; |
| 33 | + if next_state == INVALID_STATE { |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + if self.states[next_state].accept_lexer_rule_id.is_some() { |
| 38 | + accepted = self.states[next_state].accept_lexer_rule_id; |
| 39 | + } |
| 40 | + state = next_state; |
| 41 | + } |
| 42 | + |
| 43 | + // After processing all input, check for an EOF transition |
| 44 | + // EOF is represented as 0 (byte value) for simplicity, similar to null-terminated strings |
| 45 | + let next_state = self.states[state].transitions[0]; |
| 46 | + if next_state != INVALID_STATE && self.states[next_state].accept_lexer_rule_id.is_some() { |
| 47 | + accepted = self.states[next_state].accept_lexer_rule_id; |
| 48 | + } |
| 49 | + |
| 50 | + accepted |
| 51 | + } |
| 52 | + |
| 53 | + pub fn match_string(&self, s: &str) -> Option<u32> { |
| 54 | + self.match_bytes(s.as_bytes()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<'a> From<&'a NFAState<'a>> for DFA { |
| 59 | + /// Subset construction algorithm |
| 60 | + fn from(start_state: &'a NFAState<'a>) -> Self { |
| 61 | + construct_dfa_with_state_mapping(start_state, None) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Selects the highest priority lexical rule (lowest ID) from all accepting states. |
| 66 | +/// This implements Flex-style rule selection semantics, where earlier rules in the |
| 67 | +/// definition file have higher priority when multiple rules match the same input. |
| 68 | +fn select_highest_priority_rule(set: &BTreeSet<&NFAState>) -> Option<u32> { |
| 69 | + set.iter() |
| 70 | + .fold(None, |acc, s| match *s.accept_lexer_rule_id.borrow() { |
| 71 | + Some(v) if v < acc.unwrap_or(u32::MAX) => Some(v), |
| 72 | + _ => acc, |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +/// Subset construction algorithm |
| 77 | +pub fn construct_dfa_with_state_mapping<'a>( |
| 78 | + start_state: &'a NFAState<'a>, |
| 79 | + mut dfa_to_nfa: Option<&mut Vec<Vec<usize>>>, |
| 80 | +) -> DFA { |
| 81 | + let mut first_set = BTreeSet::new(); |
| 82 | + collect_epsilon_closure(&mut first_set, start_state); |
| 83 | + |
| 84 | + if let Some(dfa_to_nfa) = dfa_to_nfa.as_mut() { |
| 85 | + dfa_to_nfa.push(first_set.iter().map(|s| s.state_id).collect()); |
| 86 | + } |
| 87 | + |
| 88 | + let mut nfa_map = BTreeMap::new(); |
| 89 | + nfa_map.insert(first_set.clone(), 0); |
| 90 | + |
| 91 | + let mut dfa_states = Vec::new(); |
| 92 | + dfa_states.push(DFAState { |
| 93 | + transitions: [INVALID_STATE; 256], |
| 94 | + accept_lexer_rule_id: select_highest_priority_rule(&first_set), |
| 95 | + }); |
| 96 | + |
| 97 | + let mut nfa_states_vec = vec![first_set]; |
| 98 | + |
| 99 | + while let Some(nfa_states) = nfa_states_vec.pop() { |
| 100 | + let src_state_index = *nfa_map.get(&nfa_states).unwrap(); |
| 101 | + |
| 102 | + for b in 0..=255 { |
| 103 | + let mut next_set = BTreeSet::new(); |
| 104 | + |
| 105 | + let t = Transition::Char(b); |
| 106 | + |
| 107 | + for nfa_state in &nfa_states { |
| 108 | + if let Some(new_states) = nfa_state.transitions.borrow().get(&t) { |
| 109 | + for &new_state in new_states { |
| 110 | + collect_epsilon_closure(&mut next_set, new_state); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if next_set.is_empty() { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + let dst_state_index = if let Some(index) = nfa_map.get(&next_set) { |
| 120 | + *index |
| 121 | + } else { |
| 122 | + let index = nfa_map.len(); |
| 123 | + |
| 124 | + let accept_lexer_rule_id = select_highest_priority_rule(&next_set); |
| 125 | + |
| 126 | + if let Some(dfa_to_nfa) = dfa_to_nfa.as_mut() { |
| 127 | + dfa_to_nfa.push(next_set.iter().map(|s| s.state_id).collect()); |
| 128 | + } |
| 129 | + |
| 130 | + nfa_map.insert(next_set.clone(), index); |
| 131 | + nfa_states_vec.push(next_set); |
| 132 | + |
| 133 | + dfa_states.push(DFAState { |
| 134 | + transitions: [!0; 256], |
| 135 | + accept_lexer_rule_id, |
| 136 | + }); |
| 137 | + |
| 138 | + index |
| 139 | + }; |
| 140 | + |
| 141 | + dfa_states[src_state_index].transitions[b as usize] = dst_state_index; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + DFA { states: dfa_states } |
| 146 | +} |
| 147 | + |
| 148 | +#[cfg(test)] |
| 149 | +mod tests { |
| 150 | + use crate::nfa::NFA; |
| 151 | + |
| 152 | + use super::*; |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_simple1() { |
| 156 | + let nfa = NFA::new(); |
| 157 | + |
| 158 | + // a(b|c)*d |
| 159 | + let start = nfa.new_state(); |
| 160 | + let s_a1 = nfa.new_state(); |
| 161 | + let s_a2 = nfa.new_state(); |
| 162 | + let s_bc1 = nfa.new_state(); |
| 163 | + let s_bc2 = nfa.new_state(); |
| 164 | + let s_b1 = nfa.new_state(); |
| 165 | + let s_b2 = nfa.new_state(); |
| 166 | + let s_c1 = nfa.new_state(); |
| 167 | + let s_c2 = nfa.new_state(); |
| 168 | + let s_kleene1 = nfa.new_state(); |
| 169 | + let s_kleene2 = nfa.new_state(); |
| 170 | + let s_d1 = nfa.new_state(); |
| 171 | + let s_d2 = nfa.new_state(); |
| 172 | + |
| 173 | + // Concatenation |
| 174 | + s_a1.add_transition(s_a2, Transition::Char('a' as u8)); |
| 175 | + |
| 176 | + // Alternation |
| 177 | + s_bc1.add_transition(s_b1, Transition::Epsilon); |
| 178 | + s_bc1.add_transition(s_c1, Transition::Epsilon); |
| 179 | + s_b1.add_transition(s_b2, Transition::Char('b' as u8)); |
| 180 | + s_c1.add_transition(s_c2, Transition::Char('c' as u8)); |
| 181 | + s_b2.add_transition(s_bc2, Transition::Epsilon); |
| 182 | + s_c2.add_transition(s_bc2, Transition::Epsilon); |
| 183 | + |
| 184 | + // Kleene closure (zero or more repetitions) |
| 185 | + s_kleene1.add_transition(s_kleene2, Transition::Epsilon); |
| 186 | + s_kleene1.add_transition(s_bc1, Transition::Epsilon); |
| 187 | + s_bc2.add_transition(s_kleene2, Transition::Epsilon); |
| 188 | + s_bc2.add_transition(s_bc1, Transition::Epsilon); |
| 189 | + |
| 190 | + // Concatenation |
| 191 | + s_d1.add_transition(s_d2, Transition::Char('d' as u8)); |
| 192 | + |
| 193 | + // Connecting the components |
| 194 | + start.add_transition(s_a1, Transition::Epsilon); |
| 195 | + s_a2.add_transition(s_bc1, Transition::Epsilon); |
| 196 | + s_bc2.add_transition(s_d1, Transition::Epsilon); |
| 197 | + |
| 198 | + s_d2.set_accept(0); |
| 199 | + |
| 200 | + assert!(nfa.matches_string(start, "abd")); |
| 201 | + assert!(nfa.matches_string(start, "acd")); |
| 202 | + assert!(nfa.matches_string(start, "abccbd")); |
| 203 | + assert!(!nfa.matches_string(start, "ad")); |
| 204 | + |
| 205 | + let dfa = DFA::from(start); |
| 206 | + assert_eq!(dfa.match_string("abd"), Some(0)); |
| 207 | + assert_eq!(dfa.match_string("acd"), Some(0)); |
| 208 | + assert_eq!(dfa.match_string("abccbd"), Some(0)); |
| 209 | + assert_eq!(dfa.match_string("ad"), None); |
| 210 | + } |
| 211 | +} |
0 commit comments