def test_derivation_empty(self): var_s = Variable("S") productions = [Production(var_s, [Epsilon()])] cfg = CFG(productions=productions, start_symbol=var_s) parse_tree = cfg.get_cnf_parse_tree([]) derivation = parse_tree.get_rightmost_derivation() self.assertEqual([[var_s], []], derivation)
def test_derivation_does_not_exist(self): var_s = Variable("S") ter_a = Terminal("a") ter_b = Terminal("b") cfg = CFG(productions=[], start_symbol=var_s) with self.assertRaises(DerivationDoesNotExist): parse_tree = cfg.get_cnf_parse_tree([ter_a, ter_b]) parse_tree.get_rightmost_derivation()
def test_get_rightmost_derivation(self): ter_a = Terminal("a") ter_b = Terminal("b") var_s = Variable("S") var_a = Variable("A") var_b = Variable("B") var_c = Variable("C") productions = [ Production(var_s, [var_c, var_b]), Production(var_c, [var_a, var_a]), Production(var_a, [ter_a]), Production(var_b, [ter_b]) ] cfg = CFG(productions=productions, start_symbol=var_s) parse_tree = cfg.get_cnf_parse_tree([ter_a, ter_a, ter_b]) derivation = parse_tree.get_rightmost_derivation() self.assertEqual( derivation, [[var_s], [var_c, var_b], [var_c, ter_b], [var_a, var_a, ter_b], [var_a, ter_a, ter_b], [ter_a, ter_a, ter_b]])