def test_grammar_restrict_to(self): G = Grammar.from_string(""" Z -> E $ E -> T | E + T | X X -> x T -> i | ( E ) """) Gr = Grammar.from_string(""" Z -> E $ E -> T | E + T T -> i | ( E ) """) self.assertEqual(G.restrict_to((G.T | G.N) - {'X', 'x'}), Gr)
def test_TDID_init(self): G = Grammar.from_string(""" S -> a B C B -> a B | b C -> a """) i = TopDownInstantaneousDescription(G, 'aaba') self.assertEqual('(), S♯, a̲aba♯', str(i))
def test_alternatives(self): actual = set(Grammar.from_string(""" Z -> E $ E -> T | E + T T -> i | ( E ) """).alternatives('E')) expected = set([('T',), ('E', '+', 'T')]) self.assertEqual(expected, actual)
def test_BUID_init(self): G = Grammar.from_string(""" S -> A C A -> a b C -> c """) i = BottomUpInstantaneousDescription(G, 'abc') self.assertEqual('(), , a̲bc', str(i))
def test_grammar_from_to_string(self): G = Grammar.from_string(""" Z -> E $ E -> T | E + T T -> i | ( E ) """) s = "Grammar(N={E, T, Z}, T={$, (, ), +, i}, P=(Z -> E $, E -> T, E -> E + T, T -> i, T -> ( E )), S=Z)" self.assertEqual(s, str(G))
def test_derivation_possible_steps_pos(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) expected = [(2, 1)] actual = list(Derivation(G).step(0,0).possible_steps(pos = 1)) self.assertEqual(expected, actual)
def test_derivation_rightmost_allterminals(self): G = Grammar.from_string(""" E -> M | A | n M -> E * E A -> E + E """) d = d = Derivation(G).rightmost(0).rightmost(3).rightmost(2).rightmost(2) with self.assertRaisesRegex(ValueError, 'there are no nonterminals'): d.rightmost(2)
def test_derivation_leftmost_list(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """) d = Derivation(G).leftmost([0, 1, 2]) steps = ((0, 0), (1, 0), (2, 1)) self.assertEqual(steps, d.steps())
def test_derivation_steps_list(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) steps = ((0, 0), (1, 0), (2, 1)) d = Derivation(G).step(steps) self.assertEqual(steps, d.steps())
def test_derivation_byprod(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """) d = Derivation(G) p = Production('S', ('A', 'B')) self.assertEqual(d.leftmost(p).sentential_form(), ('A', 'B'))
def test_derivation_sf(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) d = Derivation(G) for prod, pos in [(0, 0), (1, 0), (2, 1)]: d = d.step(prod, pos) self.assertEqual(('a', 'b'), d.sentential_form())
def test_derivation_repr(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) d = Derivation(G) for prod, pos in [(0, 0), (1, 0), (2, 1)]: d = d.step(prod, pos) self.assertEqual('S -> A B -> a B -> a b', str(d))
def test_derivation_rightmost(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """) d = Derivation(G).rightmost(0).rightmost(2).rightmost(1) steps = ((0, 0), (2, 1), (1, 0)) self.assertEqual(steps, d.steps())
def test_grammar_restrict_to_no_start(self): G = Grammar.from_string(""" Z -> E $ E -> T | E + T | X X -> x T -> i | ( E ) """) with self.assertRaisesRegex(ValueError, 'start symbol'): G.restrict_to((G.T | G.N) - {'Z'})
def test_TDID_done(self): G = Grammar.from_string(""" S -> a B C B -> a B | b C -> a """) i = TopDownInstantaneousDescription(G, 'aaba') i = i.predict(G.P[0]).match().predict(G.P[1]).match().predict(G.P[2]).match().predict(G.P[3]).match() self.assertTrue(i.is_done())
def test_BUID_done(self): G = Grammar.from_string(""" S -> A C A -> a b C -> c """) i = BottomUpInstantaneousDescription(G, 'abc') i = i.shift().shift().reduce(G.P[1]).shift().reduce(G.P[2]).reduce(G.P[0]) self.assertTrue(i.is_done())
def test_TDID_match_exception(self): G = Grammar.from_string(""" S -> a B C B -> a B | b C -> a """) i = TopDownInstantaneousDescription(G, 'aaba') with self.assertRaisesRegex(ValueError, r'.*top of the stack.*head symbol'): i = i.match()
def test_BUID_shift(self): G = Grammar.from_string(""" S -> A C A -> a b C -> c """) i = BottomUpInstantaneousDescription(G, 'abc') i = i.shift() self.assertEqual('(), (a), ab̲c', str(i))
def test_BUID_reduce_exception1(self): G = Grammar.from_string(""" S -> A C A -> a b C -> c """) i = BottomUpInstantaneousDescription(G, 'abc') with self.assertRaisesRegex(ValueError, r'production does not belong to the grammar'): i.reduce(Production('X', ('y', )))
def test_TDID_match(self): G = Grammar.from_string(""" S -> a B C B -> a B | b C -> a """) i = TopDownInstantaneousDescription(G, 'aaba') i = i.predict(G.P[0]).match() self.assertEqual('(S -> a B C,), BC♯, aa̲ba♯', str(i))
def test_BUID_reduce(self): G = Grammar.from_string(""" S -> A C A -> a b C -> c """) i = BottomUpInstantaneousDescription(G, 'abc') i = i.shift().shift().reduce(G.P[1]) self.assertEqual('(A -> a b,), (A: (a), (b)), abc̲', str(i))
def test_BUID_reduce_exception0(self): G = Grammar.from_string(""" S -> A C A -> a b C -> c """) i = BottomUpInstantaneousDescription(G, 'abc') with self.assertRaisesRegex(ValueError, r'rhs does not correspond to the symbols on the stack'): i.shift().shift().reduce(G.P[0])
def test_derivation_rightmost_wrongsymbol(self): G = Grammar.from_string(""" E -> M | A | n M -> E * E A -> E + E """) d = Derivation(G).rightmost(0).rightmost(3) with self.assertRaisesRegex(ValueError, 'Cannot apply M'): d.rightmost(3)
def test_TDID_predict_exception1(self): G = Grammar.from_string(""" S -> a B C B -> a B | b C -> a """) i = TopDownInstantaneousDescription(G, 'aaba') with self.assertRaisesRegex(ValueError, r'.*top of the stack.*production'): i = i.predict(G.P[1])
def test_derivation_steps(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) d = Derivation(G) steps = ((0, 0), (1, 0), (2, 1)) for prod, pos in steps: d = d.step(prod, pos) self.assertEqual(steps, d.steps())
def test_derivation_wrong_step(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) d = Derivation(G) steps = ((0, 0), (1, 1)) with self.assertRaisesRegex(ValueError, 'at position'): for prod, pos in steps: d = d.step(prod, pos)
def test_derivation_eq(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) d0 = Derivation(G) for prod, pos in [(0, 0), (1, 0), (2, 1)]: d0 = d0.step(prod, pos) d1 = Derivation(G) for prod, pos in [(0, 0), (1, 0), (2, 1)]: d1 = d1.step(prod, pos) self.assertEqual(d0, d1)
def test_automaton_δ(self): # fig 5.6, pag 142 states = Automaton.from_grammar(Grammar.from_string(""" S -> a A S -> a B A -> b B A -> b C B -> c A B -> c C C -> a """)).δ('S', 'a') self.assertEqual({'A', 'B'}, states)
def test_derivation_hash(self): G = Grammar.from_string(""" S -> A B A -> a B -> b """, False) d0 = Derivation(G) for prod, pos in [(0, 0), (1, 0), (2, 1)]: d0 = d0.step(prod, pos) d1 = Derivation(G) for prod, pos in [(0, 0), (1, 0), (2, 1)]: d1 = d1.step(prod, pos) S = {d0: 1, d1: 2} self.assertEqual(1, len(S))
def test_automaton_from_ε_grammar(self): # fig 5.14 pag 147 A = Automaton.from_grammar(Grammar.from_string(""" S -> A S -> a B A -> a A A -> ε B -> b B B -> b """)) s = "Automaton(N={A, B, S, ◇}, T={a, b}, transitions=(S-ε->A, S-a->B, A-a->A, A-ε->◇, B-b->B, B-b->◇), F={◇}, q0=S)" self.assertEqual(s, str(A))