def __init_productions_to_CFG(self, start): productions = [] for p in self.states: productions.append( CFGRule(start, [ self.__get_variable( str(self.start_state) + "," + str(self.start_symbol) + "," + str(p)) ])) return productions
def __init__(self, terminals, query): self.variables = [Variable("S"), Variable("C")] self.terminals = terminals[:] self.productions = [ CFGRule( Variable("S"), [Variable("C"), Terminal(query), Variable("C")]), CFGRule(Variable("C"), [Variable("C"), Variable("C")]), CFGRule(Variable("C"), []) ] for terminal in self.terminals: self.productions.append( CFGRule(Variable("C"), [ Terminal(terminal), Variable("C"), Terminal(inverse(terminal)) ])) self.start = Variable("S") self.__empty = None
def test_emptiness(self): cfg = CFG([Variable("S"), Variable("A"), Variable("B")], [Terminal("a"), Terminal("b")], [ CFGRule(Variable("S"), [Variable("A"), Variable("B")]), CFGRule(Variable("S"), [Terminal("a")]), CFGRule(Variable("A"), [Terminal("b")]) ], Variable("S")) self.assertEqual( set([Variable("S"), Variable("A"), Terminal("a"), Terminal("b")]), cfg.get_reachable()) self.assertFalse(cfg.is_empty()) cfg = CFG([Variable("S"), Variable("A"), Variable("B")], [Terminal("a"), Terminal("b")], [ CFGRule(Variable("S"), [Variable("A"), Variable("B")]), CFGRule(Variable("S"), [Terminal("a")]), CFGRule(Variable("A"), [Terminal("b")]) ], Variable("B")) self.assertEqual( set([Variable("S"), Variable("A"), Terminal("a"), Terminal("b")]), cfg.get_reachable()) self.assertTrue(cfg.is_empty())
def test_repr(self): cfg = CFG([Variable("I"), Variable("J")], [Terminal("a"), Terminal("b")], [CFGRule(Variable("I"), [Terminal("a"), Variable("I")])], Variable("I")) r = str(cfg) self.assertIn(str(Variable("I")), r) self.assertIn(str(Variable("J")), r) self.assertIn(str(Terminal("a")), r) self.assertIn(str(Terminal("b")), r) self.assertIn("Variables", r) self.assertIn("Terminals", r) self.assertIn("Productions", r) self.assertIn("Start", r) self.assertEqual(r.count("->"), 1) self.assertEqual(r.count("\n"), 4)
def test_paper(self): cfg = CFG([Variable("S"), Variable("C")], [ Terminal("a"), Terminal("b"), Terminal("c"), Terminal("q"), Terminal("am"), Terminal("bm"), Terminal("cm"), Terminal("qm") ], [ CFGRule( Variable("S"), [Variable("C"), Terminal("q"), Variable("C")]), CFGRule( Variable("C"), [Terminal("a"), Variable("C"), Terminal("am")]), CFGRule( Variable("C"), [Terminal("b"), Variable("C"), Terminal("bm")]), CFGRule( Variable("C"), [Terminal("c"), Variable("C"), Terminal("cm")]), CFGRule( Variable("C"), [Terminal("q"), Variable("C"), Terminal("qm")]), CFGRule(Variable("C"), [Variable("C"), Variable("C")]), CFGRule(Variable("C"), []) ], Variable("S")) regex = RegexTree(Node("(a,b)|(bm,c)|(cm,am,q)")) fsm = regex.to_fsm() fsm.close() cfg_temp = cfg.intersect(fsm) self.assertFalse(cfg_temp.is_empty()) regex = RegexTree(Node("(a,b)|(b,c)|(cm,am,q)")) fsm = regex.to_fsm() fsm.close() cfg_temp = cfg.intersect(fsm) self.assertTrue(cfg_temp.is_empty())
def __process_states_transition(self, trans, productions, states, size): head = self.__get_head(trans, size, states) body = self.__get_body(trans, size, states) productions.append(CFGRule(head, body))
def test_iter(self): cfg = CFG([Variable("S"), Variable("A"), Variable("B"), Variable("C")], [Terminal("a"), Terminal("b")], [ CFGRule(Variable("S"), [Variable("A"), Variable("B")]), CFGRule(Variable("S"), [Terminal("a")]), CFGRule(Variable("A"), [Terminal("b")]), CFGRule(Variable("A"), [Variable("C")]) ], Variable("S")) words = [x for x in cfg] self.assertEqual(words, [[Terminal("a")]]) cfg = CFG([Variable("S"), Variable("A"), Variable("C")], [Terminal("a"), Terminal("b")], [ CFGRule(Variable("S"), [Variable("A")]), CFGRule(Variable("S"), [Terminal("a")]), CFGRule(Variable("A"), [Terminal("b")]), CFGRule(Variable("A"), [Variable("C")]) ], Variable("S")) words = [x for x in cfg] self.assertIn([Terminal("a")], words) self.assertIn([Terminal("b")], words) self.assertEqual(len(words), 2) cfg = CFG([Variable("S"), Variable("A"), Variable("B")], [Terminal("a"), Terminal("b")], [ CFGRule(Variable("S"), [Variable("S"), Variable("S")]), CFGRule(Variable("S"), [Terminal("a")]), CFGRule(Variable("A"), [Terminal("b")]) ], Variable("S")) it = iter(cfg) word = next(it) self.assertEqual(set(word), set([Terminal("a")])) word = next(it) self.assertEqual(set(word), set([Terminal("a")])) word = next(it) self.assertEqual(set(word), set([Terminal("a")])) word = next(it) self.assertEqual(set(word), set([Terminal("a")])) word = next(it) self.assertEqual(set(word), set([Terminal("a")])) cfg = CFG([Variable("S"), Variable("A"), Variable("B")], [Terminal("a"), Terminal("b")], [ CFGRule(Variable("S"), [Variable("A"), Variable("S")]), CFGRule(Variable("S"), [Terminal("a")]), CFGRule(Variable("A"), [Terminal("b")]) ], Variable("S")) it = iter(cfg) temp = [next(it) for _ in range(100)] self.assertIn( [Terminal("b"), Terminal("b"), Terminal("b"), Terminal("a")], temp)