def nfaSyntactic(): nfaForm = forms.NFASyn(request.form) nfas = set([]) dfaER = None msg = None if request.method == 'POST': regularExpressions = nfaForm.regularExpressions.data regularExpressions = regularExpressions.split('\r\n') print(regularExpressions) try: for re in regularExpressions: auxRE = re.split(' ') lex = Lexer(syntacticDFA, auxRE[0]) syn = SyntacticNFA(lex) nfaAux = syn.start() if (nfaAux == None): print('Error') msg = 2 break nfaAux.setToken(int(auxRE[1])) nfas.add(nfaAux) if (msg != 2): nfaER = NFA.specialJoin(nfas) dfaER = nfaER.convertToDFA() dfaDictionary[dfaER.getId()] = dfaER msg = 1 except: msg = 2 NFA.resetId() DFA.resetId() return render_template('analysis/nfa.html', nfaF=nfaForm, dfaER=dfaER, msg=msg)
def __init__(self, src): self.line = 1 self.col = 0 self.fp = open(src, "r") self.buffer = self.fp.readline() self.buffer_size = len(self.buffer) self.dfa = DFA(7, ARIT_GOL[1], ARIT_GOL[0]) token_list = []
class Lexer: tokens = [ TokenType.DELIMETERS_ARIT.value, TokenType.IDENTIFIER_ARIT.value, TokenType.OPERATORS_ARIT.value, TokenType.TOKEN_NUM_ARIT.value ] def __init__(self, src): self.line = 1 self.col = 0 self.fp = open(src, "r") self.buffer = self.fp.readline() self.buffer_size = len(self.buffer) self.dfa = DFA(7, ARIT_GOL[1], ARIT_GOL[0]) token_list = [] def hasNextTok(self): return True if self.buffer != "" else False def getInput(self): self.buffer = self.fp.readline() if self.buffer != "": self.buffer_size = len(self.buffer) self.line += 1 self.col = 0 #Function used for getting tokens and adding them to the symbol def getToken(self): self.dfa.current_state = 0 #If line over, get new line if self.buffer_size == self.col + 1: self.getInput() if not self.hasNextTok(): return None #Current lexeme lexeme = "" ''' Get characters from the buffer while dfa is not in a final state''' while True: char = self.buffer[self.col] #print("Line {0}, col {1} : {2}".format(self.line,self.col,char)) self.dfa.transition(char) if self.dfa.isFinalState(): break lexeme += char self.col += 1 #print(lexeme) #Return the match by checking all tokA for type in Lexer.tokens: for regex in type: exp = re.compile(regex) if exp.match(lexeme): return Token(type[regex], lexeme, self.line) #if it gets here Exception()
def _NumberDFA(): dfa = DFA() dfa.addStates(["number_0", "NUMBER"]) dfa.state = dfa.states[0] dfa.final = dfa.states[-1] dfa.addTransition("number_0", "NUMBER", DIGITS) dfa.addTransition("NUMBER", "NUMBER", DIGITS) return dfa
def _IdentifierDFA(): dfa = DFA() dfa.addStates(["id0", "idfin"]) dfa.state = dfa.states[0] dfa.final = dfa.states[-1] dfa.addTransition("id0", "idfin", LETTERS + "_") dfa.addTransition("idfin", "idfin", DIGITS + LETTERS + "_") return dfa
def toDFA(self): newStates = [] initialState = self.getInitialState() newStates.append( '|'.join( list( map(lambda x: x.label, self.clausura(initialState) ) ) ) ) alphabet = self.alphabet.split(',') stableTable = [] x = 0 for state in newStates: stableTable.append([None]*len(alphabet)) y = 0 for a in alphabet: stateTo = getStateTo(self, state, a) if(stateTo): stableTable[x][y] = stateTo newStates.append(stateTo) y = y+1 x = x+1 newDFA = DFA(self.name+"-toDFA", alphabet) newDFA.addState(normalizeState(newStates[0]), True) for i in range(1, len(newStates)): newDFA.addState(normalizeState(newStates[i]), False, lookForFinal(self, newStates[i])) for i in range( len(newStates) ): for j in range( len(stableTable[i]) ): if(stableTable[i][j]): newDFA.addTransition(alphabet[j], normalizeState(newStates[i]), normalizeState( stableTable[i][j]) ) return newDFA
def toDFA(self, followpos, positions, sigma): s0 = self.firstpos dstates = {s0: False} dtran = defaultdict(dict) s = self._getNextState(dstates) start = s ends = [] maxState = max(positions) while True: dstates[s] = True if maxState in s: ends.append(s) for a in sigma: u = frozenset() for p in s: if positions[p] == a: u = u.union(followpos[p]) if not u in dstates: dstates[u] = False dtran[s][a] = u end = s s = self._getNextState(dstates) if s is None: break return DFA(dtran, start, ends)
def rpni(self, pos_examples, neg_examples): dfa = DFA.from_word_list(pos_examples, self.alphabet) self.reds = {dfa.start} self.blues = { next_state for token, next_state in dfa.transitions[dfa.start].items() } self.draw(dfa=dfa, operation='pta') while len(self.blues) > 0: temp_dfa = 0 blue = choose(dfa, self.blues) for red in list(self.reds): temp_dfa = self.rpni_merge(deepcopy(dfa), red, blue) if rpni_compatible(temp_dfa, neg_examples): # print('compatible') break temp_dfa = 0 if temp_dfa != 0: if blue in self.blues: self.blues.remove(blue) dfa = temp_dfa for r_state in self.reds: for token, next_state in dfa.transitions[r_state].items(): if next_state not in self.reds: self.blues.add(next_state) self.draw(dfa=dfa, operation='merge') else: self.rpni_promote(dfa, blue) self.draw(dfa=dfa, operation='promoted') return dfa
def generate_begins_with(self): alphabet = self.dfa_alphabet.text().split(',') if alphabet[0] != '' and self.lineEdit.text() != '': dfa = DFA.dfa_begins_with(self.lineEdit.text(), alphabet) dfa_list.append(dfa) item = QStandardItem(dfa.get_tuple_string()) dfa_model_list.appendRow(item) self.dialog.accept()
def reduce_unreachable(dfa): states = reachable(dfa) vocabulary = dfa.vocabulary transition_function = {(s, a): dfa.step_transit(s, a) for s in states for a in vocabulary} initial_state = dfa.initial_state final_states = set(filter(lambda state: state in states, dfa.final_states)) return DFA(states, vocabulary, transition_function, initial_state, final_states)
def learn_dfa(teacher, num_examples): for i in range(num_examples): teacher.example() examples = teacher.used_examples pos_examples = [ex[0] for ex in examples if ex[1] == '+'] neg_examples = [ex[0] for ex in examples if ex[1] == '-'] pta = DFA.from_word_list(pos_examples, alphabet) return pta
def generate_contains(self): alphabet = self.dfa_alphabet.text().split(',') input = self.lineEdit_3.text() if alphabet[0] != '' and input != '': dfa = DFA.dfa_contains(input, alphabet) dfa_list.append(dfa) item = QStandardItem(dfa.get_tuple_string()) dfa_model_list.appendRow(item) self.dialog.accept()
def convertToDFA(eNFA): Q = [] Qd = [] initialState = eNFA.initialState for y in initialState: for state in eNFA.ECLOSE(y): if not state in initialState: initialState.append(state) initialState.sort() ctr = 0 Q.append(tuple(initialState)) # print ('s', initialState) initialState = tuple(initialState) alpha = '01' delta = {} for stateQ in Q: # print('s', stateQ) adder = [] for a in alpha: nextState = [] for y in stateQ: # print ('y', y, stateQ) nextState = list(set(nextState + eNFA.delta(y, a))) for y in nextState: for state in eNFA.ECLOSE(y): if not state in nextState: nextState.append(state) nextState.sort() # print ('n', nextState) if not tuple(nextState) in Q: adder.append(tuple(nextState)) delta[(stateQ, a)] = tuple(nextState) # print ('d', delta[(stateQ, a)]) ctr += 1 # print( 'a', adder) Q += adder F = [] for state in Q: for y in state: if eNFA.Q[y].isFinal: F.append(state) break # print ('Q', Q) # print ('initialState', initialState) # print ('F', F) return DFA(Q, initialState, alpha, delta, F)
def _convert(self): #BLOCKING BLOCKING BLOCKING string = self.dfa_textbox.get(1.0, "end-1c") if string == '': messagebox.showerror("Lỗi", "Bạn chưa nhập đủ các trường!") return dfa = DFA.from_text(string) dfa.minimize() dfa.draw() self.result['text'] = dfa.to_regex().string self._rebuild_rframe()
def testMinimize(self): # https://en.wikipedia.org/wiki/DFA_minimization tt = { frozenset(['a']): { '0': frozenset(['b']), '1': frozenset(['c']) }, frozenset(['b']): { '0': frozenset(['a']), '1': frozenset(['d']) }, frozenset(['c']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['d']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['e']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['f']): { '0': frozenset(['f']), '1': frozenset(['f']) } } sigma = set(['0', '1']) start = 'a' end = set([frozenset(['c']), frozenset(['d']), frozenset(['e'])]) dfa = DFA(tt, start, end) self.assertEqual(len(dfa.ends), 3) self.assertEqual(len(dfa.transitions), 6) eqCls = dfa.findEquivalenceClasses(sigma) dfa.mergeEquivalentStates(eqCls) self.assertEqual(len(dfa.ends), 1) self.assertEqual(len(dfa.transitions), 3)
def createDFA(): newDFA = DFA("newDFA", alphabet) print("nodes addState") for node in nodeList: newDFA.addState(str(node.label.text()), node.first, node.final) for con in conList: newDFA.addTransition(str(con.name), str(con.node.label.text()), str(con.nextNode.label.text())) return newDFA
def rename(dfa): states_dict = {j: 'q' + str(i) for (i, j) in enumerate(dfa.states)} states = states_dict.values() vocabulary = dfa.vocabulary transition_function = dict() for state in dfa.states: for char in vocabulary: transition_function[(states_dict[state], char)] = states_dict[dfa.step_transit( state, char)] initial_state = states_dict[dfa.initial_state] final_states = list(map(lambda x: states_dict[x], dfa.final_states)) return DFA(states, vocabulary, transition_function, initial_state, final_states)
def create_dfa(self): alphabet = self.dfa_alphabet.text().split(',') start = self.dfa_start.text() finals = self.dfa_finals.text().split(',') transitions = {} for row in range(0, self.dfa_transitions.rowCount()): state = self.dfa_transitions.item(row, 0) char = self.dfa_transitions.item(row, 1) next_state = self.dfa_transitions.item(row, 2) if state is not None and char is not None and next_state is not None: state = state.text() char = char.text() next_state = next_state.text() transitions[(state, char)] = next_state if alphabet[0] != '' and start != '' and finals[ 0] != '' and transitions != {}: dfa = DFA(alphabet, transitions, start, finals) dfa_list.append(dfa) item = QStandardItem(dfa.get_tuple_string()) dfa_model_list.appendRow(item) self.dialog.accept()
def setUp(self): # Accepts a string with odd number of 'a's states = {1, 2} alphabet = {"a", "b"} final_states = {2} start_state = 1 invalid_transitions = { T(1, "a"): 2, # Missing transition (1, "b) T(2, "a"): 1, T(2, "b"): 2 } self.invalid_dfa = DFA(states, alphabet, invalid_transitions, start_state, final_states)
def to_dfa(self): from DFA import DFA def combine_state(states): name = ','.join(sorted([e.name for e in states])) return State(name) states = [] closures = [] marked = [] final_states = [] e_closure = self.e_closure((self.init_state,)) init_state = combine_state(e_closure) states.append(init_state) closures.append(e_closure) marked.append(False) table = dict() while False in marked: for i in range(len(marked)): if marked[i] == False: current_state = states[i] closure = closures[i] marked[i] = True break current_dict = dict() table[current_state] = current_dict print(self.alphabet) for c in self.alphabet: move = self.move(closure, c) #print('Move:', closure, c, move) current_closure = self.e_closure(move) #print('Closure:', current_closure) if len(current_closure) != 0 and current_closure not in closures: new_state = combine_state(current_closure) states.append(new_state) closures.append(current_closure) marked.append(False) current_dict[c] = new_state elif len(current_closure) != 0: for state_index in range(len(states)): if current_closure == closures[state_index]: current_dict[c] = states[state_index] else: current_dict[c] = None for i in range(len(states)): for s in self.final_states: if s in closures[i]: final_states.append(states[i]) return DFA(states, self.alphabet, init_state, final_states, table)
def setUp(self): # Accepts a string with odd number of 'a's states = {1, 2} alphabet = {"a", "b"} final_states = {2} start_state = 1 transitions = { T(1, "a"): 2, T(1, "b"): 1, T(2, "a"): 1, T(2, "b"): 2 } self.dfa = DFA(states, alphabet, transitions, start_state, final_states) validate_dfa(self.dfa)
def collectData(): # Read a JSON file for collecting DFA data with open("data1.json", "r") as myfile: data = json.load(myfile) # Extract data sets from the JSON object states = set(data["states"]) alphabets = set(data["alphabets"]) init_state = set(data["init_state"]) final_states = set(data["final_states"]) edges = set((tuple(edge) for edge in data["edges"])) # Create an object instance of DFA class dfa = DFA(states,alphabets,init_state,final_states,edges) # Return to the main procedure return dfa
def showMinimization4(): tt = { frozenset(['a']): { '0': frozenset(['b']), '1': frozenset(['c']) }, frozenset(['b']): { '0': frozenset(['e']), '1': frozenset(['f']) }, frozenset(['c']): { '0': frozenset(['a']), '1': frozenset(['a']) }, frozenset(['d']): { '0': frozenset(['f']), '1': frozenset(['e']) }, frozenset(['e']): { '0': frozenset(['d']), '1': frozenset(['f']) }, frozenset(['f']): { '0': frozenset(['d']), '1': frozenset(['e']) } } sigma = set(['0', '1']) start = 'a' end = set([frozenset(['e']), frozenset(['f'])]) dfa = DFA(tt, start, end) dfa.visualize() eqCls = dfa.findEquivalenceClasses(sigma) dfa.mergeEquivalentStates(eqCls) dfa.visualize(fname='fsm1.gv')
def minimize_dfa(dfa): mark = {(x, y): False for x in dfa.states for y in dfa.states} mark_old = {(x, y): False for x in dfa.states for y in dfa.states} # final state와 non-final state는 구분 가능함 for x in dfa.final_states: for y in dfa.states.difference(dfa.final_states): mark[(x, y)] = mark[(y, x)] = True while mark_old != mark: mark_old = mark.copy() for x in dfa.states: for y in dfa.states: if mark[(x, y)] == True: continue if any(mark[(dfa.step_transit(x, char), dfa.step_transit(y, char))] for char in dfa.vocabulary): mark[(x, y)] = mark[(y, x)] = True undistinguishable = list( map(lambda z: z[0], filter(lambda z: z[1], mark.items()))) def partition(state): return tuple(sorted(filter(lambda y: not mark[(state, y)], dfa.states))) states = set() for x in dfa.states: states.add(partition(x)) vocabulary = dfa.vocabulary transition_function = dict() for state in states: for char in dfa.vocabulary: transition_function[(state, char)] = partition( dfa.transit(state[0], char)) initial_state = partition(dfa.initial_state) final_states = set(map(partition, dfa.final_states)) return DFA(states, vocabulary, transition_function, initial_state, final_states)
def to_dfa(self): """ Translate this ndfa to dfa """ from DFA import DFA dfa_transitions = {} final_states = set() start_state = ','.join(sorted(self.start)) def create_new_state(states): name = ','.join(sorted(states)) if not any(i[0] == name for i in dfa_transitions): # Check final state if len(set(states).intersection(self.finals)) > 0: final_states.add(name) next_states = {key: set() for key in [char for char in self.alphabet]} states = list(states) for s in states: # Search next states if (s, '$') in self.transitions: states.extend(self.transitions[(s, '$')]) for char in self.alphabet: if (s, char) in self.transitions: next_states[char] |= set(self.transitions[(s, char)]) for s in next_states: # Create next states next_state = ','.join(sorted(next_states[s])) if next_state == '': dfa_transitions[(name, s)] = '{}' else: dfa_transitions[(name, s)] = next_state create_new_state(next_states[s]) # Generate the new states create_new_state(self.start) # Check if there is an fuik created if any(dfa_transitions[i] == '{}' for i in dfa_transitions): for char in self.alphabet: dfa_transitions[('{}', char)] = '{}' return DFA(self.alphabet, dfa_transitions, start_state, final_states)
def regex_to_dfa(root, visitor): Dstates = [frozenset(root.firstpos)] Dtran = defaultdict(dict) q_0 = frozenset(root.firstpos) queue = [frozenset(root.firstpos)] while queue: S = queue.pop(0) for a in ['a', 'b']: U = set() for p in S: if visitor.charmap[p - 1] == a: U = U.union(visitor.followpos[p]) U = frozenset(U) if U not in Dstates: Dstates.append(U) queue.append(U) Dtran[S][a] = U F = {q for q in Dstates if len(visitor.charmap) in q} return DFA(Dstates, {'a', 'b'}, Dtran, q_0, F)
def from_dict(dct): accepts = set(dct['accepts']) start = dct['start'] transitions = dct['transitions'] states = { state for state_transitions in transitions.values() for state in state_transitions.values() }.union(set(transitions.keys())) alphabet = { token for state_transitions in transitions.values() for token in state_transitions.keys() } return DFA.DFA(states=states, alphabet=alphabet, transitions=transitions, start=start, accepts=accepts)
def dfa(self): # dictionary btw dfa states and e-nfa states dfa_states_dict = dict(zip(['q'+str(i) for i in range(2**len(self.states))], powerset(self.states))) #print(dfa_states_dict) dfa_states_inverse_dict = dict(zip(dfa_states_dict.values(), dfa_states_dict.keys())) # vocabulary dfa_vocabulary = self.vocabulary #print(dfa_vocabulary) # transition function dfa_transition_function = dict() for i in dfa_states_dict.keys(): nfa_state = dfa_states_dict[i] for j in dfa_vocabulary: nfa_transit = tuple(sorted(self.step_transit(nfa_state, j))) #print(nfa_transit) dfa_result = dfa_states_inverse_dict[nfa_transit] dfa_transition_function[(i, j)] = dfa_result #print(dfa_transition_function) # initial state dfa_initial_state = dfa_states_inverse_dict[ tuple(sorted(self.e_closure(self.initial_state)))] #print(dfa_initial_state) # final states dfa_final_states = set() for i in dfa_states_dict.keys(): if any(state in dfa_states_dict[i] for state in self.final_states): dfa_final_states.add(i) #print(dfa_final_states) return DFA(dfa_states_dict.keys(), dfa_vocabulary, dfa_transition_function, dfa_initial_state, dfa_final_states)
def toDFA(self): newStates = [] initialState = self.getInitialState() newStates.append(initialState.label) alphabet = self.alphabet.split(',') stateTable = [] x = 0 stateTmp = '' for state in newStates: stateTable.append([None] * len(alphabet)) y = 0 for a in alphabet: print("ALPHAB ", a, " state ", state, " -- ", stateTmp) if stateTmp != state: stateTo = getStateTo(self, state, a) if stateTo: print("YES") stateTmp = state stateTable[x][y] = stateTo newStates.append(stateTo) y = y + 1 x = x + 1 newDFA = DFA(self.name + "-toDFA", alphabet) newDFA.addState(normalizeState(newStates[0]), True) for i in range(1, len(newStates)): newDFA.addState(normalizeState(newStates[i]), False, lookForFinal(self, newStates[i])) for i in range(len(stateTable)): for j in range(len(stateTable[i])): if (stateTable[i][j]): newDFA.addTransition(alphabet[j], normalizeState(newStates[i]), normalizeState(stateTable[i][j])) return newDFA
def minimize_dfa(nodes, start): """ building a DFA automaton from the nodes, and returning an equal graph using MN algorithm. :param nodes: the original nodes. :return: the minimized graph's set of nodes. """ fail_vec = init_state - 2 # a "garbage" state fail_state = SearchNode(State(fail_vec, quantized=tuple(fail_vec))) nodes.update({fail_state: {}}) states = nodes # start = SearchNode(State(init_state, quantized=tuple(init_state))) accepts = [node for node in nodes if node.is_accept] alphabet = set() for node in nodes: # making sure this is the alphabet that is actually being used alphabet.update(key for key in node.transitions) def delta(state, char): return nodes[state].get( char, fail_state) # return fail state in case no transition is set d = DFA.DFA(states=states, start=start, accepts=accepts, alphabet=alphabet, delta=delta) eq_classes = d.minimize() for state_class in eq_classes: # updating the nodes MN representatives representative = state_class[0] for node in state_class: node.representative = representative # update the node's transitions by the new delta function of the minimized DFA for node in d.states: new_transitions = {} for char in alphabet: new_transitions[char] = d.delta(node, char) node.transitions = new_transitions return d.states
from DFA import DFA """ The following parameters specify a DFA D which recgonizes the following language L(D) = {s | s contains an even number of zeros} """ set_states = {"s1", "s2"} set_alphabet = {"0", "1"} transition_dict = {("s1","0"): "s2", ("s1","1"):'s1', ("s2","0"):"s1", ("s2","1"):"s2"} start_state = "s1" set_final_states = {"s1"} D= DFA(set_states, set_alphabet, transition_dict, start_state, set_final_states) str = "100101010110111110101" print("{0} is accepted by D: {1}" .format(str, D.process_string(str)))
def _LineCommentDFA(): dfa = DFA() dfa.addStates(["lc0", "lc1", "lcloop", "lcfin"]) dfa.state = dfa.states[0] dfa.setFinal("lcfin") dfa.addTransition("lc0", "lc1", "-") dfa.addTransition("lc1", "lcloop", "-") dfa.addTransition("lcloop", "lcloop", LETTERS + DIGITS + OTHERS + " \t") dfa.addTransition("lcloop", "lcfin", "\n") return dfa
def __construct_min_dfa_from_marked_states(self, marked_states): sigma = self.get_sigma() final_states = self.get_final_states() unmarked_states = self.__get_unmarked_states(marked_states) new_delta_table = {} new_states = [] ##print 'Unmarked state pairs:' ##print_unmarked_state_pairs(marked_states) for old_state in self.get_states(): eqc = self.__construct_equivalence_class_for_state(old_state, unmarked_states) if len(eqc) > 0 and not eqc in new_states: new_states.append(eqc) old_start_state = self.get_start_state() ##print 'New States: ', str(new_states) new_start_state = [s for s in new_states if old_start_state in s][0] ##print 'New Start State: ', new_start_state ##return None q_of_states = [new_start_state] explored_states = {} while len(q_of_states) != 0: ##print q_of_states curr_state = q_of_states.pop() explored_states[curr_state] = True for s in sigma: old_state = self.delta(curr_state[0], s) new_state = self.__find_equivalence_class_for_state(old_state, new_states) if new_state == None: new_state = old_state, new_delta_table[(curr_state, s)] = new_state if not explored_states.has_key(new_state): q_of_states.append(new_state) rslt_dfa = DFA() rslt_dfa.set_states(set([x[0] for x in new_delta_table.iterkeys()])) rslt_dfa.set_sigma(sigma) rslt_dfa.set_start_state(new_start_state) rslt_dfa.set_delta_table(new_delta_table) rslt_dfa.set_final_states(set([x[0] for x in new_delta_table.iterkeys() if len(final_states.intersection(set(x[0]))) > 0])) return rslt_dfa
alphabet = ['a', 'b'] transitions = { ('q0', 'a'): 'q0', ('q0', 'b'): 'q1', ('q1', 'a'): 'q2', ('q1', 'b'): 'q1', ('q2', 'a'): 'q0', ('q2', 'b'): 'q3', ('q3', 'a'): 'q2', ('q3', 'b'): 'q1', } start = 'q0' finals = ['q3'] dfa = DFA(alphabet, transitions, start, finals) dfa_list.append(dfa) dfa_model_list.appendRow(QStandardItem(dfa.get_tuple_string())) # ----------------------------- # # Example 2 # # Simple DFA # # "Starts with 'ba'" # ----------------------------- # alphabet = ['a', 'b'] transitions = { ('q0', 'a'): '{}', ('q0', 'b'): 'q1', ('q1', 'a'): 'q2', ('q1', 'b'): '{}', ('q2', 'a'): 'q2',
def _LiteralDFA(): dfa = DFA() dfa.addStates(["l0", "lloop", "lescape", "lfin"]) dfa.state = dfa.states[0] dfa.setFinal("lfin") dfa.addTransition("l0", "lloop", "\"") loopchars = (LETTERS + DIGITS + OTHERS + " \n\r\t").replace("\\", "").replace("\"", "") dfa.addTransition("lloop", "lloop", loopchars) dfa.addTransition("lloop", "lescape", "\\") dfa.addTransition("l0", "lescape", "\\") dfa.addTransition("lescape", "lloop", "ntr\"\'\\") dfa.addTransition("lloop", "lfin", "\"") return dfa
def _MultiLineCommentDFA(): dfa = DFA() dfa.addStates(["q0", "q1", "q2", "q3", "qloop", "q4", "q5"]) dfa.state = "q0" dfa.setFinal("q5") dfa.addTransition("q0", "q1", "-") dfa.addTransition("q1", "q2", "-") dfa.addTransition("q2", "q3", "[") dfa.addTransition("q3", "qloop", "[") loopchars = (LETTERS + DIGITS + OTHERS + " \t\n\r").replace("]", "") dfa.addTransition("qloop", "qloop", loopchars) dfa.addTransition("qloop", "q4", "]") dfa.addTransition("q4", "qloop", loopchars) dfa.addTransition("q4", "q5", "]") return dfa
from DFA import DFA model = DFA(["a", "b"], "aabaa") model.plot()
dfa.addStates(["q0", "q1", "q2", "q3", "qloop", "q4", "q5"]) dfa.state = "q0" dfa.setFinal("q5") dfa.addTransition("q0", "q1", "-") dfa.addTransition("q1", "q2", "-") dfa.addTransition("q2", "q3", "[") dfa.addTransition("q3", "qloop", "[") loopchars = (LETTERS + DIGITS + OTHERS + " \t\n\r").replace("]", "") dfa.addTransition("qloop", "qloop", loopchars) dfa.addTransition("qloop", "q4", "]") dfa.addTransition("q4", "qloop", loopchars) dfa.addTransition("q4", "q5", "]") return dfa LuaTokens = map(lambda str: (str, DFA.createString(str)), _operators + _keywords) LuaTokens += [("LINE_COMMENT", _LineCommentDFA()), ("MULTILINE_COMMENT", _MultiLineCommentDFA()), ("NUMBER", _NumberDFA()), ("LITERAL", _LiteralDFA()), ("IDENTIFIER", _IdentifierDFA())] #LuaConfig = LexerConfig() #LuaConfig.addToken(r'--\[\[(.|\n)*\]\]', 'MULTILINE_COMMENT') #LuaConfig.addToken(r'--.*\n', 'LINE_COMMENT') #LuaConfig.addToken(r'[a-zA-Z]+\w*', 'IDENTIFIER') #LuaConfig.addToken(r'[0-9]+', 'NUMBER') #LuaConfig.addToken(r'\"([^\\\n]|(\\(.|\n)))*?\"', 'STRING') #[LuaConfig.addKeyword(re.escape(keyword)) for keyword in _keywords] #[LuaConfig.addKeyword(re.escape(op)) for op in _operators]