def match(infix, string):
    """
    Matches string to the infix regular expression.
    :param infix: Infix regex.
    :param string: String for match the regex.
    :return: True if the regex match on the string.
    """

    # shunt and compile the regular expresion
    postfix = Shunting.Converter().toPofix(infix);
    nfa = compile(postfix)

    # The current set of states and the next set of states;
    current = set()
    next = set()

    # Add the initial state to the current set.
    current |= followes(nfa.initial)

    # loop through each character in the string.
    for s in string:
        # Loop through the current set of states.
        for c in current:
            # Check if that state is labelled s.
            if c.label == s:
                # Add the edge1 state to the next set.
                next |= followes(c.edge1)
        # set current to next, and clear out next.
        current = next
        next = set()

    # Check if the accept stare is in the set of current states.
    return (nfa.accept in current)
    def test_no_dot_shunting(self):
        """
        Test the use of no dot for concatenation on shunting algorithm.
        :return: Nothing.
        """
        testCases = [
            ("ab", "a.b"), ("abc", "a.b.c"), ("a*b*", "a*.b*"),
            ("(a-z)b?", "(a-z).b?"), ("a?b+c*(a-z)*t", "a?.b+.c*.(a-z)*.t"),
            ("(0|(1(01*(00)*0)*1)*)*", "(0|(1.(0.1*.(0.0)*.0)*.1)*)*"),
            ("((a-z)|(A-Z)|(0-9)).((a-z)|(A-Z)|(0-9)|_|/.)*.@.((a-z)|(A-Z)|/.)*./..(((a-z)|(A-Z)).((a-z)|(A-Z)).((a-z)|(A-Z))|((a-z)|(A-Z)).((a-z)|(A-Z)))",
             "((a-z)|(A-Z)|(0-9))((a-z)|(A-Z)|(0-9)|_|/.)*@((a-z)|(A-Z)|/.)*/.(((a-z)|(A-Z))((a-z)|(A-Z))((a-z)|(A-Z))|((a-z)|(A-Z))((a-z)|(A-Z)))"
             ), ("abc", "abc")
        ]

        for case in testCases:
            print(case[0])
            self.assertEqual(Shunting.Converter().toPofix(case[0]),
                             Shunting.Converter().toPofix(case[1]))
Exemplo n.º 3
0
    def __init__(self, infix):
        """
        Create object with a infix regex, compile the nfa with it.
        :param infix: The infix regex
        """
        # Convert infix string into postfix and then compile the NFA.
        self.nfa = compile(Shunting.Converter().toPofix(infix))

        # add initial state
        self.actualState |= self.nfa.tf.getTransition(0, -1)
    def __init__(self, infix):
        """
        Create object with a infix regex, compile the nfa with it.
        :param infix: The infix regex
        """
        # Convert infix string into postfix and then compile the NFA.
        self.nfa = compile(Shunting.Converter().toPofix(infix))

        # Add the initial state to the current set.
        self.current |= followes(self.nfa.initial)
Exemplo n.º 5
0
 def take_rules(self, line):
     if self.current != "rules":
         errors.parse(self.line, "You cannot insert a rule here, respect the order : " \
                 + "Rules => Initial facts => Queries")
     elif not rule_is_valid(line):
         errors.parse(self.line, "Rule is not well formated")
     eq = line.split("=>")
     if eq[0][-1] == '<':
         eq[0] = eq[0][:-1]
         equi = 1
     else:
         equi = 0
     while equi > -1:
         if len(eq[1]) == 1:
             refDic = self.nodes
         else:
             refDic = self.multi_rules
         opKey = rpn.shunting(rpn.get_input(eq[1 if equi == 0 else 0]))[-1][2]
         if opKey not in refDic:
             refDic[opKey] = []
         refDic[opKey].append(rpn.shunting(rpn.get_input(eq[0 if equi == 0 else 1]))[-1][2])
         equi -= 1
 def match(self):
     if (self.var1.get() == 1):
         result = Thomsons.match(self.inReg.get().strip(),
                                 self.inStr.get().strip())
         if (result == 1):
             self.otext.insert(
                 INSERT, "[" + self.inReg.get() + "," + self.inStr.get() +
                 "] -> " + "Yes\n")
         else:
             self.otext.insert(
                 INSERT, "[" + self.inReg.get() + "," + self.inStr.get() +
                 "] -> " + "No\n")
     else:
         nfa = ThomsonsMap.compile(Shunting.Converter().toPofix(
             self.inReg.get().strip()))
         result = nfa.run(self.inStr.get().strip())
         if (result == 1):
             self.otext.insert(
                 INSERT, "[" + self.inReg.get() + "," + self.inStr.get() +
                 "] -> " + "Yes\n")
         else:
             self.otext.insert(
                 INSERT, "[" + self.inReg.get() + "," + self.inStr.get() +
                 "] -> " + "No\n")
 def test_thomsonsMap(self):
     for case in self.matchtestcases:
         self.assertEqual(
             ThomsonsMap.compile(Shunting.Converter().toPofix(case[0])).run(
                 case[1]), case[2])