Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the same assumptions about the AST and the generated code as for Question 1, including the fact that an expression's operands can be evaluated

image text in transcribed
Use the same assumptions about the AST and the generated code as for Question 1, including the fact that an expression's operands can be evaluated either left-to-right or right-to-left. Also assume that all operators arc non-commutative and non-associative (i.e., the expression a op b is not equivalent to the expression b op a, and the expression (a op b) op c is not equivalent to the expression a op (b op c)). You are to give a recursive algorithm that works as follows: The input to the algorithm is an expression node in the AST (recall from Question 1 that we are assuming that we only have expressions involving non short-circuited binary operators). The output of the algorithm is the number of registers required to generated code for the whole expression. Note that generating code to evaluate a leaf node requires one register (into which the value of the identifier or literal is loaded).For non-leaf nodes, your algorithm should recursively calculate the number of registers required to generate code for the left operand (leaving the value in a register) and the number of registers required to generate code for the right operand (leaving the value in a register) and then determine the number of registers required for the entire expression represented by the node. In other words, complete the following method: For example, for the expression a - b, the input to the algorithm is the squareroot node of the expression tree. The algorithm will recursively determine that the number of registers required to generate code for each operand is 1 (because each operand is a leaf and thus requires one register). The output of the algorithm for this example should be 2 (because the whole expression can be evaluated using two registers, as illustrated above in Part 1. but it cannot be evaluated using just one register). Use the same assumptions about the AST and the generated code as for Question 1, including the fact that an expression's operands can be evaluated either left-to-right or right-to-left. Also assume that all operators arc non-commutative and non-associative (i.e., the expression a op b is not equivalent to the expression b op a, and the expression (a op b) op c is not equivalent to the expression a op (b op c)). You are to give a recursive algorithm that works as follows: The input to the algorithm is an expression node in the AST (recall from Question 1 that we are assuming that we only have expressions involving non short-circuited binary operators). The output of the algorithm is the number of registers required to generated code for the whole expression. Note that generating code to evaluate a leaf node requires one register (into which the value of the identifier or literal is loaded).For non-leaf nodes, your algorithm should recursively calculate the number of registers required to generate code for the left operand (leaving the value in a register) and the number of registers required to generate code for the right operand (leaving the value in a register) and then determine the number of registers required for the entire expression represented by the node. In other words, complete the following method: For example, for the expression a - b, the input to the algorithm is the squareroot node of the expression tree. The algorithm will recursively determine that the number of registers required to generate code for each operand is 1 (because each operand is a leaf and thus requires one register). The output of the algorithm for this example should be 2 (because the whole expression can be evaluated using two registers, as illustrated above in Part 1. but it cannot be evaluated using just one register)

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

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

c. Will leaders rotate periodically?

Answered: 1 week ago