Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 16th, 2012  |  syntax: None  |  size: 0.36 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python2
  2. print "Enter a string"
  3. str = raw_input()
  4.  
  5. state = 0
  6. dfa = [{'a':1,'b':0,'c':0},{'b':2,'a':0,'c':0},{'a':3,'b':1,'c':0},{'c':4,'a':2,'b':1},{'a':4,'b':4,'c':4}]
  7.  
  8. for i in str:
  9.         print i,': ',state,' -> ',
  10.         state = dfa[state][i]
  11.         print state
  12.  
  13. if state == 4:
  14.         print "String belongs to the language"
  15. else:
  16.         print "String does not belong to the language"