예제 #1
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);
        }
예제 #2
0
        private static void EbnfPlay()
        {
            var      input         = Sentence.FromLetters(Grammars.Properties.Resources.Ebnf_bench);
            Sentence inputNoLayout = Ebnf.RemoveLayout(input, out var layoutSppf);

            //DotRunner.Run(DotBuilder.GetRawDot(layoutSppf), "arithmetic_ebnf_layout");
            Console.WriteLine(inputNoLayout.AsTerminals());
            var layoutGrammar = Ebnf.GrammarSyntax();
            var earley        = new EarleyParser2(layoutGrammar);

            var sppf = earley.ParseGetForest(inputNoLayout);

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

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

            //var traversal = new Traversal(sppf, input, layoutGrammar);
            //var result = traversal.Traverse();
            //if (result.Count() != 1) {
            //	throw new Exception();
            //}
            //var inputNoLayout = new Sentence((List<Terminal>)result.First().Payload);
            //return inputNoLayout;

            //Console.WriteLine(inputNoLayout);
        }
예제 #3
0
        private static Tuple <Sentence, double> EbnfBenchLayout()
        {
            var      sw            = Stopwatch.StartNew();
            var      input         = Sentence.FromLetters(Grammars.Properties.Resources.Ebnf_bench);
            Sentence inputNoLayout = Ebnf.RemoveLayout(input, out var layoutSppf);
            var      ms1           = sw.Elapsed.TotalMilliseconds;

            Console.WriteLine("Layout: {0:0.#}ms", ms1);
            return(Tuple.Create(inputNoLayout, ms1));
        }
예제 #4
0
        private static void EbnfParse(Sentence sentence)
        {
            var noLayoutSentence = Ebnf.RemoveLayout(sentence, out var layoutSppf);

            var g = Ebnf.GrammarSyntax();
            //var earley = new EarleyParser(g);
            var earley2 = new EarleyParser2(g);

            //var sppf1 = earley.ParseGetForest(noLayoutSentence);
            //Assert.IsNotNull(sppf1);
            var sppf2 = earley2.ParseGetForest(noLayoutSentence);

            Assert.IsNotNull(sppf2);
        }