/// <summary> /// Adds a transition to the specified state, on the specified character. /// </summary> /// <param name="character">The character on which to transition</param> /// <param name="destState">The destination state to which this state transitions</param> /// <returns>True if this transition could be added (the character is in the parent /// DFA's alphabet, and a transition for this character did not exist, else False.</returns> public bool AddTransition(char character, DFA_State destState) { if (false == this.Parent.Alphabet.Contains(character)) { return(false); } if (this.Transitions.ContainsKey(character)) { return(false); } this.Transitions.Add(character, destState); return(true); }
/// <summary> /// Steps on the specified character and determines whether this is a valid /// transition /// </summary> /// <param name="character">The character on which to transition</param> /// <returns>Whether this step was valid</returns> public bool StepOn(char character) { DFA_State nextState = this.CurrentState.TransitionOn(character); this.CurrentState = nextState; if (null == this.CurrentState) { return(false); } else { return(true); } }
/// <summary> /// Parses the accept states from the given string /// </summary> /// <param name="accept_string"></param> private void _parseAcceptStates(string accept_string) { StringBuilder sb = new StringBuilder(); HashSet <DFA_State> acceptStates = new HashSet <DFA_State>(); foreach (char c in accept_string) { if (c == ',') { DFA_State newAccept = this.getStateBy(sb.ToString()); if (false == acceptStates.Add(newAccept)) { throw new ArgumentException("Illegal accept state string"); } } else { sb.Append(c); } } this.AcceptStates = acceptStates; }
/// <summary> /// Parses the transitions from the given string /// </summary> /// <param name="transition_string"></param> private void _parseTransitions(string transition_string) { StringBuilder sb = new StringBuilder(); DFA_State workingOnState = null; DFA_State transDest = null; char transChar = Char.MinValue; char prevChar = Char.MinValue; foreach (char c in transition_string) { if (c == ':') { workingOnState = this.getStateBy(sb.ToString()); if (workingOnState == null) { throw new ArgumentException("Specified transition for nonexistent state"); } sb.Clear(); } else if (c == ',') { if (prevChar == Char.MinValue) { throw new ArgumentException("Illegal transition string"); } transChar = prevChar; sb.Clear(); } else if (c == ';') { transDest = this.getStateBy(sb.ToString()); if ((null == workingOnState) || (null == transDest) || (char.MinValue == prevChar)) { throw new ArgumentException("Illegal transition string"); } workingOnState.AddTransition(transChar, transDest); sb.Clear(); } else if (c == '.') { transDest = this.getStateBy(sb.ToString()); if ((null == workingOnState) || (null == transDest) || (char.MinValue == prevChar)) { throw new ArgumentException("Illegal transition string"); } workingOnState.AddTransition(transChar, transDest); workingOnState = null; sb.Clear(); } else { sb.Append(c); } prevChar = c; } }