Exemplo n.º 1
0
        public void ShouldBuildAutomatonTableFromTwoSequencesUsingAnyTerminal()
        {
            AutomatonTableFactory <char> factory;
            IAutomatonTable <char>       automatonTable;
            LexicalRule          rule1, rule2;
            AutomatonTableParser parser;

            factory = new AutomatonTableFactory <char>();

            rule1 = RuleHelper.BuildRule("A*=a.c;");
            rule2 = RuleHelper.BuildRule("B*=abc;");

            automatonTable = factory.BuildAutomatonTable(SituationCollectionFactoryHelper.BuildSituationCollectionFactory(new LexicalRule[] { rule1, rule2 }), new DistinctInputFactory(new RangeValueProvider()));
            Assert.IsNotNull(automatonTable);

            parser = new AutomatonTableParser(automatonTable);


            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(3, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('b')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));

            parser.Reset();

            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(3, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
        }
Exemplo n.º 2
0
        public void ShouldBuildAutomatonTableFromExtendedSequence()
        {
            AutomatonTableFactory <char> factory;
            IAutomatonTable <char>       automatonTable;
            LexicalRule          rule;
            Sequence             predicate;
            AutomatonTableParser parser;

            factory = new AutomatonTableFactory <char>( );

            predicate = new Sequence();
            predicate.Items.Add(new Terminal('a'));
            predicate.Items.Add(new Terminal('b'));
            predicate.Items.Add(new Terminal('c'));

            rule = new LexicalRule()
            {
                Name = "rule", IsAxiom = true
            };
            rule.Predicate = new ZeroOrMore()
            {
                Item = predicate
            };

            automatonTable = factory.BuildAutomatonTable(SituationCollectionFactoryHelper.BuildSituationCollectionFactory(rule.AsEnumerable()), new DistinctInputFactory(new RangeValueProvider()));
            Assert.IsNotNull(automatonTable);

            parser = new AutomatonTableParser(automatonTable);

            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('b')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
        }
Exemplo n.º 3
0
        public void ShouldBuildAutomatonTableFromBasicSequence()
        {
            AutomatonTableFactory <char> factory;
            IAutomatonTable <char>       automatonTable;
            LexicalRule          rule;
            AutomatonTableParser parser;

            factory = new AutomatonTableFactory <char>(  );

            rule = RuleHelper.BuildRule("A*=abc;");

            automatonTable = factory.BuildAutomatonTable(SituationCollectionFactoryHelper.BuildSituationCollectionFactory(rule.AsEnumerable()), new DistinctInputFactory(new RangeValueProvider()));
            Assert.IsNotNull(automatonTable);

            parser = new AutomatonTableParser(automatonTable);

            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('b')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
        }