could simply run a // in our evaluation logic to retain prior behavior (or allow for
both / and // in our grammar), but we’ll follow Python 3.X’s lead here.
Example 19-15. PP4E\Lang\Parser\testparser.py
"""
############################################################################
parser test code
############################################################################
"""
def test(ParserClass, msg):
print(msg, ParserClass)
x = ParserClass('4 / 2 + 3') # allow different Parsers
x.parse()
x.parse('3 + 4 / 2') # like eval('3 + 4 / 2')...
x.parse('(3 + 4) / 2') # 3.X: / is now true div
x.parse('4 / (2 + 3)') # // is not supported (yet)
x.parse('4.0 / (2 + 3)')
x.parse('4 / (2.0 + 3)')
x.parse('4.0 / 2 3')
x.parse('(4.0 / 2) 3')
x.parse('4.0 / (2 * 3)')
x.parse('(((3))) + 1')
y = ParserClass()
y.parse('set a 4 / 2 + 1')
y.parse('a * 3')
y.parse('set b 12 / a')
y.parse('b')
z = ParserClass()
z.parse('set a 99')
z.parse('set a a + 1')
z.parse('a')
z = ParserClass()
z.parse('pi')
z.parse('2 * pi')
z.parse('1.234 + 2.1')
def interact(ParserClass): # command-line entry
print(ParserClass)
x = ParserClass()
while True:
cmd = input('Enter=> ')
if cmd == 'stop':
break
x.parse(cmd)
Correlate the following results to print call statements in the self-test module:
C:\...\PP4E\Lang\Parser> python parser1.py
parser1 <class '__main__.Parser'>
1446 | Chapter 19: Text and Language