Example #1
0
        public static void Closure(CachedGrammar inst, LinkedHashSet <LRxItem> items)
        {
            Grammar g = inst.m_value;
            IForwardIterator <LRxItem> e1 = items.Begin();
            IForwardIterator <LRxItem> e2 = items.Begin();

            for (; e1.IsAlive; e1.Add())
            {
                LRxItem current = e1.Value;
                if (current.IsEnd)
                {
                    continue;
                }

                int ms = current.MarkSymbol;
                if (!ms.IsTerminal())
                {
                    CHashSet <int> la =
                        current.IsLast
                        ? new CHashSet <int>(current.lookahead)
                        : inst.GetFirst(current.express, current.index + 1);

                    IForwardIterator <GItem> e3 = g.Find(ms);
                    do
                    {
                        LRxItem newit = new LRxItem(e3.Value, la);

                        for (; e2.IsAlive; e2.Add())
                        {
                            if (newit.CoreEquals(e2.Value))
                            {
                                CHashSet <int> ela = e2.Value.lookahead;
                                if (!ela.IsSupersetOf(la))
                                {
                                    ela.UnionWith(la);
                                }

                                goto exit;
                            }
                        }

                        items.Add(newit);

exit:
                        e2.Reset();
                    } while (e3.LazyAdd());
                    e3.Dispose();
                }
            }

            e1.Dispose();
        }
        public static bool IsUnmanaged(this Type type)
        {
            while (type.IsPointer || type.IsArray)
            {
                type = type.GetElementType();
            }

            if (type.IsPrimitive || type.IsEnum || bag.Contains(type))
            {
                return(true);
            }

            if (type.IsValueType)
            {
                HashSet <Type> done = new HashSet <Type>();
                Stack <IForwardIterator <FieldInfo> > stack = new Stack <IForwardIterator <FieldInfo> >();

                IForwardIterator <FieldInfo> e1 = type.GetFields(flg).Begin();
loop:
                for (; e1.IsAlive; e1.Add())
                {
                    Type tmp = e1.Value.FieldType;
                    if (tmp.IsPointer)
                    {
                        continue;
                    }
                    else if (!tmp.IsValueType)
                    {
                        return(false);
                    }
                    else if (!tmp.IsPrimitive && done.Add(tmp))
                    {
                        stack.Push(e1);
                        e1.Add();

                        e1 = tmp.GetFields(flg).Begin();
                        goto loop;
                    }
                }

                if (stack.Count > 0)
                {
                    e1 = stack.Pop();
                    goto loop;
                }

                bag.Add(type);
                return(true);
            }

            return(false);
        }
Example #3
0
        public void RotateTest2()
        {
            int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            IList <int> lst = new List <int>();

            using (IForwardIterator <int> inputIterator = new ForwardIterator <int>(data))
            {
                IShallowClone cloneable = (IShallowClone)inputIterator;

                IForwardIterator <int> cloneObj = cloneable.ShallowClone() as IForwardIterator <int>;
                cloneObj.End();

                ICursor cursor = (ICursor)cloneObj;
                cursor.SetPosition(cursor.GetPosition() - 2);

                //set the first element to 8 element position

                Algorithm.Rotate(inputIterator, cloneObj);

                inputIterator.Begin();

                using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst))
                {
                    Algorithm.Copy(inputIterator, outputIterator);
                }
            }

            bool isCorrectData = (lst[0] == 8 && lst[1] == 9 &&
                                  lst[2] == 1 && lst[3] == 2 &&
                                  lst[4] == 3 && lst[5] == 4 &&
                                  lst[6] == 5 && lst[7] == 6 &&
                                  lst[8] == 7);

            Assert.IsTrue(isCorrectData);
        }
Example #4
0
        private List <LALRState> CreateCLRState(CachedGrammar cg)
        {
            LALRState now = new LALRState(new LRxItem(cg.m_value.StartItem, new CHashSet <int> {
                ExpressInt.end_symbol
            }));

            LR1Helper.Closure(cg, now.items);

            List <LALRState> list = new List <LALRState> {
                now
            };

            for (int x = 0; x < list.Count; x++)
            {
                now = list[x];

                IForwardIterator <LRxItem> iter = now.items.Begin();
                IForwardIterator <LRxItem> e1   = now.items.Begin();
                do
                {
                    if (iter.Value.IsEnd)
                    {
                        continue;
                    }

                    LinkedHashSet <LRxItem> nextit = new LinkedHashSet <LRxItem>();
                    int ms = iter.Value.MarkSymbol;
                    do
                    {
                        if (e1.Value.MarkSymbol == ms)
                        {
                            nextit.Add(e1.Value.GetNext());
                        }
                    } while (e1.LazyAdd());
                    e1.Reset();

                    LR1Helper.Closure(cg, nextit);

                    bool exist = false;
                    for (int z = 0; z < list.Count; z++)
                    {
                        if (list[z].items.SetEquals(nextit))
                        {
                            exist = true;
                            now.map.Add(ms, list[z]);
                        }
                    }

                    if (!exist)
                    {
                        LALRState next = new LALRState(nextit);
                        list.Add(next);
                        now.map.Add(ms, next);
                    }
                } while (iter.LazyAdd());

                iter.Dispose();
                e1.Dispose();
            }

            return(list);
        }
Example #5
0
        public Grammar(StreamReader reader, ISymbolMap map)
        {
            if (map == null)
            {
                map = new EnumTM();
            }

            List <GItem> its = new List <GItem>(64);
            List <Range> ntm = new List <Range>(64);

            int x;

            map.BeginParseGrammar(ntm);

loop:
            while (!reader.EndOfStream)
            {
                string vtmp = reader.MoveNext();
                if (vtmp.Length == 0 || vtmp[0] == '#') // comment
                {
                    goto loop;
                }

                while (vtmp.Last() == '|') // more
                {
                    vtmp += reader.MoveNext();
                }

                string[] tmp  = vtmp.Split(Constants.eqs, 2);
                string   left = tmp[0].Trim();

                if (map.ContainsNT(left))
                {
                    throw new InvalidOperationException("invalid grammar: already exist non-terminal symbol -> " + tmp[0].Trim());
                }

                IForwardIterator <string> e1 = tmp[1].Trim().SplitExceptRange('|', '\'', '\'').Begin();

                int produce = map.NonTerminal(left);
                x = its.Count;
                do
                {
                    its.Add(new GItem(produce, new ExpressInt(e1.Value, map)));
                }while (e1.LazyAdd());

                ntm[produce.ToIndex()] = new Range(x, its.Count);
#if DEBUG
                bag.Add(produce, tmp[0].Trim());
#endif
            }

            if (map.ContainsNT("S"))
            {
                int   idx = map.NonTerminal("S").ToIndex();
                Range rng = ntm[idx];
                if (rng.end - rng.start == 1) // solo
                {
                    startIdx = idx;
                }
                else
                {
                    x = its.Count;

                    int newstart = ntm.Count;
                    its.Add(new GItem(newstart, new ExpressInt(idx.ToNonterminal())));
                    ntm.Add(new Range(x, x));
                    startIdx = newstart;
#if DEBUG
                    bag.Add(newstart, "S'");
#endif
                }
            }
            else
            {
                throw new InvalidOperationException("invalid grammar: start nonterminal");
            }

            map.EndParseGrammar();

            m_items        = its.ToArray();
            m_nonterminals = ntm.ToArray();
        }