コード例 #1
0
ファイル: thompson.py プロジェクト: gmunk/py-eac
def build_concatenation_nfa(first: NFA, second: NFA) -> NFA:
    for s in first.accepting_states:
        s.add_epsilon_transition(second.initial_state)

    return NFA(states=first.states.union(second.states),
               alphabet=first.alphabet.union(second.alphabet),
               initial_state=first.initial_state,
               accepting_states=second.accepting_states)
コード例 #2
0
ファイル: thompson.py プロジェクト: gmunk/py-eac
def build_symbol_nfa(s: str) -> NFA:
    initial, accepting = State("initial_{symbol}".format(symbol=s)), State(
        "accepting_{symbol}".format(symbol=s))

    initial.add_transition(s, accepting)

    return NFA(states={initial, accepting},
               alphabet={s},
               initial_state=initial,
               accepting_states={accepting})
コード例 #3
0
ファイル: thompson.py プロジェクト: gmunk/py-eac
def build_closure_nfa(source: NFA) -> NFA:
    accepting = State("accepting_closure")
    initial = State("initial_closure",
                    epsilon_transitions=[source.initial_state, accepting])

    for s in source.accepting_states:
        s.add_epsilon_transitions([source.initial_state, accepting])

    return NFA(states=source.states.union({initial, accepting}),
               alphabet=source.alphabet,
               initial_state=initial,
               accepting_states={accepting})
コード例 #4
0
ファイル: thompson.py プロジェクト: gmunk/py-eac
def build_union_nfa(first: NFA, second: NFA) -> NFA:
    initial, accepting = State("initial_union",
                               epsilon_transitions=[first.initial_state, second.initial_state]), \
                         State("accepting_union")

    for sf, ss in zip(first.accepting_states, second.accepting_states):
        sf.add_epsilon_transition(accepting)
        ss.add_epsilon_transition(accepting)

    return NFA(states=first.states.union(second.states).union(
        {initial, accepting}),
               alphabet=first.alphabet.union(second.alphabet),
               initial_state=initial,
               accepting_states={accepting})
コード例 #5
0
ファイル: fio.py プロジェクト: mebusy/codeLib
    def startNFASemRule(self, lst, context=None):
        """

        :param lst:
        :param context:"""
        new = NFA()
        new.Sigma = self.alphabet
        while self.states:
            x = self.states.pop()
            new.addState(x)
        while self.initials:
            x = self.initials.pop()
            new.addInitial(new.stateIndex(x))
        while self.finals:
            x = self.finals.pop()
            new.addFinal(new.stateIndex(x))
        while self.transitions:
            (x1, x2, x3) = self.transitions.pop()
            new.addTransition(new.stateIndex(x1), x2, new.stateIndex(x3))
        self.theList.append(new)
        self.initLocal()
コード例 #6
0
 def getAutomata(self):
     """ deal with the information collected"""
     isDeterministic = True
     if len(self.initials) > 1 or "@epsilon" in self.states:
         isDeterministic = False
     else:
         for s in self.transitions:
             for c in self.transitions[s]:
                 if len(self.transitions[s][c]) > 1:
                     isDeterministic = False
                     break
             if not isDeterministic:
                 break
     if isDeterministic:
         if "l" in self.eq.keys():
             fa = DFCA()
             fa.setLength = self.eq["l"]
         else:
             fa = DFA()
     else:
         fa = NFA()
     for s in self.states:
         fa.addState(s)
     fa.setFinal(fa.indexList(self.finals))
     if isDeterministic:
         fa.setInitial(fa.stateIndex(common.uSet(self.initials)))
         for s1 in self.transitions:
             for c in self.transitions[s1]:
                 fa.addTransition(
                     fa.stateIndex(s1), c,
                     fa.stateIndex(common.uSet(self.transitions[s1][c])))
     else:
         fa.setInitial(fa.indexList(self.initials))
         for s1 in self.transitions:
             for c in self.transitions[s1]:
                 for s2 in fa.indexList(self.transitions[s1][c]):
                     fa.addTransition(fa.stateIndex(s1), c, s2)
     return fa
コード例 #7
0
    def compile(self, grammar_type="regex"):
        """
        根据文法类型进行编译, 产生dfa. regex 表示 正则表达式, regular 表示 正规文法
        :param grammar: 文法类型
        :return:
        """
        if grammar_type == 'regex':
            nfas = []
            for le in self.lexs:
                # print le
                nfas.append(Regex.compile_nfa(le[1], extend=True, type=le[0]))
            nfa = NFA.combine(*nfas)
            self.lex_dfa = nfa.convert_dfa(copy_meta=["type"])
            return
        elif grammar_type == "regular":
            """
            本来没有想到会做三型文法解析, 由于parser里也有文法解析.. 此处应该跟那边合并..
            """
            nfas = []
            grammar = defaultdict(list)
            g_in, g_out = defaultdict(int), defaultdict(int)
            all_symbol = set()
            for l_hand, r_hand in self.lexs:
                l_hand = l_hand[1:-1]
                r_hands = [[x[1:-1] for x in r.strip().split()]
                           for r in r_hand.split('|')]
                for hand in r_hands:
                    for h in hand:
                        g_in[h] += 1
                        all_symbol.add(h)
                g_out[l_hand] += 1
                all_symbol.add(l_hand)
                grammar[l_hand].extend(r_hands)
            grammar['limit'] = [[' '], ['\t'], ['\n']]
            ter, not_ter = [], []
            for sym in all_symbol:
                if g_in[sym] == 0:
                    not_ter.append(sym)
                if g_out[sym] == 0:
                    ter.append(sym)
            # print ter, not_ter
            nfas = []
            for token_type in not_ter:
                nfa = NFA()
                nfa.start = NFANode(r_name=token_type)
                end_node = NFANode(type=token_type)
                end_node.end = True
                nfa.end = {end_node}
                vis = {token_type: nfa.start}

                def get_node(name):
                    if name in vis:
                        return vis[name]
                    vis[name] = NFANode(r_name=name)
                    return vis[name]

                que = Queue()
                que.put(token_type)
                while not que.empty():
                    t = que.get()
                    node = get_node(t)
                    if node.meta.get('vis', 0) > 0:
                        continue
                    node.meta['vis'] = node.meta.get('vis', 0) + 1
                    for r_hand in grammar[t]:
                        node.next.setdefault(r_hand[0], set())
                        if len(r_hand) == 2:
                            node.next[r_hand[0]].add(get_node(r_hand[1]))
                            que.put(r_hand[1])
                        else:
                            node.next[r_hand[0]].add(end_node)
                nfas.append(nfa)
            nfa = NFA.combine(*nfas)
            self.lex_dfa = nfa.convert_dfa(copy_meta=["type"])
            return
コード例 #8
0
ファイル: ex12.py プロジェクト: VitalyArtemiev/labs
import sys
from fa import NFA, DFA

filename = "test2.txt"

file = open(filename, 'r')
lines = file.readlines()

file.close()

nfa = NFA()
dfa = DFA()

nfa.construct_nfa_from_lines(lines)

nfa.print_nfa()
print()

dfa.convert_from_nfa(nfa)

dfa.print_dfa()