Subversion Repositories programming

Rev

Rev 161 | Rev 163 | 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, False
except NameError:
  (True, False) = (1, 0)

import sys
    
class RecursiveDescentParser:
    def __init__(self):
        self.__clear()

    def __clear(self):
        self.str = ""   # the string of input to test
        self.strpos = 0 # the current position in str

    def __input_test_str(self):
        self.str = raw_input("input str: ")

    def main_menu(self):

        done = False

        while not done:
            print 'Menu:'
            print '========================================'
            print '1. Test a string'
            print '2. Quit'
            print
            s = raw_input('Choice >>> ')
            print

            if s == '1':
                self.__clear()
                self.__input_test_str()
                self.__test_str()
            elif s == '2':
                done = True
            else:
                print 'Bad Selection'
                print

    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 procT(self):
        print 'procT(%s) ->' % (self.str[self.strpos:])
        
        if self.str[self.strpos] == '0':
            self.strpos += 1
            return True

        return False

    def procEprm(self):
        # Check empty str
        if self.strpos >= len(self.str):
            return True

        print 'procEprm(%s) ->' % (self.str[self.strpos:])

        if self.str[self.strpos] == '+' or self.str[self.strpos] == '-':
            self.strpos += 1
            return self.procT() and self.procEprm()

        return False

if __name__ == '__main__':
    rdp = RecursiveDescentParser()
    rdp.main_menu()