Subversion Repositories programming

Rev

Rev 127 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 127 Rev 128
Line 24... Line 24...
24
#       Made verbose mode into a toggle.
24
#       Made verbose mode into a toggle.
25
#
25
#
26
#   -- 2005-10-12 2000
26
#   -- 2005-10-12 2000
27
#       Added True/False compatibility for versions of python that
27
#       Added True/False compatibility for versions of python that
28
#       are very old.
28
#       are very old.
-
 
29
#       Changed the "bad input" error handler to use try / except.
29
#
30
#
30
 
31
 
31
import sys, string
32
import sys, string
32
 
33
 
33
### Define True and False to compatible values if we are running on a version
34
### Define True and False to compatible values if we are running on a version
Line 37... Line 38...
37
except:
38
except:
38
    True = 1
39
    True = 1
39
    False = 0
40
    False = 0
40
 
41
 
41
class automaton:
42
class automaton:
-
 
43
    """The main workhorse class. Call the main_menu() function to let the user
-
 
44
    enter and run an automaton."""
42
    
45
    
43
    def __init__(self):
46
    def __init__(self):
44
        """Constructor for the automaton object"""
47
        """Constructor for the automaton object"""
45
        self.clear()
48
        self.clear()
46
        self.verbose = False
49
        self.verbose = False
Line 188... Line 191...
188
    def next_state(self, cur_state, letter):
191
    def next_state(self, cur_state, letter):
189
        """Return the next state (as an integer) given the current state, and
192
        """Return the next state (as an integer) given the current state, and
190
        the next letter of input"""
193
        the next letter of input"""
191
        letter_num = self.get_letter_num(letter)
194
        letter_num = self.get_letter_num(letter)
192
 
195
 
-
 
196
        # There is a bad letter in the input
193
        if letter_num == -1 or letter_num >= self.num_letters:
197
        if letter_num == -1 or letter_num >= self.num_letters:
194
            print 'Bad letter in the input *** I WILL STAY AT THE CURRENT STATE ***'
-
 
195
            return cur_state
198
            raise ValueError
196
            #sys.exit(1)
-
 
197
    
199
    
198
        next_state = self.trans_function[cur_state][letter_num]
200
        next_state = self.trans_function[cur_state][letter_num]
199
    
201
    
200
        if self.verbose:
202
        if self.verbose:
201
            print 'state %d letter %s -> state %d' % (cur_state, letter, next_state)
203
            print 'state %d letter %s -> state %d' % (cur_state, letter, next_state)
202
 
204
 
203
        return next_state
205
        return next_state
204
 
206
 
205
    def run_automaton(self, input_string):
207
    def run_automaton(self, input_string):
206
        """Run the automaton through the input string given by input_string"""
208
        """Run the automaton through the input string given by input_string"""
207
        cur_state = 0 #initial state is always q0
209
        cur_state = 0 # initial state is always q0
208
        
210
        
-
 
211
        try:
209
        for c in input_string:
212
            for c in input_string:
210
            cur_state = self.next_state(cur_state, c)
213
                cur_state = self.next_state(cur_state, c)
-
 
214
        except ValueError:
-
 
215
            print 'There was a bad letter in the input, stopping here!'
211
 
216
 
212
        print 'Done Running'
217
        print 'Done Running'
213
        print
218
        print
214
        print 'Final State: %d' % cur_state
219
        print 'Final State: %d' % cur_state
215
        print 'Accepted: %s' % (self.is_final_state(cur_state) and 'Yes' or 'No', )
220
        print 'Accepted: %s' % (self.is_final_state(cur_state) and 'Yes' or 'No', )
Line 236... Line 241...
236
 
241
 
237
### The "main" function
242
### The "main" function
238
if __name__ == '__main__':
243
if __name__ == '__main__':
239
    a = automaton()
244
    a = automaton()
240
    a.main_menu()
245
    a.main_menu()
-
 
246