Question

briefly describe the application of stack in Infix, postfix, and prefix expressions and evaluations briefly describe...

briefly describe the application of stack in Infix, postfix, and prefix expressions and evaluations

briefly describe the application of expression trees, Huffman trees

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Stack is simple architecture. It is very powerfull to check whether the parantheses
are balanced and to evaluate the arithmetic expressions.

Compiler task is to evaluate the arithmetic expressions. Stack is very much usefull for this
type of evaluation. There are three 3 types of algebric expression:
1. Infix
2. Postfix
3. Prefix

Infix :

Every binary operators comes inbetwwen the operands.
Eg : A + B

A, B - Operands
+ - operator

Here we can use 2 Stacks.

Operand stack: This stack will be used to keep track of numbers.
Operator stack: This stack will be used to keep operations (+, -, *, /, ^)


1.    If the character is an operand, push it to the operand stack.
2.   If the character is an operator,
3.   If the operator stack is empty then push it to the operator stack.
   Else If the operator stack is not empty,
4.   If the character’s precedence is greater than or equal to the precedence
   of the stack top of the operator stack, then push the character to the operator stack.
5.   If the character’s precedence is less than the precedence of the stack top
   of the operator stack then do Process (as explained above) until character’s precedence
   is less or stack is not empty.
6.   If the character is “(“, then push it onto the operator stack.
7.   If the character is “)”, then do Process (as explained above) until the
   corresponding “(” is encountered in operator stack. Now just pop out the “(“.

Once the expression iteration is completed and the operator stack is not empty, do Process
until the operator stack is empty. The values left in the operand stack is our final result.

Post Fix:
Operators appears after the operands

Eg : 315 + 2 - +

Output : 7

Evaluation :

1) Scan ‘3’, it’s a number, so push it to stack. Stack contains ‘3’
2) Scan ‘1’, again a number, push it to stack, stack now contains ‘3 1’ (from bottom to top)
3) Scan ‘5’, again a number, push it to stack, stack now contains ‘3 1 5’
4) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 1+5 which results in 6. We push the result ‘6’ to stack. Stack now becomes ‘3 6’.
5) Scan ‘2’, it’s a number, we push it to the stack. Stack now becomes ‘3 6 2’.
6) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 6 – 2 which results in 4. We push the result ‘4’ to stack. Stack now becomes ‘3 4’.
7) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3+4 which results in 7. We push the result ‘7’ to stack. Stack now becomes ‘7’.
8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack).


Prefix :
Operators appears before the operands


EVALUATE_PREFIX(STRING)
1: Put a pointer variable Pi at the end of the end
2: If character at Pi is an operand push it to Stack
3: If the character at Pi is an operator pop two
elements from the Stack. Operate on these elements
according to the operator, and push the result
back to the Stack
4: Decrement Pi by 1 and go to Step 2 as long as there
are characters left to be scanned in the expression.
5: The Result is stored at the top of the Stack,
return it
6: End

Add a comment
Know the answer?
Add Answer to:
briefly describe the application of stack in Infix, postfix, and prefix expressions and evaluations briefly describe...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT