Question

In: Computer Science

Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) *...

Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) * (7 − 2) into a postfix expression, and then how a second stack-based algorithm will compute the value of this postfix expression.

Solutions

Expert Solution

Answer:

Token #1 Evaluation

Infix: (1+2)*(7-2)

Since ( is an opening parenthesis, push it to the top of the stack.

Postfix:

Stack:

(

Token #2 Evaluation

Infix: (1+2)*(7-2)

Since 1 is an operand, append it to the postfix expression.

Postfix: 1

Stack:

(

Token #3 Evaluation

Infix: (1+2)*(7-2)

Since + is an operator and has greater precedence than the ( on the top of the stack, push + to the top of the stack.

Postfix: 1

Stack:

+
(

Token #4 Evaluation

Infix: (1+2)*(7-2)

Since 2 is an operand, append it to the postfix expression.

Postfix: 1 2

Stack:

+
(

Token #5 Evaluation

Infix: (1+2)*(7-2)

Since the ) is a closing parenthesis, pop each operator from the stack one at a time and append to the postfix expression. Keep popping from the stack until an opening parenthesis is encountered.

Pop + from the top of the stack and append to the postfix expression.

Postfix: 1 2 +

Stack:

(

Since we are done with the expression inside the current parenthesis, pop the opening parenthesis from the top of the stack and discard.

Postfix: 1 2 +

Stack:

Token #6 Evaluation

Infix: (1+2)*(7-2)

Since the * is an operator and the stack is empty, push the * to the stack.

Postfix: 1 2 +

Stack:

*

Token #7 Evaluation

Infix: (1+2)*(7-2)

Since ( is an opening parenthesis, push it to the top of the stack.

Postfix: 1 2 +

Stack:

(
*

Token #8 Evaluation

Infix: (1+2)*(7-2)

Since 7 is an operand, append it to the postfix expression.

Postfix: 1 2 + 7

Stack:

(
*

Token #9 Evaluation

Infix: (1+2)*(7-2)

Since - is an operator and has greater precedence than the ( on the top of the stack, push - to the top of the stack.

Postfix: 1 2 + 7

Stack:

-
(
*

Token #10 Evaluation

Infix: (1+2)*(7-2)

Since 2 is an operand, append it to the postfix expression.

Postfix: 1 2 + 7 2

Stack:

-
(
*

Token #11 Evaluation

Infix: (1+2)*(7-2)

Since the ) is a closing parenthesis, pop each operator from the stack one at a time and append to the postfix expression. Keep popping from the stack until an opening parenthesis is encountered.

Pop - from the top of the stack and append to the postfix expression.

Postfix: 1 2 + 7 2 -

Stack:

(
*

Since we are done with the expression inside the current parenthesis, pop the opening parenthesis from the top of the stack and discard.

Postfix: 1 2 + 7 2 -

Stack:

*

All 11 Token Evaluations Completed

Since we are done processing the infix expression, pop the remaining operator from top of the stack and append it to the postfix expression.

Pop * from the stack and append it to the postfix expression.

Postfix: 1 2 + 7 2 - *

Stack:

Final Conversion:

Infix: (1+2)*(7-2)
to
Postfix: 1 2 + 7 2 - *

//if you have any doubt please comment ....


Related Solutions

Show the step by step multiplication using Booth’s Algorithm 1. -8 * 2
Show the step by step multiplication using Booth’s Algorithm 1. -8 * 2
Show step-by-step how Java will evaluate the following expression: 8 % 3 + 3 % 2...
Show step-by-step how Java will evaluate the following expression: 8 % 3 + 3 % 2 – 9 % 2.0 * 5 15/15 % 7 *2   – 11. /4 * 5
Illustrate step-by-step the stack algorithm to convert this to postfix. a + b * (d *...
Illustrate step-by-step the stack algorithm to convert this to postfix. a + b * (d * e + f)
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
Show how to obtain equation (4) from equations (1), (2), and (3). Please show each step....
Show how to obtain equation (4) from equations (1), (2), and (3). Please show each step. (1) Tr=Iα (2)α=a/r (3) mg-T-f=ma (4) I=mr2[(g/a)-1-(f/ma)]
1 Draw a stack diagram to show how the following code is executed and write the...
1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10)) 2 Draw a stack diagram to show how the following code is executed...
Starting from a symbolic expression of the Second Law of Thermodynamics, show how arguments based purely...
Starting from a symbolic expression of the Second Law of Thermodynamics, show how arguments based purely upon considerations of entropy lead to the formalism which we call Gibbs
(show step by step calculation and formula, no excel or fina calculator) Based on the below...
(show step by step calculation and formula, no excel or fina calculator) Based on the below cash flows, HappyBear Ltd. uses the NPV decision rule. At a required return of 12 percent, should the firm accept this project? What if the required return is 30 percent? Year Cash Flow 0 -$32,000 1 16,000 2 20,000 3 17,000
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT