python - How can I convert a regex to an NFA? -


are there modules available in python convert regular expression corresponding nfa, or have build code scratch (by converting regex infix postfix , implementing thompson's algorithm corresponding nfa)?

is possible in python state diagram of nfa transition table?

regex=''.join(postfix)  keys=list(set(re.sub('[^a-za-z0-9]+', '', regex)+'e'))  s=[];stack=[];start=0;end=1  counter=-1;c1=0;c2=0  in regex:     if in keys:         counter=counter+1;c1=counter;counter=counter+1;c2=counter;         s.append({});s.append({})         stack.append([c1,c2])         s[c1][i]=c2     elif i=='*':         r1,r2=stack.pop()         counter=counter+1;c1=counter;counter=counter+1;c2=counter;         s.append({});s.append({})         stack.append([c1,c2])         s[r2]['e']=(r1,c2);s[c1]['e']=(r1,c2)         if start==r1:start=c1          if end==r2:end=c2      elif i=='.':         r11,r12=stack.pop()         r21,r22=stack.pop()         stack.append([r21,r12])         s[r22]['e']=r11         if start==r11:start=r21          if end==r22:end=r12      else:         counter=counter+1;c1=counter;counter=counter+1;c2=counter;         s.append({});s.append({})         r11,r12=stack.pop()         r21,r22=stack.pop()         stack.append([c1,c2])         s[c1]['e']=(r21,r11); s[r12]['e']=c2; s[r22]['e']=c2         if start==r11 or start==r21:start=c1          if end==r22 or end==r12:end=c2  print keys  print s 

this pretty code sample after postfix. s contains transition table , keys contains terminals used including e. e used epsilon.

it's based on thompson's algorithm.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -