Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java full code Just use any algebra equation like 3*4-5 and complete the code just take any equation and give me the code Introduction We

image text in transcribed

image text in transcribed

image text in transcribed

java full code

Just use any algebra equation like 3*4-5 and complete the code

just take any equation and give me the code

Introduction We will work on the interpreter itself now. We will build the interpreter class and implement methods to process each AST node. When we think about a function, there are a few steps that happen. For now, we will ignore function parameters. 1) Create the local variables. 2) Interpret the statements. 3) Free the local variables. We will break this into two Java functions: interpretFunction() will handle step 1 and interpretBlock() will handle step 2. Java will give us stp 3 for free because we will make the variable structure local to interpretFunction(); when interpretFunction() ends, the variables will disappear. Once we have variables, we can reasonably process the other statements. The "magic" is in splitting interpretBlock() into a separate function; this makes all of the control structures pretty easy to implement. Details Add interpretFunction(FunctionNode) as a method to a new Interpreter class. Create a hash map (name IDT) for local variables. Loop over the constants and local variables and create new IDTs and add them to the hash map. Finally, pass the hash map and the collection of StatementNode to interpretBlock(). InterpretBlock should loop over the StatementNode collection. For each node type (use instanceOf) call a method for that node type (IfNode, VariableReferenceNode,MathOpNode, etc.) Those methods will varv in return type and parameters. InterpretBlock is a little bit of recursive magic-you will be in the Details Add interpretFunction(FunctionNode) as a method to a new Interpreter class. Create a hash map (name IDT) for local variables. Loop over the constants and local variables and create new IDTs and add them to the hash map. Finally, pass the hash map and the collection of StatementNode to interpretBlock(). InterpretBlock should loop over the StatementNode collection. For each node type (use instanceOf) call a method for that node type (IfNode, VariableReferenceNode,MathOpNode, etc.) Those methods will vary in return type and parameters. InterpretBlock is a little bit of recursive magic - you will be in the middle of this and call it again for loops or conditionals of any kind. Start with the easiest nodes first. Consider VariableReferenceNode. It has "name". Look that name up in the hash map we just created. If it is not there, throw an exception (because someone used a variable that doesn't exist). If it is there, you have an IDT. Consider MathOpNode. It has a left and a right side. We don't know what the left and the right side are. Make a function (call it expression) that looks at the node types that could be there (MathOpNode, VariableReferenceNode, IntegerNode, FloatNode, etc.) and returns a Node which will be one of three: IntegerNode, RealNode or StringNode (because we can add two strings). If we call expression() on left and right, we now have a limited set of possibilities. Both sides have to be the same type. If they are, add, subtract, multiply, divide, modulo the values and return a new IDT with the result. Now consider BooleanCompareNode. Both sides of that are also expression(), so we can reuse that, then compare and return true or false. With BooleanCompareNode processed, we can now think about if, while, repeat. Those have a boolean compare and a collection of statements. Evaluate the boolean compare, then call InterpretBlock() if the boolean compare succeeds. If has a special requirement - if it evaluates to false, follow the linked list, trying each linked ifNode until the compare is false or the list ends. ForNode and AssignmentNode are pretty straightforward, once you have evaluate() completed. At the end of this process, there is one more StatementNode that we can't process - FunctionCallNode. That is the most complex, so we will leave it for last

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago