Esempio n. 1
0
        public void Test1()
        {
            string t = " S   ";

            t = t.Trim();


            //arrange
            List <string> nonTerminals = new List <string>()
            {
                "S", "X", "Y"
            };

            List <string> terminals = new List <string>()
            {
                "a", "b", "d"
            };

            List <RuleDTO> rules = new List <RuleDTO>()
            {
                new RuleDTO()
                {
                    LeftPart = "S", RightPart = "X"
                },
                new RuleDTO()
                {
                    LeftPart = "S", RightPart = "Y"
                },
                new RuleDTO()
                {
                    LeftPart = "X", RightPart = "aXab"
                },
                new RuleDTO()
                {
                    LeftPart = "X", RightPart = "ab"
                },
                new RuleDTO()
                {
                    LeftPart = "Y", RightPart = "aYd"
                },
                new RuleDTO()
                {
                    LeftPart = "Y", RightPart = "b"
                },
            };

            GrammarDTO grammar = new GrammarDTO()
            {
                NonTerminals = nonTerminals,
                Terminals    = terminals,
                Rules        = rules
            };

            //act
            bool res = _worker.CheckForLL1(grammar);

            //assert
            Assert.AreEqual(res, false);
        }
Esempio n. 2
0
        public bool CheckForLL1(GrammarDTO grammar)
        {
            _grammar = grammar;

            bool isLL1 = true;

            foreach (var t in _grammar.NonTerminals)
            {
                isLL1 = CheckLL1ForNonTerminal(t);

                if (!isLL1)
                {
                    return(isLL1);
                }
            }
            return(isLL1);
        }
Esempio n. 3
0
        public Grammar GetGrammarModel(GrammarDTO grammar)
        {
            var nonTerminals = grammar.NonTerminals;
            var terminals    = grammar.Terminals;

            var rules = grammar.Rules.Select(x => new Rule()
            {
                LeftPart = x.LeftPart, RightPart = x.RightPart
            });

            return(new Grammar()
            {
                NonTerminals = nonTerminals,
                Terminals = terminals,
                Rules = rules
            });
        }
Esempio n. 4
0
        public (List <(string nonTerminal, List <string> firsts, string follow)> details, bool isLL1) CheckForLL1WithDetails(GrammarDTO grammar)
        {
            _grammar = grammar;

            List <(string nonTerminal, List <string> firsts, string follow)> details = new List <(string nonTerminal, List <string> firsts, string follow)>();

            foreach (var t in _grammar.NonTerminals)
            {
                var firsts = new List <string>();
                var rules  = _grammar.Rules.Where(x => x.LeftPart == t).Select(x => x.RightPart);

                foreach (var r in rules)
                {
                    firsts.Add(GetFirstForRule(r).Aggregate((x, y) => x + y));
                }

                var follow = GetFollow(t).Count() == 0 ? "" : GetFollow(t).Aggregate((x, y) => x + y);

                details.Add((t, firsts, follow));
            }
            bool isLL1 = CheckForLL1(grammar);

            return(details, isLL1);
        }
Esempio n. 5
0
 public async Task <(List <(string nonTerminal, List <string> firsts, string follow)> details, bool isLL1)> CheckForLL1WithDetailsAsync(GrammarDTO grammar)
 {
     _grammar = grammar;
     return(await Task.Run(() => CheckForLL1WithDetails(grammar)));
 }
Esempio n. 6
0
 public async Task <bool> CheckForLL1Async(GrammarDTO grammar)
 {
     _grammar = grammar;
     return(await Task.Run(() => CheckForLL1(grammar)));
 }
Esempio n. 7
0
        public void Test2()
        {
            //arrange
            List <string> nonTerminals = new List <string>()
            {
                "S", "T", "K", "M", "L"
            };

            List <string> terminals = new List <string>()
            {
                "+", "*", "(", ")", "c"
            };

            List <RuleDTO> rules = new List <RuleDTO>()
            {
                new RuleDTO()
                {
                    LeftPart = "S", RightPart = "TK"
                },
                new RuleDTO()
                {
                    LeftPart = "K", RightPart = "+TK"
                },
                new RuleDTO()
                {
                    LeftPart = "K", RightPart = "e"
                },
                new RuleDTO()
                {
                    LeftPart = "T", RightPart = "ML"
                },
                new RuleDTO()
                {
                    LeftPart = "L", RightPart = "*MT"
                },
                new RuleDTO()
                {
                    LeftPart = "L", RightPart = "e"
                },
                new RuleDTO()
                {
                    LeftPart = "M", RightPart = "(S)"
                },
                new RuleDTO()
                {
                    LeftPart = "M", RightPart = "c"
                },
            };

            GrammarDTO grammar = new GrammarDTO()
            {
                NonTerminals = nonTerminals,
                Terminals    = terminals,
                Rules        = rules
            };

            //act
            bool res = _worker.CheckForLL1(grammar);

            //assert
            Assert.AreEqual(res, true);
        }
Esempio n. 8
0
        public void Test2WithDetails()
        {
            //arrange
            List <string> nonTerminals = new List <string>()
            {
                "S", "T", "K", "M", "L"
            };

            List <string> terminals = new List <string>()
            {
                "+", "*", "(", ")", "c"
            };

            List <RuleDTO> rules = new List <RuleDTO>()
            {
                new RuleDTO()
                {
                    LeftPart = "S", RightPart = "TK"
                },
                new RuleDTO()
                {
                    LeftPart = "K", RightPart = "+TK"
                },
                new RuleDTO()
                {
                    LeftPart = "K", RightPart = "e"
                },
                new RuleDTO()
                {
                    LeftPart = "T", RightPart = "ML"
                },
                new RuleDTO()
                {
                    LeftPart = "L", RightPart = "*MT"
                },
                new RuleDTO()
                {
                    LeftPart = "L", RightPart = "e"
                },
                new RuleDTO()
                {
                    LeftPart = "M", RightPart = "(S)"
                },
                new RuleDTO()
                {
                    LeftPart = "M", RightPart = "c"
                },
            };

            GrammarDTO grammar = new GrammarDTO()
            {
                NonTerminals = nonTerminals,
                Terminals    = terminals,
                Rules        = rules
            };

            //act
            var res = _worker.CheckForLL1WithDetails(grammar);

            //assert
            Assert.AreEqual(res.isLL1, true);

            Assert.AreEqual(res.details[0].nonTerminal, "S");
            Assert.AreEqual(res.details[0].firsts[0].OrderBy(x => x), "(c");
            Assert.AreEqual(res.details[0].follow.OrderBy(x => x), ")");
        }