lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
//! Vector-based weighted pushdown automaton implementation.

use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hash;

use super::{
    PdaAcceptMode, PdaConfiguration, PdaTransition, StackAction, StackSymbol, WeightedPda,
};
use crate::semiring::Semiring;
use crate::wfst::StateId;

/// State information for a PDA.
#[derive(Debug, Clone)]
pub struct PdaState<L, W: Semiring> {
    /// Whether this is a final state.
    pub is_final: bool,
    /// Final weight (only meaningful if is_final).
    pub final_weight: W,
    /// Outgoing transitions.
    pub transitions: Vec<PdaTransition<L, W>>,
}

impl<L, W: Semiring> PdaState<L, W> {
    /// Create a non-final state.
    pub fn non_final() -> Self {
        Self {
            is_final: false,
            final_weight: W::zero(),
            transitions: Vec::new(),
        }
    }

    /// Create a final state with the given weight.
    pub fn final_with_weight(weight: W) -> Self {
        Self {
            is_final: true,
            final_weight: weight,
            transitions: Vec::new(),
        }
    }
}

impl<L, W: Semiring> Default for PdaState<L, W> {
    fn default() -> Self {
        Self::non_final()
    }
}

/// Vector-based implementation of a weighted pushdown automaton.
#[derive(Debug, Clone)]
pub struct VectorPda<L, W: Semiring> {
    /// States indexed by ID.
    states: Vec<PdaState<L, W>>,
    /// Initial state.
    start: StateId,
    /// Initial stack symbol.
    initial_stack: StackSymbol,
    /// Acceptance mode.
    accept_mode: PdaAcceptMode,
    /// Total number of transitions.
    num_transitions: usize,
}

impl<L, W: Semiring> VectorPda<L, W> {
    /// Get the initial stack symbol (inherent method).
    pub fn get_initial_stack(&self) -> StackSymbol {
        self.initial_stack
    }

    /// Get the start state (inherent method).
    pub fn get_start(&self) -> StateId {
        self.start
    }

    /// Get the acceptance mode (inherent method).
    pub fn get_accept_mode(&self) -> PdaAcceptMode {
        self.accept_mode
    }

    /// Get the number of states (inherent method).
    pub fn get_num_states(&self) -> usize {
        self.states.len()
    }

    /// Get the number of transitions (inherent method).
    pub fn get_num_transitions(&self) -> usize {
        self.num_transitions
    }

    /// Get transitions from a state (inherent method).
    pub fn get_transitions(&self, state: StateId) -> &[PdaTransition<L, W>] {
        self.states
            .get(state as usize)
            .map(|s| s.transitions.as_slice())
            .unwrap_or(&[])
    }

    /// Check if a state is final (inherent method).
    pub fn get_is_final(&self, state: StateId) -> bool {
        self.states
            .get(state as usize)
            .map(|s| s.is_final)
            .unwrap_or(false)
    }

    /// Get the final weight of a state (inherent method).
    pub fn get_final_weight(&self, state: StateId) -> W
    where
        W: Clone,
    {
        self.states
            .get(state as usize)
            .map(|s| s.final_weight.clone())
            .unwrap_or_else(W::zero)
    }
}

impl<L: Clone + Eq + Hash, W: Semiring + Clone> VectorPda<L, W> {
    /// Create a new empty PDA.
    pub fn new() -> Self {
        Self {
            states: Vec::new(),
            start: 0,
            initial_stack: StackSymbol::BOTTOM,
            accept_mode: PdaAcceptMode::FinalState,
            num_transitions: 0,
        }
    }

    /// Create a PDA with a specific acceptance mode.
    pub fn with_accept_mode(accept_mode: PdaAcceptMode) -> Self {
        Self {
            states: Vec::new(),
            start: 0,
            initial_stack: StackSymbol::BOTTOM,
            accept_mode,
            num_transitions: 0,
        }
    }

    /// Add a state and return its ID.
    pub fn add_state(&mut self) -> StateId {
        let id = self.states.len() as StateId;
        self.states.push(PdaState::non_final());
        id
    }

    /// Add a final state with the given weight.
    pub fn add_final_state(&mut self, weight: W) -> StateId {
        let id = self.states.len() as StateId;
        self.states.push(PdaState::final_with_weight(weight));
        id
    }

    /// Set the start state.
    pub fn set_start(&mut self, state: StateId) {
        self.start = state;
    }

    /// Set the initial stack symbol.
    pub fn set_initial_stack(&mut self, symbol: StackSymbol) {
        self.initial_stack = symbol;
    }

    /// Set the acceptance mode.
    pub fn set_accept_mode(&mut self, mode: PdaAcceptMode) {
        self.accept_mode = mode;
    }

    /// Make a state final.
    pub fn set_final(&mut self, state: StateId, weight: W) {
        if let Some(s) = self.states.get_mut(state as usize) {
            s.is_final = true;
            s.final_weight = weight;
        }
    }

    /// Remove final status from a state.
    pub fn unset_final(&mut self, state: StateId) {
        if let Some(s) = self.states.get_mut(state as usize) {
            s.is_final = false;
            s.final_weight = W::zero();
        }
    }

    /// Add a transition.
    pub fn add_transition(&mut self, transition: PdaTransition<L, W>) {
        if let Some(s) = self.states.get_mut(transition.from as usize) {
            s.transitions.push(transition);
            self.num_transitions += 1;
        }
    }

    /// Add a transition with explicit parameters.
    pub fn add_transition_parts(
        &mut self,
        from: StateId,
        input: Option<L>,
        stack_top: StackSymbol,
        stack_action: StackAction,
        to: StateId,
        weight: W,
    ) {
        self.add_transition(PdaTransition::new(
            from,
            input,
            stack_top,
            stack_action,
            to,
            weight,
        ));
    }

    /// Add an epsilon transition.
    pub fn add_epsilon_transition(
        &mut self,
        from: StateId,
        stack_top: StackSymbol,
        stack_action: StackAction,
        to: StateId,
        weight: W,
    ) {
        self.add_transition(PdaTransition::epsilon(
            from,
            stack_top,
            stack_action,
            to,
            weight,
        ));
    }

    /// Get mutable access to a state.
    pub fn state_mut(&mut self, state: StateId) -> Option<&mut PdaState<L, W>> {
        self.states.get_mut(state as usize)
    }

    /// Reserve capacity for states.
    pub fn reserve_states(&mut self, additional: usize) {
        self.states.reserve(additional);
    }

    /// Check if the PDA accepts the given input using breadth-first search.
    ///
    /// This is a simple recognition algorithm that may not terminate
    /// for PDAs with epsilon cycles. For production use, consider
    /// implementing a more sophisticated algorithm with cycle detection.
    pub fn accepts<I>(&self, input: I) -> bool
    where
        I: IntoIterator<Item = L>,
        L: PartialEq,
    {
        let input_vec: Vec<L> = input.into_iter().collect();
        let initial = PdaConfiguration::initial(self.start, input_vec, self.initial_stack);

        let mut visited: HashSet<(StateId, usize, Vec<StackSymbol>)> = HashSet::new();
        let mut queue: VecDeque<PdaConfiguration<L>> = VecDeque::new();
        queue.push_back(initial);

        while let Some(config) = queue.pop_front() {
            // Create a key for cycle detection
            let key = (
                config.state,
                config.remaining_input.len(),
                config.stack.clone(),
            );
            if visited.contains(&key) {
                continue;
            }
            visited.insert(key);

            // Check if we're in an accepting configuration
            if self.is_config_accepting(&config) {
                return true;
            }

            // Get stack top
            let stack_top = match config.stack_top() {
                Some(st) => st,
                None => continue, // Empty stack, no transitions possible
            };

            // Try epsilon transitions first
            for trans in self.get_epsilon_transitions(config.state, stack_top) {
                if let Some(new_config) = config.apply_transition(trans) {
                    queue.push_back(new_config);
                }
            }

            // Try consuming transitions
            if let Some(next_input) = config.next_input() {
                for trans in
                    self.get_matching_transitions(config.state, Some(next_input), stack_top)
                {
                    if !trans.is_epsilon() {
                        if let Some(new_config) = config.apply_transition(trans) {
                            queue.push_back(new_config);
                        }
                    }
                }
            }
        }

        false
    }

    /// Check if a configuration is accepting (inherent method).
    fn is_config_accepting(&self, config: &PdaConfiguration<L>) -> bool {
        if !config.input_exhausted() {
            return false;
        }

        match self.accept_mode {
            PdaAcceptMode::FinalState => self.get_is_final(config.state),
            PdaAcceptMode::EmptyStack => config.stack_empty(),
            PdaAcceptMode::Both => self.get_is_final(config.state) || config.stack_empty(),
        }
    }

    /// Get epsilon transitions from a state with a given stack top (inherent method).
    fn get_epsilon_transitions(
        &self,
        state: StateId,
        stack_top: StackSymbol,
    ) -> Vec<&PdaTransition<L, W>>
    where
        L: PartialEq,
    {
        self.get_transitions(state)
            .iter()
            .filter(|t| t.is_epsilon() && t.stack_top == stack_top)
            .collect()
    }

    /// Get transitions matching a specific input and stack top (inherent method).
    fn get_matching_transitions(
        &self,
        state: StateId,
        input: Option<&L>,
        stack_top: StackSymbol,
    ) -> Vec<&PdaTransition<L, W>>
    where
        L: PartialEq,
    {
        self.get_transitions(state)
            .iter()
            .filter(|t| t.matches(input, stack_top))
            .collect()
    }

    /// Get the accepting weight for a configuration (inherent method).
    fn get_accepting_weight(&self, config: &PdaConfiguration<L>) -> Option<W> {
        if !config.input_exhausted() {
            return None;
        }

        match self.accept_mode {
            PdaAcceptMode::FinalState => {
                if self.get_is_final(config.state) {
                    Some(self.get_final_weight(config.state))
                } else {
                    None
                }
            }
            PdaAcceptMode::EmptyStack => {
                if config.stack_empty() {
                    Some(W::one())
                } else {
                    None
                }
            }
            PdaAcceptMode::Both => {
                if self.get_is_final(config.state) {
                    Some(self.get_final_weight(config.state))
                } else if config.stack_empty() {
                    Some(W::one())
                } else {
                    None
                }
            }
        }
    }

    /// Compute the total weight of all accepting paths for the given input.
    ///
    /// Returns None if the input is not accepted.
    pub fn total_weight<I>(&self, input: I) -> Option<W>
    where
        I: IntoIterator<Item = L>,
        L: PartialEq,
    {
        let input_vec: Vec<L> = input.into_iter().collect();
        let initial = PdaConfiguration::initial(self.start, input_vec, self.initial_stack);

        // Map from configurations to accumulated weights
        let mut weights: HashMap<(StateId, usize, Vec<StackSymbol>), W> = HashMap::new();
        let mut queue: VecDeque<(PdaConfiguration<L>, W)> = VecDeque::new();
        queue.push_back((initial, W::one()));

        let mut total = W::zero();

        while let Some((config, path_weight)) = queue.pop_front() {
            let key = (
                config.state,
                config.remaining_input.len(),
                config.stack.clone(),
            );

            // Update weight for this configuration
            let current_weight = weights.entry(key.clone()).or_insert_with(W::zero);
            *current_weight = current_weight.clone().plus(&path_weight);

            // Check if we're in an accepting configuration
            if let Some(accept_weight) = self.get_accepting_weight(&config) {
                total = total.plus(&path_weight.clone().times(&accept_weight));
            }

            // Get stack top
            let stack_top = match config.stack_top() {
                Some(st) => st,
                None => continue,
            };

            // Try epsilon transitions
            for trans in self.get_epsilon_transitions(config.state, stack_top) {
                if let Some(new_config) = config.apply_transition(trans) {
                    let new_weight = path_weight.clone().times(&trans.weight);
                    queue.push_back((new_config, new_weight));
                }
            }

            // Try consuming transitions
            if let Some(next_input) = config.next_input() {
                for trans in
                    self.get_matching_transitions(config.state, Some(next_input), stack_top)
                {
                    if !trans.is_epsilon() {
                        if let Some(new_config) = config.apply_transition(trans) {
                            let new_weight = path_weight.clone().times(&trans.weight);
                            queue.push_back((new_config, new_weight));
                        }
                    }
                }
            }
        }

        if total == W::zero() {
            None
        } else {
            Some(total)
        }
    }

    /// Approximate this PDA as a finite state transducer with bounded stack depth.
    ///
    /// This creates an FST where states are (PDA state, stack content) pairs,
    /// limited to stacks of at most `max_depth` symbols.
    pub fn approximate_fst(&self, max_depth: usize) -> crate::wfst::VectorWfst<L, W>
    where
        L: Send + Sync,
    {
        use crate::wfst::{MutableWfst, VectorWfst, WeightedTransition};

        let mut fst: VectorWfst<L, W> = VectorWfst::new();

        // Map from (state, stack) to FST state
        let mut state_map: HashMap<(StateId, Vec<StackSymbol>), StateId> = HashMap::new();
        let mut queue: VecDeque<(StateId, Vec<StackSymbol>)> = VecDeque::new();

        // Initial FST state
        let initial_stack = vec![self.initial_stack];
        let initial_key = (self.start, initial_stack.clone());
        let initial_fst_state = fst.add_state();
        state_map.insert(initial_key.clone(), initial_fst_state);
        fst.set_start(initial_fst_state);
        queue.push_back((self.start, initial_stack));

        while let Some((pda_state, stack)) = queue.pop_front() {
            let key = (pda_state, stack.clone());
            let fst_state = *state_map.get(&key).expect("state should exist");

            // Check if accepting
            let config = PdaConfiguration::new(pda_state, vec![], stack.clone());
            if let Some(accept_weight) = self.get_accepting_weight(&config) {
                fst.set_final(fst_state, accept_weight);
            }

            // Get stack top
            let stack_top = match stack.last() {
                Some(&st) => st,
                None => continue,
            };

            // Process transitions
            for trans in self.get_transitions(pda_state) {
                if trans.stack_top != stack_top {
                    continue;
                }

                // Apply stack action
                let mut new_stack = stack.clone();
                if !trans.stack_action.apply(&mut new_stack) {
                    continue;
                }

                // Enforce depth limit
                if new_stack.len() > max_depth {
                    continue;
                }

                // Get or create target FST state
                let target_key = (trans.to, new_stack.clone());
                let target_fst_state = if let Some(&existing) = state_map.get(&target_key) {
                    existing
                } else {
                    let new_state = fst.add_state();
                    state_map.insert(target_key.clone(), new_state);
                    queue.push_back((trans.to, new_stack));
                    new_state
                };

                // Add FST transition
                fst.add_transition(WeightedTransition::new(
                    fst_state,
                    trans.input.clone(),
                    trans.input.clone(),
                    target_fst_state,
                    trans.weight.clone(),
                ));
            }
        }

        fst
    }
}

impl<L: Clone + Eq + Hash, W: Semiring + Clone> Default for VectorPda<L, W> {
    fn default() -> Self {
        Self::new()
    }
}

impl<L, W> WeightedPda<L, W> for VectorPda<L, W>
where
    L: Clone + Eq + Hash + Send + Sync,
    W: Semiring + Clone,
{
    fn start(&self) -> StateId {
        self.start
    }

    fn initial_stack(&self) -> StackSymbol {
        self.initial_stack
    }

    fn is_final(&self, state: StateId) -> bool {
        self.states
            .get(state as usize)
            .map(|s| s.is_final)
            .unwrap_or(false)
    }

    fn final_weight(&self, state: StateId) -> W {
        self.states
            .get(state as usize)
            .map(|s| s.final_weight.clone())
            .unwrap_or_else(W::zero)
    }

    fn accept_mode(&self) -> PdaAcceptMode {
        self.accept_mode
    }

    fn transitions(&self, state: StateId) -> &[PdaTransition<L, W>] {
        self.states
            .get(state as usize)
            .map(|s| s.transitions.as_slice())
            .unwrap_or(&[])
    }

    fn num_states(&self) -> usize {
        self.states.len()
    }

    fn num_transitions(&self) -> usize {
        self.num_transitions
    }

    fn states(&self) -> impl Iterator<Item = StateId> {
        0..self.states.len() as StateId
    }

    fn final_states(&self) -> impl Iterator<Item = StateId> {
        self.states
            .iter()
            .enumerate()
            .filter(|(_, s)| s.is_final)
            .map(|(i, _)| i as StateId)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::semiring::TropicalWeight;
    use crate::wfst::Wfst;

    #[test]
    fn test_empty_pda() {
        let pda: VectorPda<char, TropicalWeight> = VectorPda::new();
        assert_eq!(pda.num_states(), 0);
        assert_eq!(pda.num_transitions(), 0);
        assert!(pda.is_empty());
    }

    #[test]
    fn test_add_states() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        let s1 = pda.add_final_state(TropicalWeight::one());

        assert_eq!(s0, 0);
        assert_eq!(s1, 1);
        assert_eq!(pda.num_states(), 2);
        assert!(!pda.is_final(s0));
        assert!(pda.is_final(s1));
    }

    #[test]
    fn test_set_start() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        let s1 = pda.add_state();

        pda.set_start(s0);
        assert_eq!(pda.start(), s0);

        pda.set_start(s1);
        assert_eq!(pda.start(), s1);
    }

    #[test]
    fn test_set_final() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        assert!(!pda.is_final(s0));

        pda.set_final(s0, TropicalWeight::new(2.0));
        assert!(pda.is_final(s0));
        assert_eq!(pda.final_weight(s0).value(), 2.0);

        pda.unset_final(s0);
        assert!(!pda.is_final(s0));
    }

    #[test]
    fn test_add_transitions() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        let s1 = pda.add_state();

        pda.add_transition_parts(
            s0,
            Some('a'),
            StackSymbol::BOTTOM,
            StackAction::Push(vec![StackSymbol::BOTTOM, StackSymbol::new(1)]),
            s1,
            TropicalWeight::one(),
        );

        assert_eq!(pda.num_transitions(), 1);
        assert_eq!(pda.transitions(s0).len(), 1);
        assert_eq!(pda.transitions(s1).len(), 0);
    }

    #[test]
    fn test_epsilon_transition() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        let s1 = pda.add_state();

        pda.add_epsilon_transition(
            s0,
            StackSymbol::BOTTOM,
            StackAction::Noop,
            s1,
            TropicalWeight::one(),
        );

        let trans = &pda.transitions(s0)[0];
        assert!(trans.is_epsilon());
    }

    #[test]
    fn test_balanced_parentheses_pda() {
        // PDA for balanced parentheses: { (^n )^n | n >= 0 }
        // Uses two states: s0 (start/final when balanced), s1 (processing with markers)
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_final_state(TropicalWeight::one()); // Accept when stack = [Zâ‚€]
        let s1 = pda.add_state(); // Processing state (not final)
        pda.set_start(s0);

        let z0 = StackSymbol::BOTTOM;
        let left = StackSymbol::new(1);

        // From s0 (balanced state): On '(', push marker and go to s1
        pda.add_transition_parts(
            s0,
            Some('('),
            z0,
            StackAction::Push(vec![z0, left]),
            s1,
            TropicalWeight::one(),
        );

        // From s1: On '(', push another marker
        pda.add_transition_parts(
            s1,
            Some('('),
            left,
            StackAction::Push(vec![left, left]),
            s1,
            TropicalWeight::one(),
        );

        // From s1: On ')', pop marker. If z0 is revealed, go back to s0
        pda.add_transition_parts(
            s1,
            Some(')'),
            left,
            StackAction::Pop,
            s1,
            TropicalWeight::one(),
        );

        // Epsilon transition: when in s1 with z0 on top, go to s0
        pda.add_epsilon_transition(s1, z0, StackAction::Noop, s0, TropicalWeight::one());

        // Test acceptance
        assert!(pda.accepts("".chars()));
        assert!(pda.accepts("()".chars()));
        assert!(pda.accepts("(())".chars()));
        assert!(pda.accepts("((()))".chars()));
        assert!(!pda.accepts("(".chars()));
        assert!(!pda.accepts(")".chars()));
        assert!(!pda.accepts("(()".chars()));
        assert!(!pda.accepts("())".chars()));
        assert!(!pda.accepts(")(()".chars()));
    }

    #[test]
    fn test_a_n_b_n_pda() {
        // PDA for { a^n b^n | n >= 1 }
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state(); // Start, reading a's
        let s1 = pda.add_state(); // Reading b's
        let s2 = pda.add_final_state(TropicalWeight::one()); // Accepting

        pda.set_start(s0);

        let z0 = StackSymbol::BOTTOM;
        let a_marker = StackSymbol::new(1);

        // Read first 'a', push marker
        pda.add_transition_parts(
            s0,
            Some('a'),
            z0,
            StackAction::Push(vec![z0, a_marker]),
            s0,
            TropicalWeight::one(),
        );

        // Read more 'a's, push markers
        pda.add_transition_parts(
            s0,
            Some('a'),
            a_marker,
            StackAction::Push(vec![a_marker, a_marker]),
            s0,
            TropicalWeight::one(),
        );

        // Switch to reading 'b's (epsilon transition)
        pda.add_epsilon_transition(s0, a_marker, StackAction::Noop, s1, TropicalWeight::one());

        // Read 'b', pop marker
        pda.add_transition_parts(
            s1,
            Some('b'),
            a_marker,
            StackAction::Pop,
            s1,
            TropicalWeight::one(),
        );

        // Accept when stack has only z0 (epsilon transition to final)
        pda.add_epsilon_transition(s1, z0, StackAction::Noop, s2, TropicalWeight::one());

        // Test acceptance
        assert!(pda.accepts("ab".chars()));
        assert!(pda.accepts("aabb".chars()));
        assert!(pda.accepts("aaabbb".chars()));
        assert!(!pda.accepts("".chars()));
        assert!(!pda.accepts("a".chars()));
        assert!(!pda.accepts("b".chars()));
        assert!(!pda.accepts("aab".chars()));
        assert!(!pda.accepts("abb".chars()));
        assert!(!pda.accepts("ba".chars()));
    }

    #[test]
    fn test_accept_mode_empty_stack() {
        // PDA accepting by empty stack
        let mut pda: VectorPda<char, TropicalWeight> =
            VectorPda::with_accept_mode(PdaAcceptMode::EmptyStack);

        let s0 = pda.add_state();
        pda.set_start(s0);

        let z0 = StackSymbol::BOTTOM;

        // Read 'a', pop the stack
        pda.add_transition_parts(
            s0,
            Some('a'),
            z0,
            StackAction::Pop,
            s0,
            TropicalWeight::one(),
        );

        // Should accept "a" (empties the stack)
        assert!(pda.accepts("a".chars()));
        assert!(!pda.accepts("".chars())); // Stack not empty
        assert!(!pda.accepts("aa".chars())); // Can't process second 'a'
    }

    #[test]
    fn test_states_iterator() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        pda.add_state();
        pda.add_state();
        pda.add_state();

        let states: Vec<_> = pda.states().collect();
        assert_eq!(states, vec![0, 1, 2]);
    }

    #[test]
    fn test_final_states_iterator() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        pda.add_state(); // not final
        pda.add_final_state(TropicalWeight::one()); // final
        pda.add_state(); // not final
        pda.add_final_state(TropicalWeight::one()); // final

        let final_states: Vec<_> = pda.final_states().collect();
        assert_eq!(final_states, vec![1, 3]);
    }

    #[test]
    fn test_approximate_fst() {
        // Simple PDA: accepts "ab"
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        let s1 = pda.add_state();
        let s2 = pda.add_final_state(TropicalWeight::one());

        pda.set_start(s0);

        let z0 = StackSymbol::BOTTOM;
        let marker = StackSymbol::new(1);

        // Read 'a', push marker
        pda.add_transition_parts(
            s0,
            Some('a'),
            z0,
            StackAction::Push(vec![z0, marker]),
            s1,
            TropicalWeight::one(),
        );

        // Read 'b', pop marker
        pda.add_transition_parts(
            s1,
            Some('b'),
            marker,
            StackAction::Pop,
            s2,
            TropicalWeight::one(),
        );

        // Approximate as FST with max depth 5
        let fst = pda.approximate_fst(5);

        // Should have 3 states (one for each PDA state with appropriate stack)
        assert!(fst.num_states() >= 3);
    }

    #[test]
    fn test_matching_transitions() {
        let mut pda: VectorPda<char, TropicalWeight> = VectorPda::new();

        let s0 = pda.add_state();
        let s1 = pda.add_state();

        let z0 = StackSymbol::BOTTOM;
        let marker = StackSymbol::new(1);

        // Add several transitions
        pda.add_transition_parts(
            s0,
            Some('a'),
            z0,
            StackAction::Noop,
            s1,
            TropicalWeight::one(),
        );
        pda.add_transition_parts(
            s0,
            Some('b'),
            z0,
            StackAction::Noop,
            s1,
            TropicalWeight::one(),
        );
        pda.add_transition_parts(
            s0,
            Some('a'),
            marker,
            StackAction::Noop,
            s1,
            TropicalWeight::one(),
        );
        pda.add_epsilon_transition(s0, z0, StackAction::Noop, s1, TropicalWeight::one());

        // Test matching
        let matches = pda.matching_transitions(s0, Some(&'a'), z0);
        assert_eq!(matches.len(), 2); // 'a' on z0 and epsilon on z0

        let matches = pda.matching_transitions(s0, Some(&'b'), z0);
        assert_eq!(matches.len(), 2); // 'b' on z0 and epsilon on z0

        let matches = pda.matching_transitions(s0, Some(&'c'), z0);
        assert_eq!(matches.len(), 1); // Only epsilon

        let matches = pda.matching_transitions(s0, Some(&'a'), marker);
        assert_eq!(matches.len(), 1); // 'a' on marker
    }
}