def post_dfa(array: list): try: scales, fluct, alpha = dfa(array) calresult = alpha return '{result}'.format(result=calresult) except: return NoContent, 404
def postproc(outfname, debug = 0) : machlist = [] for idx in range(len(gt.statemachs)) : unanch,anch = gt.statemachs[idx] if anch == None : mach = unanch else : mach = gt.n.dualmach(unanch, anch) if mach == None : # XXX use state name not number raise SpecError("No regular expressions for state %d!" % idx) machlist.append((mach, unanch),) if debug >= 3 : # XXX this is flawed. the NFA dot printer doesnt work # properly for the dualmach() joined machines because # their states are not strictly sequential! This will # print extra nodes not really in the NFA! gt.n.dot(mach) d = dfa.dfa(machlist, gt.relist) # XXX flag to sanity to specify debug level? printfull = (debug >= 1) d.sanity(printfull) if debug >= 2 : shownfa,showccl = 0,0 if debug >= 3 : shownfa,showccl = 1,1 d.dot(shownfa, showccl) gen(outfname, d)
fh = fh[1:n+1] fw = fw[1:n+1] ftot = fh * fw # matching the conventions of the Numerical Recipes ftot = np.hstack((ftot, np.zeros(n-1))) x = np.fft.ifft(ftot) return np.real(x[:n]) if __name__=='__main__': x1 = power_law_noise(2**12, 0.5) x2 = power_law_noise(2**12, 0.8) x3 = power_law_noise(2**12, 1.2) plt.subplot(311) plt.plot(x1) plt.subplot(312) plt.plot(x2) plt.subplot(313) plt.plot(x3) plt.show() for e,xx in enumerate([x1,x2,x3]): scales, fluct, alpha = dfa(xx) print("DFA exponent {}: {}".format(e+1, alpha))
def regexp_machine (exp, ignore_case=1): import dfa fa, finals = regexp_to_dfa (exp) state_table, finals_table = build_dfa_tables (fa, finals, ignore_case) m = dfa.dfa (state_table, finals_table) return m
"""defines the main line for the program""" import dfa import sys myDFA = dfa.dfa(); "Begin functions" def main(): """Defines the main execution line for the program""" print("Welcome to the DFA->RegEx converter program"); print("A default DFA has been created for you: "); myDFA.printTuple(); option = 0; while(option != 'x'): option = input("What would you like to do?\n" + "Press 1 to add/remove a state\n" + "Press 2 to add/remove a symbol to/from the alphabet\n" + "Press 3 to add/remove a transition to a state\n" + "Press 4 to set the start state for the DFA\n" + "Press 5 to add/remove accept states to/from the DFA\n" + "Press 0 to convert to Regex\n" + "Press x to exit\n"); handle_input(option); def handle_input(option): """handles user input""" if(option == '1'): option = input("Press 0 to remove a state or 1 to add a state:\n");
from nfa import nfa from dfa import dfa infile = raw_input('automata file: ') instr = raw_input('input string: ') n0 = nfa(infile) n0.draw('nfa.png') d0 = dfa() d0.set(n0.to_dfa()) d0.draw('cdfa.png') print "accepts %s: %s" %(instr, n0.consume(instr))
def nfaTodfa(nfa): if nfa.is_dfa(): return states_map = {} #new_state_in_dfa:[states in nfa] states_mapR = {} que = [] #a que to find new states d=dfa() if len(nfa.get_initialStates()) != 1: raise "Initial state Error!!" new_state = state(create_new_name([i.name for i in d.states])) new_state.isInitial = True states_map.update({ new_state.name : nfa.get_initialStates() }) states_mapR.update({tuple(id(i) for i in nfa.get_initialStates()): new_state}) d.add_state(new_state) que.append(new_state) while 1.1+2.2!=3.3: #input() if len(que): current_state = que.pop(0) else: break #print(states_map) ''' poping a state from que means: 1.state has been added to dictionary and dfa 2.state is not connected properly so this loop will try to find proper connections per aplphabet ''' tmp = {} for t in sorted(nfa.get_alphabet()): if t=="": continue dest = [] # print("t: "+t+"\tcurstate: "+current_state.name+"\tstates_map: {"+str(",".join([i+"->"+",".join([j.name for j in states_map[i]]) for i in states_map] ))) for s in states_map[current_state.name]: #for i in s.neighbours.get(t): for i in s.transition_function(t): if not i in dest: dest.append(i) if dest != []: dest.sort() dest_id = tuple(id(i) for i in dest) # print("||".join([i.name for i in dest])) # print(dest_id) if not dest in states_map.values(): # print("newstateadded") new_state = state(create_new_name([i.name for i in d.states])) for i in dest: if i.isInitial: #new_state.isInitial = True pass if i.isFinal: new_state.isFinal = True if new_state.isInitial and new_state.isFinal: break states_map.update({new_state.name : dest }) states_mapR.update({tuple(id(i) for i in dest): new_state}) d.add_state(new_state) que.append(new_state) current_state.connect(new_state,t) else: # print("no need for new state") current_state.connect(states_mapR[tuple(id(i) for i in dest)],t) # print("-"*20+"\n") # print("t: "+t+"\tcurstate: "+current_state.name+"\tstates_map: {"+str(",".join([i+"->"+",".join([j.name for j in states_map[i]]) for i in states_map] ))) # print(d) #ADD DEAD STATE alphabet = d.get_alphabet() dead_state = state("dead_state") is_dead_state_used = False for s in d.states: for a in alphabet: if not a in s.neighbours: s.connect(dead_state,a) is_dead_state_used = True if is_dead_state_used: d.add_state(dead_state) for i in alphabet: dead_state.connect(dead_state,i) #ADD INITIAL AND FINAL STATES '''for s in nfa.states: if s.isInitial: for i in ''' return d,states_map
for i in [a,b,c,d,e]: a.connect(i,'0') a.connect(d,'1') a.connect(e,'1') b.connect(c,'0') b.connect(e,'1') c.connect(b,'1') d.connect(e,'0') ''' a = state("a",isInitial=True) b = state("b") c = state("c",isFinal=True) d = state("d",isFinal=True) e = state("e",isFinal=True) f = state("f") m0 = dfa() for i in [a,b,c,d,e,f]: m0.add_state(i) a.connect(b,'0') a.connect(d,'1') b.connect(c,'1') b.connect(a,'0') c.connect(f,'1') c.connect(e,'0') d.connect(e,'0') d.connect(f,'1') f.connect(f,"0") f.connect(f,"1") e.connect(e,"0") e.connect(f,"1")
list_token.append(token) content = '' state = dfaTable['T0'] if len(line) == 1: token = Token(col, state.StateName, content, dfaTable) list_token.append(token) return list_token if __name__ == '__main__': #设置工作目录 os.chdir('Complier_task1\\') #读取产生式文件 prodlist = ls.LoadRule('src\\rule1.txt') #构造nfa表 nfaTable = nfa(prodlist) #nfaTable.prt() #构造dfa表 dfaTable = dfa(nfaTable) #dfaTable.prt() #根据dfa表将输入的源程序转换为token表 lines = ls.LoadSrc('src\\src.txt') list_token = [] for index in range(len(lines)): tokens = rcg(lines[index], index, dfaTable.dfaTable) list_token.extend(tokens) ls.SaveToken('res\\tokens.txt', list_token)
from dfa import dfa infile = raw_input('automata file: ') instr = raw_input('input string: ') d0 = dfa(infile) print "accepts %s: %s" %(instr, d0.consume(instr)) d0.draw('dfa.png')
def __init__(self, regex): self.regex = regex fs = nfa(regex) trans, start, final = fs.nfa() d = dfa(trans, start, final) self.dfa, self.final = d.dfa(fs.input)