Пример #1
0
 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)
Пример #2
0
 def test_grammar_extrasymbol(self):
     with self.assertRaisesRegex(
             ValueError,
             'neither terminals or nonterminals.*\(S -> s t,\)'):
         Grammar({'S'}, {'s'},
                 (Production('S', ('s', )), Production(('S', ),
                                                       ('s', 't'))), 'S')
Пример #3
0
 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)
Пример #4
0
 def test_grammar_wrong_cf(self):
     with self.assertRaisesRegex(ValueError, 'not a nonterminal.*\(T -> s,\)'):
         Grammar(
             {'S'}, {'s'}, (
                 Production('S', ('s', )),
                 Production('T', ('s', ))
             ), 'S'
         )
Пример #5
0
 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))
Пример #6
0
 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))
Пример #7
0
 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))
Пример #8
0
 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'})
Пример #9
0
 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))
Пример #10
0
 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)
Пример #11
0
 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)
Пример #12
0
 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())
Пример #13
0
 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())
Пример #14
0
 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'))
Пример #15
0
 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())
Пример #16
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)
Пример #17
0
 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())
Пример #18
0
 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())
Пример #19
0
 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))
Пример #20
0
 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())
Пример #21
0
 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))
Пример #22
0
 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()
Пример #23
0
 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])
Пример #24
0
 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', )))
Пример #25
0
 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))
Пример #26
0
 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])
Пример #27
0
 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())
Пример #28
0
 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)
Пример #29
0
 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)
Пример #30
0
 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)