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
use bit_matrix::BitMatrix;

use ContextFree;
use ContextFreeRef;
use rule::GrammarRule;

/// Returns the direct derivation matrix.
pub fn direct_derivation_matrix<'a, G>(grammar: &'a G) -> BitMatrix
    where G: ContextFree,
          &'a G: ContextFreeRef<'a, Target = G>
{
    let num_syms = grammar.sym_source().num_syms();
    let mut derivation = BitMatrix::new(num_syms, num_syms);

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

/// Returns the derivation matrix.
pub fn reachability_matrix<'a, G>(grammar: &'a G) -> BitMatrix
    where G: ContextFree,
          &'a G: ContextFreeRef<'a, Target = G>
{
    let mut result = direct_derivation_matrix(grammar);
    result.transitive_closure();
    result.reflexive_closure();
    result
}

/// Returns the unit derivation matrix.
pub fn unit_derivation_matrix<'a, G>(grammar: &'a G) -> BitMatrix
    where G: ContextFree,
          &'a G: ContextFreeRef<'a, Target = G>
{
    let num_syms = grammar.num_syms();
    let mut unit_derivation = BitMatrix::new(num_syms, num_syms);

    for rule in grammar.rules() {
        // A rule of form `A ::= A` is not a cycle. We can represent unit rules in the form of
        // a directed graph. The rule `A ::= A` is then presented as a self-loop. Self-loops
        // aren't cycles.
        if rule.rhs().len() == 1 && rule.lhs() != rule.rhs()[0] {
            unit_derivation.set(rule.lhs().into(), rule.rhs()[0].into(), true);
        }
    }

    unit_derivation.transitive_closure();
    unit_derivation
}