/// <summary> /// Read LR state information. /// </summary> private void ReadLRStates() { int index = ReadInt16Entry(); ReadEmptyEntry(); LRStateAction[] stateTable = new LRStateAction[m_entryCount / 4]; for (int i = 0; i < stateTable.Length; i++) { Symbol symbol = m_symbolTable[ReadInt16Entry()]; LRAction action = (LRAction)ReadInt16Entry(); int targetIndex = ReadInt16Entry(); ReadEmptyEntry(); stateTable[i] = new LRStateAction(i, symbol, action, targetIndex); } // Create the transition vector LRStateAction[] transitionVector = new LRStateAction[m_symbolTable.Length]; for (int i = 0; i < transitionVector.Length; i++) { transitionVector[i] = null; } for (int i = 0; i < stateTable.Length; i++) { transitionVector[stateTable[i].Symbol.Index] = stateTable[i]; } LRState lrState = new LRState(index, stateTable, transitionVector); m_lrStateTable[index] = lrState; }
/// <summary> /// Creates a new instance of the <c>LRState</c> class /// </summary> /// <param name="index">Index of the LR state in the LR state table.</param> /// <param name="actions">List of all available LR actions in this state.</param> /// <param name="transitionVector">Transition vector which has symbol index as an index.</param> public LRState(int index, LRStateAction[] actions, LRStateAction[] transitionVector) { m_index = index; m_actions = actions; m_transitionVector = transitionVector; }
private TokenParseResult ParseToken() { LRStateAction stateAction = m_lrState.m_transitionVector[m_token.m_symbol.m_index]; if (stateAction != null) { //Work - shift or reduce if (m_reductionCount > 0) { int newIndex = m_lrStackIndex - m_reductionCount; m_lrStack[newIndex] = m_lrStack[m_lrStackIndex]; m_lrStackIndex = newIndex; } m_reductionCount = Undefined; switch (stateAction.Action) { case LRAction.Accept: m_reductionCount = 0; return(TokenParseResult.Accept); case LRAction.Shift: m_lrState = m_grammar.m_lrStateTable[stateAction.m_value]; LRStackItem nextToken = new LRStackItem(); nextToken.m_token = m_token; nextToken.m_state = m_lrState; if (m_lrStack.Length == ++m_lrStackIndex) { LRStackItem[] larger_m_lrStack = new LRStackItem[m_lrStack.Length + MinimumLRStackSize]; Array.Copy(m_lrStack, larger_m_lrStack, m_lrStack.Length); m_lrStack = larger_m_lrStack; } m_lrStack[m_lrStackIndex] = nextToken; return(TokenParseResult.Shift); case LRAction.Reduce: //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token int ruleIndex = stateAction.m_value; Rule currentRule = m_grammar.m_ruleTable[ruleIndex]; //======== Create Reduction LRStackItem head; TokenParseResult parseResult; LRState nextState; if (m_trimReductions && currentRule.m_hasOneNonTerminal) { //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_lrStack[m_lrStackIndex]; head.m_token.m_symbol = currentRule.m_nonTerminal; head.m_token.m_text = null; parseResult = TokenParseResult.ReduceEliminated; //========== Goto nextState = m_lrStack[m_lrStackIndex - 1].m_state; } else { //Build a Reduction head = new LRStackItem(); head.m_rule = currentRule; head.m_token.m_symbol = currentRule.m_nonTerminal; head.m_token.m_text = null; m_reductionCount = currentRule.m_symbols.Length; parseResult = TokenParseResult.ReduceNormal; //========== Goto nextState = m_lrStack[m_lrStackIndex - m_reductionCount].m_state; } //========= If nextAction is null here, then we have an Internal Table Error!!!! LRStateAction nextAction = nextState.m_transitionVector[currentRule.m_nonTerminal.m_index]; if (nextAction != null) { m_lrState = m_grammar.m_lrStateTable[nextAction.m_value]; head.m_state = m_lrState; if (parseResult == TokenParseResult.ReduceNormal) { if (m_lrStack.Length == ++m_lrStackIndex) { LRStackItem[] larger_m_lrStack = new LRStackItem[m_lrStack.Length + MinimumLRStackSize]; Array.Copy(m_lrStack, larger_m_lrStack, m_lrStack.Length); m_lrStack = larger_m_lrStack; } m_lrStack[m_lrStackIndex] = head; } else { m_lrStack[m_lrStackIndex] = head; } return(parseResult); } else { return(TokenParseResult.InternalError); } } } //=== Syntax Error! Fill Expected Tokens m_expectedTokens = new Symbol[m_lrState.ActionCount]; int length = 0; for (int i = 0; i < m_lrState.ActionCount; i++) { switch (m_lrState.GetAction(i).Symbol.SymbolType) { case SymbolType.Terminal: case SymbolType.End: m_expectedTokens[length++] = m_lrState.GetAction(i).Symbol; break; } } if (length < m_expectedTokens.Length) { Symbol[] newArray = new Symbol[length]; Array.Copy(m_expectedTokens, newArray, length); m_expectedTokens = newArray; } return(TokenParseResult.SyntaxError); }
/// <summary> /// Creates a new instance of the <c>LRState</c> class /// </summary> /// <param name="index">Index of the LR state in the LR state table.</param> /// <param name="actions">List of all available LR actions in this state.</param> /// <param name="transitionVector">Transition vector which has symbol index as an index.</param> public LRState(Int32 index, LRStateAction[] actions, LRStateAction[] transitionVector) { m_Index = index; m_Actions = actions; m_TransitionVector = transitionVector; }
/// <summary> /// Read LR state information. /// </summary> private void ReadLRStates(BinaryReader reader) { Int32 index = ReadInt16Entry(reader); ReadEmptyEntry(reader); var stateTable = new LRStateAction[m_EntryCount / 4]; for (Int32 i = 0; i < stateTable.Length; i++) { Symbol symbol = m_SymbolTable[ReadInt16Entry(reader)]; var action = (LRAction)ReadInt16Entry(reader); Int32 targetIndex = ReadInt16Entry(reader); ReadEmptyEntry(reader); stateTable[i] = new LRStateAction(i, symbol, action, targetIndex); } // Create the transition vector var transitionVector = new LRStateAction[m_SymbolTable.Length]; Array.Clear(transitionVector, 0, transitionVector.Length); for (Int32 i = 0; i < stateTable.Length; i++) { transitionVector[stateTable[i].Symbol.Index] = stateTable[i]; } var lrState = new LRState(index, stateTable, transitionVector); m_LRStateTable[index] = lrState; }
/// <summary> /// Read LR state information. /// </summary> private void ReadLRStates() { int index = ReadInt16Entry(); ReadEmptyEntry(); LRStateAction[] stateTable = new LRStateAction[m_entryCount/4]; for (int i = 0; i < stateTable.Length; i++) { Symbol symbol = m_symbolTable[ReadInt16Entry()]; LRAction action = (LRAction) ReadInt16Entry(); int targetIndex = ReadInt16Entry(); ReadEmptyEntry(); stateTable[i] = new LRStateAction(i, symbol, action, targetIndex); } // Create the transition vector LRStateAction[] transitionVector = new LRStateAction[m_symbolTable.Length]; for (int i = 0; i < transitionVector.Length; i++) { transitionVector[i] = null; } for (int i = 0; i < stateTable.Length; i++) { transitionVector[stateTable[i].Symbol.Index] = stateTable[i]; } LRState lrState = new LRState(index, stateTable, transitionVector); m_lrStateTable[index] = lrState; }