Beispiel #1
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)
Beispiel #2
0
 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))
Beispiel #3
0
 def test_automaton_from_grammar(self):
     # fig 5.6, pag 142
     A = 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 = 'Automaton(N={A, B, C, S, ◇}, T={a, b, c}, transitions=(S-a->A, S-a->B, A-b->B, A-b->C, B-c->A, B-c->C, C-a->◇), F={◇}, q0=S)'
     self.assertEqual(s, str(A))
Beispiel #4
0
 def test_automaton_from_grammar_fail2b(self):
     with self.assertRaisesRegex(ValueError, 'not of the aB form'):
         Automaton.from_grammar(Grammar.from_string('S -> B B'))
Beispiel #5
0
 def test_automaton_from_grammar_fail3(self):
     with self.assertRaisesRegex(ValueError, 'has more than two symbols'):
         Automaton.from_grammar(Grammar.from_string('S -> a b c'))