Question: # this stores variables # and their values variables = {} # validators def isVar(v): # can only be made of letters return v.isalpha() def

# this stores variables # and their values variables = {} # validators def isVar(v): # can only be made of letters return v.isalpha() def isVal(v): # can be a var or only be made of digits return isVar(v) or v.isnumeric() # command handlers def handleInput(var): # input handler # validating var if not isVar(var): return handleSyntaxError() # reading and validating val val = input(f'Enter a value for {var}: ') if not isVal(val): return handleSyntaxError() # parsing val if isVar(val): # must be an exising variable if val not in variables: return handleSyntaxError() else: val = variables[val] else: val = int(val) # assigning val to var variables[var] = val def handlePrint(val): # print handler # validating val if not isVal(val): return handleSyntaxError() # parsing val if isVar(val): if val not in variables: val = f'{val} is undefined.' else: val = f'{val} equals {variables[val]}' else: val = int(val) # printing val print(val) def handleGets(left, right): # gets handler # validating left if not isVar(left): return handleSyntaxError() # validating right if not isVal(right): return handleSyntaxError() # parsing right if isVar(right): if right not in variables: return handleSyntaxError() else: right = variables[right] else: right = int(right) # assigning right to left variables[left] = right def handleAdds(left, right): # adds handler # validating left if not isVar(left): return handleSyntaxError() # validating right if not isVal(right): return handleSyntaxError() # parsing right if isVar(right): if right not in variables: return handleSyntaxError() else: right = variables[right] else: right = int(right) # left must exist in variables if left not in variables: return handleSyntaxError() # adding right to left variables[left] += right def handleSyntaxError(): # syntax error handler print('Syntax error.') # sends the commands to the handlers def execute(command): # splitting the command # into a list of tokens command = command.split() # quit handler if len(command) == 1 and command[0] == 'quit': # False so that the REPL stops return False # input handler elif len(command) == 2 and command[0] == 'input': handleInput(command[1]) # print handler elif len(command) == 2 and command[0] == 'print': handlePrint(command[1]) # gets handler elif len(command) == 3 and command[1] == 'gets': handleGets(command[0], command[2]) # adds handler elif len(command) == 3 and command[1] == 'adds': handleAdds(command[0], command[2]) # syntax error handler else: handleSyntaxError() # True so that the REPL continues return True if __name__ == '__main__': # starting the Adder REPL print('Welcome to the Adder REPL.') # main loop while True: # reading a command command = input('??? ') # executing the command # exiting if 0 has been # returned if not execute(command): break # displaying a 'Goodbye' message print('Goodbye.') 

NOTE: MODIFY THE CODE ABOVE TO MATCH THE QUESTION BELOW

# this stores variables # and their values variables = {} #

FILE: children.ad

Problem: Write an Adder interpreter, that prompts for and executes an Adder script. For example if the file children.ad contains: input sons input daughters children gets sons children adds daughters print children quit The interpreter would run like this Script name: children.ad Enter a value for sons: 3 Enter a value for daughters: 4 children equals 7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!