Beispiel #1
0
        public void ShouldDevelopSituationWithLeftRecursiveReduction()
        {
            SituationGraphFactory <char> situationGraphFactory;
            ISituationGraph <char>       graph;
            LexicalRule                 rule1, rule2, rule3;
            Situation <char>            situation;
            ISituationCollection <char> situations;
            Terminal    p1;
            NonTerminal p2;
            Terminal    p3;
            Terminal    p4;
            Sequence    sequence;
            SituationCollectionFactory <char> factory;

            //A*=•{S}a
            //S=•{S}b
            //S=•c

            p1 = new Terminal('a');
            p2 = new NonTerminal("S");
            p3 = new Terminal('b');
            p4 = new Terminal('c');

            sequence = new Sequence();
            sequence.Items.Add(p2);
            sequence.Items.Add(p1);
            rule1 = new LexicalRule()
            {
                Name = "A", Predicate = sequence
            };

            sequence = new Sequence();
            sequence.Items.Add(p2);
            sequence.Items.Add(p3);
            rule2 = new LexicalRule()
            {
                Name = "S", Predicate = sequence
            };

            rule3 = new LexicalRule()
            {
                Name = "S", Predicate = p4
            };

            situationGraphFactory = new SituationGraphFactory <char>(new SituationGraphSegmentFactory <char>());
            graph   = situationGraphFactory.BuildSituationGraph(new LexicalRule[] { rule1, rule2, rule3 });
            factory = new SituationCollectionFactory <char>(graph);

            situation  = new Situation <char>(rule1, p2, new MockedReduceInput());
            situations = factory.Develop(situation.AsEnumerable());

            Assert.AreEqual(5, situations.Count);
            Assert.AreEqual('a', ((ITerminalInput <char>)situations[1].Input).Value);
            Assert.AreEqual('b', ((ITerminalInput <char>)situations[2].Input).Value);
            Assert.AreEqual('a', ((ITerminalInput <char>)situations[3].Input).Value);
            Assert.AreEqual('b', ((ITerminalInput <char>)situations[4].Input).Value);
        }
Beispiel #2
0
        public void ShouldDevelopSituations()
        {
            SituationGraphFactory <char> situationGraphFactory;
            ISituationGraph <char>       graph;
            LexicalRule                 rule1, rule2, rule3, rule4;
            Situation <char>            situation;
            ISituationCollection <char> situations;
            NonTerminal                 p1, p2;
            Terminal p3, p4;
            Sequence sequence;
            SituationCollectionFactory <char> factory;

            p1 = new NonTerminal("B");
            p2 = new NonTerminal("C");
            p3 = new Terminal('b');
            p4 = new Terminal('c');

            sequence = new Sequence();
            sequence.Items.Add(p1);
            sequence.Items.Add(new Terminal('d'));

            rule1 = new LexicalRule()
            {
                Name = "A", Predicate = sequence
            };
            rule2 = new LexicalRule()
            {
                Name = "B", Predicate = p2
            };
            rule3 = new LexicalRule()
            {
                Name = "B", Predicate = p3
            };
            rule4 = new LexicalRule()
            {
                Name = "C", Predicate = p4
            };

            situationGraphFactory = new SituationGraphFactory <char>(new SituationGraphSegmentFactory <char>());
            graph   = situationGraphFactory.BuildSituationGraph(new LexicalRule[] { rule1, rule2, rule3, rule4 });
            factory = new SituationCollectionFactory <char>(graph);

            situation = new Situation <char>(rule1, p1, new MockedReduceInput());

            situations = factory.Develop(situation.AsEnumerable());


            Assert.AreEqual(4, situations.Count);
            Assert.AreEqual(p1, situations[0].Predicate);
            Assert.AreEqual(p2, situations[1].Predicate);
            Assert.AreEqual('d', ((ITerminalInput <char>)situations[1].Input).Value);
            Assert.AreEqual(p3, situations[2].Predicate);
            Assert.AreEqual('d', ((ITerminalInput <char>)situations[2].Input).Value);
            Assert.AreEqual(p4, situations[3].Predicate);
            Assert.AreEqual('d', ((ITerminalInput <char>)situations[3].Input).Value);
        }
Beispiel #3
0
        public void ShouldDevelopSituationWithLoopReduction()
        {
            SituationGraphFactory <char> situationGraphFactory;
            ISituationGraph <char>       graph;
            LexicalRule                 rule1, rule2, rule3;
            Situation <char>            situation;
            ISituationCollection <char> situations;
            Terminal    p1;
            NonTerminal p2;
            NonTerminal p3;
            SituationCollectionFactory <char> factory;
            OneOrMore oneOrMore;

            //"L=a"
            //"N={L}+"
            //"A*={N}"

            p1 = new Terminal('a');
            p2 = new NonTerminal("L");
            p3 = new NonTerminal("N");

            rule1 = new LexicalRule()
            {
                Name = "L", Predicate = new Sequence(p1, new Reduce())
            };

            oneOrMore      = new OneOrMore();
            oneOrMore.Item = p2;
            rule2          = new LexicalRule()
            {
                Name = "N", Predicate = new Sequence(oneOrMore, new Reduce())
            };

            rule3 = new LexicalRule()
            {
                Name = "A", Predicate = new Sequence(p3, new Reduce())
            };

            situationGraphFactory = new SituationGraphFactory <char>(new SituationGraphSegmentFactory <char>());
            graph   = situationGraphFactory.BuildSituationGraph(new LexicalRule[] { rule1, rule2, rule3 });
            factory = new SituationCollectionFactory <char>(graph);

            situation  = new Situation <char>(rule3, p3, new MockedReduceInput());
            situations = factory.Develop(situation.AsEnumerable());

            Assert.AreEqual(5, situations.Count);
            Assert.AreEqual('a', ((ITerminalInput <char>)situations[2].Input).Value);
            Assert.AreEqual('a', ((ITerminalInput <char>)situations[4].Input).Value);
        }
Beispiel #4
0
        public void ShouldDevelopSituationEndingWithReductionOnNonTerminal()
        {
            SituationGraphFactory <char> situationGraphFactory;
            ISituationGraph <char>       graph;
            LexicalRule                 rule1, rule2;
            Situation <char>            situation;
            ISituationCollection <char> situations;
            Terminal    p1;
            NonTerminal p2;
            Terminal    p3;
            Sequence    sequence;
            SituationCollectionFactory <char> factory;

            p1 = new Terminal('a');
            p2 = new NonTerminal("B");
            p3 = new Terminal('b');

            sequence = new Sequence();
            sequence.Items.Add(p1);
            sequence.Items.Add(p2);
            sequence.Items.Add(new Reduce());

            rule1 = new LexicalRule()
            {
                Name = "A", Predicate = sequence
            };
            rule2 = new LexicalRule()
            {
                Name = "B", Predicate = p3
            };

            situationGraphFactory = new SituationGraphFactory <char>(new SituationGraphSegmentFactory <char>());
            graph   = situationGraphFactory.BuildSituationGraph(new LexicalRule[] { rule1, rule2 });
            factory = new SituationCollectionFactory <char>(graph);

            situation  = new Situation <char>(rule1, p2, new MockedReduceInput());
            situations = factory.Develop(situation.AsEnumerable());

            Assert.AreEqual(2, situations.Count);
            Assert.AreEqual(p3, situations[1].Predicate);
        }