Beispiel #1
0
        internal override TokenParseResult Execute <T>(IParser <T> parser, T token)
        {
            bool           trim       = reduceRule.ContainsOneNonterminal && parser.CanTrim(reduceRule);
            T              head       = trim ? parser.PopToken() : parser.CreateReduction(reduceRule);
            LalrActionGoto gotoAction = parser.TopState.GetActionBySymbol(reduceRule.RuleSymbol) as LalrActionGoto;

            if (gotoAction == null)
            {
                Debug.Fail("Internal table error.");
                return(TokenParseResult.InternalError);
            }
            parser.PushTokenAndState(head, gotoAction.State);
            return(trim ? TokenParseResult.ReduceEliminated : TokenParseResult.ReduceNormal);
        }
Beispiel #2
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);
        }