public void Add()
 {
     Nonterminal nt = new Nonterminal();
     Production p = new Production();
     nt.Add(p);
     Assert.IsTrue(nt.Contains(p));
 }
Exemple #2
0
        public void GetItems()
        {
            State state = new State();
            Nonterminal nt = new Nonterminal();
            Production p = new Production(nt);
            nt.Add(p);
            Item item = new Item(p, state);
            state.Add(item);
            state.Add(new Item(new Production(), state));

            IList<Item> items = state.GetItems(p);

            Assert.AreEqual(1, items.Count);
            Assert.AreSame(item, items[0]);
        }
Exemple #3
0
        public void Reduce()
        {
            Reduction n1 = new Reduction("return 1;");
            Reduction n2 = new Reduction("return 2;");
            Reduction n10 = new Reduction("return 10;");
            Nonterminal exp = new Nonterminal();
            Reduction plus = new Reduction("return (int)$0 + (int)$1;", exp, exp);

            exp.Add(n1);
            exp.Add(n2);
            exp.Add(n10);
            exp.Add(plus);

            ReductionCompiler comp = new ReductionCompiler();
            comp.Add(n1);
            comp.Add(n2);
            comp.Add(n10);
            comp.Add(plus);
            comp.Compile();

            //     +
            //    / \
            //   +   \
            //  / \   \
            //  ?  1   ?
            // / \    / \
            // 1 2   +  10
            //      / \
            //      ?  1
            //	   / \
            //	   1 2

            Item inner = new Item(plus, new State());
            inner = inner.NextItem;
            inner.Add(new Item(n1, new State()));
            inner.Add(new Item(n2, new State()));

            inner = inner.NextItem;
            inner.Add(new Item(n1, new State()));

            Item item = new Item(plus, new State());
            item = item.NextItem;
            item.Add(inner);

            item = item.NextItem;
            item.Add(inner);
            item.Add(new Item(n10, new State()));

            IList<object> result = item.Reduce();

            int[] precomputed = new int[] {
                4, 5, 12, 5, 6, 13
            };

            Assert.AreEqual(precomputed.Length, result.Count);

            for (int i = 0; i < precomputed.Length; i++)
            {
                Assert.AreEqual(precomputed[i], result[i]);
            }
        }