Subversion Repositories programming

Rev

Rev 161 | Rev 163 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
161 ira 1
#!/usr/bin/env python
2
# Copyright: Ira W. Snyder
3
# Start Date: 2005-11-18
4
# End Date:
5
# License: Public Domain
6
#
7
# Changelog Follows:
8
#
9
# 2005-11-18
10
# * Just getting the basics in place, since we haven't been given
11
#   the whole description of the project yet.
12
#
13
 
14
# Check for <Python-2.3 compatibility (boolean values)
15
try:
16
  True, False
17
except NameError:
18
  (True, False) = (1, 0)
19
 
162 ira 20
import sys
21
 
22
class RecursiveDescentParser:
161 ira 23
    def __init__(self):
162 ira 24
        self.__clear()
161 ira 25
 
26
    def __clear(self):
27
        self.str = ""   # the string of input to test
28
        self.strpos = 0 # the current position in str
29
 
30
    def __input_test_str(self):
162 ira 31
        self.str = raw_input("input str: ")
161 ira 32
 
33
    def main_menu(self):
34
 
35
        done = False
36
 
37
        while not done:
38
            print 'Menu:'
39
            print '========================================'
40
            print '1. Test a string'
41
            print '2. Quit'
42
            print
43
            s = raw_input('Choice >>> ')
44
            print
45
 
46
            if s == '1':
162 ira 47
                self.__clear()
161 ira 48
                self.__input_test_str()
49
                self.__test_str()
50
            elif s == '2':
51
                done = True
52
            else:
53
                print 'Bad Selection'
54
                print
55
 
162 ira 56
    def __test_str(self):
57
        print 'Parsing: %s' % (self.procE() and 'Completed' or 'Failed', )
161 ira 58
 
162 ira 59
    def procE(self):
60
        print 'procE(%s) ->' % (self.str[self.strpos:])
61
 
62
        return self.procT() and self.procEprm()
63
 
64
    def procT(self):
65
        print 'procT(%s) ->' % (self.str[self.strpos:])
66
 
67
        if self.str[self.strpos] == '0':
68
            self.strpos += 1
69
            return True
70
 
71
        return False
72
 
73
    def procEprm(self):
74
        # Check empty str
75
        if self.strpos >= len(self.str):
76
            return True
77
 
78
        print 'procEprm(%s) ->' % (self.str[self.strpos:])
79
 
80
        if self.str[self.strpos] == '+' or self.str[self.strpos] == '-':
81
            self.strpos += 1
82
            return self.procT() and self.procEprm()
83
 
84
        return False
85
 
161 ira 86
if __name__ == '__main__':
162 ira 87
    rdp = RecursiveDescentParser()
88
    rdp.main_menu()
161 ira 89