Exemplo n.º 1
0
        private static void ParserGenerator()
        {
            var g      = Ebnf.GrammarLayout();
            var earley = new EarleyParser2(g);

            var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Arithmetic_ebnf);
            // var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Bnf);
            // var sentence2 = Sentence.FromLetters("<S> ::= <S> '+' <S>\r\n<S> ::= '1'\r\n");
            // if (!sentence1.Equals(sentence2)) {          }
            // int index = sentence1.Zip(sentence2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count() + 1;

            var sppf = earley.ParseGetForest(sentence1);

            if (sppf == null)
            {
                throw new Exception();
            }

            DotRunner.Run(DotBuilder.GetRawDot(sppf), "arithmetic_ebnf");

            //var traversal = new Traversal(sppf, sentence1, g);
            //var result = traversal.Traverse();
            //if (result.Count() != 1) {
            //	throw new Exception();
            //}
            //var generatedGrammar = new Grammar((IEnumerable<Production>)result.First().Payload, Nonterminal.Of("S"));
            //Console.WriteLine(generatedGrammar);
        }
Exemplo n.º 2
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();
            //	}
            //}
        }
Exemplo n.º 3
0
        private static void DevelopAnnotations()
        {
            var sw    = Stopwatch.StartNew();
            var input = Sentence.FromLetters(Grammars.Properties.Resources.Arithmetic_annotated);
            var inputWithoutLayout = Ebnf.RemoveLayout(input, out _);
            var grammar            = Ebnf.GrammarSyntax();
            var parser             = new EarleyParser2(grammar);
            var sppf = parser.ParseGetForest(inputWithoutLayout);

            if (sppf == null)
            {
                throw new Exception();
            }
            var ms1 = sw.Elapsed.TotalMilliseconds;

            Console.WriteLine("Parse: {0:0.#}ms", ms1);
            DotRunner.Run(DotBuilder.GetRawDot(sppf), "annotations");

            var traversal     = new Traversal(sppf, grammar);
            var resCollection = traversal.Traverse();

            if (resCollection.Count() > 1)
            {
                throw new Exception("ambiguous");
            }
            var res = resCollection.Single();

            Console.WriteLine(res);
        }
Exemplo n.º 4
0
        // from http://dx.doi.org/10.1016/j.entcs.2008.03.044
        private static void PaperExamples()
        {
            var ex3 = new Grammar(new List <Production> {
                CFGParser.Production("<S> → <A> <T>"),
                CFGParser.Production("<S> → 'a' <T>"),
                CFGParser.Production("<A> → 'a'"),
                CFGParser.Production("<A> → <B> <A>"),
                CFGParser.Production("<B> → ε"),
                CFGParser.Production("<T> → 'b' 'b' 'b'"),
            }, Nonterminal.Of("S"));

            var input = Sentence.FromLetters("abbb");
            var sppf1 = new EarleyParser(ex3).ParseGetForest(input);
            var sppf2 = new EarleyParser2(ex3).ParseGetForest(input);

            DotRunner.Run(DotBuilder.GetRawDot(sppf1), "example3_old");
            DotRunner.Run(DotBuilder.GetRawDot(sppf2), "example3_new");
        }
Exemplo n.º 5
0
        private static void BnfPlay()
        {
            var bnf    = Bnf.Grammar();
            var earley = new EarleyParser2(bnf);

            var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Arithmetic);
            // var sentence1 = Sentence.FromLetters(Grammars.Properties.Resources.Bnf);
            // var sentence2 = Sentence.FromLetters("<S> ::= <S> '+' <S>\r\n<S> ::= '1'\r\n");
            // if (!sentence1.Equals(sentence2)) {          }
            // int index = sentence1.Zip(sentence2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count() + 1;
            var sppf = earley.ParseGetForest(sentence1);

            if (sppf == null)
            {
                throw new Exception();
            }
            DotRunner.Run(DotBuilder.GetRawDot(sppf), "bnfPlay");
        }
Exemplo n.º 6
0
        private static void VisitorPlay()
        {
            var gp = AdditionGrammar(argList => string.Format("({0} + {1})", argList[0].Payload, argList[2].Payload));

            //var actions2 = new Dictionary<Production, IParserAction> {
            //	[p1] = new ParserAction<long>((argList) => (long)(argList[0].Payload) + (long)(argList[2].Payload)),
            //};
            //var termAction2 = new ParserAction<long>(x => Convert.ToInt64(x[0].Payload));
            //foreach (var num in nums) {
            //	actions2[num] = termAction2;
            //}

            var ep = new EarleyParser2(gp);

            var inputString = AdditionInput(5);
            var input       = Sentence.FromWords(inputString);
            var sppf        = ep.ParseGetForest(input);

            var rawdot = DotBuilder.GetRawDot(sppf);

            DotRunner.Run(rawdot, "newSppf");

            Console.WriteLine(sppf.ToString());
            Console.WriteLine();

            Console.WriteLine("Starting Traversal...");
            var trav       = new Traversal(sppf, gp);
            var resultList = trav.Traverse();

            Console.WriteLine("-----------------");
            foreach (var result in resultList)
            {
                Console.WriteLine(result.Payload);
            }

            //Console.WriteLine("-----------------");
            //foreach (var result in new Traversal(sppf, input, gp).Traverse()) {
            //	Console.WriteLine(result.Payload);
            //}
        }
Exemplo n.º 7
0
        private static void DebugGrammar()
        {
            BaseGrammar g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → ε"),
            }, Nonterminal.Of("S"));
            var sentence = Sentence.FromWords("1 + 1 + 1");
            var grammar  = AdditionGrammar(argList => string.Format("({0} + {1})", argList[0].Payload, argList[2].Payload));

            g = grammar;
            var earley  = new EarleyParser(g);
            var earley2 = new EarleyParser2(g);

            //DotRunner.Run(earley.ParseGetForest(sentence).GetRawDot(), "testEarleyOld");
            //DotRunner.Run(earley2.ParseGetForest(sentence).GetRawDot(), "testEarleyNew");
            DotRunner.Run(DotBuilder.GetRawDot(earley.ParseGetForest(sentence)), "testEarleyOld");
            DotRunner.Run(DotBuilder.GetRawDot(earley2.ParseGetForest(sentence)), "testEarleyNew");
            // DotRunner.Run(DotBuilder.GetFlattenedDot(earley2.ParseGetForest(sentence)), "testEarleyFlat");


            // var prob0 = earley.ParseGetProbability(sentence);
            var prob = earley2.ParseGetProbability(sentence);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            //var rand = new Random(0);
            //Experimental.TestSolver(rand);
            // RandomTests.RandomJacobianTest();

            var t     = new TestCFGToCNF();
            var tp    = new TestCFGToCNFEmptyProb();
            var tr    = new RegressionTests();
            var testp = new TestParsing();

            // testp.TestParsing21();
            // testp.TestWeirdSppf06();
            // testp.TestWeirdSppf07();

            // Console.Read();

            //var g = new Grammar(new List<Production>{
            //	CFGParser.Production("<S> → 'x' <X>"),
            //	CFGParser.Production("<S> → <X> 'x'"),
            //	CFGParser.Production("<S> → 'x' 'x'"),
            //	CFGParser.Production("<X> → 'x'"),
            //}, Nonterminal.Of("S"));
            //var g = new Grammar(new List<Production>{
            //	CFGParser.Production("<S> → <S> <S> <S>"),
            //	CFGParser.Production("<S> → 'x'"),
            //	CFGParser.Production("<S> → ε"),
            //}, Nonterminal.Of("S"));

            //var g = new Grammar(new List<Production>{
            //	CFGParser.Production("<S> → <S> <S>"),
            //	CFGParser.Production("<S> → 'b'"),
            //	CFGParser.Production("<S> → ε"),
            //}, Nonterminal.Of("S"));

            var g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → <S> '+' <S>"),
                // CFGParser.Production("<S> → <S> '*' <S>"),
                // CFGParser.Production("<S> → [0-9]+"),
                CFGParser.Production("<S> → '0'"),
                // CFGParser.Production("<S> → '2'"),
            }, Nonterminal.Of("S"));
            //var ests = g.EstimateProbabilities(10000);
            //foreach (var est in ests) {
            //	Console.WriteLine("{0}: {1}", est.Key, est.Value);
            //}

            // 0 + 123 * 72

            var ep   = new EarleyParser(g);
            var sppf = ep.ParseGetForest(Sentence.FromWords("0 + 0 + 0"));

            // var sppf = ep.ParseGetForest(Sentence.FromWords("x x"));
            // var sppf = ep.ParseGetForest(Sentence.FromWords("b"));
            //Console.WriteLine();
            Console.WriteLine(sppf);
            // var dot = ForestHelpers.ToDot(sppf);

            var rawdot = sppf.GetRawDot();

            DotRunner.Run(rawdot, "rawGraph");

            var dot = sppf.ToDot();

            DotRunner.Run(dot, "addition");

            // var dotShared = ForestHelpers.ToDot(sppf, true);
            //var dotShared = sppf.ToDot(true);
            //DotRunner.Run(dotShared, "additionShared");

            //var pp = new PrettyPrinter();
            //sppf.Accept(pp);
            //Console.WriteLine(pp.Result);

            //// Console.WriteLine(sppf.ToStringHelper("", new HashSet<Sppf>()));
            //Console.WriteLine();
            // Readme.Do();
            //var p = CFGParser.Production("<S> -> 'a' [5]");
            //Console.WriteLine(p);

            //Console.Read();
            //return;

            //var rt = new CFGLibTest.RandomTests();
            //var sw = Stopwatch.StartNew();
            //// rt.RandomParsingTest(50000, 4, 3, 5, 4, 6, 1);
            //// rt.RandomParsingTest(500, 10, 5, 30, 8, 6);
            //rt.RandomParsingTest(1, 10, 5, 50, 8, 6);
            //sw.Stop();
            //Console.WriteLine("Elapsed: {0}s", sw.Elapsed.TotalMilliseconds / 1000.0);


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