Subversion Repositories programming

Rev

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

Rev 161 Rev 162
Line 15... Line 15...
15
try:
15
try:
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
-
 
21
    
20
class RecursiveDescentParser():
22
class RecursiveDescentParser:
21
    def __init__(self):
23
    def __init__(self):
22
        pass
24
        self.__clear()
23
 
25
 
24
    def __clear(self):
26
    def __clear(self):
25
        self.str = ""   # the string of input to test
27
        self.str = ""   # the string of input to test
26
        self.strpos = 0 # the current position in str
28
        self.strpos = 0 # the current position in str
27
 
29
 
28
    def __input_test_str(self):
30
    def __input_test_str(self):
29
        pass
31
        self.str = raw_input("input str: ")
30
 
32
 
31
    def main_menu(self):
33
    def main_menu(self):
32
 
34
 
33
        done = False
35
        done = False
34
 
36
 
Line 40... Line 42...
40
            print
42
            print
41
            s = raw_input('Choice >>> ')
43
            s = raw_input('Choice >>> ')
42
            print
44
            print
43
 
45
 
44
            if s == '1':
46
            if s == '1':
-
 
47
                self.__clear()
45
                self.__input_test_str()
48
                self.__input_test_str()
46
                self.__test_str()
49
                self.__test_str()
47
            elif s == '2':
50
            elif s == '2':
48
                done = True
51
                done = True
49
            else:
52
            else:
50
                print 'Bad Selection'
53
                print 'Bad Selection'
51
                print
54
                print
52
 
55
 
-
 
56
    def __test_str(self):
-
 
57
        print 'Parsing: %s' % (self.procE() and 'Completed' or 'Failed', )
-
 
58
 
-
 
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
53
 
85
 
54
if __name__ == '__main__':
86
if __name__ == '__main__':
55
    print 'Not complete yet, don\'t bother'
87
    rdp = RecursiveDescentParser()
-
 
88
    rdp.main_menu()
56
 
89