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

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 - /
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 -   * +
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
What is TimSort and how does it work? TimSort example with step by step explanation. Algorithm...
What is TimSort and how does it work? TimSort example with step by step explanation. Algorithm analysis of Timsort with python implementation, including pseudo code and T(n) time complexity analysis
1) How i measure the chromatography step by step? 2) How i measure the Spectrscopy step...
1) How i measure the chromatography step by step? 2) How i measure the Spectrscopy step by step?
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2)...
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2) Use Euclidean algorithm to find gcd(248, 198) 3) (ABCDEF)16 to binary, octal and decimal
Note This is a guideline and is not meant to show you exactly step-by-step how to...
Note This is a guideline and is not meant to show you exactly step-by-step how to do the project (although it comes close to doing that). In the “real world”, your boss is not going to show you how to do everything. She is going to ask you to do something and you are going to have to do it to the best of your ability. Objective The assignment is to estimate the weighted average cost of capital (WACC) for...
a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and...
a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and find integers s1 and t1 such that 5s1 + 99t1 = 1. [Hint: You should find that 5(20) + 99(?1) = 1] b. Solve the congruence 5x 17 (mod 99) c. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 42 (mod 99) d. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 6 (mod 9)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT