Skip to content

Instantly share code, notes, and snippets.

@pujansrt
Last active December 28, 2021 10:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pujansrt/1218502 to your computer and use it in GitHub Desktop.
Save pujansrt/1218502 to your computer and use it in GitHub Desktop.
A Generator for generating a Finite State Machine in Python

Finite State Machine Generator

Tested using: Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin

Mac OS X 10.6.2

Creation

To create a FSM machine: 1) Make or use one of the sample input files (input, input1, or abc2) input is the example shown in class. input1 and abc2's FSM diagrams can be found in their corresponding name.png 2) At this time, the input file is hardcoded into the fsm_machine.py at line 13; sorry for any inconvenience at the moment. Please take a look at the input file sample for proper formatting of your data. Please don't include any special characters or parentheses. 3) The input order is: q=states (with first item being start state) f=accept states a=alphabet transition function maps 4) To create your machine: python fsm_machine.py check output and make sure there are no odd characters or garbage in the output. Then feel free to export the machine code from shell i.e. python fsm_machine.py > your_machine.py

Running

To run your machine: python your_machine.py

  1. Enter string
  2. the fsm will say "yes" if acceptable "no" if not acceptable by this machine
#fsm_machine created using fsm_maker.py
#Created by Sam Jp on 2010-02-15
import sys
import os
def main():
char=raw_input("Enter a string: ")
fsm(char)
def fsm(char):
state="s"
f=list()
f= ['q1', 'r1']
for i in range(len(char)):
ch=char[i]
if state is "s":
if ch is "a":
state="q1"
continue
if ch is "b":
state="r1"
continue
elif state is "q1":
if ch is "a":
state="q1"
continue
if ch is "b":
state="q2"
continue
elif state is "q2":
if ch is "a":
state="q1"
continue
if ch is "b":
state="q2"
continue
elif state is "r1":
if ch is "a":
state="r2"
continue
if ch is "b":
state="r1"
continue
elif state is "r2":
if ch is "a":
state="r2"
continue
if ch is "b":
state="r1"
continue
if state in f:
print "Yes"
else:
print "No"
if __name__ == '__main__':
main()
s,q1,q2,r1,r2
q1,r1
a,b
s,a,q1
s,b,r1
q1,a,q1
q1,b,q2
q2,a,q1
q2,b,q2
r1,a,r2
r1,b,r1
r2,a,r2
r2,b,r1
#!/usr/bin/env python
# encoding: utf-8
"""
fsm_maker.py
Created by Sam Jp on 2010-02-11.
"""
import sys
import os
import time
import datetime
def main():
fin = file("abc2","r")
q=[]
f=[]
a=[]
q=fin.readline().strip().split(',') #q is states
f=fin.readline().strip().split(',') #f is final/accept state
a=fin.readline().strip().split(',') #a is alphabet
#fun is transition function
fun=list()
for line in fin.readlines():
fun.append(line.strip().split(','))
fin.close()
printPartOne()
printPartTwo(q,a,fun,f)
def printPartOne():
print "#fsm_machine created using fsm_maker.py"
print "#Created by Sam Jp on ", datetime.date.today()
print "import sys"
print "import os"
print "\n"
print "def main():"
print "\tchar=raw_input(\"Enter a string: \")"
print "\tfsm(char)"
print "\ndef fsm(char):"
def printPartTwo(q,a,fun,f):
j=0
print "\tstate=\""+q[0]+"\""
print "\tf=list()"
print "\tf=",f
print "\tfor i in range(len(char)):"
print "\t\tch=char[i]"
print "\t\tif state is \""+q[0]+"\":"
for k in range(len(a)):
print "\t\t\tif ch is \""+fun[j][1]+"\":"
print "\t\t\t\tstate=\""+fun[j][2]+"\""
j=j+1
print "\t\t\t\tcontinue"
for i in range(1,len(q)):
print "\t\telif state is \""+q[i]+"\":"
for k in range(len(a)):
print "\t\t\tif ch is \""+fun[j][1]+"\":"
print "\t\t\t\tstate=\""+fun[j][2]+"\""
print "\t\t\t\tcontinue"
j=j+1
print"\n\tif state in f:"
print "\t\tprint \"Yes\""
print "\telse:"
print "\t\tprint \"No\""
print "\nif __name__ == '__main__':"
print "\tmain()"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment