Exemplo n.º 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)
Exemplo n.º 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')
Exemplo n.º 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)
Exemplo n.º 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'
         )
Exemplo n.º 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))
Exemplo n.º 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))
Exemplo n.º 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))
Exemplo n.º 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'})
Exemplo n.º 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))
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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())
Exemplo n.º 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())
Exemplo n.º 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'))
Exemplo n.º 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())
Exemplo n.º 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)
Exemplo n.º 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())
Exemplo n.º 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())
Exemplo n.º 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))
Exemplo n.º 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())
Exemplo n.º 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))
Exemplo n.º 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()
Exemplo n.º 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])
Exemplo n.º 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', )))
Exemplo n.º 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))
Exemplo n.º 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])
Exemplo n.º 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())
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)