Ejemplo 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);
        }
Ejemplo n.º 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);
        }
Ejemplo 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);
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        private static void EbnfBench()
        {
            var tup           = EbnfBenchLayout();
            var inputNoLayout = tup.Item1;
            var ms1           = tup.Item2;
            var sw            = Stopwatch.StartNew();

            var grammar = Ebnf.GrammarSyntax();
            var earley  = new EarleyParser2(grammar);
            var sppf    = earley.ParseGetForest(inputNoLayout);
            var ms2     = sw.Elapsed.TotalMilliseconds;

            if (sppf == null)
            {
                throw new Exception();
            }
            Console.WriteLine("Parse: {0:0.#}ms", ms2);
            Console.WriteLine("Total: {0:0.#}ms", ms1 + ms2);
        }