Exemplo n.º 1
0
        public void TestStateDistinguisher_EnumerateAllDistinguishingSequences()
        {
            var solver = ContextFreeGrammar.GetContext();
            var regex  = "^([0-9]{4,}|[3-9][0-9]{2}|2[5-9][0-9])$";

            string[] regexes           = File.ReadAllLines(regexesFile);
            var      nfa               = solver.Convert(regex).RemoveEpsilons();
            var      dfa               = nfa.Determinize();//.MakeTotal();
            var      mfa               = dfa.Minimize();
            var      dist              = new StateDistinguisher <BDD>(dfa, x => (char)x.GetMin(), false);
            int      minimalStateCount = mfa.StateCount;
            var      distinguishers    = new Dictionary <Tuple <int, int>, List <ConsList <BDD> > >();

            foreach (int p in mfa.GetStates())
            {
                foreach (int q in mfa.GetStates())
                {
                    if (p != q)
                    {
                        var pq = (p < q ? new Tuple <int, int>(p, q) : new Tuple <int, int>(q, p));
                        if (!distinguishers.ContainsKey(pq))
                        {
                            distinguishers[pq] = new List <ConsList <BDD> >(dist.EnumerateAllDistinguishingSequences(p, q));
                        }
                    }
                }
            }
            Assert.AreEqual <int>((mfa.StateCount * (mfa.StateCount - 1)) / 2, distinguishers.Count);
        }
        public WordsInGrammarProblem Generate(int targetLevel)
        {
            ContextFreeGrammar grammar = null;
            var inNeeded  = 1;
            var outNeeded = 1;

            // We iterate until we get a CNF
            grammar = GrammarGenerator.GenerateRandom();
            while (GrammarUtilities.getEquivalentCNF(grammar) == null)
            {
                grammar = GrammarGenerator.GenerateRandom();
            }

            inNeeded  = rnd.Next(1, 5);
            outNeeded = rnd.Next(1, 5);
            if (targetLevel == Generation.HIGH)
            {
                inNeeded  = rnd.Next(3, 5);
                outNeeded = rnd.Next(3, 5);
            }
            else if (targetLevel == Generation.MEDIUM)
            {
                inNeeded  = rnd.Next(2, 4);
                outNeeded = rnd.Next(2, 4);
            }
            else if (targetLevel == Generation.LOW)
            {
                inNeeded  = rnd.Next(1, 4);
                outNeeded = rnd.Next(1, 4);
            }
            return(new WordsInGrammarProblem(grammar, inNeeded, outNeeded));
        }
        public void Should_check_grammar_for_chomsky_normal_form()
        {
            var grammar = new ContextFreeGrammar("S");
            grammar.Produce("S", "t1", "t2", "t3");

            Assert.That(grammar.IsInChomskyNormalForm, Is.False);
        }
Exemplo n.º 4
0
        public void TestCFG_IntersectionWithRegexOptions()
        {
            var input = @"S -> \( S \) | (a)";
            ContextFreeGrammar cfg = ContextFreeGrammar.Parse(input);

            int    k = 5;
            string w;

            Assert.IsTrue(cfg.IntersectsWith(@"a.{" + k + "}", out w));

            var s = "";

            for (int i = 0; i < k; i++)
            {
                s += "(";
            }
            s += "a";
            for (int i = 0; i < k; i++)
            {
                s += ")";
            }

            Assert.AreEqual <string>(s, w);

            //determinization causes blowup of states in this case
            //will not terminate if determinization of regex is set to true, for large k
            Assert.IsTrue(cfg.IntersectsWith(@"a.{" + k + "}", out w, true));

            Assert.AreEqual <string>(s, w);
        }
        Tuple <HashSet <Nonterminal>, List <Tuple <Production, int> > >[][] cykTable; //resulting CYK table

        public CYKProblem(ContextFreeGrammar grammar, string word)
        {
            this.grammar = GrammarUtilities.getEquivalentCNF(grammar);
            this.word    = word;

            this.cykTable = GrammarUtilities.cyk(grammar, word);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Converts the given cfg into a new one in 2NF, that means that every production has at most 2 symbols on the right hand side
        /// </summary>
        /// <param name="cfg">cfg</param>
        /// <returns>cfg in 2NF</returns>
        public static ContextFreeGrammar To2NF(ContextFreeGrammar cfg)
        {
            var productions = new List<Production>();

            int currentNumber = 0;

            var productionsToShorten = new Queue<Production>(cfg.GetProductions());
            while (productionsToShorten.Count() > 0)
            {
                var next = productionsToShorten.Dequeue();
                if (next.Rhs.Length <= 2)
                {
                    productions.Add(next);
                }
                else
                {
                    var newNonTerminal = new Nonterminal(PrefixOfAddedNonterminals + currentNumber);
                    currentNumber++;

                    Assertion.Assert(!cfg.Variables.Any(v => v.Name.Equals(newNonTerminal.Name)), 
                        string.Format("The nonterminal with id {0} already existed. Please ensure, that the CFG does not use ints as ids", newNonTerminal.Name));

                    productions.Add(new Production(next.Lhs, next.Rhs[0], newNonTerminal));
                    productionsToShorten.Enqueue(new Production(newNonTerminal, next.Rhs.Skip(1).ToArray()));
                }
            }

            return new ContextFreeGrammar(cfg.StartSymbol, productions);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Creates a new parse table from the given context free grammar.
        /// </summary>
        /// <param name="cfg"></param>
        public ParseTable(ContextFreeGrammar <T> cfg)
        {
            ActionTable = new Table <int, Terminal <T>, List <ParserAction <T> > >();
            GotoTable   = new Table <int, NonTerminal <T>, int?>();

            buildParseTable(cfg.CreateStateGraph(), cfg.StartElement);
        }
 public PerformanceCFG(ContextFreeGrammar cfg)
 {
     Cfg = cfg;
     InitIds();
     BinaryProductions = Cfg.GetProductions().Where(p => p.Rhs.Count() == 2).ToList();
     InitializeProductionsByGrammarSymbolsInRightHandSide();
 }
Exemplo n.º 9
0
 public FindDerivationProblem(ContextFreeGrammar grammar, string word, int derivationType, List <GrammarSymbol[]> derivation)
 {
     this.grammar        = grammar;
     this.word           = word;
     this.derivationType = derivationType;
     this.derivation     = derivation;
 }
Exemplo n.º 10
0
        public void TestStateDistinguisher()
        {
            var solver = ContextFreeGrammar.GetContext();
            var A      = solver.Convert("A.*A").Determinize().RemoveEpsilons();
            var B      = solver.Convert("B.*B.*B").Determinize().RemoveEpsilons();
            var AB     = A.Intersect(B);
            var states = new List <int>(AB.GetStates());
            var dist   = new StateDistinguisher_Moore <BDD>(AB, bdd => (char)bdd.GetMin());

            //check suffix-closedness
            foreach (var s in dist.GetAllDistinguishingStrings())
            {
                for (int i = 0; i < s.Length; i++)
                {
                    Assert.IsTrue(dist.IsDistinguishingString(s.Substring(i)));
                }
            }
            //check that the states in the minimized automaton are all distiguishable
            var ABMin = AB.Minimize();
            //here we know that the minimization algorithm keeps the same state ids
            Func <int, int, Tuple <int, int> > MkPair = (x, y) => (x <= y ? new Tuple <int, int>(x, y) : new Tuple <int, int>(y, x));

            foreach (var p in ABMin.States)
            {
                foreach (var q in ABMin.States)
                {
                    if (p != q)
                    {
                        string s;
                        Assert.IsTrue(dist.AreDistinguishable(p, q, out s));
                    }
                }
            }
        }
Exemplo n.º 11
0
        public void TestCFG_PDAConstruction_IntersectMultipleRegexes()
        {
            var input               = @"S -> \( S \) | (A) | (BC)+ ";
            var cfg                 = ContextFreeGrammar.Parse(input);
            var pda                 = cfg.ToPDA <BDD>();
            var aut_contains_2LPs   = cfg.BuiltinTerminalAlgebra.Convert(@"\({2}").RemoveEpsilons();
            var aut_contains_two_Bs = cfg.BuiltinTerminalAlgebra.Convert("B.*B").RemoveEpsilons();
            var aut_contains_A      = cfg.BuiltinTerminalAlgebra.Convert("A").RemoveEpsilons();
            var pda2                = pda.Intersect(aut_contains_2LPs, aut_contains_two_Bs);

            BDD[] w;
            Assert.IsTrue(pda2.IsNonempty(out w));
            string s = cfg.BuiltinTerminalAlgebra.ChooseString(w);

            Assert.AreEqual <string>("((BCBC))", s);
            //---
            var pda3 = pda.Intersect(aut_contains_2LPs, aut_contains_two_Bs, aut_contains_A);

            Assert.IsFalse(pda3.IsNonempty(out w));
            //---
            var pda4 = pda.Intersect(aut_contains_2LPs, aut_contains_A);

            Assert.IsTrue(pda4.IsNonempty(out w));
            s = cfg.BuiltinTerminalAlgebra.ChooseString(w);
            Assert.AreEqual <string>("((A))", s);
        }
Exemplo n.º 12
0
        public void TestCFG_Overlaps()
        {
            var input =
                @"S -> \( S \) | [abx] [bxd] [xde] 
";
            ContextFreeGrammar cfg = GrammarParser <BDD> .Parse(MkAutomaton, input);

            Assert.IsNotNull(cfg.BuiltinTerminalAlgebra);
            Assert.AreEqual("S", cfg.StartSymbol.Name);
            Assert.AreEqual(1, cfg.NonterminalCount);
            Assert.AreEqual(2, cfg.ProductionCount);
            Assert.IsFalse(cfg.IsInGNF());

            var aut = MkAutomaton(@"\((xx)+\)");

            BDD[] witness;
            var   no = cfg.Overlaps <BDD>(aut, out witness);

            Assert.IsFalse(no);

            var aut2 = MkAutomaton(@"\(x+\)");

            BDD[] witness2 = null;
            var   yes      = cfg.Overlaps <BDD>(aut2, out witness2);

            Assert.IsTrue(yes);
            string concrete_witness = new string(Array.ConvertAll(witness2, GetChar));

            Assert.AreEqual <string>("(xxx)", concrete_witness);
        }
Exemplo n.º 13
0
        public void TestCFG_Parse_Medium()
        {
            var input = @"
S -> NAME MAIN
MAIN -> SEARCH_CONDITION 
SEARCH_CONDITION -> (OR) PREDICATE | (AND) PREDICATE 
PREDICATE -> COMPARISON_PREDICATE | BETWEEN_PREDICATE | LIKE_PREDICATE | TEST_FOR_NULL | IN_PREDICATE | ALL_OR_ANY_PREDICATE | EXISTENCE_TEST
COMPARISON_PREDICATE -> SCALAR_EXP COMPARISON_OP SCALAR_EXP | SCALAR_EXP COMPARISON SUBQUERY
BETWEEN_PREDICATE -> SCALAR_EXP BETWEEN SCALAR_EXP (AND) SCALAR_EXP
LIKE_PREDICATE ->  SCALAR_EXP (LIKE) ATOM 
TEST_FOR_NULL -> COLUMN_REF (IS) (NULL)
IN_PREDICATE -> SCALAR_EXP (IN) \( SUBQUERY \) | SCALAR_EXP (IN) \( ATOM \) 
ALL_OR_ANY_PREDICATE -> SCALAR_EXP COMPARISON_OP ALL_ANY_SOME SUBQUERY
EXISTENCE_TEST -> (EXISTS) SUBQUERY
SCALAR_EXP ->  SCALAR_EXP OP SCALAR_EXP | ATOM | COLUMN_REF  | \( SCALAR_EXP \) 
ATOM -> PARAMETER | INTNUM 
SUBQUERY -> SELECT_EXP
SELECT_EXP -> (SELECT) NAME
ALL_ANY_SOME -> (ANY) | (ALL) | (SOME)
COLUMN_REF -> NAME
PARAMETER -> NAME
INTNUM -> (\d*)
OP -> (\+)  | (-) | (\*) | (\/) 
COMPARISON_OP -> (=) | (<) | (>)  
NAME -> (\w*)
";
            var cfg   = ContextFreeGrammar.Parse(input);

            Assert.IsTrue(cfg.ProductionCount == 46);
        }
        private static void InitIds(ContextFreeGrammar cfg)
        {
            int i = 1;

            foreach (var v in cfg.Variables.Concat(cfg.GetNonVariableSymbols()).ToList())
            {
                v.UnqiueId = i++;
            }
        }
Exemplo n.º 15
0
        public void GrammarGradingTest1() //   balanced parenthesis (wordsInGrammar)        !!!could change if grading scale is changed!!!
        {
            String             sg1 = "S -> S S|(S)|";
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            var res = GrammarGrading.gradeWordsInGrammar(g, new[] { "()", "())()()(", "()((" }, new[] { "()(", "())()(", "xyz" }, 10);

            Assert.IsTrue(res.Item1 == 5);
        }
Exemplo n.º 16
0
 public void Should_not_allow_creation_of_topdown_parser_with_invalid_grammar()
 {
     Assert.That(() =>
     {
         var grammar = new ContextFreeGrammar("S");
         grammar.Produce("S", "A", "B");
         new TopDownParser(grammar);
     }, Throws.ArgumentException);
 }
        private static ContextFreeGrammar RemoveNonGeneratingSymbols(ContextFreeGrammar cfg, CancellationToken token)
        {
            InitIds(cfg);

            var res = new HashSet <int>(cfg.GetNonVariableSymbols().Select(v => v.UnqiueId));

            var remainingProductions = new Dictionary <int, List <Production> >();

            foreach (var s in cfg.Variables)
            {
                remainingProductions[s.UnqiueId] = new List <Production>();
            }
            foreach (var prod in cfg.GetProductions())
            {
                remainingProductions[prod.Lhs.UnqiueId].Add(prod);
            }

            int oldLength;

            do
            {
                token.ThrowIfCancellationRequested();

                oldLength = res.Count;

                var newSymbols = new HashSet <int>();

                foreach (var e in remainingProductions)
                {
                    token.ThrowIfCancellationRequested();

                    if (e.Value.Any(p => p.Rhs.All(s => res.Contains(s.UnqiueId))))
                    {
                        res.Add(e.Key);
                        newSymbols.Add(e.Key);
                    }
                }

                foreach (var s in newSymbols)
                {
                    token.ThrowIfCancellationRequested();

                    remainingProductions.Remove(s);
                }
            }while (res.Count > oldLength);

            if (!res.Contains(cfg.StartSymbol.UnqiueId))
            {
                return(null);
            }
            else
            {
                var prods = FilterProductions(res, cfg.GetProductions());
                return(new ContextFreeGrammar(cfg.StartSymbol, prods));
            }
        }
        public void Should_delete_all_occurrences_of_variable_if_it_produces_only_epsilon()
        {
            var grammar = new ContextFreeGrammar("S")
                .Produce("S", "vA", "t1")
                .Produce("A")
                .ToChomskyNormalForm();
            Debug.WriteLine(grammar);

            Assert.That(grammar.Variables, Has.None.EqualTo(new Variable("A")));
        }
Exemplo n.º 19
0
        public void TestCFG_EmptyRHS()
        {
            var input = "S -> (x) S (y) | () | (a)";
            ContextFreeGrammar cfg = ContextFreeGrammar.Parse(input);

            TestCFG_EmptyRHS_check(cfg);
            ContextFreeGrammar cfg_ = ContextFreeGrammar.Parse(cfg.ToString());

            TestCFG_EmptyRHS_check(cfg_);
        }
Exemplo n.º 20
0
        public void TestCFG_MultipleRegexesAsTerminals()
        {
            var input = @"S -> \( S \) | \x20 | (cd) | (a+b+)";
            ContextFreeGrammar cfg = ContextFreeGrammar.Parse(input);

            Assert.IsTrue(cfg.NonterminalCount == 5);
            string w;

            Assert.IsTrue(cfg.IntersectsWith(@"\)", out w));
            Assert.AreEqual <string>("( )", w);
        }
Exemplo n.º 21
0
 /// <summary>
 /// builds a filled CYK-table out of the cfg
 /// </summary>
 /// <param name="word"></param>
 /// <param name="cfg">cfg in 2NF</param>
 public CYKTable(A[] word, ContextFreeGrammar cfg)
 {
     this.word = word;
     this.cfg  = new PerformanceCFG(cfg);
     InitializeCykTable();
     CalculateEpsilonSymbols();
     if (word.Length > 0)
     {
         FillTable();
     }
 }
 public void BeforeEveryTest()
 {
     _grammar = new ContextFreeGrammar("S");
     _grammar.Produce("S");
     _grammar.Produce("S", "t2");
     _grammar.Produce("S", "vA", "vT");
     _grammar.Produce("A", "t0");
     _grammar.Produce("A", "t1");
     _grammar.Produce("T", "vS", "vB");
     _grammar.Produce("B", "t1");
 }
Exemplo n.º 23
0
        public void BeforeEveryTest()
        {
            var grammar = new ContextFreeGrammar('S');
            grammar.Produce("S", "t2");
            grammar.Produce("S", "vA", "vT");
            grammar.Produce("A", "t0");
            grammar.Produce("T", "vS", "vB");
            grammar.Produce("B", "t1");

            _parser = new TopDownParser(grammar);
        }
Exemplo n.º 24
0
        public void EqualityTest2()
        {
            String             sg1 = "S->aSb|absjjfhghs|";
            String             sg2 = "S->aSb|aaSbb|";
            ContextFreeGrammar g1  = GrammarParser <char> .Parse(f1, sg1);

            ContextFreeGrammar g2 = GrammarParser <char> .Parse(f1, sg2);

            var res = GrammarUtilities.findDifferenceWithTimelimit(g1, g2, true, 100);

            Assert.IsTrue(res.Item2[0].Equals("absjjfhghs") && res.Item3.Count == 0);
        }
Exemplo n.º 25
0
        public void EqualityTest4() //   (a|b|c|d|e|f|g|h|i|j)*
        {
            String             sg1 = "S->|a|b|c|d|e|f|g|h|i|j|S S";
            String             sg2 = "S->X|X S|   X->a|b|c|d|e|f|g|h|i|j";
            ContextFreeGrammar g1  = GrammarParser <char> .Parse(f1, sg1);

            ContextFreeGrammar g2 = GrammarParser <char> .Parse(f1, sg2);

            var res = GrammarUtilities.findDifferenceWithTimelimit(g1, g2, true, 50);

            Assert.IsTrue(res.Item2.Count == 0 && res.Item3.Count == 0);
        }
Exemplo n.º 26
0
        public void EqualityTest5() //   (a|b)^n
        {
            String             sg1 = "S -> a|b|aa|aS|bS|bbbbS";
            String             sg2 = "S->X|X S   X->a|b";
            ContextFreeGrammar g1  = GrammarParser <char> .Parse(f1, sg1);

            ContextFreeGrammar g2 = GrammarParser <char> .Parse(f1, sg2);

            var res = GrammarUtilities.findDifferenceWithTimelimit(g1, g2, true, 100);

            Assert.IsTrue(res.Item2.Count == 0 && res.Item3.Count == 0);
        }
Exemplo n.º 27
0
        public void EqualityTest6() //   balanced parenthesis
        {
            String             sg1 = "S -> S S|(S)|";
            String             sg2 = "X -> | X X | (L    L->X) | X X)";
            ContextFreeGrammar g1  = GrammarParser <char> .Parse(f1, sg1);

            ContextFreeGrammar g2 = GrammarParser <char> .Parse(f1, sg2);

            var res = GrammarUtilities.findDifferenceWithTimelimit(g1, g2, true, 100);

            Assert.IsTrue(res.Item2.Count == 0 && res.Item3.Count == 0);
        }
Exemplo n.º 28
0
        public void TestCFG_EmptyRHS_check(ContextFreeGrammar cfg)
        {
            Assert.AreEqual("S", cfg.StartSymbol.Name);
            Assert.AreEqual(1, cfg.NonterminalCount);
            Assert.AreEqual(3, cfg.ProductionCount);

            string w;

            Assert.IsFalse(cfg.IntersectsWith(@"^x+a$", out w));
            Assert.IsTrue(cfg.IntersectsWith(@"^x+ay+$", out w));
            Assert.AreEqual <string>("xay", w);
        }
Exemplo n.º 29
0
        public void prefixTest1() //   balanced parenthesis
        {
            String             sg1 = "S -> S S|(S)|";
            ContextFreeGrammar g   = GrammarParser <char> .Parse(f1, sg1);

            g = GrammarUtilities.getEquivalentCNF(g);

            Assert.AreEqual(-2, GrammarUtilities.longestPrefixLength(g, "()(())()()"));  // full
            Assert.AreEqual("()(())".Length, GrammarUtilities.longestPrefixLength(g, "()(()))()()"));
            Assert.AreEqual("()(())((((".Length, GrammarUtilities.longestPrefixLength(g, "()(())(((("));
            Assert.AreEqual("".Length, GrammarUtilities.longestPrefixLength(g, ")(()()())"));
        }
Exemplo n.º 30
0
        public void EqualityTest3() //   a^n b^n
        {
            String             sg1 = "S->aT T->aT U|b U->b";
            String             sg2 = "P->aR R->abb|aRb|b";
            ContextFreeGrammar g1  = GrammarParser <char> .Parse(f1, sg1);

            ContextFreeGrammar g2 = GrammarParser <char> .Parse(f1, sg2);

            var res = GrammarUtilities.findDifferenceWithTimelimit(g1, g2, true, 100);

            Assert.IsTrue(res.Item2.Count == 0 && res.Item3.Count == 0);
        }
Exemplo n.º 31
0
        /// <summary>
        ///     Sets Parse Table from the given grammar.
        /// </summary>
        /// <param name="grammar"></param>
        /// <exception cref="ArgumentNullException">The value of 'grammar' cannot be null. </exception>
        /// <exception cref="InvalidParseTableException">
        ///     Thrown when the given parse table contains either a 'Shift
        ///     Reduce' conflict or a 'Reduce Reduce' conflict.
        /// </exception>
        public virtual void SetParseTable(ContextFreeGrammar <T> grammar)
        {
            if (grammar == null)
            {
                throw new ArgumentNullException("grammar", "The given grammar must be non-null");
            }

            StateGraph <GrammarElement <T>, LRItem <T>[]> graph = grammar.CreateStateGraph();

            SetParseTable(new ParseTable <T>(graph, grammar.StartElement), graph);
            EndOfInputElement = grammar.EndOfInputElement;
        }
Exemplo n.º 32
0
        public void TestCFG_Intersect_Nontrivial()
        {
            var input = @"S -> \( S \) | [\w-[\d]]{3} | 0{2,}";
            ContextFreeGrammar cfg = ContextFreeGrammar.Parse(input);

            string w;

            Assert.IsFalse(cfg.IntersectsWith(@"^\((xy)+\)$", out w));
            Assert.IsTrue(cfg.IntersectsWith(@"^\({4}x+\)+$", out w));
            Assert.AreEqual <string>("((((xxx))))", w);
            Assert.IsTrue(cfg.IntersectsWith(@"^\(+0+\){7}$", out w));
            Assert.AreEqual <string>("(((((((00)))))))", w);
        }
Exemplo n.º 33
0
        public void GrammarDerivationTest() //tests derivation computation in combination with parsing (grammar and derivation)
        {
            String             sg = "S->S S S | x | A | ab |   A -> A A | S | a | ";
            ContextFreeGrammar g  = GrammarParser <char> .Parse(f1, sg);

            var der1 = DerivationParser <char> .Parse(f1, "S \n A \n A A \n A \n A A \n A S \n A \n S \n S S S \n S a b S \n x a b S \n x a b S S S \n x a b x S S \n x a b x x S \n x a b x x x");

            Assert.IsTrue(Derivation.comparator.Equals(der1[0], new GrammarSymbol[] { g.StartSymbol })); //check start
            for (int i = 1; i < der1.Count; i++)
            {
                Assert.IsTrue(Derivation.isValidDerivationStep(g.GetProductions(), der1[i - 1], der1[i])); //check step
            }
        }
        public void Should_eliminate_all_epsilon_rules_when_converting_grammer_to_chomsky_normal_form()
        {
            var grammar = new ContextFreeGrammar("S");
            grammar.Produce("S", "vA", "vB");
            grammar.Produce("A", "t0");
            grammar.Produce("B");
            grammar.Produce("B", "t1");

            var cnf = grammar.ToChomskyNormalForm();
            var rules = cnf.GetRules("B");

            Assert.That(rules, Has.None.Empty);
        }
Exemplo n.º 35
0
        public void TestCFG_PDAConstruction()
        {
            var input = "S -> (x) (y)";
            var cfg   = ContextFreeGrammar.Parse(input);
            var pda   = cfg.ToPDA <BDD>();
            var aut   = ContextFreeGrammar.GetContext().Convert("xy");
            var pda2  = pda.Intersect(aut);

            BDD[] w;
            Assert.IsTrue(pda2.IsNonempty(out w));
            string s = cfg.BuiltinTerminalAlgebra.ChooseString(w);

            Assert.AreEqual <string>("xy", s);
        }
Exemplo n.º 36
0
        public void TestCFG_PDAConstruction_Large()
        {
            var sqlgrammar = System.IO.File.ReadAllText("../../../../Automata.Tests/Samples/grammar-sql.txt");
            var cfg        = ContextFreeGrammar.Parse(sqlgrammar);
            var cfg2       = cfg.RemoveEpsilonsAndUselessSymbols().RemoveUnitProductions();
            var pda        = cfg2.ToPDA <BDD>();

            //var pda2 = pda.Intersect(cfg.BuiltinTerminalAlgebra.Convert("^(SELECT.*)$").RemoveEpsilons());
            //BDD[] w;
            //bool ne = pda2.IsNonempty(out w);
            //var s = cfg.BuiltinTerminalAlgebra.ChooseString(w);
            //Assert.IsTrue(s.StartsWith("SELECT"));
            Assert.IsTrue(cfg.StartSymbol.Name == "sql_clauses");
        }
Exemplo n.º 37
0
        private static void CYKTest()
        {
            string[] nonterminals = new[] { "S" },
                    terminals = new[] { "0", "1" };
            string starting = "S";
            ProductionInfo[] rules = new[] { new ProductionInfo("S", new[] {"0", "S", "1"}),
                                             new ProductionInfo("S", new string[] {}) };

            var contextFreeGrammar = new ContextFreeGrammar(nonterminals, terminals, rules, starting);
            var normalForm = contextFreeGrammar.GetChomskyNormalForm();

            string[] sentence1 = new[] { "0", "1", "1", "0", "0", "0" },
                     sentence2 = new[] { "0", "0", "0", "1", "1", "1" };
            Debug.Assert(!normalForm.HasSentence(sentence1));
            Debug.Assert(normalForm.HasSentence(sentence2));
        }
Exemplo n.º 38
0
        private static void GrammarTest()
        {
            string[] nonterminals = new[] { "S" },
                    terminals = new[] { "0", "1" };
            string starting = "S";
            ProductionInfo[] rules = new[] { new ProductionInfo("S", new string[] {}),
                                        new ProductionInfo("S", new[] { "0", "S", "1"}),
                                        new ProductionInfo("S", new[] { "0", "S", "1"}) };

            try
            {
                var grammar = new ContextFreeGrammar(nonterminals, terminals, rules, starting);
            }
            catch (ArgumentException ex)
            {
                Debug.Assert(ex.Message == "Rules cannot contain duplicates.");
            }
        }
Exemplo n.º 39
0
        public static void Main()
        {
            var grammer = new ContextFreeGrammar("S")
                .Produce("S", "vA", "t1", "vA")
                .Produce("A")
                .Produce("A", "vS")
                .Produce("A", "vA", "t0", "vS")
                .Produce("A", "vS", "t0", "vA");

            Console.WriteLine(grammer);
            TopDownParser parser = grammer;

            Console.WriteLine(grammer.ToChomskyNormalForm().ToString());

            Measure.AndDisplayPerformance("comparison", () =>
            {
                Console.Write("Starting comparison.. ");

                var allWords1 = Permute.AllWordsOver(7, '0', '1').Where(w => w.Where(c => c == '0').Count() < w.Where(c => c == '1').Count()).ToArray();
                var allWords2 = parser.Language(7).ToArray();

                foreach (var word in allWords1)
                {
                    if (!allWords2.Contains(word))
                    {
                        Console.WriteLine("Word {0} not enumerated.", word);
                    }

                    if (!parser.Compute(word))
                    {
                        Console.WriteLine("Word {0} not accepted.", word);
                    }
                }

                Console.WriteLine("Finished.");
            });

            Console.ReadKey(true);

            var stopwatch = Stopwatch.StartNew();
            parser.Compute("001101110");
            Console.WriteLine("Computing '001101110' took {0} ms.", stopwatch.ElapsedMilliseconds);

            //Console.ReadKey(true);

            var performance = Measure.Performance(() => {
            foreach (var word in parser.Language(8))
            {
                var zeros = word.Where(c => c == '0').Count();
                var ones = word.Where(c => c == '1').Count();

                if (ones <= zeros)
                    throw new ArgumentException();

                Console.WriteLine(word);
            }});

            Console.WriteLine("Enumeration took {0} ms", performance.TotalMilliseconds);

            //Console.ReadKey(true);
        }
 public void Should_not_allow_creation_of_recursive_unit_rules()
 {
     var grammar = new ContextFreeGrammar("S");
     Assert.That(() => grammar.Produce("S", "vS"), Throws.Exception);
 }
 public void Should_not_convert_grammar_to_chomsky_normal_form_if_it_is_already_in_chomsky_normal_form()
 {
     var grammar = new ContextFreeGrammar("S");
     Assert.That(grammar.IsInChomskyNormalForm, Is.True);
     Assert.That(grammar.ToChomskyNormalForm(), Is.SameAs(grammar));
 }
        public void Should_not_produce_unit_rules_when_transforming_grammar_to_chomsky_normal_form()
        {
            var grammar = new ContextFreeGrammar("S")
                .Produce("S", "vA")
                .Produce("A", "t1")
                .ToChomskyNormalForm();
            Debug.WriteLine(grammar);

            var existsUnitRules = grammar.GetRules(grammar.StartVariable).Any(r => r.Count() == 1 && r.Single() is Variable);
            Assert.That(existsUnitRules, Is.False);
        }
        public void Should_replace_epsilon_rules_occurences_one_by_one()
        {
            var grammar = new ContextFreeGrammar("S")
                .Produce("S", "vA", "t1", "vA")
                .Produce("A")
                .Produce("A", "t1")
                .ToChomskyNormalForm();
            Debug.WriteLine(grammar);

            var any = grammar.GetRules(grammar.StartVariable).Any(r => r.First().Equals(new Variable("A")));
            Assert.That(any, Is.True);
        }
        public void Should_eliminate_all_unit_rules_when_converting_grammer_to_chomsky_normal_form()
        {
            var grammar = new ContextFreeGrammar("S");
            grammar.Produce("S", "vA", "vB");
            grammar.Produce("A", "vB");
            grammar.Produce("B", "t1");

            var cnf = grammar.ToChomskyNormalForm();
            var rules = cnf.GetRules("A");

            Debug.WriteLine(cnf);

            Assert.That(rules.Single().Single(), Is.Not.EqualTo(new Variable("B")));
        }
 public void Should_determine_wheter_grammar_does_not_accept_empty_word()
 {
     var grammar = new ContextFreeGrammar("S").Produce("S", "t1");
     Assert.That(grammar.AcceptsEmptyWord, Is.False);
 }
 public void Should_determine_wheter_grammar_accepts_empty_word()
 {
     var grammar = new ContextFreeGrammar("S").Produce("S");
     Assert.That(grammar.AcceptsEmptyWord, Is.True);
 }
        public void Should_transform_all_rules_into_proper_form_when_converting_grammer_to_chomsky_normal_form()
        {
            var grammar = new ContextFreeGrammar("S");
            grammar.Produce("S", "t1", "t2", "t3");

            var cnf = grammar.ToChomskyNormalForm();
            var rules = cnf.GetRules("S");

            Assert.That(rules.Select(r => r.ToList()), Has.All.Count.LessThanOrEqualTo(2));
        }