Ejemplo n.º 1
0
 def test_dot_1_e(self):
     ''' Testing with 1.e'''
     r = RegexTree("(1.e)")
     s = r.root
     self.assertEqual(
         str(s), str(DotNode(RegexTreeNode('1', []), RegexTreeNode('e',
                                                                   []))))
Ejemplo n.º 2
0
 def test_bar_e_1(self):
     ''' Testing with e|1'''
     r = RegexTree("(e|1)")
     s = r.root
     self.assertEqual(
         str(s), str(BarNode(RegexTreeNode('e', []), RegexTreeNode('1',
                                                                   []))))
Ejemplo n.º 3
0
 def test_bar_star(self):
     ''' Testing with 0*|1*'''
     r = RegexTree("(0*|1*)")
     s = r.root
     self.assertEqual(
         str(s),
         str(
             BarNode(StarNode(RegexTreeNode('0', [])),
                     StarNode(RegexTreeNode('1', [])))))
Ejemplo n.º 4
0
 def test_dot_dot(self):
     ''' Testing with (0.1).0'''
     r = RegexTree("((0.1).0)")
     s = r.root
     self.assertEqual(
         str(s),
         str(
             DotNode(
                 DotNode(RegexTreeNode('0', []), RegexTreeNode('1', [])),
                 RegexTreeNode('0', []))))
Ejemplo n.º 5
0
 def test_complete(self):
     ''' Testing with all Nodes'''
     r = RegexTree("((1.(0|1)*).0)")
     s = r.root
     self.assertEqual(
         str(s),
         str(
             DotNode(
                 DotNode(
                     RegexTreeNode('1', []),
                     StarNode(
                         BarNode(RegexTreeNode('0', []),
                                 RegexTreeNode('1', [])))),
                 RegexTreeNode('0', []))))
Ejemplo n.º 6
0
def build_regex_tree(regex):
    '''(str) -> RegexTree
    takes a string regex, build a regex tree, and returns the root of the tree
    >>>build_regex_tree("(1.2)") == RegexTree('.', [Leaf('1'), Leaf('2')])
    True
    >>>build_regex_tree("(1*|e)") == RegexTree('|', [StarTree(Leaf('1')), Leaf(
    'e')])
    True
    >>>build_regex_tree("(((e.1).2*)|((0***.e).(1|2*)))") == RegexTree('|', [
    RegexTree('.', [RegexTree('.', [Leaf('e'), Leaf('1')]), StarTree(Leaf(
    '2'))]), RegexTree('.', [RegexTree('.', [StarTree(StarTree(StarTree(Leaf(
    '0')))), Leaf('e')]), RegexTree('|', [Leaf('1'), StarTree(Leaf('2'))])])])
    True
    '''
    degree = 0
    # base case if length is 1,regex is a leaf since it does not have any child
    if len(regex) == 1:
        return Leaf(regex)
    # if it ends with a "*"
    elif regex != "" and regex[-1] == '*':
        # set the star to be the parent of a tree built with the rest of regex
        return StarTree(build_regex_tree(regex[:-1]))
    # if it is in brackets
    elif regex != "" and regex[0] == "(" and regex[-1] == ")":
        # loop and index of the dot or bar that's in the first brackets
        for i in range(len(regex)):
            if regex[i] == "(":
                degree += 1
            elif regex[i] == ")":
                degree -= 1
            if degree == 1 and (regex[i] == "." or regex[i] == '|'):
                I = i
                # recursion: remove the two brackets
                # divide to 2 parts at the dot or bar found and build trees
                # for each of the parts
                return RegexTree(regex[I], [build_regex_tree(
                    regex[1:I]), build_regex_tree(regex[I+1:-1])])
Ejemplo n.º 7
0
 def test_e(self):
     ''' Testing with e.'''
     r = RegexTree("e")
     s = r.root
     self.assertEqual(str(s), str(RegexTreeNode('e', [])))
Ejemplo n.º 8
0
 def test_star_0(self):
     ''' Testing with 0*.'''
     r = RegexTree("0*")
     s = r.root
     self.assertEqual(str(s), str(StarNode(RegexTreeNode('0', []))))
Ejemplo n.º 9
0
 def test_star_1(self):
     ''' Testing with 1*.'''
     r = RegexTree("1*")
     s = r.root
     self.assertEqual(str(s), str(StarNode(RegexTreeNode('1', []))))
Ejemplo n.º 10
0
 def test_0(self):
     ''' Testing with 0.'''
     r = RegexTree("0")
     s = r.root
     self.assertEqual(str(s), str(RegexTreeNode('0', [])))
Ejemplo n.º 11
0
 def test_4(self):
     r = RegexTree("(0.(0|(1.0))*)")
     self.assertTrue((regex_match(r, "001010")))
Ejemplo n.º 12
0
 def test_1(self):
     ''' Testing with 1.'''
     r = RegexTree("1")
     s = r.root
     self.assertEqual(str(s), str(RegexTreeNode('1', [])))
Ejemplo n.º 13
0
 def test_3(self):
     r = RegexTree("(1.(0.1)*)")
     self.assertTrue((regex_match(r, "10101")))
Ejemplo n.º 14
0
 def test_2(self):
     r = RegexTree("(0.(1|e))*")
     self.assertTrue((regex_match(r, "000")))
Ejemplo n.º 15
0
 def test_1(self):
     r = RegexTree("(0.(1|0))*")
     self.assertTrue((regex_match(r, "010001")))
Ejemplo n.º 16
0
 def test_1_1(self):
     ''' Testing with r = 1, s = 1.'''
     r = RegexTree("1")
     self.assertTrue((regex_match(r, "1")))
Ejemplo n.º 17
0
 def test_star_e(self):
     ''' Testing with e*.'''
     r = RegexTree("e*")
     s = r.root
     self.assertEqual(str(s), str(StarNode(RegexTreeNode('e', []))))
Ejemplo n.º 18
0
            return root
    # the form of (r)*
    elif valid_r[0] == '(' and valid_r[-1] == star:
        for i in range(len(valid_r)):
            if valid_r[-i - 1] == ')':
                r_ind = -i - 1
                c = build_regex_tree(valid_r[0:r_ind + 1])
                root = StarTree(c)
                if len(valid_r[r_ind:]) > 2:
                    for i in range(len(valid_r[r_ind:]) - 2):
                        root = StarTree(root)
                return root


if __name__ == '__main__':
    br1 = RegexTree('0', [])
    br2 = RegexTree('1', [])
    br3 = RegexTree('2', [])
    br4 = RegexTree('e', [])
    dt = DotTree(br1, br2)
    bt = BarTree(dt, br2)
    dt2 = DotTree(br1, br2)
    st = StarTree(dt)
    print('THEY MATCH -> ' + str(regex_match(st, '1111')))
    print('THEY MATCH -> ' + str(regex_match(dt, '01')))
    print('THEY MATCH -> ' + str(regex_match(bt, '01')))
    print('THEY MATCH -> ' + str(regex_match(dt2, '01')))
    print('THEY MATCH -> ' + str(regex_match(bt, '1')))
    #print('perm'+ str(all_regex_permutations('((1|0*).0)')))
    a = build_regex_tree('1')
    print(a)