Esempio n. 1
0
 def __init__(self, rules):
     self.recognizers = dict([(r, dfa.DFiniteAutomata(ere.Regex(rules[r])) \
                                  .minimalDFA()) for r in rules])
Esempio n. 2
0
                new_y = (start_y + end_y) / 2. + 0.16 * (start_x - end_x)
            else:
                new_x = (start_x + end_x) / 2.
                new_y = (start_y + end_y) / 2.
            trans_dict[tup].set_position((new_x, new_y))
            trans_dict[tup].set_bbox(
                dict(alpha=0.3, color='#e6e6e6', linewidth=0))
            trans_dict[tup].set_text(boreStr(self.trans_dict[tup[0]][tup[1]]))

        # Hide pyplot axis and show the figure.
        plt.axis('off')
        plt.show()


if __name__ == '__main__':
    # rexpr = ere.Regex('0*10*10*10*')
    # rexpr = ere.Regex('(a|~)b*c[0-2]?')
    rexpr = ere.Regex('0*(1|10|100)*0*1')
    my_nfa = nfa.NFiniteAutomata(rexpr)
    FADrawer(my_nfa).staticShow()

    my_dfa = dfa.DFiniteAutomata(my_nfa)
    FADrawer(my_dfa).staticShow()

    min_dfa = my_dfa.minimalDFA()
    FADrawer(min_dfa).staticShow()

    my_nfa = nfa.NFiniteAutomata('../input/NFA2')
    my_dfa = dfa.DFiniteAutomata(my_nfa)
    FADrawer(my_nfa).staticShow()
Esempio n. 3
0
##############################################################################
# Author: Jose, Robert & King                                                #
#  Date:  2019.01.15                                                         #
##############################################################################

from prefa import ere, nfa, dfa, pgui

input('\n#1. Parse a given RE \'a+(a|~)?[0-2]?\':\n')
my_re = ere.Regex('a+(a|~)?[0-2]?')
print(my_re)
input('(Press \"Enter\")')

input('\n#2. Build an NFA from the previous RE:\n')
my_nfa1 = nfa.NFiniteAutomata(my_re)
print(my_nfa1)
input('(Press \"Enter\")')

input('\n#3. Build a DFA from the previous RE directly:\n')
my_dfa1 = dfa.DFiniteAutomata(my_re)
print(my_dfa1)
input('(Press \"Enter\")')

input('\n#4. Build a DFA from the previous NFA \"my_nfa1\":\n')
my_dfa2 = dfa.DFiniteAutomata(my_nfa1)
print(my_dfa2)
input('(Press \"Enter\")')

input('\n#5. Minimize this DFA:\n')
min_dfa = my_dfa2.minimalDFA()
print(min_dfa)
input('(Press \"Enter\")')
Esempio n. 4
0
            state_dict['S'+str(count)] = S
            count += 1
        min_dfa.acceptings, min_dfa.states = set(), []
        for s in state_dict:
            min_dfa.table[s] = dict([(a, set()) for a in self.alphabet])
            if self.initial in state_dict[s]:
                min_dfa.initial = s
            if len(self.acceptings & state_dict[s]) > 0:
                min_dfa.acceptings.add(s)
            for a in self.alphabet:
                for s_end in state_dict:
                    if len(self.table[list(state_dict[s])[0]][a] & \
                       state_dict[s_end]) > 0:
                        min_dfa.table[s][a] = {s_end}
                        break
        min_dfa.states = sorted(list(state_dict.keys()))
        return min_dfa

if __name__ == '__main__':
    print(DFiniteAutomata('../input/DFA'))
    print(DFiniteAutomata(nfa.NFiniteAutomata('../input/NFA')))

    rexpr = ere.Regex('(a|~)*b*a|ba')
    print(rexpr)
    print(DFiniteAutomata(rexpr))

    min_dfa = DFiniteAutomata(rexpr).minimalDFA()
    print(min_dfa)
    print(min_dfa.simulate('aabbbaba', verbose=True))
    print(min_dfa.simulate('aaaabba'))
Esempio n. 5
0
                        table_l['sf'][a].update(table_r['sf'][a])
                    else:
                        table_l['sf'][a] = table_r['sf'][a]
                return table_l, count_l

        # Generate and complete table at root recursively, then collects all
        # other fields.
        self.alphabet = deepcopy(input_regex.alphabet)
        if '~' not in self.alphabet:
            self.alphabet.append('~')
        self.table = calcTable(input_regex.tree)[0]
        for s in self.table:
            for a in self.alphabet:
                if a not in self.table[s]:
                    self.table[s][a] = set()
        self.states = sorted(self.table.keys())
        self.initial, self.acceptings = 's0', {'sf'}


if __name__ == '__main__':
    print(NFiniteAutomata('../input/NFA'))

    rexpr = ere.Regex('(a|~)bc*')
    print(rexpr)
    print(NFiniteAutomata(rexpr))

    my_nfa = NFiniteAutomata(ere.Regex('[a-c]+b*|a'))
    print(my_nfa)
    print(my_nfa.simulate('acbabbb', verbose=True))
    print(my_nfa.simulate('ad'))