Exemple #1
0
        public void ClosureTest()
        {
            var aTerminal       = new TerminalPredicate(makeIdentifierPredicate("a"));
            var nt_S            = new KeyedNonTerminalPredicate("S");
            var nt_A            = new KeyedNonTerminalPredicate("A");
            var nt_B            = new KeyedNonTerminalPredicate("B");
            var nt_C            = new KeyedNonTerminalPredicate("C");
            var productionRules = new List <ProductionRule>();

            productionRules.Add(new ProductionRule("S", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_A, nt_B
            }));
            productionRules.Add(new ProductionRule("A", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_S
            }));
            productionRules.Add(new ProductionRule("A", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_C
            }));
            productionRules.Add(new ProductionRule("B", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_A
            }));
            productionRules.Add(new ProductionRule("C", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_S
            }));
            productionRules.Add(new ProductionRule("C", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                aTerminal
            }));
            var d      = CommonUtils.MakeProductionRuleDictionary(productionRules);
            var result =
                CommonUtils.Closure(new HashSet <Item>()
            {
                new Item()
                {
                    ProductionRule = productionRules[0], Cursor = 1
                }
            },
                                    d);

            Assert.AreEqual(7, result.Count);
        }
Exemple #2
0
        public void FirstAndFollowSetCalculation()
        {
            var aTerminal       = new TerminalPredicate(makeIdentifierPredicate("a"));
            var bTerminal       = new TerminalPredicate(makeIdentifierPredicate("b"));
            var dTerminal       = new TerminalPredicate(makeIdentifierPredicate("d"));
            var gTerminal       = new TerminalPredicate(makeIdentifierPredicate("g"));
            var hTerminal       = new TerminalPredicate(makeIdentifierPredicate("h"));
            var nt_S            = new KeyedNonTerminalPredicate("S");
            var nt_A            = new KeyedNonTerminalPredicate("A");
            var nt_B            = new KeyedNonTerminalPredicate("B");
            var nt_C            = new KeyedNonTerminalPredicate("C");
            var productionRules = new List <ProductionRule>();

            productionRules.Add(new ProductionRule("S", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_A, nt_C, nt_B
            }));
            productionRules.Add(new ProductionRule("S", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_C, bTerminal, bTerminal
            }));
            productionRules.Add(new ProductionRule("S", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_B, aTerminal
            }));
            productionRules.Add(new ProductionRule("A", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                dTerminal, aTerminal
            }));
            productionRules.Add(new ProductionRule("A", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                nt_B, nt_C
            }));
            productionRules.Add(new ProductionRule("B", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                gTerminal
            }));
            productionRules.Add(new ProductionRule("B", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                EpsilonPredicate.Instance
            }));
            productionRules.Add(new ProductionRule("C", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                hTerminal
            }));
            productionRules.Add(new ProductionRule("C", typeof(SyntaxNode), new List <SyntaxPredicate>()
            {
                EpsilonPredicate.Instance
            }));
            var first = CommonUtils.CalculateFirstSet(productionRules);

            Assert.AreEqual(4, first.Count);
            Assert.AreEqual(true, first["S"].SetEquals(new List <SyntaxPredicate>()
            {
                aTerminal, bTerminal, dTerminal, gTerminal, hTerminal, EpsilonPredicate.Instance
            }));
            Assert.AreEqual(true, first["A"].SetEquals(new List <SyntaxPredicate>()
            {
                dTerminal, gTerminal, hTerminal, EpsilonPredicate.Instance
            }));
            Assert.AreEqual(true, first["B"].SetEquals(new List <SyntaxPredicate>()
            {
                gTerminal, EpsilonPredicate.Instance
            }));
            Assert.AreEqual(true, first["C"].SetEquals(new List <SyntaxPredicate>()
            {
                hTerminal, EpsilonPredicate.Instance
            }));
            var follow = CommonUtils.CalculateFollowSet(productionRules, first);

            Assert.AreEqual(4, follow.Count);
            Assert.AreEqual(true, follow["S"].SetEquals(new List <SyntaxPredicate>()));
            Assert.AreEqual(true, follow["A"].SetEquals(new List <SyntaxPredicate>()
            {
                hTerminal, gTerminal
            }));
            Assert.AreEqual(true, follow["B"].SetEquals(new List <SyntaxPredicate>()
            {
                aTerminal, hTerminal, gTerminal
            }));
            Assert.AreEqual(true, follow["C"].SetEquals(new List <SyntaxPredicate>()
            {
                bTerminal, hTerminal, gTerminal
            }));
        }