Subversion Repositories programming

Rev

Rev 124 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
123 ira 1
#!/usr/bin/python
2
#Copyright: Ira W. Snyder
3
# Start Date: 2005-10-08
4
# End Date: 
5
# License: Public Domain
6
#
7
# Changelog Follows:
8
#   -- 2005-10-08 1830
9
#       Wrote and debugged the entire automaton class
10
#
11
 
12
import sys, string
13
 
14
class automaton:
15
 
16
    def __init__(self):
17
        self.__clear()
18
        self.verbose = False
19
 
20
    def __clear(self):
21
        self.num_states = 0
22
        self.num_letters = 0
23
        self.final_states = []
24
        self.trans_function = []
25
 
26
    def main_menu(self):
27
        done = False
28
 
29
        while(not done):
30
            print "Menu:"
31
            print "========================================"
32
            print "1. Enter an automaton"
33
            print "2. Run automaton"
34
            print "3. Enable Verbose Mode"
35
            print "4. Disable Verbose Mode"
36
            print "5. Quit"
37
            print
38
            s = raw_input("Choice >>> ")
39
            print
40
 
41
            if s == '1':
42
                self.__clear()
43
                self.read_automaton()
44
                print
45
            elif s == '2':
46
                input = raw_input("Enter string to test: ")
47
                self.run_automaton(input)
48
                print
49
            elif s == '3':
50
                self.verbose = True
51
                print "Verbose Mode Enabled!"
52
                print
53
            elif s == '4':
54
                self.verbose = False
55
                print "Verbose Mode Disabled!"
56
                print
57
            elif s == '5':
58
                done = True
59
            else:
60
                print 'Bad Selection'
61
                print
62
 
63
    def read_automaton(self):
64
        done = False
65
 
66
        ### Find out the number of states
67
        while(not done):
68
            s = raw_input("How many states?: ")
69
 
70
            try:
71
                self.num_states = int(s)
72
                done = True
73
            except (TypeError, ValueError):
74
                print "That wasn't an integer, try again!"
75
                print
76
 
77
        ### Find out what the last letter is
78
        done = False
79
 
80
        while(not done):
81
            s = raw_input("What is the last letter?: ")
82
 
83
            if len(s) != 1:
84
                print "One letter only please, try again!"
85
                print
86
 
87
            ### Lowercase the input
88
            s = s.lower()
89
 
90
            for i in range(26):
91
                if string.letters[i] == s:
92
                    self.num_letters = i + 1
93
                    done = True
94
 
95
            if not done:
96
                print "That was not a letter, try again!"
97
                print
98
 
99
        ### Find out which states are final states
100
        done = False
101
 
102
        print
103
        print "Enter a blank line to stop entering Final States"
104
        print
105
 
106
        while(not done):
107
            s = raw_input("Final state: ")
108
 
109
            ### Done reading input
110
            if len(s) == 0:
111
                done = True
112
                break
113
 
114
            if self.check_state(s):
115
                self.final_states.append(int(s))
116
            else:
117
                print "That was not a valid state, try again!"
118
 
119
        ### Read each state / letter pair
120
        for i in range(self.num_states):
121
            sublist = []
122
 
123
            for j in range(self.num_letters):
124
                done = False
125
                while not done:
126
                    s = raw_input("Enter state %d, letter %s -> state " % (i, string.letters[j]))
127
 
128
                    if self.check_state(s):
129
                        sublist.append(int(s))
130
                        done = True
131
                    else:
132
                        print "Not a valid state, try again!"
133
                        print
134
 
135
            self.trans_function.append(sublist)
136
 
137
        print "Done reading the automaton!"
138
 
139
    def check_state(self, s):
140
        """Checks the string representing a state to see if it's a valid state.
141
        Returns True if it is valid, False otherwise"""
142
        retVal = True
143
 
144
        try:
145
            state = int(s)
146
        except (TypeError, ValueError):
147
            retVal = False
148
            state = 99999999
149
 
150
        if state >= self.num_states:
151
            retVal = False
152
 
153
        return retVal
154
 
155
    def get_letter_num(self, s):
156
        s = s.lower()
157
 
158
        for i in range(26):
159
            if s == string.letters[i]:
160
                return i
161
 
162
        return -1
163
 
164
    def next_state(self, cur_state, letter):
165
        letter_num = self.get_letter_num(letter)
166
 
167
        if letter_num == -1 or letter_num >= self.num_letters:
168
            print "There was a bad letter in the input"
169
            print "*** I WILL STAY AT THE CURRENT STATE ***"
170
            return cur_state
171
            #sys.exit(1)
172
 
173
        next_state = self.trans_function[cur_state][letter_num]
174
 
175
        if self.verbose:
176
            print "state %d letter %s -> state %d" % (cur_state, letter, next_state)
177
 
178
        return next_state
179
 
180
    def run_automaton(self, input_string):
181
        cur_state = 0 #initial state is always q0
182
 
183
        for c in input_string:
184
            cur_state = self.next_state(cur_state, c)
185
 
186
        print "Done Running"
187
        print
188
        print "Final State: %d" % cur_state
189
        print "Accepted: %s" % (self.is_final_state(cur_state) and "Yes" or "No", )
190
        print
191
        print
192
        s = raw_input('Press ENTER to return to the main menu')
193
 
194
    def is_final_state(self, s):
195
        """Returns True if s is a final state, False otherwise"""
196
        retVal = True
197
 
198
        try:
199
            self.final_states.index(s)
200
        except ValueError:
201
            retVal = False
202
 
203
        return retVal
204
 
205
    def enable_verbose(self):
206
        self.verbose = True
207
 
208
    def disable_verbose(self):
209
        self.verbose = False
210
 
211
if __name__ == '__main__':
212
    a = automaton()
213
    a.main_menu()
214