Question: Principles of Computer Science Question 2 ( 2 2 points ) : Purpose: To build a simple interpret and test it . To get warmed
Principles of Computer Science
Question points:
Purpose: To build a simple interpret and test it To get warmed up with Python, in case you are using it for
the first time.
Degree of Difficulty: Easy. This is just for fun, and to emphasize some of the ideas of Chapter
References: You may wish to review the following chapters:
General: CMPT Readings, Chapter
File IO Review: CMPT Readings: Chapter
Restrictions: This question is homework assigned to students and will be graded. This question shall not
be distributed to any person except by the instructors of CMPT Solutions will be made available
to students registered in CMPT after the due date. There is no educational or pedagogical reason
for tutors or experts outside the CMPT instructional team to provide solutions to this question
to a student registered in the course. Students who solicit such solutions are committing an act of
Academic Misconduct, according to the University of Saskatchewan Policy on Academic Misconduct.
Note: aq asks about your work on aq
Keep track of how long you spend working on this question, including time spent reading the ques
tion, reviewing notes and the course readings, implementing the solution, testing, and preparing to
submit. Also keep track of how many times you experienced unscheduled interruptions, delays, or
distractions during your work; include things like social media, phone calls, texting, but not such
things as breaking for a meal, exercise, commuting to or from the university. In aq you will be
asked to summarize the time you spend working on aq and your answer will be more helpful to
you if you have more precise records. Maybe go and read Q before you start Q
A simple calculating language: Tho The end goal of this question is a Python program that interprets a
very simple calculating language called Tho. Dont worry, this is easy!
Your Python program will be able to open a file, read the commands that are in the file, perform the cal
culations, and will output the numerical result. The file can be considered a program or a script in the
Tho language, and your Python program that reads the Tho script will be considered a very simple Tho
interpreter.
The Tho language is very limited and thats what makes it viable for a first question on a first assignment.
Tho Register The language is not like Python. In Python, we can use as many variables as we need. The
Tho language will use exactly one register, called R whose value can be modified by the Tho commands.
At the beginning of every script, this register is initialized to be zero.
Tho commands The commands of the Tho language are not like in Python, which has assignment state
ments and expressions, and loops and functions, etc. In Tho, we only have very simple commands. Each
command is rather like an instruction in assembly language, for a very simple kind of CPU.
To describe the commands, we have to give the syntax of the commands, and then clearly describe the
behaviour or effect of the commands. In the description that follows, the command keywords will be
written in upper case, eg ASK. Commands sometimes take an argument, which depends on that the script
is trying to do For arguments, we will use italics, eg T
There are only two kinds of arguments for a Tho script. Usually, an argument will be a floating point number.
Sometimes, a Tho command will use a name as an argument. A name is simply a letter or work, similar to
names in Python.
Well start with the ASK and TELL commands, which Tho uses to interact with the user.
Page
Department of Computer Science
Thorvaldson Building
Science Place, Saskatoon, SK SN C Canada
Telephine: Facimile:
CMPT
Winter
Principles of Computer Science
Command Effect
ASK T Acquire a value from the console, and store it as the value of the name T
TELL Display the value of R on the console
The effect of the ASK T command requires a bit more detail:
Display a simple prompt to the user, who will then type in a floating point value, followed by a return.
The users value will be stored as the value for T
We can say that the ASK command creates the name, and records its value. After the ASK command, the
name can be used in other commands. Its important to understand that a name in Tho is not a variable;
the value associated with a name created by the ASK command cannot be changed. Its a named constant
that can be used elsewhere in the Tho script.
The TELL command displays the value stored in the register to the console. The syntax of this command
is simply the word TELL on a line by itself.
The Tho language will also have arithmetic commands:
Command Effect
ADD e R R e
SUB e R R e
MUL e R R times e
DIV e R Re floating point division
In this table, the e represents a simple expression. In the Tho language, a simple expression is either a
floating point literal, like or a name created by an
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
