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
//! Analysis of rule usefulness.

use bit_matrix::BitMatrix;
use bit_vec::BitVec;

use analysis::{self, RhsClosure};
use grammar::{ContextFree, ContextFreeRef, ContextFreeMut};
use rule::GrammarRule;
use rule::container::RuleContainer;
use symbol::{Symbol, SymbolBitSet};

/// Contains the information about usefulness of the grammar's rules.
/// Useful rules are both reachable and productive.
pub struct Usefulness<G> {
    grammar: G,
    reachability: BitMatrix,
    reachable_syms: BitVec,
    productivity: BitVec,
    all_useful: bool,
    all_productive: bool,
}

/// An iterator over the grammar's useless rules.
pub struct UselessRules<'a, G, R>
    where G: 'a
{
    rules: R,
    usefulness: &'a Usefulness<&'a mut G>,
}

/// A reference to a useless rule, together with the reason for its uselessness.
#[derive(Clone, Debug)]
pub struct UselessRule<R> {
    /// Reference to a rule.
    pub rule: R,
    /// Indicates whether the rule is unreachable.
    pub unreachable: bool,
    /// Indicates whether the rule is unproductive.
    pub unproductive: bool,
}

/// Returns the set of used symbols.
fn used_syms<'a, G>(grammar: &'a G) -> BitVec
    where G: ContextFree,
          &'a G: ContextFreeRef<'a, Target = G>
{
    let num_syms = grammar.sym_source().num_syms();
    let mut used_syms = BitVec::from_elem(num_syms, false);

    for rule in grammar.rules() {
        used_syms.set(rule.lhs().usize(), true);
        for &sym in rule.rhs() {
            used_syms.set(sym.usize(), true);
        }
    }
    used_syms
}

/// Returns the set of productive symbols.
fn productive_syms<'a, G>(grammar: &'a G) -> BitVec
    where G: ContextFree,
          &'a G: ContextFreeRef<'a, Target = G>
{
    let mut productive_syms = SymbolBitSet::terminal_or_nulling_set(&grammar).into_bit_vec();
    RhsClosure::new(grammar).rhs_closure(&mut productive_syms);
    productive_syms
}

impl<'a, G> Usefulness<&'a mut G>
    where G: ContextFree,
          for<'b> &'b G: ContextFreeRef<'b, Target = G>,
          for<'b> &'b mut G: ContextFreeMut<'b, Target = G>
{
    /// Analyzes usefulness of the grammar's rules. In particular, it checks for reachable
    /// and productive symbols.
    pub fn new(grammar: &'a mut G) -> Usefulness<&'a mut G> {
        let mut productivity = productive_syms(grammar);
        let reachability = analysis::reachability_matrix(grammar);
        let used_syms = used_syms(grammar);
        let mut reachable_syms = BitVec::from_elem(grammar.sym_source().num_syms(), false);

        unsafe {
            for ((productive, reachable), &used) in productivity.storage_mut()
                                                                .iter_mut()
                                                                .zip(reachable_syms.storage_mut()
                                                                                   .iter_mut())
                                                                .zip(used_syms.storage().iter()) {
                *productive |= !used;
                *reachable |= !used;
            }
        }

        let all_productive = productivity.storage().iter().all(|&productive| productive == !0);

        Usefulness {
            grammar: grammar,
            productivity: productivity,
            reachability: reachability,
            reachable_syms: reachable_syms,
            all_useful: false,
            all_productive: all_productive,
        }
    }

    /// Checks whether a symbol is productive. Can be used to determine the precise reason
    /// of a rule's unproductiveness.
    pub fn productivity(&self, sym: Symbol) -> bool {
        self.productivity[sym.usize()]
    }

    /// Sets symbol reachability. Takes an array of reachable symbols.
    pub fn reachable<Sr>(mut self, syms: Sr) -> Self
        where Sr: AsRef<[Symbol]>
    {
        for &sym in syms.as_ref().iter() {
            let reachability = self.reachability[sym.usize()].iter();
            unsafe {
                for (dst, &src) in self.reachable_syms.storage_mut().iter_mut().zip(reachability) {
                    *dst |= src;
                }
            }
        }
        self.all_useful = self.all_productive &
                          self.reachable_syms.storage().iter().all(|&reachable| reachable == !0);
        self
    }

    /// Checks whether all rules in the grammar are useful.
    pub fn all_useful(&self) -> bool {
        self.all_useful
    }

    /// Checks whether all rules in the grammar are productive.
    pub fn all_productive(&self) -> bool {
        self.all_productive
    }
}

// Watch out: Normal type bounds conflict with HRTB.
impl<'a, G> Usefulness<&'a mut G>
    where G: ContextFree,
          &'a G: ContextFreeRef<'a, Target = G>,
          &'a mut G: ContextFreeMut<'a, Target = G>
{
    /// Returns an iterator over the grammar's useless rules.
    pub fn useless_rules(&'a self) -> UselessRules<'a, G, <&'a G as ContextFreeRef<'a>>::Rules> {
        UselessRules {
            rules: self.grammar.rules(),
            usefulness: self,
        }
    }

    /// Removes useless rules. The language represented by the grammar doesn't change.
    pub fn remove_useless_rules(&mut self) {
        if !self.all_useful {
            let productivity = &self.productivity;
            let reachable_syms = &self.reachable_syms;
            self.grammar.retain(|lhs, rhs, _| {
                let productive = rhs.iter().all(|sym| productivity[sym.usize()]);
                let reachable = reachable_syms[lhs.usize()];
                productive && reachable
            });
        }
    }
}

impl<'a, G> Iterator for UselessRules<'a, G, <&'a G as ContextFreeRef<'a>>::Rules> where
            G: ContextFree + 'a,
            &'a G: ContextFreeRef<'a, Target=G> {
    type Item = UselessRule<<<&'a G as ContextFreeRef<'a>>::Rules as Iterator>::Item>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.usefulness.all_useful {
            return None;
        }

        for rule in &mut self.rules {
            let lhs = rule.lhs().usize();
            let usefulness = &self.usefulness;
            let productive = rule.rhs().iter().all(|sym| usefulness.productivity[sym.usize()]);
            let reachable = usefulness.reachable_syms[lhs];

            if !reachable || !productive {
                return Some(UselessRule {
                    rule: rule,
                    unreachable: !reachable,
                    unproductive: !productive,
                });
            }
        }

        None
    }
}