Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must be in C++. Write an interpreter to parse a command online and immediately p

ID: 3805635 • Letter: M

Question

Must be in C++. Write an interpreter to parse a command online and immediately produce the result on the screen. The command is either a set command or an arithmetic expression. The set command allows a value to be assigned to a variable x, y, or z. Initially, the values of x, y, and z are set to 0. An arithmetic expression will contain four operators such as +, -, *, and / operating on the variables x, y, and z. During the course of the computation, you are allowed to change the values of x, y, and z using the set command. An arithmetic expression may contain parentheses to specify the priorities of the arithmetic terms in the expression. For example, in the y + (x * z) expression, x and z need to be multiplied first and the sum is then added to y. Since you are writing a command interpreter, whenever an arithmetic expression is entered your interpreter should be able to parse the command, compute the value of the arithmetic expression, and display the value on the screen.

Sorry for posting such a long question, but I'm really not sure where to start with this and it's very important for the class.

Explanation / Answer

I have written a composite tree that has a number of different nodes. The most important of these is a root node which sits at the root of the tree and has all other nodes underneath. The reason for writing this root node concerns a particular detail of writing parsers that convert input into composite trees. Often, when you are writing a parser that generates a composite tree, you tend to create the tree starting with the leaf nodes, rather than with the root node. I tend to solve this particular problem by growing the tree below a customized root node. As each node in the tree is created, it is added to the root node. Initially, leaf nodes are added, but as composite nodes are added, they detach certain leaf nodes from underneath the root node, and add them to their own children lists. This grows the tree from the root, gradually pushing other nodes further down the hierarchy. To those of you familiar with the process of building a balanced binary tree, this process is remarkably similar. The diagram below demonstrates the process:

we can see that the first two steps involve adding numeric leaf nodes to the root node. The next step involves adding a plus operator which takes charge of the two children and attaches itself under the root node. The root node therefore contains a function to do this operation:

You can see that this function looks for the last two nodes under the root node, removes them from the root node, adding them to the new node, before finally adding the new node underneath the root.

The other nodes in the composite tree represent a variety of basic entities in the expression syntax, for example, integer numbers, and the various operators such as plus or minus.

I have written two visitors for the composite tree. The first is a left to right visitor that will print the expression represented by the tree. The second is a bottom to top visitor that evaluates the expression represented by the tree.