public static void Do() { // S -> aSa | bSb | ε var productions = new List<Production> { // construct productions by passing arguments... new Production( lhs: Nonterminal.Of("S"), rhs: new Sentence { Terminal.Of("a"), Nonterminal.Of("S"), Terminal.Of("a") }, weight: 20 ), // or from a string... CFGParser.Production(@"<S> -> 'b' <S> 'b' [10]"), CFGParser.Production(@"<S> -> ε [5]"), }; var cfg = new Grammar(productions, Nonterminal.Of("S")); // var cnf = cfg.ToCNF(); //var probs = cfg.EstimateProbabilities(1000000); //foreach (var entry in probs) { // var key = entry.Key; // var value = entry.Value; // if (key.Length <= 4) { // Console.WriteLine("{0}: {1}", key, value); // } //} // Print out the new CNF grammar // Console.WriteLine(cnf); var ep = new EarleyParser(cfg); // var cp = new CykParser(cnf); // Does this grammar accept the string "aabb"? Console.WriteLine("aabb: {0}", ep.ParseGetProbability(Sentence.FromLetters("aabb"))); // How about "abba"? Console.WriteLine("abba: {0}", ep.ParseGetProbability(Sentence.FromLetters("abba"))); Console.WriteLine(ep.ParseGetForest(Sentence.FromLetters("abba"))); for (int i = 0; i < 5; i++) { Console.WriteLine(cfg.ProduceRandom().AsTerminals()); } var sentences = cfg.ProduceToDepth(3); foreach (var sentence in sentences) { Console.WriteLine(sentence.Value.AsTerminals()); } var gg = new GrammarGenerator(1); var terminals = new List<Terminal> { Terminal.Of("a"), Terminal.Of("b") }; var randGram = gg.NextCFG( numNonterminals: 4, numProductions: 10, maxProductionLength: 4, terminals: terminals ); Console.WriteLine(randGram); }
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); }
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); }
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(); }
public static void Do() { // S -> aSa | bSb | ε var productions = new List <Production> { // construct productions by passing arguments... new Production( lhs: Nonterminal.Of("S"), rhs: new Sentence { Terminal.Of("a"), Nonterminal.Of("S"), Terminal.Of("a") }, weight: 20 ), // or from a string... CFGParser.Production(@"<S> -> 'b' <S> 'b' [10]"), CFGParser.Production(@"<S> -> ε [5]"), }; var cfg = new Grammar(productions, Nonterminal.Of("S")); // var cnf = cfg.ToCNF(); //var probs = cfg.EstimateProbabilities(1000000); //foreach (var entry in probs) { // var key = entry.Key; // var value = entry.Value; // if (key.Length <= 4) { // Console.WriteLine("{0}: {1}", key, value); // } //} // Print out the new CNF grammar // Console.WriteLine(cnf); var ep = new EarleyParser(cfg); // var cp = new CykParser(cnf); // Does this grammar accept the string "aabb"? Console.WriteLine("aabb: {0}", ep.ParseGetProbability(Sentence.FromLetters("aabb"))); // How about "abba"? Console.WriteLine("abba: {0}", ep.ParseGetProbability(Sentence.FromLetters("abba"))); Console.WriteLine(ep.ParseGetForest(Sentence.FromLetters("abba"))); for (int i = 0; i < 5; i++) { Console.WriteLine(cfg.ProduceRandom().AsTerminals()); } var sentences = cfg.ProduceToDepth(3); foreach (var sentence in sentences) { Console.WriteLine(sentence.Value.AsTerminals()); } var gg = new GrammarGenerator(1); var terminals = new List <Terminal> { Terminal.Of("a"), Terminal.Of("b") }; var randGram = gg.NextCFG( numNonterminals: 4, numProductions: 10, maxProductionLength: 4, terminals: terminals ); Console.WriteLine(randGram); }
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(); }
private bool ProcessOneGrammar() { var(g, terminals) = NextGrammar(); var h = g.ToCNF(); // Console.WriteLine(g.Productions.Count()); var preparedSentences = new List <Sentence>(); for (int length = 0; length <= _maxInputLength; length++) { var combinations = CFGLibTest.Helpers.CombinationsWithRepetition(terminals, length); foreach (var target in combinations) { var sentence = new Sentence(target); preparedSentences.Add(sentence); } } AddRandomSentences(preparedSentences, terminals); var uniquifySentences = new Dictionary <string, Sentence>(); foreach (var sentence in preparedSentences) { uniquifySentences[sentence.AsTerminals()] = sentence; } preparedSentences = uniquifySentences.Values.ToList(); // Console.WriteLine("Parsing sentences..."); EarleyParser earley1; EarleyParser2 earley2; CykParser cyk; try { earley1 = new EarleyParser(g); earley2 = new EarleyParser2(g); cyk = new CykParser(h); } catch (Exception e) { Report(g, e); return(true); } foreach (var sentence in preparedSentences) { try { var sppf1 = earley1.ParseGetForest(sentence); var sppf2 = earley2.ParseGetForest(sentence); if (sppf1 == null && sppf2 != null) { throw new Exception(); } if (sppf2 == null && sppf1 != null) { throw new Exception(); } var p1 = earley1.ProbabilityOfSppf(sppf1); var p2 = earley2.ProbabilityOfSppf(sppf2); var p3 = cyk.ParseGetProbability(sentence); if (!Helpers.IsNear(p1, p2)) { throw new Exception(); } if (!Helpers.IsNear(p1, p3)) { throw new Exception(); } try { TestTraversal.CheckTraversal(g, sentence, sppf1); } catch (TraversalLoopException) { } try { TestTraversal.CheckTraversal(g, sentence, sppf2); } catch (TraversalLoopException) { } } catch (Exception e) { Report(g, e, sentence); return(true); // throw new RandomTestException(e, g, sentence); } } return(false); }