コード例 #1
0
ファイル: RnGlrParser.cs プロジェクト: sucaba/IronTextLibrary
        private ParserAction GetShiftReduce(State state, int token)
        {
            ParserAction action = GetDfaCell(state, token);

            switch (action.Kind)
            {
            case ParserActionKind.ShiftReduce:
                return(action);

            case ParserActionKind.Conflict:
                int start = action.Value1;
                int last  = action.Value1 + action.Value2;
                while (start != last)
                {
                    var conflictAction = ParserAction.Decode(conflictActionsTable[start++]);
                    if (conflictAction.Kind == ParserActionKind.ShiftReduce)
                    {
                        return(conflictAction);
                    }
                }

                break;
            }

            return(ParserAction.FailAction);
        }
コード例 #2
0
ファイル: RnGlrParser.cs プロジェクト: sucaba/IronTextLibrary
        private int GetShift(State state, int token)
        {
            int shift = -1;

            ParserAction action = GetDfaCell(state, token);

            switch (action.Kind)
            {
            case ParserActionKind.Shift:
                shift = action.State;
                break;

            case ParserActionKind.Conflict:
                int start = action.Value1;
                int last  = action.Value1 + action.Value2;
                while (start != last)
                {
                    var conflictAction = ParserAction.Decode(conflictActionsTable[start++]);
                    if (conflictAction.Kind == ParserActionKind.Shift)
                    {
                        shift = conflictAction.State;
                        break;
                    }
                }
                break;
            }

            return(shift);
        }
コード例 #3
0
        private ParserAction GetDfaCell(State state, int token)
        {
            int cell   = transition(state, token);
            var result = ParserAction.Decode(cell);

            return(result);
        }
コード例 #4
0
ファイル: RnGlrParser.cs プロジェクト: sucaba/IronTextLibrary
        private IEnumerable <ParserAction> GetConflictActions(int start, short count)
        {
            int last = start + count;

            while (start != last)
            {
                yield return(ParserAction.Decode(conflictActionsTable[start++]));
            }
        }
コード例 #5
0
        private int NonTermGoTo(State state, int token)
        {
            var action = ParserAction.Decode(transition(state, token));

            if (action == null || action.Kind != ParserActionKind.Shift)
            {
                throw new InvalidOperationException("Non-term action should be shift");
            }

            return(action.State);
        }
コード例 #6
0
        private ParserAction LookaheadAction(int token)
        {
            var   act = ParserAction.Decode(actionTable(stateStack.PeekTag(), token));
            TNode value;

            while (act.Kind == ParserActionKind.Reduce)
            {
                this.currentRule = grammar.Productions[act.ProductionId];
                stateStack.Start = stateStack.Count - currentRule.PatternTokens.Length;
                value            = producer.CreateBranch(
                    currentRule,
                    stateStack.PeekTail(currentRule.PatternTokens.Length),
                    (IStackLookback <TNode>)stateStack);

                stateStack.Pop(currentRule.PatternTokens.Length);
                act = ParserAction.Decode(actionTable(stateStack.PeekTag(), currentRule.OutcomeToken));

                while (act.Kind == ParserActionKind.ShiftReduce) // == GotoReduce
                {
                    stateStack.Push(-1, value);

                    this.currentRule = grammar.Productions[act.ProductionId];
                    stateStack.Start = stateStack.Count - currentRule.PatternTokens.Length;
                    value            = producer.CreateBranch(
                        currentRule,
                        stateStack.PeekTail(currentRule.PatternTokens.Length),
                        (IStackLookback <TNode>)stateStack);

                    stateStack.Pop(currentRule.PatternTokens.Length);
                    act = ParserAction.Decode(actionTable(stateStack.PeekTag(), currentRule.OutcomeToken));
                }

                stateStack.Push(act.State, value);

                // reduce or final shift
                act = ParserAction.Decode(actionTable(act.State, token));
            }

            return(act);
        }
コード例 #7
0
ファイル: RnGlrParser.cs プロジェクト: sucaba/IronTextLibrary
        private void GetReductions(State state, int token)
        {
            pendingReductionsCount = 0;

            ParserAction action = GetDfaCell(state, token);
            Production   rule;

            switch (action.Kind)
            {
            case ParserActionKind.Reduce:
                rule = grammar.Productions[action.ProductionId];
                pendingReductionsCount = 1;
                pendingReductions[0]   = new ModifiedReduction(rule, action.Size);
                break;

            case ParserActionKind.Accept:
                accepted = true;
                break;

            case ParserActionKind.Conflict:
                int start = action.Value1;
                int last  = action.Value1 + action.Value2;
                while (start != last)
                {
                    var conflictAction = ParserAction.Decode(conflictActionsTable[start++]);
                    switch (conflictAction.Kind)
                    {
                    case ParserActionKind.Reduce:
                        var crule = grammar.Productions[conflictAction.ProductionId];
                        pendingReductions[pendingReductionsCount++]
                            = new ModifiedReduction(crule, conflictAction.Size);
                        break;
                    }
                }

                break;
            }
        }
コード例 #8
0
        public IReceiver <Msg> Next(Msg envelope)
        {
            stateStack.BeginEdit();

            int     id   = envelope.Id;
            MsgData data = envelope.FirstData;

START:
            ParserAction action = LookaheadAction(id);

            switch (action.Kind)
            {
            case ParserActionKind.Fail:
                if (isVerifier)
                {
                    return(null);
                }

                // ReportUnexpectedToken(msg, stateStack.PeekTag());
                return(RecoverFromError(envelope));

            case ParserActionKind.Resolve:
                id = action.RolvedToken;
                while (true)
                {
                    if (data.Token == id)
                    {
                        // Successfully resolved to a particular token
                        goto START;
                    }

                    data = data.Next;
                    if (data == null)
                    {
                        // Desired token was not present in Msg
                        goto case ParserActionKind.Fail;
                    }
                }

            case ParserActionKind.Fork:
            case ParserActionKind.Conflict:
                logging.Write(
                    new LogEntry
                {
                    Severity  = Severity.Error,
                    Location  = envelope.Location,
                    HLocation = envelope.HLocation,
                    Message   = "Hit parser conflict on token " + grammar.SymbolName(envelope.Id)
                });
                return(null);

            case ParserActionKind.Shift:
            {
                stateStack.Push(action.State, producer.CreateLeaf(envelope, data));
                break;
            }

            case ParserActionKind.ShiftReduce:
            {
                TNode value = producer.CreateLeaf(envelope, data);
                do
                {
                    stateStack.Push(-1, value);
                    this.currentRule = grammar.Productions[action.ProductionId];
                    stateStack.Start = stateStack.Count - currentRule.PatternTokens.Length;
                    value            = producer.CreateBranch(
                        currentRule,
                        stateStack.PeekTail(currentRule.PatternTokens.Length),
                        (IStackLookback <TNode>)stateStack);
                    stateStack.Pop(currentRule.PatternTokens.Length);
                    action = ParserAction.Decode(actionTable(stateStack.PeekTag(), currentRule.OutcomeToken));
                }while (action.Kind == ParserActionKind.ShiftReduce);

                if (action.Kind == ParserActionKind.Fail)
                {
                    return(null);
                }

                Debug.Assert(action.Kind == ParserActionKind.Shift);
                stateStack.Push(action.State, value);
                break;
            }

            case ParserActionKind.Accept:
                producer.Result = stateStack.Peek();
                return(FinalReceiver <Msg> .Instance);

            default:
                throw new InvalidOperationException("Internal error: Unsupported parser action");
            }

            stateStack.EndEdit();
            this.priorInput = envelope;
            return(this);
        }
コード例 #9
0
ファイル: RnGlrParser.cs プロジェクト: sucaba/IronTextLibrary
 private ParserAction GetDfaCell(State state, int token)
 {
     return(ParserAction.Decode(transition(state, token)));
 }
コード例 #10
0
ファイル: RnGlrParser.cs プロジェクト: sucaba/IronTextLibrary
        private void Reducer(int lookahead = -1)
        {
            while (!R.IsEmpty)
            {
                GssReducePath <T> path = R.Dequeue();

                int X = path.Rule.OutcomeToken;
                int m = path.Size;

                GssNode <T> u = path.LeftNode;
                State       k = u.State;
                T           z;
                if (m == 0)
                {
                    z = producer.GetDefault(X, (IStackLookback <T>)u);
                }
                else
                {
                    path.CopyDataTo(nodeBuffer);
                    T Λ = producer.CreateBranch(
                        path.Rule,
                        new ArraySlice <T>(nodeBuffer, 0, path.Size),
                        lookback: path.LeftNode);

                    int c = u.Layer;
                    T   currentValue;
                    var Nkey = GetNKey(X, c);
                    if (N.TryGetValue(Nkey, out currentValue))
                    {
                        z = producer.Merge(currentValue, Λ, (IStackLookback <T>)u);
                    }
                    else
                    {
                        z = Λ;
                    }

                    N[Nkey] = z;
                }

                State l;

                var action = ParserAction.Decode(transition(k, X));
                switch (action.Kind)
                {
                case ParserActionKind.Shift:
                    l = action.State;
                    break;

                case ParserActionKind.ShiftReduce:     // Goto-Reduce action
                    PlanShiftReduce(
                        u,
                        X,
                        z,
                        action.ProductionId,
                        action.Size);
                    continue;

                default:
                    throw new InvalidOperationException(
                              "Internal error: Non-term action should be shift or shift-reduce, but got "
                              + Enum.GetName(typeof(ParserActionKind), action.Kind));
                }

                bool stateAlreadyExists = gss.GetFrontNode(l, lookahead) != null;

                // Goto on non-term produced by rule.
                var newLink = gss.Push(u, l, z, lookahead);

                if (lookahead < 0)
                {
                    continue;
                }

                if (stateAlreadyExists)
                {
                    if (newLink != null && m != 0)
                    {
                        GetReductions(l, lookahead);
                        for (int i = 0; i != pendingReductionsCount; ++i)
                        {
                            var red = pendingReductions[i];
                            if (red.Size != 0)
                            {
                                R.Enqueue(newLink, red.Rule, red.Size);
                            }
                        }
                    }
                }
                else
                {
                    var w = gss.GetFrontNode(l, lookahead);

                    GetReductions(l, lookahead);
                    //foreach (var red in reductions)
                    for (int i = 0; i != pendingReductionsCount; ++i)
                    {
                        var red = pendingReductions[i];
                        if (red.Size == 0)
                        {
                            R.Enqueue(w, red.Rule, 0);
                        }
                    }

                    if (m != 0)
                    {
                        for (int i = 0; i != pendingReductionsCount; ++i)
                        {
                            var red = pendingReductions[i];
                            if (red.Size != 0)
                            {
                                R.Enqueue(newLink, red.Rule, red.Size);
                            }
                        }
                    }
                }
            }
        }