Пример #1
0
        private static void TraversePlay()
        {
            var g = new Grammar(new List <Production> {
                CFGParser.Production("<A> → <B>"),
                CFGParser.Production("<B> → <A>"),
                CFGParser.Production("<B> → 'x'"),
            }, Nonterminal.Of("A"));

            g = IdentityActions.Annotate(g);

            var earley2  = new EarleyParser2(g);
            var sentence = Sentence.FromWords("x");
            var sppf2    = earley2.ParseGetForest(sentence);

            DotRunner.Run(DotBuilder.GetRawDot(sppf2), "infinite");

            var t2 = new Traversal(sppf2, g);
            var r2 = t2.Traverse();
            //foreach (var option in r2) {
            //	var s2 = (Sentence)option.Payload;
            //	if (!sentence.SequenceEqual(s2)) {
            //		throw new Exception();
            //	}
            //}
        }
Пример #2
0
        private static void ExecuteTest(Grammar g, string input)
        {
            var ag       = IdentityActions.Annotate(g);
            var earley1  = new EarleyParser(ag);
            var earley2  = new EarleyParser2(ag);
            var sentence = Sentence.FromWords(input);

            var sppf1 = earley1.ParseGetForest(sentence);
            var sppf2 = earley2.ParseGetForest(sentence);

            CheckTraversal(ag, sentence, sppf1);
            CheckTraversal(ag, sentence, sppf2);
        }
Пример #3
0
        private (Grammar, List <Terminal>) NextGrammar()
        {
            var numNonterminals     = _r.Next(1, _maxNonterminals);
            var numProductions      = _r.Next(1, _maxProductions);
            var maxProductionLength = _maxProductionLength;
            var numTerminals        = _r.Next(1, _maxTerminals);

            var range     = Enumerable.Range(0, numTerminals);
            var terminals = new List <Terminal>(range.Select((x) => Terminal.Of("x" + x)));

            Grammar g = null;

            for (int i = 0; i < 200; i++)
            {
                g = _gramGen.NextCFG(numNonterminals, numProductions, maxProductionLength, terminals, true);
                if (g.Productions.Count() > 0)
                {
                    break;
                }
            }
            g = IdentityActions.Annotate(g);
            return(g, terminals);
        }