Subversion Repositories programming

Rev

Rev 162 | Rev 164 | 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
163 ira 21
 
162 ira 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', )
163 ira 58
        print
161 ira 59
 
162 ira 60
    def procE(self):
61
        print 'procE(%s) ->' % (self.str[self.strpos:])
163 ira 62
 
162 ira 63
        return self.procT() and self.procEprm()
64
 
163 ira 65
    def procEprm(self):
66
        # Check empty str
67
        if self.strpos >= len(self.str):
68
            return True
69
 
70
        print 'procEprm(%s) ->' % (self.str[self.strpos:])
71
 
72
        # Check +
73
        if self.str[self.strpos] == '+':
74
            self.strpos += 1
75
            return self.procT() and self.procEprm()
76
 
77
        # Check -
78
        if self.str[self.strpos] == '-':
79
            self.strpos += 1
80
            return self.procT() and self.procEprm()
81
 
82
        # We accept empty str, so take it
83
        return True
84
 
162 ira 85
    def procT(self):
86
        print 'procT(%s) ->' % (self.str[self.strpos:])
87
 
163 ira 88
        return self.procF() and self.procTprm()
162 ira 89
 
163 ira 90
    def procTprm(self):
162 ira 91
        # Check empty str
92
        if self.strpos >= len(self.str):
93
            return True
94
 
163 ira 95
        print 'procTprm(%s) ->' % (self.str[self.strpos:])
162 ira 96
 
163 ira 97
        # Check *
98
        if self.str[self.strpos] == '*':
162 ira 99
            self.strpos += 1
163 ira 100
            return self.procF() and self.procTprm()
162 ira 101
 
163 ira 102
        # we accept empty str, so take it
103
        return True
104
 
105
    def procF(self):
106
        print 'procF(%s) ->' % (self.str[self.strpos:])
107
 
108
        # Check x
109
        if self.str[self.strpos] == 'x':
110
            self.strpos += 1
111
            return self.procV()
112
 
113
        # Check (E)
114
        if self.str[self.strpos] == '(':
115
            self.strpos += 1
116
 
117
            if self.procE():
118
                if self.str[self.strpos] == ')':
119
                    return True
120
 
121
            return False
122
 
123
        # Must be a number
124
        return self.procN()
125
 
126
    def procN(self):
127
        print 'procN(%s) ->' % (self.str[self.strpos:])
128
 
129
        if self.str[self.strpos] in '0123456789':
130
            self.strpos += 1
131
            return True
132
 
162 ira 133
        return False
134
 
163 ira 135
    def procV(self):
136
        print 'procV(%s) ->' % (self.str[self.strpos:])
137
 
138
        return self.procN()
139
 
161 ira 140
if __name__ == '__main__':
162 ira 141
    rdp = RecursiveDescentParser()
142
    rdp.main_menu()
161 ira 143