Ejemplo n.º 1
0
def test_accepts(word):
    # copy paste because parsing is easier with transition tuple
    states = set()
    for x in range(random.randint(0, 20)):
        states.add("".join(random.choice(string.ascii_letters)))
    length = len(states)
    # pick initial
    I = set(random.sample(states, random.randint(0, length)))
    # pick final
    F = set(random.sample(states, random.randint(0, length)))

    # pick transition
    T = []
    # dictionary for parsing
    trans = {}
    for x in range(random.randint(0, length)):
        cur = random.sample(states, 1)[0]
        l = "".join(random.choice(string.ascii_letters))
        next = random.sample(states, 1)[0]
        T.append((cur, l, next))

        trans[cur] = trans.get(cur, {})
        temp = trans[cur].get(l, set())
        temp.add(next)
        trans[cur][l] = temp

    tester = FSA(I, F, T)

    if word == "":
        # empty string condition
        # true if initial and final contains a state that is the same
        truth = True if tester.INITIAL.intersection(tester.FINAL) else False
    else:
        def check_initial():
            """ Returns true if there is a valid path through the
            transition dictionary for the word. """
            def parse(start, word):
                if word == "":
                    return True
                else:
                    next = trans.get(cur, {}).get(word[0], {})
                    while next:
                        if parse(next.pop(), word[1:]):
                            return True
                    return False

            I = tester.INITIAL.copy()
            while I:
                if parse(I.pop(), word):
                    return True
            return False
        truth = check_initial()

    assert tester.accepts(word) == truth
Ejemplo n.º 2
0
 def testDeterminization(self):
     states = [0, 1, 2, 3]
     alphabet = ['a']
     transitions = [(0, 1, 'a'),
                    (0, 2, 'a'),
                    (1, 0, 'b'),
                    (1, 3, 'a'),
                    (2, 0, 'a'),
                    (2, 2, 'b'),
                    (2, 3, 'a')]
     initialState = 0
     finalStates = [3]
     fsa = FSA(states, alphabet, transitions, initialState, finalStates)
     self.assertTrue(fsa.accepts('aba'))
     dfa = fsa.determinized()
     self.assertTrue(dfa.accepts('aba'))
Ejemplo n.º 3
0
    print(f"Initials {test.INITIAL}")
    print("Inital matrix")
    print(test.get_initial_matrix())

    print("Final matrix")
    print(f"Initials {test.FINAL}")
    print(test.get_final_matrix())

    print("'a' matrix")
    print("Transitions")
    pprint(test.TRANSITIONS, width=1)
    print(test.get_letter_matrix('a'))


    print("\nTESTING ACCEPTS")
    print("aab: " + str(test.accepts('aab'))) # False
    print("aba: " + str(test.accepts('aba'))) # False
    print("a: " + str(test.accepts('a'))) # True
    print("abaa: " + str(test.accepts('abaa'))) # True
    print("'':" + str(test.accepts(''))) # False

    print("\nTESTING memoization")
    pprint(test.MEM_LETTER)
    test.add_state("W");
    print(f"States: {test.STATES}")
    print()
    print("Inital matrix")
    print(test.get_initial_matrix())
    print("Final matrix")
    print(test.get_final_matrix())
    print("a: " + str(test.accepts('a'))) # True