Subversion Repositories programming

Rev

Rev 162 | Rev 164 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 162 Rev 163
Line 16... Line 16...
16
  True, False
16
  True, False
17
except NameError:
17
except NameError:
18
  (True, False) = (1, 0)
18
  (True, False) = (1, 0)
19
 
19
 
20
import sys
20
import sys
21
    
21
 
22
class RecursiveDescentParser:
22
class RecursiveDescentParser:
23
    def __init__(self):
23
    def __init__(self):
24
        self.__clear()
24
        self.__clear()
25
 
25
 
26
    def __clear(self):
26
    def __clear(self):
Line 53... Line 53...
53
                print 'Bad Selection'
53
                print 'Bad Selection'
54
                print
54
                print
55
 
55
 
56
    def __test_str(self):
56
    def __test_str(self):
57
        print 'Parsing: %s' % (self.procE() and 'Completed' or 'Failed', )
57
        print 'Parsing: %s' % (self.procE() and 'Completed' or 'Failed', )
-
 
58
        print
58
 
59
 
59
    def procE(self):
60
    def procE(self):
60
        print 'procE(%s) ->' % (self.str[self.strpos:])
61
        print 'procE(%s) ->' % (self.str[self.strpos:])
61
        
62
 
62
        return self.procT() and self.procEprm()
63
        return self.procT() and self.procEprm()
63
 
64
 
-
 
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
 
64
    def procT(self):
85
    def procT(self):
65
        print 'procT(%s) ->' % (self.str[self.strpos:])
86
        print 'procT(%s) ->' % (self.str[self.strpos:])
66
        
-
 
67
        if self.str[self.strpos] == '0':
-
 
68
            self.strpos += 1
-
 
69
            return True
-
 
70
 
87
 
71
        return False
88
        return self.procF() and self.procTprm()
72
 
89
 
73
    def procEprm(self):
90
    def procTprm(self):
74
        # Check empty str
91
        # Check empty str
75
        if self.strpos >= len(self.str):
92
        if self.strpos >= len(self.str):
76
            return True
93
            return True
77
 
94
 
78
        print 'procEprm(%s) ->' % (self.str[self.strpos:])
95
        print 'procTprm(%s) ->' % (self.str[self.strpos:])
79
 
96
 
-
 
97
        # Check *
80
        if self.str[self.strpos] == '+' or self.str[self.strpos] == '-':
98
        if self.str[self.strpos] == '*':
-
 
99
            self.strpos += 1
-
 
100
            return self.procF() and self.procTprm()
-
 
101
 
-
 
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] == '(':
81
            self.strpos += 1
115
            self.strpos += 1
-
 
116
 
-
 
117
            if self.procE():
82
            return self.procT() and self.procEprm()
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
83
 
132
 
84
        return False
133
        return False
85
 
134
 
-
 
135
    def procV(self):
-
 
136
        print 'procV(%s) ->' % (self.str[self.strpos:])
-
 
137
 
-
 
138
        return self.procN()
-
 
139
 
86
if __name__ == '__main__':
140
if __name__ == '__main__':
87
    rdp = RecursiveDescentParser()
141
    rdp = RecursiveDescentParser()
88
    rdp.main_menu()
142
    rdp.main_menu()
89
 
143