Exemple #1
0
 public bool IsReduce(LR0State state)
 {
     if (state.Items.Count == 1)
     {
         var item  = state.Items[0];
         var index = item.rhs.IndexOf('.');
         if (index != item.rhs.Length - 1)
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #2
0
        public bool IsShift(LR0State state)
        {
            foreach (var item in state.Items)
            {
                var index = item.rhs.IndexOf('.');
                if (index == item.rhs.Length - 1)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #3
0
        public bool IsAccepted(LR0State state)
        {
            if (state.Items.Count == 1)
            {
                var accItem = new LR0Item(augmentedGrammar.StartingSymbol, grammar.StartingSymbol + ".");
                if (accItem.Equals(state.Items[0]))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #4
0
        public List <LR0State> CanonicalCollection()
        {
            var stateID             = -1;
            var unprocessedStats    = new Stack <LR0State>();
            var canonicalCollection = new List <LR0State>();

            var initalElements = augmentedGrammar.Productions[augmentedGrammar.StartingSymbol];
            var initalState    = new LR0State(Closure(initalElements));

            unprocessedStats.Push(initalState);
            calculatedStates.Add(new KeyValuePair <ClosureAction, LR0State>(new ClosureAction(initalElements), initalState));

            while (unprocessedStats.Count != 0)
            {
                var currentState = unprocessedStats.Pop();
                stateID++;
                currentState.StateID = stateID;
                canonicalCollection.Add(currentState);

                Log("State:");
                Log(currentState);

                augmentedGrammar.AllSymbols.ForEach(symbol =>
                {
                    var gotoResult = Goto(currentState, symbol);
                    if (gotoResult.Key != null)
                    {
                        var state = gotoResult.Key;
                        var isNew = gotoResult.Value;
                        Log(symbol);
                        Log(state);

                        if (isNew == true)
                        {
                            unprocessedStats.Push(state);
                        }
                    }
                });
            }

            return(canonicalCollection);
        }
Exemple #5
0
        private KeyValuePair <LR0State, bool> Goto(LR0State state, string symbol)
        {
            LR0State resultState = null;
            var      isNew       = true;

            var inputItems = state.Items
                             .Where(item =>
            {
                var itmeSymbol = item.GetSymbolAfterDot();
                if (itmeSymbol == null)
                {
                    return(false);
                }

                return(itmeSymbol.Equals(symbol));
            })
                             .ToList();

            var newItems = inputItems.Select(item => item.MoveDot()).ToList();

            if (newItems.Count != 0)
            {
                foreach (var calculatedState in calculatedStates)
                {
                    if (calculatedState.Key.Equals(new ClosureAction(newItems)))
                    {
                        resultState = calculatedState.Value;
                        isNew       = false;
                    }
                }

                if (resultState == null)
                {
                    var closure = Closure(newItems);
                    resultState = new LR0State(closure);

                    calculatedStates.Add(new KeyValuePair <ClosureAction, LR0State>(new ClosureAction(newItems), resultState));
                }
            }

            return(new KeyValuePair <LR0State, bool>(resultState, isNew));
        }
Exemple #6
0
        public int ReduceTo(LR0State state)
        {
            var item = state.Items[0];

            var lhsItem = item.lhs;
            var rhsItem = item.rhs.Substring(0, item.rhs.Length - 1);
            var newItem = new LR0Item(lhsItem, rhsItem);

            var index = 0;

            foreach (var originalItem in grammar.ProductionList)
            {
                if (originalItem.Equals(newItem))
                {
                    return(index);
                }
                index++;
            }

            return(-1);
        }