public override bool Equals(object obj) { MultiState <T> state = obj as MultiState <T>; if (state == null) { return(false); } return(state.SetEquals(this)); }
public String GetString(MultiState <T> state) { if (state.Count == 0) { return(this.Empty); } StringBuilder builder = new StringBuilder(); bool isFirst = true; IOrderedEnumerable <T> enumerator = state.OrderBy((t) => t); foreach (T subState in enumerator) { if (!isFirst) { builder.Append(Glue); } isFirst = false; builder.Append(subState); } return(builder.ToString()); }
private static DFA <MultiState <T>, S> GrammarConvertToDFA(Grammar <T, S> grammar, MultiState <T> startState, IMultiStateView <T> view, S epsilon) { DFA <MultiState <T>, S> dfa = new DFA <MultiState <T>, S>(grammar.Alphabet); HashSet <MultiState <T> > todo = new HashSet <MultiState <T> >(); HashSet <MultiState <T> > done = new HashSet <MultiState <T> >(); MultiState <T> newStartState = new MultiState <T>(view); foreach (T subStartState in startState) { newStartState.Add(subStartState); HashSet <T> otherStates = getAllEpsilonStatesFromState(grammar, subStartState, epsilon); foreach (T otherState in otherStates) { newStartState.Add(otherState); } } startState = newStartState; todo.Add(startState); //While there are items that needs to be progressed while (todo.Count > 0) { MultiState <T> from = todo.First <MultiState <T> >(); Dictionary <S, MultiState <T> > states = new Dictionary <S, MultiState <T> >(); foreach (T part in from) { Dictionary <S, HashSet <T> > partStates = grammar.GetStates(part); foreach (S symbol in grammar.Alphabet) { if (!states.ContainsKey(symbol)) { states.Add(symbol, new MultiState <T>(view)); } if (partStates.ContainsKey(symbol)) { HashSet <T> partToStates = partStates[symbol]; foreach (T partToState in partToStates) { states[symbol].Add(partToState); foreach (T epsilonState in getAllEpsilonStatesFromState(grammar, partToState, epsilon)) { states[symbol].Add(epsilonState); } } } } } foreach (S symbol in grammar.Alphabet) { dfa.AddTransition(from, states[symbol], symbol); if (!done.Contains(states[symbol])) { //If the to state is 'empty' if (states[symbol].Count == 0) { //Add fail state done.Add(states[symbol]); foreach (S s in grammar.Alphabet) { dfa.AddTransition(states[symbol], s); } } else { todo.Add(states[symbol]); } } } todo.Remove(from); done.Add(from); } foreach (MultiState <T> state in dfa.GetStates()) { foreach (T partState in state) { if (grammar.EndStates.Contains(partState)) { dfa.EndStates.Add(state); } } } dfa.StartState = startState; return(dfa); }
//NDFA --> Regram public static Regram <MultiState <T>, S> ConvertToRegram(NDFA <T, S> ndfa, IMultiStateView <T> view) { Regram <MultiState <T>, S> regram = new Regram <MultiState <T>, S>(ndfa.Alphabet); MultiState <T> startState = new MultiState <T>(view); HashSet <MultiState <T> > todo = new HashSet <MultiState <T> >(); HashSet <MultiState <T> > done = new HashSet <MultiState <T> >(); foreach (T subStartState in ndfa.StartStates) { startState.Add(subStartState); HashSet <T> otherStates = getAllEpsilonStatesFromState(ndfa, subStartState, ndfa.Epsilon); foreach (T otherState in otherStates) { startState.Add(otherState); } } todo.Add(startState); //While there are items that needs to be progressed while (todo.Count > 0) { MultiState <T> from = todo.First <MultiState <T> >(); foreach (T part in from) { if (isRegramStateValid(ndfa, part)) { regram.AddState(from); } Dictionary <S, HashSet <T> > partStates = ndfa.GetStates(part); foreach (S symbol in partStates.Keys) { if (symbol.Equals(ndfa.Epsilon)) { continue; } HashSet <T> partToStates = partStates[symbol]; foreach (T partToState in partToStates) { MultiState <T> newState = new MultiState <T>(view); newState.Add(partToState); if (isRegramStateValid(ndfa, partToState) || ndfa.EndStates.Contains(partToState)) { regram.AddTransition(from, newState, symbol); } if (!done.Contains(newState)) { todo.Add(newState); } foreach (T epsilonState in getAllEpsilonStatesFromState(ndfa, partToState, ndfa.Epsilon)) { MultiState <T> newEpsilonState = new MultiState <T>(view); newEpsilonState.Add(epsilonState); if (isRegramStateValid(ndfa, epsilonState) || ndfa.EndStates.Contains(epsilonState)) { regram.AddTransition(from, newEpsilonState, symbol); } if (!done.Contains(newEpsilonState)) { todo.Add(newEpsilonState); } } } } } todo.Remove(from); done.Add(from); } foreach (MultiState <T> state in regram.GetStates().Where((t) => t.Any((a) => ndfa.EndStates.Contains(a)))) { regram.EndStates.Add(state); } regram.StartState = startState; return(regram); }