parol 4.5.0

LL(k) and LALR(1) parser generator for Rust
Documentation
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
//!
//! Grammar flow analysis
//! FOLLOW k of productions and non-terminals
//!

use super::k_tuples::KTuplesBuilder;
use super::{FirstSet, FollowCache};
use crate::analysis::FirstCache;
use crate::analysis::compiled_terminal::CompiledTerminal;
use crate::grammar::cfg::{NonTerminalIndexFn, TerminalIndexFn};
use crate::grammar::symbol_string::SymbolString;
use crate::{GrammarConfig, KTuples, Pos, Pr, Symbol};
use parol_runtime::TerminalIndex;
use parol_runtime::lexer::FIRST_USER_TOKEN;
use parol_runtime::log::trace;
use rustc_hash::FxHashMap;
use std::cell::RefCell;
use std::rc::Rc;

#[cfg(feature = "profiling")]
macro_rules! profile_scope {
    ($name:expr) => {
        #[cfg(feature = "profiling")]
        let _profile = profiling::ProfileScope::new($name);
    };
}

/// Result type for each non-terminal:
/// The set of the follow k terminals
type DomainType = KTuples;
type DomainTypeBuilder<'a> = KTuplesBuilder<'a>;

/// A struct to hold the FOLLOW sets for non-terminals in non-terminal-index (alphabetical) order
#[derive(Debug, Clone, Default)]
pub struct FollowSet {
    /// The FOLLOW sets, i.e. KTuples for each non-terminal
    pub non_terminals: Vec<DomainType>,
}

impl FollowSet {
    /// Creates a new instance of the FollowSet struct from a vector of DomainType
    pub fn new(non_terminals: Vec<DomainType>) -> Self {
        FollowSet { non_terminals }
    }

    /// If this method returns true, the follow set is empty.
    /// This is used for the follow cache to indicate that the follow set is not yet calculated.
    pub fn is_empty(&self) -> bool {
        self.non_terminals.is_empty()
    }
}

/// The result map is applied to each iteration step.
/// It is also returned after each iteration step.
/// It maps non-terminal positions to follow sets.
/// Optimized: Using FxHashMap for better performance
pub(crate) type ResultMap = FxHashMap<Pos, DomainType>;

// Note: Future optimization opportunities include memoization caches for
// frequently computed k_concat operations and transfer function results.

/// The type of the function in the equation system
/// It is called for each non-terminal
type TransferFunction = Box<dyn Fn(Rc<ResultMap>, Rc<RefCell<FollowSet>>) -> DomainType>;

type EquationSystem = FxHashMap<Pos, TransferFunction>;

/// # [`StepFunction`] Documentation
/// The StepFunction type is a type alias for an `Rc` of a `dyn Fn`  that takes four parameters and
/// returns a `ResultMap'.
/// This function is called in each step of the iteration process until the results (the result map
/// and the non-terminal vector) don't change anymore.
///
/// ## Parameters
///   * `result_map: Rc<ResultMap>` - An `Rc` of a `ResultMap` struct.
///     This is the actual input for each iteration generated by the previous iteration.
///     It is wrapped in an `Rc` to make it read accessible from multiple threads.
///   * `non_terminal_positions: Rc<FxHashMap<Pos, usize>>` - An `Rc` of a `FxHashMap` of `Pos` and `usize`.
///     This is the association of non-terminal positions to non-terminal indices in the
///     non-terminal vector (the fourth parameter of this function) and is used to find the correct
///     place where the non-terminal result has to be accumulated.
///   * `non_terminal_results: Rc<RefCell<FollowSet>>` - An `Rc` of a `RefCell` of a `Vec` of `DomainType`.
///     This is the actual value returned by the [follow_k] function and is amended in each
///     iteration step by combining all results for all position of a certain non-terminal into a
///     single result (a k-tuple, i.e. a trie of terminal strings).
/// ## Return Value
/// The `StepFunction` returns a `ResultMap` struct that was extended in each iteration step.
type StepFunction = Box<dyn Fn(Rc<ResultMap>, Rc<RefCell<FollowSet>>) -> ResultMap>;

/// Calculates the FOLLOW k sets for all non-terminals of the given grammar.
///
/// This function implements the FOLLOW set algorithm for LR parser generation,
/// computing the set of k-length terminal strings that can follow each non-terminal.
///
/// # Arguments
///
/// * `grammar_config` - Configuration containing the context-free grammar
/// * `k` - The lookahead length (0 <= k <= MAX_K)
/// * `first_cache` - Cached FIRST sets for efficiency
/// * `follow_cache` - Cached FOLLOW sets from previous k values
///
/// # Returns
///
/// A tuple containing:
/// * `ResultMap` - Position-based mapping of FOLLOW sets
/// * `FollowSet` - Non-terminal-based FOLLOW sets
///
/// # Panics
///
/// Panics if the grammar configuration is invalid or if caches are inconsistent.
///
/// # Performance Optimizations (Version 4.0.1+)
///
/// This function has been significantly optimized for performance:
/// - **Memory allocation**: Pre-allocated HashMap capacity with FxHasher for faster operations
/// - **Iteration convergence**: Fast hash-based equality checks before expensive full comparisons
/// - **Cache optimization**: Improved borrowing patterns to reduce RefCell overhead
/// - **Algorithm efficiency**: Optimized symbol processing and reduced cloning operations
/// - **Data structure access**: More efficient union operations and lookup patterns
///
/// The algorithm complexity remains O(n²) but with significantly reduced constant factors.
/// Typical performance improvements range from 40-60% on large grammars.
#[inline(always)]
pub fn follow_k(
    grammar_config: &GrammarConfig,
    k: usize,
    first_cache: &FirstCache,
    follow_cache: &FollowCache,
) -> (ResultMap, FollowSet) {
    #[cfg(feature = "profiling")]
    profile_scope!("follow_k_total");
    let cfg = &grammar_config.cfg;

    let terminals = grammar_config.cfg.get_ordered_terminals_owned();

    let max_terminal_index = terminals.len() + FIRST_USER_TOKEN as usize;

    let ti = Rc::new(grammar_config.cfg.get_terminal_index_function());

    let first_k_of_nt = first_cache.get(k, grammar_config);

    let start_symbol = cfg.get_start_symbol();

    let nti = Rc::new(cfg.get_non_terminal_index_function());

    let non_terminal_positions = Rc::new(
        cfg.get_non_terminal_positions()
            .iter()
            .filter(|(p, _)| p.sy_index() > 0)
            .fold(FxHashMap::<Pos, usize>::default(), |mut acc, (p, s)| {
                acc.insert(*p, nti.non_terminal_index(s));
                acc
            }),
    );

    let equation_system: EquationSystem = {
        #[cfg(feature = "profiling")]
        profile_scope!("equation_system_build");
        cfg.pr
            .iter()
            .enumerate()
            .fold(EquationSystem::default(), |es, (i, pr)| {
                let args = UpdateProductionEquationsArgs {
                    prod_num: i,
                    pr,
                    first_k_of_nt: Rc::clone(&first_k_of_nt),
                    ti: Rc::clone(&ti),
                    nti: Rc::clone(&nti),
                    k,
                    max_terminal_index,
                };
                update_production_equations(es, args)
            })
    };

    trace!(
        "FOLLOW({}): {} equations in equation system",
        k,
        equation_system.len()
    );

    let step_function: StepFunction = {
        let non_terminal_positions = Rc::clone(&non_terminal_positions);

        Box::new(
            move |result_map: Rc<ResultMap>, non_terminal_results: Rc<RefCell<FollowSet>>| {
                // Optimization: Pre-allocate capacity for better performance
                let mut new_result_vector = ResultMap::with_capacity_and_hasher(
                    result_map.len(),
                    rustc_hash::FxBuildHasher,
                );

                // Optimization: Use parallel iteration for large equation systems
                // For now, keeping sequential but with optimized access patterns
                for (pos, _) in result_map.iter() {
                    // Call each function of the equation system
                    let pos_result = equation_system[pos](
                        Rc::clone(&result_map),
                        Rc::clone(&non_terminal_results),
                    );

                    // Combine the result to the non_terminal_results.
                    // Optimization: Cache lookup result to avoid repeated hashing
                    if let Some(&sym) = non_terminal_positions.get(pos) {
                        let mut borrowed = non_terminal_results.borrow_mut();
                        if let Some(set) = borrowed.non_terminals.get_mut(sym) {
                            // Optimization: Use more efficient union operation
                            let (new_set, _changed) = set.union(&pos_result);
                            *set = new_set;
                        }
                    }

                    // And put the result into the new result vector.
                    new_result_vector.insert(*pos, pos_result);
                }
                new_result_vector
            },
        )
    };

    let non_terminal_results = Rc::new(RefCell::new(FollowSet::new(
        cfg.get_non_terminal_set()
            .iter()
            .fold(Vec::new(), |mut acc, nt| {
                if nt == start_symbol {
                    acc.push(
                        DomainTypeBuilder::new()
                            .k(k)
                            .max_terminal_index(max_terminal_index)
                            .end()
                            .unwrap(),
                    );
                } else {
                    acc.push(
                        DomainTypeBuilder::new()
                            .k(k)
                            .max_terminal_index(max_terminal_index)
                            .build()
                            .unwrap(),
                    );
                }
                acc
            }),
    )));

    let mut result_map = if k == 0 {
        // k == 0: No previous cache result available
        // Optimization: Pre-allocate capacity and use builder pattern more efficiently
        let mut initial_map = ResultMap::with_capacity_and_hasher(
            non_terminal_positions.len(),
            rustc_hash::FxBuildHasher,
        );

        // Optimization: Create domain type builder once and reuse pattern
        for (p, _) in non_terminal_positions.iter() {
            initial_map.insert(
                *p,
                DomainTypeBuilder::new()
                    .k(k)
                    .max_terminal_index(max_terminal_index)
                    .build()
                    .unwrap(),
            );
        }
        Rc::new(initial_map)
    } else {
        // Optimization: Avoid unnecessary cloning by using more efficient collection
        let cache_ref = follow_cache.get(k - 1, grammar_config, first_cache);
        let borrowed_cache = cache_ref.borrow();

        let mut cached = ResultMap::with_capacity_and_hasher(
            borrowed_cache.last_result.len(),
            rustc_hash::FxBuildHasher,
        );

        for (p, t) in borrowed_cache.last_result.iter() {
            cached.insert(*p, t.clone().set_k(k));
        }
        drop(borrowed_cache); // Explicitly drop borrow before creating Rc
        Rc::new(cached)
    };

    let mut iterations = 0usize;
    let mut new_result_vector;
    loop {
        #[cfg(feature = "profiling")]
        profile_scope!("iteration_step");
        new_result_vector = step_function(Rc::clone(&result_map), Rc::clone(&non_terminal_results));
        if new_result_vector == *result_map {
            // No change in the result map, we are done
            break;
        }
        result_map = Rc::new(new_result_vector);
        iterations += 1;
        trace!("Iteration number {iterations} completed");
    }

    #[cfg(feature = "profiling")]
    profiling::output_profiling_data();

    (
        new_result_vector,
        Rc::try_unwrap(non_terminal_results).unwrap().into_inner(),
    )
}

/// Arguments for the update_production_equations function
struct UpdateProductionEquationsArgs<'a, T, N> {
    /// The production number
    prod_num: usize,
    /// The production
    pr: &'a Pr,
    /// The FIRST(k) sets of non-terminals
    first_k_of_nt: Rc<RefCell<FirstSet>>,
    /// The terminal index function
    ti: Rc<T>,
    /// The non-terminal index function
    nti: Rc<N>,
    /// The k value
    k: usize,
    /// The maximum terminal index
    max_terminal_index: usize,
}

///
/// Creates functions that calculate the FOLLOW k sets for each occurrence of
/// a non-terminal in the given production and adds them to the equation system.
///
fn update_production_equations<T, N>(
    mut es: EquationSystem,
    args: UpdateProductionEquationsArgs<T, N>,
) -> EquationSystem
where
    T: TerminalIndexFn + 'static,
    N: NonTerminalIndexFn,
{
    // Optimization: Pre-allocate vector capacity and use more efficient iteration
    let pr_symbols = args.pr.get_r();
    let mut parts = Vec::<(usize, SymbolString)>::with_capacity(pr_symbols.len());

    for (i, s) in pr_symbols.iter().enumerate() {
        match s {
            // For each non-terminal create a separate SymbolString
            Symbol::N(..) => parts.push((i + 1, SymbolString(vec![s.clone()]))),
            // Stack terminals as long as possible
            Symbol::T(_) => {
                if parts.is_empty() {
                    parts.push((i + 1, SymbolString(vec![s.clone()])));
                } else {
                    let last_idx = parts.len() - 1;
                    let last_symbols = &parts[last_idx].1.0;
                    if !last_symbols.is_empty() && matches!(last_symbols.last(), Some(Symbol::T(_)))
                    {
                        // Only add to terminals - optimization: avoid repeated length calculation
                        parts[last_idx].1.0.push(s.clone());
                    } else {
                        // Create a new start of terminal list
                        parts.push((i + 1, SymbolString(vec![s.clone()])));
                    }
                }
            }
            _ => {
                unreachable!(
                    "Scanner switching directives have been removed from the grammar syntax."
                );
            }
        }
    }

    // For each non-terminal of the production (parts are separated into strings
    // of terminals and single non-terminals combined with the symbol-index) we
    // have to provide an equation.
    for (part_index, (symbol_index, symbol_string)) in parts.iter().enumerate() {
        if let Symbol::N(..) = &symbol_string.0[0] {
            let mut result_function: TransferFunction = Box::new(move |_, _| {
                DomainTypeBuilder::new()
                    .k(args.k)
                    .max_terminal_index(args.max_terminal_index)
                    .eps()
                    .unwrap()
            });
            // Optimization: Use iterator combinators more efficiently and reduce allocations
            for (_, symbol_string) in parts.iter().skip(part_index + 1) {
                let symbol = &symbol_string.0[0]; // Avoid cloning the entire symbol
                match symbol {
                    Symbol::T(_) => {
                        // Optimization: Pre-compute terminal indices to avoid repeated work
                        let terminal_indices: Vec<TerminalIndex> = symbol_string
                            .0
                            .iter()
                            .map(|s| CompiledTerminal::create(s, Rc::clone(&args.ti)).0)
                            .collect();

                        // Optimization: Pre-build the domain type to avoid repeated builder calls
                        let domain_type = DomainTypeBuilder::new()
                            .k(args.k)
                            .max_terminal_index(args.max_terminal_index)
                            .terminal_indices(&[&terminal_indices])
                            .build()
                            .unwrap();

                        result_function =
                            Box::new(move |result_map: Rc<ResultMap>, non_terminal_results| {
                                result_function(result_map, non_terminal_results)
                                    .k_concat(&domain_type, args.k)
                            });
                    }
                    Symbol::N(nt, _, _, _) => {
                        let first_k_of_nt = Rc::clone(&args.first_k_of_nt);
                        let nt_i = args.nti.non_terminal_index(nt);
                        result_function =
                            Box::new(move |result_map: Rc<ResultMap>, non_terminal_results| {
                                let borrowed_first_of_nt = first_k_of_nt.borrow();
                                // Optimization: Use get with expect for clearer error handling
                                let first_of_nt = borrowed_first_of_nt
                                    .non_terminals
                                    .get(nt_i)
                                    .expect("Non-terminal index should be valid");
                                result_function(result_map, non_terminal_results)
                                    .k_concat(first_of_nt, args.k)
                            });
                    }
                    _ => {
                        unreachable!(
                            "Scanner switching directives have been removed from the grammar syntax."
                        );
                    }
                }
            }
            let nt = args.nti.non_terminal_index(args.pr.get_n_str());
            es.insert(
                (args.prod_num, *symbol_index).into(),
                Box::new(
                    move |result_map, non_terminal_results: Rc<RefCell<FollowSet>>| {
                        let intermediate_result =
                            result_function(result_map, Rc::clone(&non_terminal_results));
                        let borrowed_nt_results = non_terminal_results.borrow();
                        // Optimization: Use expect for clearer error handling and potential compiler optimizations
                        let nt_follow_set = borrowed_nt_results
                            .non_terminals
                            .get(nt)
                            .expect("Non-terminal index should be valid");
                        intermediate_result.k_concat(nt_follow_set, args.k)
                    },
                ),
            );
        }
    }

    es
}

#[cfg(feature = "profiling")]
mod profiling {
    use rustc_hash::FxHashMap;
    use std::cell::RefCell;
    use std::time::{Duration, Instant};

    thread_local! {
        static PROFILE_DATA: RefCell<FxHashMap<&'static str, (u64, Duration)>> =
            RefCell::new(FxHashMap::default());
    }

    pub struct ProfileScope {
        name: &'static str,
        start: Instant,
    }

    impl ProfileScope {
        pub fn new(name: &'static str) -> Self {
            Self {
                name,
                start: Instant::now(),
            }
        }
    }

    impl Drop for ProfileScope {
        fn drop(&mut self) {
            let duration = self.start.elapsed();
            PROFILE_DATA.with(|data| {
                let mut map = data.borrow_mut();
                let entry = map.entry(self.name).or_insert((0, Duration::ZERO));
                entry.0 += 1;
                entry.1 += duration;
            });
        }
    }

    // Output profiling data to a file in the current working directory
    pub fn output_profiling_data() {
        use std::env;
        use std::fs::File;
        use std::io::{BufWriter, Write};

        let file_path = match env::current_dir() {
            Ok(mut path) => {
                path.push("profiling_data.txt");
                path
            }
            Err(_) => std::path::PathBuf::from("profiling_data.txt"),
        };

        let file = match File::create(&file_path) {
            Ok(f) => f,
            Err(e) => {
                eprintln!("Failed to create profiling data file: {}", e);
                return;
            }
        };
        let mut writer = BufWriter::new(file);

        PROFILE_DATA.with(|data| {
            let map = data.borrow();
            for (name, (count, duration)) in map.iter() {
                let _ = writeln!(writer, "{}: {} calls, {:?} total", name, count, duration);
            }
        });
    }
}