コード例 #1
0
ファイル: Parser.cs プロジェクト: usamabintariq/NCache
        private void AddDfaState(int p_index, FAState p_fastate)
        {
            if (!m_initialized)
            {
                throw new ParserException("Table sizes not initialized");
            }

            m_DfaStates[p_index] = p_fastate;
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: usamabintariq/NCache
        /// This method implements the DFA algorithm and returns a token
        /// to the LALR state machine.
        private Token RetrieveToken()
        {
            Token   result;
            int     currentPos      = 0;
            int     lastAcceptState = -1;
            int     lastAcceptPos   = -1;
            FAState currentState    = m_DfaStates[m_initDfaState];

            try
            {
                while (true)
                {
                    // This code searches all the branches of the current DFA state for the next
                    // character in the input LookaheadStream. If found the target state is returned.
                    // The InStr() function searches the string pCharacterSetTable.Member(CharSetIndex)
                    // starting at position 1 for ch.  The pCompareMode variable determines whether
                    // the search is case sensitive.
                    int  target = -1;
                    char ch     = FixCase(m_source.LookAhead(currentPos));

                    foreach (FAEdge edge in currentState.Edges)
                    {
                        String chars = edge.Characters;
                        if (chars.IndexOf(ch) != -1)
                        {
                            target = edge.TargetIndex;
                            break;
                        }
                    }

                    // This block-if statement checks whether an edge was found from the current state.
                    // If so, the state and current position advance. Otherwise it is time to exit the main loop
                    // and report the token found (if there was it fact one). If the LastAcceptState is -1,
                    // then we never found a match and the Error Token is created. Otherwise, a new token
                    // is created using the Symbol in the Accept State and all the characters that
                    // comprise it.
                    if (target != -1)
                    {
                        // This code checks whether the target state accepts a token. If so, it sets the
                        // appropiate variables so when the algorithm is done, it can return the proper
                        // token and number of characters.
                        if (m_DfaStates[target].AcceptSymbol != -1)
                        {
                            lastAcceptState = target;
                            lastAcceptPos   = currentPos;
                        }

                        currentState = m_DfaStates[target];
                        currentPos++;
                    }
                    else
                    {
                        if (lastAcceptState == -1)
                        {
                            result      = new Token(m_errorSymbol);
                            result.Data = m_source.Read(1);
                        }
                        else
                        {
                            Symbol symbol = m_symbols[m_DfaStates[lastAcceptState].AcceptSymbol];
                            result      = new Token(symbol);
                            result.Data = m_source.Read(lastAcceptPos + 1);
                        }
                        break;
                    }
                }
            }
            catch (EndOfStreamException)
            {
                result      = new Token(m_endSymbol);
                result.Data = "";
            }

            UpdateLineNumber((String)result.Data);

            return(result);
        }
コード例 #3
0
ファイル: Parser.cs プロジェクト: usamabintariq/NCache
        private void LoadTables(GrammarReader reader)
        {
            Object obj; Int16 index;

            while (reader.MoveNext())
            {
                byte id = (byte)reader.RetrieveNext();

                switch ((RecordId)id)
                {
                case RecordId.Parameters:
                    m_parameters["Name"]    = (String)reader.RetrieveNext();
                    m_parameters["Version"] = (String)reader.RetrieveNext();
                    m_parameters["Author"]  = (String)reader.RetrieveNext();
                    m_parameters["About"]   = (String)reader.RetrieveNext();
                    m_caseSensitive         = (Boolean)reader.RetrieveNext();
                    m_startSymbol           = (Int16)reader.RetrieveNext();
                    break;

                case RecordId.TableCounts:
                    m_symbols     = new Symbol[(Int16)reader.RetrieveNext()];
                    m_charsets    = new String[(Int16)reader.RetrieveNext()];
                    m_rules       = new Rule[(Int16)reader.RetrieveNext()];
                    m_DfaStates   = new FAState[(Int16)reader.RetrieveNext()];
                    m_LalrTables  = new LRActionTable[(Int16)reader.RetrieveNext()];
                    m_initialized = true;
                    break;

                case RecordId.Initial:
                    m_initDfaState  = (Int16)reader.RetrieveNext();
                    m_initLalrState = (Int16)reader.RetrieveNext();
                    break;

                case RecordId.Symbols:
                    index = (Int16)reader.RetrieveNext();
                    String     name   = (String)reader.RetrieveNext();
                    SymbolType kind   = (SymbolType)(Int16)reader.RetrieveNext();
                    Symbol     symbol = new Symbol(index, name, kind);
                    AddSymbol(symbol);
                    break;

                case RecordId.CharSets:
                    index = (Int16)reader.RetrieveNext();
                    String charset = (String)reader.RetrieveNext();
                    AddCharset(index, charset);
                    break;

                case RecordId.Rules:
                    index = (Int16)reader.RetrieveNext();
                    Symbol head = m_symbols[(Int16)reader.RetrieveNext()];
                    Rule   rule = new Rule(index, head);

                    reader.RetrieveNext();                      // reserved
                    while ((obj = reader.RetrieveNext()) != null)
                    {
                        rule.AddItem(m_symbols[(Int16)obj]);
                    }

                    AddRule(rule);
                    break;

                case RecordId.DFAStates:
                    FAState fastate = new FAState();
                    index = (Int16)reader.RetrieveNext();

                    if ((bool)reader.RetrieveNext())
                    {
                        fastate.AcceptSymbol = (Int16)reader.RetrieveNext();
                    }
                    else
                    {
                        reader.RetrieveNext();
                    }

                    reader.RetrieveNext();                      // reserverd

                    while (!reader.RetrieveDone())
                    {
                        Int16 ci = (Int16)reader.RetrieveNext();
                        Int16 ti = (Int16)reader.RetrieveNext();
                        reader.RetrieveNext();                          // reserved
                        fastate.AddEdge(m_charsets[ci], ti);
                    }

                    AddDfaState(index, fastate);
                    break;

                case RecordId.LRTables:
                    LRActionTable table = new LRActionTable();
                    index = (Int16)reader.RetrieveNext();
                    reader.RetrieveNext();                      // reserverd

                    while (!reader.RetrieveDone())
                    {
                        Int16 sid    = (Int16)reader.RetrieveNext();
                        Int16 action = (Int16)reader.RetrieveNext();
                        Int16 tid    = (Int16)reader.RetrieveNext();
                        reader.RetrieveNext();                          // reserved
                        table.AddItem(m_symbols[sid], (Action)action, tid);
                    }

                    AddLalrTable(index, table);
                    break;

                case RecordId.Comment:
                    Console.WriteLine("Comment record encountered");
                    break;

                default:
                    throw new ParserException("Wrong id for record");
                }
            }
        }
コード例 #4
0
ファイル: Parser.cs プロジェクト: javithalion/NCache
		private void LoadTables(GrammarReader reader)
		{
			Object obj; Int16 index;
			while (reader.MoveNext())
			{
				byte id = (byte)reader.RetrieveNext();

				switch ((RecordId)id)
				{
				case RecordId.Parameters:
					m_parameters["Name"] 	= (String)reader.RetrieveNext();
					m_parameters["Version"]	= (String)reader.RetrieveNext();
					m_parameters["Author"]	= (String)reader.RetrieveNext();
					m_parameters["About"]	= (String)reader.RetrieveNext();
					m_caseSensitive			= (Boolean)reader.RetrieveNext();
					m_startSymbol			= (Int16)reader.RetrieveNext();
					break;

				case RecordId.TableCounts:
					m_symbols		= new Symbol[(Int16)reader.RetrieveNext()];
					m_charsets		= new String[(Int16)reader.RetrieveNext()];
					m_rules			= new Rule[(Int16)reader.RetrieveNext()];
					m_DfaStates		= new FAState[(Int16)reader.RetrieveNext()];
					m_LalrTables	= new LRActionTable[(Int16)reader.RetrieveNext()];
					m_initialized = true;
					break;
					
				case RecordId.Initial:
					m_initDfaState		= (Int16)reader.RetrieveNext();
					m_initLalrState		= (Int16)reader.RetrieveNext();
					break;
					
				case RecordId.Symbols:
					index				= (Int16)reader.RetrieveNext();
					String name			= (String)reader.RetrieveNext();
					SymbolType kind		= (SymbolType)(Int16)reader.RetrieveNext();
					Symbol symbol = new Symbol(index, name, kind);
					AddSymbol(symbol);
					break;
					
				case RecordId.CharSets:
					index 				= (Int16)reader.RetrieveNext();
					String charset		= (String)reader.RetrieveNext();
					AddCharset(index, charset);
					break;
					
				case RecordId.Rules:
					index				= (Int16)reader.RetrieveNext();
					Symbol head			= m_symbols[(Int16)reader.RetrieveNext()];
					Rule rule = new Rule(index, head);

					reader.RetrieveNext();	// reserved
					while ((obj = reader.RetrieveNext()) != null)
						rule.AddItem(m_symbols[(Int16)obj]);
						
					AddRule(rule);
					break;
					
				case RecordId.DFAStates:
					FAState fastate = new FAState();
					index	= (Int16)reader.RetrieveNext();

					if ((bool)reader.RetrieveNext())
						fastate.AcceptSymbol	= (Int16)reader.RetrieveNext();
					else
						reader.RetrieveNext();
						
					reader.RetrieveNext();	// reserverd					

					while (! reader.RetrieveDone())
					{
						Int16 ci = (Int16)reader.RetrieveNext();
						Int16 ti = (Int16)reader.RetrieveNext();
						reader.RetrieveNext();	// reserved
						fastate.AddEdge(m_charsets[ci], ti);
					}
					
					AddDfaState(index, fastate);
					break;
					
				case RecordId.LRTables:
					LRActionTable table = new LRActionTable();
					index = (Int16)reader.RetrieveNext();
					reader.RetrieveNext();	// reserverd
					
					while (! reader.RetrieveDone())
					{
						Int16 sid		= (Int16)reader.RetrieveNext();
						Int16 action	= (Int16)reader.RetrieveNext();
						Int16 tid		= (Int16)reader.RetrieveNext();
						reader.RetrieveNext();	// reserved
						table.AddItem(m_symbols[sid], (Action)action, tid);
					}
					
					AddLalrTable(index, table);
					break;
					
				case RecordId.Comment:
					Console.WriteLine("Comment record encountered");
					break;
					
				default:
					throw new ParserException("Wrong id for record");
				}
			}
		}
コード例 #5
0
ファイル: Parser.cs プロジェクト: javithalion/NCache
		private void AddDfaState(int p_index, FAState p_fastate)
		{
			if (! m_initialized)
				throw new ParserException("Table sizes not initialized");

			m_DfaStates[p_index] = p_fastate;
		}