コード例 #1
0
ファイル: ItemLALR1.cs プロジェクト: sebgod/hime
        /// <summary>
        /// Closes this item to a set of items
        /// </summary>
        /// <param name="closure">The list to close</param>
        /// <param name="map">The current helper map</param>
        public override void CloseTo(List <Item> closure, Dictionary <Rule, Dictionary <int, List <Item> > > map)
        {
            // the item was of the form [Var -> alpha .] (reduction)
            // nothing to do
            if (Action == LRActionCode.Reduce)
            {
                return;
            }
            // Get the next symbol in the item
            Symbol next = GetNextSymbol();
            // Here the item is of the form [Var -> alpha . next beta]
            // If the next symbol is not a variable : do nothing
            // If the next symbol is a variable :
            Variable nextVar = next as Variable;

            if (nextVar == null)
            {
                return;
            }
            // Firsts is a copy of the Firsts set for beta (next choice)
            // Firsts will contains symbols that may follow Next
            // Firsts will therefore be the lookahead for child items
            TerminalSet firsts = new TerminalSet(GetNextChoice().Firsts);

            // If beta is nullifiable (contains ε) :
            if (firsts.Contains(Epsilon.Instance))
            {
                // Remove ε
                firsts.Remove(Epsilon.Instance);
                // Add the item's lookaheads
                firsts.AddRange(lookaheads);
            }
            // For each rule that has Next as a head variable :
            foreach (Rule rule in nextVar.Rules)
            {
                if (!map.ContainsKey(rule))
                {
                    map.Add(rule, new Dictionary <int, List <Item> >());
                }
                Dictionary <int, List <Item> > sub = map[rule];
                if (sub.ContainsKey(0))
                {
                    List <Item> previouses = sub[0];
                    ItemLALR1   previous   = previouses[0] as ItemLALR1;
                    previous.Lookaheads.AddRange(firsts);
                }
                else
                {
                    List <Item> items = new List <Item>();
                    sub.Add(0, items);
                    ItemLALR1 New = new ItemLALR1(rule, 0, firsts);
                    closure.Add(New);
                    items.Add(New);
                }
            }
        }