cfg_predict/last.rs
1//! FIRST sets.
2
3use cfg_grammar::RuleContainer;
4
5use super::FirstSets;
6use super::PerSymbolSets;
7use super::PredictSets;
8
9/// FIRST sets.
10pub struct LastSets {
11 map: PerSymbolSets,
12}
13
14impl LastSets {
15 /// Compute all LAST sets of the grammar.
16 ///
17 /// We define a binary relation LAST(N, S), in which N is related to S
18 /// if the grammar has a production of the form `N ⸬= α S β`, where
19 /// β is a nullable string of symbols.
20 ///
21 /// We compute the transitive closure of this relation.
22 pub fn new<G>(grammar: &G) -> Self
23 where
24 G: RuleContainer + Default,
25 {
26 let reversed_grammar = grammar.reverse();
27 let map = {
28 let first_sets = FirstSets::new(&reversed_grammar);
29 // E0597: `reversed_grammar` does not live long enough
30 // label: borrowed value does not live long enough
31 first_sets.map
32 };
33 LastSets { map }
34 }
35}
36
37impl PredictSets for LastSets {
38 /// Returns a reference to FIRST sets.
39 fn predict_sets(&self) -> &PerSymbolSets {
40 &self.map
41 }
42}