public PrecedenceBasedParserAction(BnfTerm shiftTerm, ParserState newShiftState, Production reduceProduction)
 {
     _reduceAction = new ReduceParserAction(reduceProduction);
     var reduceEntry = new ConditionalEntry(CheckMustReduce, _reduceAction, "(Precedence comparison)");
     ConditionalEntries.Add(reduceEntry);
     DefaultAction = _shiftAction = new ShiftParserAction(shiftTerm, newShiftState);
 }
Ejemplo n.º 2
0
        public PrecedenceBasedParserAction(BnfTerm shiftTerm, ParserState newShiftState, Production reduceProduction)
        {
            _reduceAction = new ReduceParserAction(reduceProduction);
            var reduceEntry = new ConditionalEntry(CheckMustReduce, _reduceAction, "(Precedence comparison)");

            ConditionalEntries.Add(reduceEntry);
            DefaultAction = _shiftAction = new ShiftParserAction(shiftTerm, newShiftState);
        }
Ejemplo n.º 3
0
        public override void Apply(LanguageData language, LRItem owner)
        {
            var state     = owner.State;
            var conflicts = state.BuilderData.Conflicts;

            if (conflicts.Count == 0)
            {
                return;
            }
            switch (_actionType)
            {
            case PreferredActionType.Shift:
                var currTerm = owner.Core.Current as Terminal;
                if (currTerm == null || !conflicts.Contains(currTerm))
                {
                    //nothing to do
                    return;
                }
                //Current term for shift item (hint owner) is a conflict - resolve it with shift action
                var shiftAction = new ShiftParserAction(owner);
                state.Actions[currTerm] = shiftAction;
                conflicts.Remove(currTerm);
                return;

            case PreferredActionType.Reduce:
                if (!owner.Core.IsFinal)
                {
                    //we take care of reduce items only here
                    return;
                }
                //we have a reduce item with "Reduce" hint. Check if any of lookaheads are in conflict
                ReduceParserAction reduceAction = null;
                foreach (var lkhead in owner.Lookaheads)
                {
                    if (conflicts.Contains(lkhead))
                    {
                        if (reduceAction == null)
                        {
                            reduceAction = new ReduceParserAction(owner.Core.Production);
                        }
                        state.Actions[lkhead] = reduceAction;
                        conflicts.Remove(lkhead);
                    }
                }
                return;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 4
0
 private void ExpandParserStateList(int initialIndex)
 {
     // Iterate through states (while new ones are created) and create shift transitions and new states
     for (int index = initialIndex; index < _data.States.Count; index++)
     {
         var state = _data.States[index];
         //Get all possible shifts
         foreach (var term in state.BuilderData.ShiftTerms)
         {
             var shiftItems = state.BuilderData.ShiftItems.SelectByCurrent(term);
             //Get set of shifted cores and find/create target state
             var shiftedCoreItems = shiftItems.GetShiftedCores();
             var newState         = FindOrCreateState(shiftedCoreItems);
             //Create shift action
             var newAction = new ShiftParserAction(term, newState);
             state.Actions[term] = newAction;
             //Link items in old/new states
             foreach (var shiftItem in shiftItems)
             {
                 shiftItem.ShiftedItem = newState.BuilderData.AllItems.FindByCore(shiftItem.Core.ShiftedItem);
             } //foreach shiftItem
         }     //foreach term
     }         //for index
 }             //method
Ejemplo n.º 5
0
 private void ExpandParserStateList(int initialIndex)
 {
     // Iterate through states (while new ones are created) and create shift transitions and new states
       for (int index = initialIndex; index < _data.States.Count; index++) {
     var state = _data.States[index];
     //Get all possible shifts
     foreach (var term in state.BuilderData.ShiftTerms) {
       var shiftItems = state.BuilderData.ShiftItems.SelectByCurrent(term);
       //Get set of shifted cores and find/create target state
       var shiftedCoreItems = shiftItems.GetShiftedCores();
       var newState = FindOrCreateState(shiftedCoreItems);
       //Create shift action
       var newAction = new ShiftParserAction(term, newState);
       state.Actions[term] = newAction;
       //Link items in old/new states
       foreach (var shiftItem in shiftItems) {
     shiftItem.ShiftedItem = newState.BuilderData.AllItems.FindByCore(shiftItem.Core.ShiftedItem);
       }//foreach shiftItem
     }//foreach term
       } //for index
 }
Ejemplo n.º 6
0
        public override void Apply(LanguageData language, LRItem owner)
        {
            var state = owner.State;

            if (!state.BuilderData.IsInadequate)
            {
                return;                                  //the state is adequate, we don't need to do anything
            }
            var conflicts = state.BuilderData.Conflicts;

            // Note that we remove lookaheads from the state conflicts set at the end of this method - to let parser builder know
            // that this conflict is taken care of.
            // On the other hand we may call this method multiple times for different LRItems if we have multiple hints in the same state.
            // Since we remove lookahead from conflicts on the first call, on the consequitive calls it will not be a conflict -
            // but we still need to add a new conditional entry to a conditional parser action for this lookahead.
            // Thus we process the lookahead anyway, even if it is not a conflict.
            // if (conflicts.Count == 0) return; -- this is a wrong thing to do
            switch (_actionType)
            {
            case PreferredActionType.Reduce:
                if (!owner.Core.IsFinal)
                {
                    return;
                }
                //it is reduce action; find lookaheads in conflict
                var lkhs = owner.Lookaheads;
                if (lkhs.Count == 0)
                {
                    return;     //if no conflicts then nothing to do
                }
                var reduceAction    = new ReduceParserAction(owner.Core.Production);
                var reduceCondEntry = new ConditionalParserAction.ConditionalEntry(CheckCondition, reduceAction, _description);
                foreach (var lkh in lkhs)
                {
                    AddConditionalEntry(state, lkh, reduceCondEntry);
                    if (conflicts.Contains(lkh))
                    {
                        conflicts.Remove(lkh);
                    }
                }
                break;

            case PreferredActionType.Shift:
                var curr = owner.Core.Current as Terminal;
                if (curr == null)
                {
                    return;     //it is either reduce item, or curr is a NonTerminal - we cannot shift it
                }
                var shiftAction    = new ShiftParserAction(owner);
                var shiftCondEntry = new ConditionalParserAction.ConditionalEntry(CheckCondition, shiftAction, _description);
                AddConditionalEntry(state, curr, shiftCondEntry);
                if (conflicts.Contains(curr))
                {
                    conflicts.Remove(curr);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }