Exemplo n.º 1
0
		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");
				}
			}
		}
Exemplo n.º 2
0
		/// <summary>Produces a reduction.</summary>
		/// <remarks>Removes as many tokens as members in the rule and pushes a 
		///          non-terminal token.</remarks>
		private ParseResult Reduce(Rule p_rule)
		{
			ParseResult result;
			Token head;
			
			if (m_trimReductions && p_rule.ContainsOneNonTerminal)
			{
				// The current rule only consists of a single nonterminal and can be trimmed from the
				// parse tree. Usually we create a new Reduction, assign it to the Data property
				// of Head and push it on the stack. However, in this case, the Data property of the
				// Head will be assigned the Data property of the reduced token (i.e. the only one
				// on the stack). In this case, to save code, the value popped of the stack is changed 
				// into the head.
				head = m_tempStack.PopToken();
				head.SetParent(p_rule.RuleNonTerminal);
				
				result = ParseResult.ReduceEliminated;
			}
			else
			{
				Reduction reduction = new Reduction();
				reduction.ParentRule = p_rule;
				
				m_tempStack.PopTokensInto(reduction, p_rule.SymbolCount);

				head = new Token();
				head.Data = reduction;
				head.SetParent(p_rule.RuleNonTerminal);
				
				m_haveReduction = true;
				result = ParseResult.ReduceNormal;
			}
			
			int index = m_tempStack.PeekToken().State;
			LRAction action = m_LalrTables[index].GetActionForSymbol(p_rule.RuleNonTerminal.TableIndex);
			
			if (action != null)
			{
				head.State = m_LalrState = action.Value;;
				m_tempStack.PushToken(head);
			}
			else
				throw new ParserException("Action for LALR state is null");
				
			return result;
		}
Exemplo n.º 3
0
		private void AddRule(Rule p_rule)
		{
			if (! m_initialized)
				throw new ParserException("Table sizes not initialized");

			int index = p_rule.TableIndex;
			m_rules[index] = p_rule;
		}