private void AddLalrTable(int p_index, LRActionTable p_table) { if (!m_initialized) { throw new ParserException("Table counts not initialized"); } m_LalrTables[p_index] = p_table; }
/// This function analyzes a token and either: /// 1. Makes a SINGLE reduction and pushes a complete Reduction object on the stack /// 2. Accepts the token and shifts /// 3. Errors and places the expected symbol indexes in the Tokens list /// The Token is assumed to be valid and WILL be checked private ParseResult ParseToken(Token p_token) { ParseResult result = ParseResult.InternalError; LRActionTable table = m_LalrTables[m_LalrState]; LRAction action = table.GetActionForSymbol(p_token.TableIndex); if (action != null) { m_haveReduction = false; m_outputTokens.Clear(); switch (action.Action) { case Action.Accept: m_haveReduction = true; result = ParseResult.Accept; break; case Action.Shift: p_token.State = m_LalrState = action.Value; m_tempStack.PushToken(p_token); result = ParseResult.Shift; break; case Action.Reduce: result = Reduce(m_rules[action.Value]); break; } } else { // syntax error - fill expected tokens. m_outputTokens.Clear(); foreach (LRAction a in table.Members) { SymbolType kind = a.Symbol.Kind; if (kind == SymbolType.Terminal || kind == SymbolType.End) { m_outputTokens.PushToken(new Token(a.Symbol)); } } result = ParseResult.SyntaxError; } return(result); }
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"); } } }