Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question Problem: Constructing a programming language named Adder. The Adder language only has a few simple statements: quit Exit the REPL or terminate a program.

Question

Problem: Constructing a programming language named Adder.

The Adder language only has a few simple statements:

quit Exit the REPL or terminate a program.

input var Prompt for and allow the user to enter a value for the variable named var.

print val Print the value val.

var gets val variable var is assigned the value val.

var adds val variable var has the value val added to it.

Where:

  • var is always a variable name that contains only letters; and
  • val can be either:

- a variable name that contains only letters; or

- a natural number that contains only digits.

The adder REPL allows the user to enter commands interactively.

Heres the example of the way it needs to appear.

Welcome to the Adder REPL.

??? gets 1

??? input b

Enter a value for b: 2

??? c gets a

??? c adds b

??? print a

a equals 1

??? print b

b equals 2

??? print c

c equals 3

??? print z

z is undefined.

??? print 32

32

??? blerg

Syntax error.

??? 23 gets z

Syntax error.

??? quit

Goodbye.

I have got this code that seems to work but it quite complicated and lengthy, is there anyway of reducing it to seperate functions or reducing lines of code to make it simpler?

def check_result(commandList, vars): n = len(commandList) se = False if n == 1 and commandList[0] != "quit": se = True elif n == 2: if commandList[0] != "print" and commandList[0] != "input": se = True elif commandList[0] == "input" and not commandList[1].isalpha(): se = True if commandList[0] == "print" and commandList[1].isalpha() and commandList[1] not in vars.keys(): print(commandList[1], "is undefined") return False elif n == 3: if not commandList[0].isalpha(): se = True elif commandList[0] not in vars.keys() and commandList[1] != "gets": print(commandList[0], "is undefined") return False if commandList[1] != "gets" and commandList[1] != "adds": se = True if not commandList[2].isdigit(): if not commandList[2].isalpha(): se = True elif commandList[2] not in vars.keys() and commandList[1] != "gets": print(commandList[2], "is undefined") return False else: se = True if se: print("Syntax error.") return False return True def main(): vars = {} command = input("??? ") while command != "quit": commandList = command.split(" ") c = check_result(commandList, vars) if len(commandList) == 2 and c: # case of command input var if commandList[0] == "input": var = input("Enter a value for " + commandList[1] + " : ") if var.isdigit(): vars[commandList[1]] = int(var) else: vars[commandList[1]] = var elif commandList[0] == "print": if commandList[1] in vars.keys(): print(commandList[1], "equals", vars[commandList[1]]) else: print(commandList[1]) # three word commands elif len(commandList) == 3 and c: if commandList[2].isdigit(): val = int(commandList[2]) elif commandList[2] in vars.keys(): val = vars[commandList[2]] else: val = commandList[2] if commandList[1] == "gets": vars[commandList[0]] = val elif commandList[1] == "adds": vars[commandList[0]] += val command = input("??? ") if command == "quit": print("Goodbye") print("Welcome to the Adder REPL") main() 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Define the term "Leasing"

Answered: 1 week ago

Question

What do you mean by Dividend ?

Answered: 1 week ago

Question

What is database?

Answered: 1 week ago

Question

What are Mergers ?

Answered: 1 week ago