Example #1
0
        private void ComputeStates()
        {
            mItemsByIndex  = new Dictionary <LalrItemSet, int>();
            mStates        = new List <LalrItemSet>();
            mCurrentItems  = new HashSet <LalrItemSet>();
            mNextItems     = new HashSet <LalrItemSet>();
            mRecordedGotos = new Dictionary <Tuple <int, Symbol>, int>();

            var initialState = new LalrItemSet(new LalrItem[] {
                new LalrItem(mDatabase[NonterminalSymbol.StartingSymbol].First(), 0, TerminalSymbol.Eof)
            });

            //mStates.Add(initialState);
            mStates.Add(initialState.Closure(mDatabase));
            //mItemsByIndex.Add(mStates[0].ToCoreSet(), 0);
            mItemsByIndex.Add(mStates[0], 0);
            mCurrentItems.Add(mStates[0]);

            while (mCurrentItems.Count != 0)
            {
                foreach (var iset in mCurrentItems)
                {
                    Expand(iset);
                }

                mCurrentItems.Clear();
                mCurrentItems.UnionWith(mNextItems);
                mNextItems.Clear();
            }

            CreateTable();
        }
Example #2
0
        private void Expand(LalrItemSet iset)
        {
            //var thisIndex = mItemsByIndex[iset.ToCoreSet()];
            var thisIndex = mItemsByIndex[iset];

            var performGoto = iset.Goto(mDatabase);

            foreach (var item in performGoto)
            {
                //var cs = item.Value.ToCoreSet();
                //if(mItemsByIndex.ContainsKey(cs)){
                if (mItemsByIndex.ContainsKey(item.Value))
                {
                    //var index = mItemsByIndex[cs];
                    var index = mItemsByIndex[item.Value];
                    //mStates[index] = LalrItemSet.Merge(mStates[index], item.Value);
                    mRecordedGotos.Add(Tuple.Create(thisIndex, item.Key), index);
                }
                else
                {
                    mStates.Add(item.Value);
                    //mItemsByIndex.Add(cs, mStates.Count - 1);
                    mItemsByIndex.Add(item.Value, mStates.Count - 1);
                    mNextItems.Add(item.Value);
                    mRecordedGotos.Add(Tuple.Create(thisIndex, item.Key), mStates.Count - 1);
                }
            }
        }
Example #3
0
        public static LalrItemSet Merge(LalrItemSet a, LalrItemSet b)
        {
            HashSet <LalrItem> items = new HashSet <LalrItem>();

            foreach (var item in a)
            {
                items.Add(item);
            }
            foreach (var item in b)
            {
                items.Add(item);
            }
            return(new LalrItemSet(items));
        }
Example #4
0
        public bool Equals(LalrItemSet other)
        {
            if (other == null)
            {
                return(false);
            }

            if (this.Count != other.Count)
            {
                return(false);
            }

            for (int i = 0; i < other.Count; i++)
            {
                if (this[i] != other[i])
                {
                    return(false);
                }
            }

            return(true);
        }