Beispiel #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));
    }
Beispiel #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));
    }
Beispiel #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));
    }
Beispiel #4
0
 /// <summary>
 /// Gets all transitions that are valid from a set of states on an input.
 /// </summary>
 /// <typeparam name="TState">The state type.</typeparam>
 /// <typeparam name="TSymbol">The symbol type.</typeparam>
 /// <param name="nfa">The NFA to get the transitions for.</param>
 /// <param name="from">The set of states to transition from.</param>
 /// <param name="on">The symbol to transition on.</param>
 /// <returns>The set of states that are valid from the starting set on the input.</returns>
 public static StateSet <TState> GetTransitions <TState, TSymbol>(
     this IReadOnlyNfa <TState, TSymbol> nfa,
     StateSet <TState> from,
     TSymbol on) => new(from.SelectMany(s => nfa.GetTransitions(s, on)), nfa.StateComparer);