Esempio n. 1
0
        public void AddDuplicate()
        {
            State state = new State();
            Production p = new Production(new Terminal('x', 'y'));
            Item itemA = new Item(p, state);
            itemA.Add('x');
            state.Add(itemA);

            Item itemB = new Item(p, state);
            itemB.Add('y');
            state.Add(itemB);
        }
Esempio n. 2
0
 public void Add()
 {
     State state = new State();
     Item item = new Item(new Production(), state);
     state.Add(item);
     Assert.AreEqual(1, state.Count);
 }
Esempio n. 3
0
 public void Contains()
 {
     State s = new State();
     Item item = new Item(new Production(), s);
     Assert.IsFalse(s.Contains(item));
     s.Add(item);
     Assert.IsTrue(s.Contains(item));
 }
Esempio n. 4
0
        public void Enumerate()
        {
            State state = new State();
            Item item = new Item(new Production(), state);
            state.Add(item);
            Assert.AreEqual(item, state[0]);

            for (int i = 0; i < state.Count; i++)
            {
                item = state[i];
            }
        }
Esempio n. 5
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]);
        }
Esempio n. 6
0
        public void ImportIncompatible()
        {
            Production p = new Production(new Terminal('a', 'b'));
            Item item = new Item(p, new State());

            State s = new State();

            Item next = item.NextItem;
            next.Add('a');
            s.Add(next);

            Item newNext = item.NextItem;
            newNext.Add('b');
            s.Import(newNext);
        }
Esempio n. 7
0
        public void Import()
        {
            Production p = new Production(new Terminal('a', 'b'));
            Item item = new Item(p, new State());

            State s = new State();

            Item next = item.NextItem;
            next.Add('a');
            s.Add(next);

            Item imported = s.Import(item.NextItem);
            Assert.AreSame(next, imported);

            item = new Item(new Production(), new State());
            imported = s.Import(item);
            Assert.AreSame(item, imported);
        }
Esempio n. 8
0
        private IList<object> Parse(TextReader input)
        {
            if (input == null) throw new ArgumentNullException();

            State initial = new State();
            initial.Add(new Item(startProduction, initial));

            State current = initial;
            State next = new State();

            do
            {
                IDictionary<Production, IList<Item>> completedNullable =
                    new Dictionary<Production, IList<Item>>();

                for (int i = 0; i < current.Count; i++)
                {
                    Item item = current[i];

                    if (!item.AtEnd && item.Symbol is Nonterminal)
                    {
                        Predictor(current, item, completedNullable);
                    }
                    else if (!item.AtEnd && item.Symbol is Terminal)
                    {
                        Scanner(item, next, input.Peek());
                    }
                    else
                    {
                        Completer(current, item, completedNullable);
                    }
                }

                current = next;
                next = new State();
            }
            while (input.Read() != -1 && current.Count > 0);

            if (current.Count == 1 &&
                current[0].AtEnd &&
                current[0].Production == startProduction &&
                current[0].Parent == initial)
            {
                return current[0].Reduce();
            }
            else
            {
                return new List<object>();
            }
        }
Esempio n. 9
0
        private void Scanner(Item item, State next, int ch)
        {
            Debug.Assert(!item.AtEnd && item.Symbol is Terminal);

            Terminal t = item.Symbol as Terminal;

            if (t.Contains(ch))
            {
                Item newItem = item.NextItem;
                newItem.Add(ch);
                next.Add(newItem);
            }
        }
Esempio n. 10
0
        private void Predictor(
            State state,
            Item item,
            IDictionary<Production,IList<Item>> completedNullable)
        {
            Debug.Assert(!item.AtEnd && item.Symbol is Nonterminal);

            Nonterminal nt = item.Symbol as Nonterminal;

            foreach (Production p in nt.Productions)
            {
                Item newItem = new Item(p, state);

                if (!state.Contains(newItem))
                {
                    state.Add(newItem);
                    ShiftCompletedNullable(state, newItem, completedNullable);
                }
            }
        }