예제 #1
0
    public static Bimachine PseudoMinimal(this Bimachine bm)
    {
        var leftProfiles = bm.Output
                           .GroupBy(
            o => o.Key.Lstate,
            o => (Symbol: o.Key.Symbol, State: o.Key.Rstate, Word: o.Value))
                           .ToDictionary(g => g.Key, g => g.ToList());

        var rightProfiles = bm.Output
                            .GroupBy(
            o => o.Key.Rstate,
            o => (Symbol: o.Key.Symbol, State: o.Key.Lstate, Word: o.Value))
                            .ToDictionary(g => g.Key, g => g.ToList());

        var alphabet   = bm.Left.Transitions.Select(t => t.Key.Label).Distinct();
        var leftEqRel  = FindBmDfaEqRel(bm.Left, leftProfiles, alphabet);
        var rightEqRel = FindBmDfaEqRel(bm.Right, rightProfiles, alphabet);

        var leftMinDfaTrans = bm.Left.Transitions
                              .Select(t => (Key: (leftEqRel[t.Key.From], t.Key.Label), Value: leftEqRel[t.Value]))
                              .Distinct()
                              .ToDictionary(p => p.Key, p => p.Value);

        var leftMinDfa = new Dfsa(
            bm.Left.States.Select(s => leftEqRel[s]).Distinct(),
            leftEqRel[bm.Left.Initial],
            Array.Empty <int>(),
            leftMinDfaTrans);

        var rightMinDfaTrans = bm.Right.Transitions
                               .Select(t => (Key: (rightEqRel[t.Key.From], t.Key.Label), Value: rightEqRel[t.Value]))
                               .Distinct()
                               .ToDictionary(p => p.Key, p => p.Value);

        var rightMinDfa = new Dfsa(
            bm.Right.States.Select(s => rightEqRel[s]).Distinct(),
            rightEqRel[bm.Right.Initial],
            Array.Empty <int>(),
            rightMinDfaTrans);

        var minBmOutput = bm.Output
                          .Select(x => (Key: (leftEqRel[x.Key.Lstate], x.Key.Symbol, rightEqRel[x.Key.Rstate]), Value: x.Value))
                          .Distinct()
                          .ToDictionary(p => p.Key, p => p.Value);

        return(new Bimachine(leftMinDfa, rightMinDfa, minBmOutput));
    }
예제 #2
0
 Lexer(Bimachine bm, IList <Rule> grammar)
 {
     this.Bm      = bm;
     this.grammar = grammar;
 }