Question
A while language is a very simple imperative programming language with 5 statements Assignconsists of a variable and an expressionWhileconsists of an expression
A "while language" is a very simple imperative programming language with 5 statements
Assignconsists of a variable and an expressionWhileconsists of an expression and a statementIfconsists of an expression and two statementsCompoudconsists of a sequence of statementsOutputconsists of a variable
You are required to code a class hierarchy in some OO language of your choice that defines the abstract syntax of while programs.
For example, you will have:
abstract class Statement { ... some methods ... }
class Assign is_subclass_of Statement { constructor Assign(Variable x, Expression y) { ... } ... some methods ... }
Expressions are very similar to those of a previous homework, but may contain variables and relational operators, e.g., < or >. Of course, there must be a symbol table containing the binding of the variables.
You must code two methods in your classes: one for pretty printing the statements and one for executing (interpreting) the statements. The execution of the Output statement prints the name and the value of its argument variable.
Below is a plausible (no specific language) abstract syntax representation of a program that computes the factorial of 5.
factorial = Compound([ Assign(Var("n"),Val(5)), Assign(Var("r"),Val(1)), While(Bin('>',Var("n"),Val(0)), Compound([ Assign(Var("r"),Bin('*',Var("r"),Var("n"))), Assign(Var("n"),Bin('-',Var("n"),Val(1))), ])), Output(Var("r")) ]);
Pretty printing the above program should produce something like the printout below. Beside the indentation, spaces and parentheses are flexible.
begin n := 5 r := 1 while (n > 0) do begin r := (r * n) n := (n - 1) end output r end
Executing (interpreting) the above program should produce
r = 120
Finally, you are required to code a simple (comparable to factorial) program of your choice, and pretty print and execute it. Turn in your code and a trace of execution.
Hints: This is a relatively simple exercise. I recommend that you code expressions, statements and programs each one in its own file. The first two should be about one page long each. For your own benefit, you should code unit test before coding the program and test your code incrementally as you add classes and methods.
(***Question from previous homework***
Consider an arithmetic expression (called "of interest") constructed by four occurrences of the number 10 with the binary operators +, -, * and /. [ e.g.: (10+10)/(10+10) or (10-10+10) * 10 etc] . The value of the first xpression is 1.
Code a program that determines which digits are the values of one such expression. Your program should execute the following intermediate steps: (1) construct all the expressions of interest and (2) print any expression whose value is between 0 and 9.
)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started