/// <summary>
            /// Read LALR state information.
            /// </summary>
            private void ReadLalrStates()
            {
                int index = ReadInt16Entry();

                ReadEmptyEntry();
                LalrStateAction[] stateTable = new LalrStateAction[m_entryCount / 4];
                for (int i = 0; i < stateTable.Length; i++)
                {
                    Symbol     symbol      = m_symbolTable[ReadInt16Entry()];
                    LalrAction action      = (LalrAction)ReadInt16Entry();
                    int        targetIndex = ReadInt16Entry();
                    ReadEmptyEntry();
                    stateTable[i] = new LalrStateAction(i, symbol, action, targetIndex);
                }

                // Create the transition vector
                LalrStateAction[] transitionVector = new LalrStateAction[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];
                }

                LalrState lalrState = new LalrState(index, stateTable, transitionVector);

                m_lalrStateTable[index] = lalrState;
            }
 /// <summary>
 /// Creats a new instance of the <c>LalrStateAction</c> class.
 /// </summary>
 /// <param name="index">Index of the LALR state action.</param>
 /// <param name="symbol">Symbol associated with the action.</param>
 /// <param name="action">Action type.</param>
 /// <param name="value">Action value.</param>
 public LalrStateAction(int index, Symbol symbol, LalrAction action, int value)
 {
     m_index  = index;
     m_symbol = symbol;
     m_action = action;
     m_value  = value;
 }
示例#3
0
		/// <summary>
		/// Read LR state information.
		/// </summary>
		/// <param name="context"></param>
		private void ReadLRState(LoadContext context) {
			int index = context.ReadIntegerEntry();
			context.ReadEmptyEntry();
			LalrAction[] table = new LalrAction[context.EntryCount/4];
			for (int i = 0; i < table.Length; i++) {
				Symbol symbol = symbolTable[context.ReadIntegerEntry()];
				LalrActionType actionType = (LalrActionType)context.ReadIntegerEntry();
				int targetIndex = context.ReadIntegerEntry();
				context.ReadEmptyEntry();
				LalrAction action;
				switch (actionType) {
				case LalrActionType.Accept:
					action = new LalrActionAccept(i, symbol);
					break;
				case LalrActionType.Goto:
					action = new LalrActionGoto(i, symbol, GetLalrState(targetIndex));
					break;
				case LalrActionType.Reduce:
					action = new LalrActionReduce(i, symbol, GetRule(targetIndex));
					break;
				case LalrActionType.Shift:
					action = new LalrActionShift(i, symbol, GetLalrState(targetIndex));
					break;
				default:
					throw new InvalidOperationException("Invalid table action data");
				}
				table[i] = action;
			}
			// Create the transition vector
			LalrAction[] transitionVector = new LalrAction[symbolTable.Length];
			for (int i = 0; i < transitionVector.Length; i++) {
				transitionVector[i] = null;
			}
			for (int i = 0; i < table.Length; i++) {
				transitionVector[table[i].Symbol.Index] = table[i];
			}
			GetLalrState(index).Initialize(table, transitionVector);
		}
示例#4
0
 protected override bool EqualsInternal(LalrAction other)
 {
     return(this.NewState == ((ShiftAction)other).NewState);
 }
示例#5
0
 protected override bool EqualsInternal(LalrAction other)
 {
     return(this.ProductionRule == ((ReduceSingleAction)other).ProductionRule);
 }
示例#6
0
        /// <summary>
        /// Executes next step of parser and returns parser currentState.
        /// </summary>
        /// <returns>Parser current currentState.</returns>
        public virtual ParseMessage Parse()
        {
            while (true)
            {
                T inputToken;
                if (currentToken == null)
                {
                    //We must read a currentToken
                    T            textInputToken;
                    ParseMessage message = tokenizer.NextToken(out textInputToken);
                    if (textInputToken == null)
                    {
                        return(ParseMessage.InternalError);
                    }
                    //					Debug.WriteLine(string.Format("State: {0} Line: {1}, Column: {2}, Parse Value: {3}, Token Type: {4}", currentState.Index, inputToken.Line, inputToken.LinePosition, inputToken.Text, inputToken.symbol.Name), "Token Read");
                    if (textInputToken.Symbol.Kind != SymbolKind.End)
                    {
                        currentToken = textInputToken;
                        return(message);
                    }
                    inputToken = textInputToken;
                }
                else
                {
                    inputToken = currentToken;
                }
                switch (inputToken.Symbol.Kind)
                {
                case SymbolKind.WhiteSpace:
                case SymbolKind.CommentStart:
                case SymbolKind.CommentLine:
                    ClearCurrentToken();
                    break;

                case SymbolKind.Error:
                    return(ParseMessage.LexicalError);

                default:
                    LalrAction action = currentState.GetActionBySymbol(inputToken.Symbol);
                    if (action == null)
                    {
                        if (RetrySyntaxError(ref inputToken))
                        {
                            currentToken = inputToken;
                            continue;
                        }
                        return(ParseMessage.SyntaxError);
                    }
                    // the Execute() is the ParseToken() equivalent
                    switch (action.Execute(this, inputToken))
                    {
                    case TokenParseResult.Accept:
                        return(ParseMessage.Accept);

                    case TokenParseResult.Shift:
                        ClearCurrentToken();
                        break;

                    case TokenParseResult.SyntaxError:
                        return(ParseMessage.SyntaxError);

                    case TokenParseResult.ReduceNormal:
                        return(ParseMessage.Reduction);

                    case TokenParseResult.InternalError:
                        return(ParseMessage.InternalError);
                    }
                    break;
                }
            }
        }