public void Should_check_grammar_for_chomsky_normal_form() { var grammar = new ContextFreeGrammar("S"); grammar.Produce("S", "t1", "t2", "t3"); Assert.That(grammar.IsInChomskyNormalForm, Is.False); }
public void Should_not_allow_creation_of_topdown_parser_with_invalid_grammar() { Assert.That(() => { var grammar = new ContextFreeGrammar("S"); grammar.Produce("S", "A", "B"); new TopDownParser(grammar); }, Throws.ArgumentException); }
public void Should_delete_all_occurrences_of_variable_if_it_produces_only_epsilon() { var grammar = new ContextFreeGrammar("S") .Produce("S", "vA", "t1") .Produce("A") .ToChomskyNormalForm(); Debug.WriteLine(grammar); Assert.That(grammar.Variables, Has.None.EqualTo(new Variable("A"))); }
public void BeforeEveryTest() { _grammar = new ContextFreeGrammar("S"); _grammar.Produce("S"); _grammar.Produce("S", "t2"); _grammar.Produce("S", "vA", "vT"); _grammar.Produce("A", "t0"); _grammar.Produce("A", "t1"); _grammar.Produce("T", "vS", "vB"); _grammar.Produce("B", "t1"); }
public void BeforeEveryTest() { var grammar = new ContextFreeGrammar('S'); grammar.Produce("S", "t2"); grammar.Produce("S", "vA", "vT"); grammar.Produce("A", "t0"); grammar.Produce("T", "vS", "vB"); grammar.Produce("B", "t1"); _parser = new TopDownParser(grammar); }
public void Should_eliminate_all_epsilon_rules_when_converting_grammer_to_chomsky_normal_form() { var grammar = new ContextFreeGrammar("S"); grammar.Produce("S", "vA", "vB"); grammar.Produce("A", "t0"); grammar.Produce("B"); grammar.Produce("B", "t1"); var cnf = grammar.ToChomskyNormalForm(); var rules = cnf.GetRules("B"); Assert.That(rules, Has.None.Empty); }
private static void CYKTest() { string[] nonterminals = new[] { "S" }, terminals = new[] { "0", "1" }; string starting = "S"; ProductionInfo[] rules = new[] { new ProductionInfo("S", new[] {"0", "S", "1"}), new ProductionInfo("S", new string[] {}) }; var contextFreeGrammar = new ContextFreeGrammar(nonterminals, terminals, rules, starting); var normalForm = contextFreeGrammar.GetChomskyNormalForm(); string[] sentence1 = new[] { "0", "1", "1", "0", "0", "0" }, sentence2 = new[] { "0", "0", "0", "1", "1", "1" }; Debug.Assert(!normalForm.HasSentence(sentence1)); Debug.Assert(normalForm.HasSentence(sentence2)); }
private static void GrammarTest() { string[] nonterminals = new[] { "S" }, terminals = new[] { "0", "1" }; string starting = "S"; ProductionInfo[] rules = new[] { new ProductionInfo("S", new string[] {}), new ProductionInfo("S", new[] { "0", "S", "1"}), new ProductionInfo("S", new[] { "0", "S", "1"}) }; try { var grammar = new ContextFreeGrammar(nonterminals, terminals, rules, starting); } catch (ArgumentException ex) { Debug.Assert(ex.Message == "Rules cannot contain duplicates."); } }
public static void Main() { var grammer = new ContextFreeGrammar("S") .Produce("S", "vA", "t1", "vA") .Produce("A") .Produce("A", "vS") .Produce("A", "vA", "t0", "vS") .Produce("A", "vS", "t0", "vA"); Console.WriteLine(grammer); TopDownParser parser = grammer; Console.WriteLine(grammer.ToChomskyNormalForm().ToString()); Measure.AndDisplayPerformance("comparison", () => { Console.Write("Starting comparison.. "); var allWords1 = Permute.AllWordsOver(7, '0', '1').Where(w => w.Where(c => c == '0').Count() < w.Where(c => c == '1').Count()).ToArray(); var allWords2 = parser.Language(7).ToArray(); foreach (var word in allWords1) { if (!allWords2.Contains(word)) { Console.WriteLine("Word {0} not enumerated.", word); } if (!parser.Compute(word)) { Console.WriteLine("Word {0} not accepted.", word); } } Console.WriteLine("Finished."); }); Console.ReadKey(true); var stopwatch = Stopwatch.StartNew(); parser.Compute("001101110"); Console.WriteLine("Computing '001101110' took {0} ms.", stopwatch.ElapsedMilliseconds); //Console.ReadKey(true); var performance = Measure.Performance(() => { foreach (var word in parser.Language(8)) { var zeros = word.Where(c => c == '0').Count(); var ones = word.Where(c => c == '1').Count(); if (ones <= zeros) throw new ArgumentException(); Console.WriteLine(word); }}); Console.WriteLine("Enumeration took {0} ms", performance.TotalMilliseconds); //Console.ReadKey(true); }
public void Should_eliminate_all_unit_rules_when_converting_grammer_to_chomsky_normal_form() { var grammar = new ContextFreeGrammar("S"); grammar.Produce("S", "vA", "vB"); grammar.Produce("A", "vB"); grammar.Produce("B", "t1"); var cnf = grammar.ToChomskyNormalForm(); var rules = cnf.GetRules("A"); Debug.WriteLine(cnf); Assert.That(rules.Single().Single(), Is.Not.EqualTo(new Variable("B"))); }
public void Should_determine_wheter_grammar_does_not_accept_empty_word() { var grammar = new ContextFreeGrammar("S").Produce("S", "t1"); Assert.That(grammar.AcceptsEmptyWord, Is.False); }
public void Should_determine_wheter_grammar_accepts_empty_word() { var grammar = new ContextFreeGrammar("S").Produce("S"); Assert.That(grammar.AcceptsEmptyWord, Is.True); }
public void Should_not_allow_creation_of_recursive_unit_rules() { var grammar = new ContextFreeGrammar("S"); Assert.That(() => grammar.Produce("S", "vS"), Throws.Exception); }
public void Should_not_convert_grammar_to_chomsky_normal_form_if_it_is_already_in_chomsky_normal_form() { var grammar = new ContextFreeGrammar("S"); Assert.That(grammar.IsInChomskyNormalForm, Is.True); Assert.That(grammar.ToChomskyNormalForm(), Is.SameAs(grammar)); }
public void Should_transform_all_rules_into_proper_form_when_converting_grammer_to_chomsky_normal_form() { var grammar = new ContextFreeGrammar("S"); grammar.Produce("S", "t1", "t2", "t3"); var cnf = grammar.ToChomskyNormalForm(); var rules = cnf.GetRules("S"); Assert.That(rules.Select(r => r.ToList()), Has.All.Count.LessThanOrEqualTo(2)); }
public void Should_replace_epsilon_rules_occurences_one_by_one() { var grammar = new ContextFreeGrammar("S") .Produce("S", "vA", "t1", "vA") .Produce("A") .Produce("A", "t1") .ToChomskyNormalForm(); Debug.WriteLine(grammar); var any = grammar.GetRules(grammar.StartVariable).Any(r => r.First().Equals(new Variable("A"))); Assert.That(any, Is.True); }
public void Should_not_produce_unit_rules_when_transforming_grammar_to_chomsky_normal_form() { var grammar = new ContextFreeGrammar("S") .Produce("S", "vA") .Produce("A", "t1") .ToChomskyNormalForm(); Debug.WriteLine(grammar); var existsUnitRules = grammar.GetRules(grammar.StartVariable).Any(r => r.Count() == 1 && r.Single() is Variable); Assert.That(existsUnitRules, Is.False); }