/// <summary> /// Inputs a character into this FSA and causes it to change state. /// </summary> /// <param name="cFood">The input character.</param> public void Feed(char cFood) { string sInput; //handle special cases like $d = all digits if (IsDigit(cFood)) { sInput = C_DIGIT_INDICATOR; } else if (IsLetter(cFood)) { sInput = C_LETTER_INDICATOR; } else if (IsSpace(cFood)) { sInput = C_SPACE_INDICATOR; } else if (cFood == '\r') { sInput = C_CARRIAGE_RETURN_INDICATOR; } else if (cFood == '\n') { sInput = C_LINEFEED_INDICATOR; } else if (cFood == '\"') { sInput = C_DBL_QUOTE_INDICATOR; } else if (cFood == '\'') { sInput = C_SGL_QUOTE_INDICATOR; } else if (cFood == ',') { sInput = C_COMMA_INDICATOR; } else if (cFood == '\t') { sInput = C_TAB_INDICATOR; } else { sInput = cFood.ToString(); //otherwise, just use character } //if the input character cannot be found in the current state's state table, //treat it as general punctuation if (!m_stCurrentState.InputStates.ContainsKey(sInput)) { sInput = C_PUNCTUATION_INDICATOR; } //remember last state, get current state, determine if a back up is needed m_stLastState = m_stCurrentState; m_stCurrentState = m_lfsAllStates[((int)m_stCurrentState.InputStates[sInput])]; bBackUp = m_stCurrentState.BackUp; }
/// <summary> /// Loads a CSV state table from a stream. /// </summary> /// <param name="srInputReader">The stream to read the state table from.</param> /// <returns>Returns an FSA containing states built from the given stream.</returns> public static FSA FromStream(StreamReader srInputReader) { FSA fsaFinal = new FSA(); string[] saStates = srInputReader.ReadLine().Split(FSAState.c_acSplitters); while (!srInputReader.EndOfStream) { fsaFinal.m_lfsAllStates.Add(FSAState.FromString(srInputReader.ReadLine(), saStates)); } srInputReader.Close(); return(fsaFinal); }
/// <summary> /// Builds a state from a string and returns it. /// </summary> /// <param name="sStateStr">The string to use when constructing the FSAState.</param> /// <returns>Returns a new FSAState containing the information found in the specified string.</returns> public static FSAState FromString(string sStateStr, string[] saStates) { FSAState fsasFinal = new FSAState(); string[] saParts = sStateStr.Split(c_acSplitters); fsasFinal.m_sDescription = saParts[0]; fsasFinal.m_bAccepting = saParts[1].Contains("#"); for (int i = 2; i < saParts.Length - 1; i++) { fsasFinal.m_htInputStates.Add(saStates[i], Int32.Parse(saParts[i]) - 1); //-1 because array starts at 0 } fsasFinal.m_bBackUp = (saParts[saParts.Length - 1] == "y"); //return the completed state return(fsasFinal); }
public void FeedEOF() { m_stLastState = m_stCurrentState; m_stCurrentState = m_lfsAllStates[((int)m_stCurrentState.InputStates[C_EOF_INDICATOR])]; bBackUp = m_stCurrentState.BackUp; }
/// <summary> /// Resets this FSA to its starting state. /// </summary> public void Reset() { m_stCurrentState = m_lfsAllStates[0]; bBackUp = false; }
/// <summary> /// Constructor. Makes a new FSA and initializes the current state to null. /// </summary> public FSA() { m_stCurrentState = null; m_lfsAllStates = new List <FSAState>(); m_stLastState = null; }
/// <summary> /// Inputs a character into this FSA and causes it to change state. /// </summary> /// <param name="cFood">The input character.</param> public void Feed(char cFood) { string sInput; //handle special cases like $d = all digits if (IsDigit(cFood)) sInput = C_DIGIT_INDICATOR; else if (IsLetter(cFood)) sInput = C_LETTER_INDICATOR; else if (IsSpace(cFood)) sInput = C_SPACE_INDICATOR; else if (cFood == '\r') sInput = C_CARRIAGE_RETURN_INDICATOR; else if (cFood == '\n') sInput = C_LINEFEED_INDICATOR; else if (cFood == '\"') sInput = C_DBL_QUOTE_INDICATOR; else if (cFood == '\'') sInput = C_SGL_QUOTE_INDICATOR; else if (cFood == ',') sInput = C_COMMA_INDICATOR; else if (cFood == '\t') sInput = C_TAB_INDICATOR; else sInput = cFood.ToString(); //otherwise, just use character //if the input character cannot be found in the current state's state table, //treat it as general punctuation if (! m_stCurrentState.InputStates.ContainsKey(sInput)) sInput = C_PUNCTUATION_INDICATOR; //remember last state, get current state, determine if a back up is needed m_stLastState = m_stCurrentState; m_stCurrentState = m_lfsAllStates[((int)m_stCurrentState.InputStates[sInput])]; bBackUp = m_stCurrentState.BackUp; }
/// <summary> /// Constructor. Makes a new FSA and initializes the current state to null. /// </summary> public FSA() { m_stCurrentState = null; m_lfsAllStates = new List<FSAState>(); m_stLastState = null; }
/// <summary> /// Builds a state from a string and returns it. /// </summary> /// <param name="sStateStr">The string to use when constructing the FSAState.</param> /// <returns>Returns a new FSAState containing the information found in the specified string.</returns> public static FSAState FromString(string sStateStr, string[] saStates) { FSAState fsasFinal = new FSAState(); string[] saParts = sStateStr.Split(c_acSplitters); fsasFinal.m_sDescription = saParts[0]; fsasFinal.m_bAccepting = saParts[1].Contains("#"); for (int i = 2; i < saParts.Length - 1; i++) fsasFinal.m_htInputStates.Add(saStates[i], Int32.Parse(saParts[i]) - 1); //-1 because array starts at 0 fsasFinal.m_bBackUp = (saParts[saParts.Length - 1] == "y"); //return the completed state return fsasFinal; }