Problem with Conversion from Infix to Postfix Notation

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Notation
Click For Summary

Discussion Overview

The discussion revolves around the conversion of an infix expression to postfix notation, specifically addressing issues encountered during the conversion process. Participants explore the algorithm for conversion, the precedence of operators, and the application of the BODMAS rule in this context.

Discussion Character

  • Homework-related
  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant describes their attempt to convert the expression (4+8)*(6-5)/((3-2)*(2+2)) and identifies a problem at the character representing an opening bracket.
  • Another participant suggests a different set of rules for conversion and questions whether the asterisk should have been pushed onto the stack at a certain point.
  • A participant raises a concern about operator precedence, asserting that division should take precedence over multiplication according to the BODMAS rule, but notes that the algorithm appears to give precedence to multiplication instead.
  • Another participant references the order of operations, clarifying that multiplication and division have the same precedence and are evaluated left-to-right in the absence of additional brackets.
  • A later reply indicates that the confusion regarding BODMAS has been resolved with help from another forum, suggesting that the problem has been addressed.

Areas of Agreement / Disagreement

Participants express differing views on the precedence of operators and the correct application of the conversion algorithm. While some participants clarify the order of operations, there remains no consensus on the initial interpretation of the algorithm's steps.

Contextual Notes

There are unresolved assumptions regarding the interpretation of operator precedence and the specific steps of the conversion algorithm. The discussion reflects varying interpretations of the rules governing infix to postfix conversion.

zak100
Messages
462
Reaction score
11

Homework Statement


I am trying to convert following from infix to post fix:

(4+8)*(6-5)/((3-2)*(2+2))

I am facing problem at character 20 which is a ‘(‘ i.e. opening bracket.

Homework Equations


There is no eq but the algorihm is given below:
I got following algorithm from internet.

1. if (t is an operand) output t.

2. else if (t is a right parentheses){ POP and output tokens until a left parentheses is popped(but do not output) }

3. else { POP and output tokens until one of lower priority than t is encountered or a left parentheses is encountered. or the stack is empty. PUSH t. }

The Attempt at a Solution


I am facing problem at character 20 which is a ‘(‘ i.e. opening bracket. At character 19 i.e. at '*' i.e. (asterisk) I have following status:

Character = *

Stack = /(*

Output = 4 8 + 6 5 - * 3 2 –

At character 20 i.e. ( , as per above algorithm, I would come to step 3. so I am popping tokens & I have following status after character 20:

Character = (

Stack = /((

Output = 4 8 + 6 5 - * 3 2 – *

But the above is wrong. Some body please guide me what is my mistake?Zulfi.
 
Last edited by a moderator:
Physics news on Phys.org
Hi,
I have got a problem with precedence. According to BODMAS rule, division has precedence over multiplication but the infix to posfix conversion algorithm is working opposite to this rule & giving precedence to division over multiplication. For instance in the expression:
(4+8)*(6-5)/((3-2)*(2+2))

At character# 12 which is /, we have following situation:
Character: /
Stack: *
Output: 4 8 + 6 5 -
I have seen that many solutions show:
Stack: /
Output: 4 8 + 6 5 - *

I think this is not right because / has more precedence over *.

Some body please guide me.
Zulfi.
 
zak100 said:
According to BODMAS rule, division has precedence over multiplication but the infix to posfix conversion algorithm is working opposite to this rule & giving precedence to division over multiplication
From Wikipedia:
https://en.wikipedia.org/wiki/Order_of_operations
The order of operations used throughout mathematics, science, technology and many computer programming languages is expressed here:[3]

exponents[1] and roots
multiplication[1] and division[1]
addition[1] and subtraction[1]
I'm no expert on the Order of Precedence, but multiplication and division have the same precedence, I believe. So in your equation, without more brackets in the equation, the usual factor that decides whether * or / is done first is left-to-right. So in your equation, the * would be applied after seeing the / character and before the terms to the right of the / character are parsed.
 
Hi,
Thanks for removing my confusion. I got the same answer from another forum & they removed my BODMAS confusion also.
https://forum.allaboutcircuits.com/threads/problem-with-conversion-from-infix-to-postfix-notation.132914/

Thanks all for providing me help and related algorithm. This problem is solved now.

Zulfi.
 
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 1 ·
Replies
1
Views
40K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
9
Views
2K