Exemple #1
0
    protected static void AssertTransitions(IReadOnlyNfa <string, char> nfa, string from, char on, string[] tos)
    {
        var expectedTos = tos.ToHashSet();
        var gotTos      = nfa.GetTransitions(from, on);

        Assert.True(expectedTos.SetEquals(gotTos));
    }
Exemple #2
0
    protected static void AssertTransitions(IReadOnlyNfa <StateSet <string>, char> nfa, string from, char on, string[] tos)
    {
        var fromSet = ParseStateSet(from);
        var toSet   = tos.Select(ParseStateSet).ToHashSet();

        var gotTos = nfa.GetTransitions(fromSet, on);

        Assert.True(toSet.SetEquals(gotTos));
    }
Exemple #3
0
    /// <summary>
    /// Implements <see cref="IReadOnlyFiniteAutomaton{TState, TSymbol}.Accepts(IEnumerable{TSymbol})"/> for NFAs.
    /// </summary>
    /// <typeparam name="TState">The state type.</typeparam>
    /// <typeparam name="TSymbol">The symbol type.</typeparam>
    /// <param name="nfa">The NFA to check acceptance on.</param>
    /// <param name="input">The input to check acceptance for.</param>
    /// <returns>True, if the <paramref name="input"/> is accepted by <paramref name="nfa"/>.</returns>
    public static bool Accepts <TState, TSymbol>(IReadOnlyNfa <TState, TSymbol> nfa, IEnumerable <TSymbol> input)
    {
        var currentState = new StateSet <TState>(nfa.InitialStates, nfa.StateComparer);

        foreach (var symbol in input)
        {
            currentState = nfa.GetTransitions(currentState, symbol);
            if (currentState.Count == 0)
            {
                return(false);
            }
        }
        return(currentState.Overlaps(nfa.AcceptingStates));
    }