Ejemplo n.º 1
0
    def test_parse_rg(self):
        a = NFA.from_rg(parse_rg("S -> aS | bB \n B -> cS | f"))
        self.assertTrue(a.accepts('aaaaabcaaaabf'))
        self.assertTrue(a.accepts('bf'))
        self.assertTrue(a.accepts('bcaaaabf'))
        self.assertTrue(a.accepts('abcabcabcabf'))
        self.assertFalse(a.accepts('bcf'))
        self.assertFalse(a.accepts('aaaacf'))
        self.assertFalse(a.accepts(''))

        a = NFA.from_rg(parse_rg("S -> a | cB | & \n B -> f"))
        self.assertTrue(a.accepts('a'))
        self.assertTrue(a.accepts('cf'))
        self.assertTrue(a.accepts(''))
        self.assertFalse(a.accepts('c'))
        self.assertFalse(a.accepts('f'))
        self.assertFalse(a.accepts('acf'))

        a = NFA.from_rg(parse_rg("S -> a \n S -> cB | & \n B -> f"))
        self.assertTrue(a.accepts('a'))
        self.assertTrue(a.accepts('cf'))
        self.assertTrue(a.accepts(''))
        self.assertFalse(a.accepts('c'))
        self.assertFalse(a.accepts('f'))
        self.assertFalse(a.accepts('acf'))

        a = NFA.from_rg(parse_rg("Start -> fZ \n Z -> zZ | yZ | z"))
        self.assertTrue(a.accepts('fyz'))
        self.assertTrue(a.accepts('fz'))
        self.assertTrue(a.accepts('fzzyyzz'))
        self.assertFalse(a.accepts('z'))
        self.assertFalse(a.accepts('fzzzy'))
        self.assertFalse(a.accepts(''))
Ejemplo n.º 2
0
    def rg_to_fa(self):
        if self.rg_text.toPlainText():
            try:
                self.rg = parse_rg(self.rg_text.toPlainText())
            except SyntaxError as e:
                self.show_error(e)
                return

            self.fa = NFA.from_rg(self.rg)
            self.fa.rg_str = self.rg.rg_str
            self.add_fa_to_list()
        else:
            self.show_error("Defina uma gramática!")
            return
Ejemplo n.º 3
0
 def test_parse_rg_error(self):
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> aS | bB \n B -> cS | f \n SA -> U"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> | aS | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> aS | a |"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("B -> aA \n S -> aS | | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S > aA | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S aA | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("s -> aA | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> AA | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> aa | a"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> aA | A"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> aA | a \n A -> a | &"))
     with self.assertRaises(SyntaxError):
         a = NFA.from_rg(parse_rg("S -> aA | a \n A -> a | a&"))