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
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};
pub struct Usefulness<G> {
grammar: G,
reachability: BitMatrix,
reachable_syms: BitVec,
productivity: BitVec,
all_useful: bool,
all_productive: bool,
}
pub struct UselessRules<'a, G, R>
where G: 'a
{
rules: R,
usefulness: &'a Usefulness<&'a mut G>,
}
#[derive(Clone, Debug)]
pub struct UselessRule<R> {
pub rule: R,
pub unreachable: bool,
pub unproductive: bool,
}
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
}
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>
{
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,
}
}
pub fn productivity(&self, sym: Symbol) -> bool {
self.productivity[sym.usize()]
}
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
}
pub fn all_useful(&self) -> bool {
self.all_useful
}
pub fn all_productive(&self) -> bool {
self.all_productive
}
}
impl<'a, G> Usefulness<&'a mut G>
where G: ContextFree,
&'a G: ContextFreeRef<'a, Target = G>,
&'a mut G: ContextFreeMut<'a, Target = G>
{
pub fn useless_rules(&'a self) -> UselessRules<'a, G, <&'a G as ContextFreeRef<'a>>::Rules> {
UselessRules {
rules: self.grammar.rules(),
usefulness: self,
}
}
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
}
}