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]))
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)
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])