5

Are there any modules available in Python to convert a regular expression to corresponding NFA, or do I have to build the code from scratch (by converting the regex from infix to postfix and then implementing Thompson's Algorithm to get the corresponding NFA)?

Is it possible in Python to get the state diagram of an NFA from the transition table?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
RatDon
  • 3,403
  • 8
  • 43
  • 85
  • 5
    Lay `off` the `monospace` blocks `man`. It `hurts` readability. – orlp Aug 01 '13 at 00:31
  • 4
    Freely available code, readily googled: http://www.ics.uci.edu/~eppstein/PADS/Automata.py – rici Aug 01 '13 at 00:36
  • @rici sorry dude. but not so much good with object orienting concept. difficult to understand that huge file. – RatDon Aug 01 '13 at 00:54
  • [Automata.py](http://www.ics.uci.edu/~eppstein/PADS/Automata.py) line 9: unable to find Util module in Google. line 15:unable to find PartitionRefinement module in Google. – RatDon Aug 01 '13 at 01:46
  • 2
    @RatDon: all the modules are in the same base directory: http://www.ics.uci.edu/~eppstein/PADS/ . Other than providing the pointer, I take no responsibility, although the code looks nice and clean to me. It was just one of many hits for the google search, though. I'm sure you can find another python library. – rici Aug 01 '13 at 05:34

1 Answers1

2
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

for i in regex:
    if i 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 is the pretty much code sample after the postfix. s contains the transition table and keys contains all the terminals used including e. e is used for Epsilon.

It's completely based on Thompson's Algorithm.

RatDon
  • 3,403
  • 8
  • 43
  • 85