Rev 162 | Rev 164 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env python# Copyright: Ira W. Snyder# Start Date: 2005-11-18# End Date:# License: Public Domain## Changelog Follows:## 2005-11-18# * Just getting the basics in place, since we haven't been given# the whole description of the project yet.## Check for <Python-2.3 compatibility (boolean values)try:True, Falseexcept NameError:(True, False) = (1, 0)import sysclass RecursiveDescentParser:def __init__(self):self.__clear()def __clear(self):self.str = "" # the string of input to testself.strpos = 0 # the current position in strdef __input_test_str(self):self.str = raw_input("input str: ")def main_menu(self):done = Falsewhile not done:print 'Menu:'print '========================================'print '1. Test a string'print '2. Quit's = raw_input('Choice >>> ')if s == '1':self.__clear()self.__input_test_str()self.__test_str()elif s == '2':done = Trueelse:print 'Bad Selection'def __test_str(self):print 'Parsing: %s' % (self.procE() and 'Completed' or 'Failed', )def procE(self):print 'procE(%s) ->' % (self.str[self.strpos:])return self.procT() and self.procEprm()def procEprm(self):# Check empty strif self.strpos >= len(self.str):return Trueprint 'procEprm(%s) ->' % (self.str[self.strpos:])# Check +if self.str[self.strpos] == '+':self.strpos += 1return self.procT() and self.procEprm()# Check -if self.str[self.strpos] == '-':self.strpos += 1return self.procT() and self.procEprm()# We accept empty str, so take itreturn Truedef procT(self):print 'procT(%s) ->' % (self.str[self.strpos:])return self.procF() and self.procTprm()def procTprm(self):# Check empty strif self.strpos >= len(self.str):return Trueprint 'procTprm(%s) ->' % (self.str[self.strpos:])# Check *if self.str[self.strpos] == '*':self.strpos += 1return self.procF() and self.procTprm()# we accept empty str, so take itreturn Truedef procF(self):print 'procF(%s) ->' % (self.str[self.strpos:])# Check xif self.str[self.strpos] == 'x':self.strpos += 1return self.procV()# Check (E)if self.str[self.strpos] == '(':self.strpos += 1if self.procE():if self.str[self.strpos] == ')':return Truereturn False# Must be a numberreturn self.procN()def procN(self):print 'procN(%s) ->' % (self.str[self.strpos:])if self.str[self.strpos] in '0123456789':self.strpos += 1return Truereturn Falsedef procV(self):print 'procV(%s) ->' % (self.str[self.strpos:])return self.procN()if __name__ == '__main__':rdp = RecursiveDescentParser()rdp.main_menu()