0
$\begingroup$

Today I was learning to convert post-fix expressions to in-fix expressions and vice versa. What I want to know is whether a post-fix expression can have more than one infix representations.

For example, if I consider the following in-fix expression:

A+B-D/X

I get these post-fix expressions:

ABDX/-+

and

AB+DX/-

I didn't use the stack table method for solving these. I did these conceptually and both these expressions evaluate to

A+B-D/X

post-fix expression. I even verified my result with this site and both the post-fix expressions evaluate to:

A+B-D/X
$\endgroup$

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • $\begingroup$ Are you aware of operator precedence and operator associativity? $\endgroup$ – Yuval Filmus Mar 26 at 15:35
  • $\begingroup$ Yes, I'm aware of these stuff:-) $\endgroup$ – user8718165 Mar 26 at 18:13
  • $\begingroup$ Every expression, whether infix or postfix, has a unique parse tree. The two parse trees should match for the two expressions to be considered the same. $\endgroup$ – Yuval Filmus Mar 26 at 18:48
  • $\begingroup$ Also known as syntax trees. See for example Wikipedia. This is a basic concept in context-free languages, which you might want to brush up on. $\endgroup$ – Yuval Filmus Mar 26 at 19:04
0
$\begingroup$

(A+b)-c and a+(b-c) are not the same. Not with integers (one expression might overflow while the other one doesn’t), not with floating-point numbers ( different rounding errors).

Only the second postfix expression is correct.

$\endgroup$
  • $\begingroup$ Thanks a lot but I don't get why should (a+b)-c be different from a+(b-c). I'm sure I'll get it if you could include an example of each case(int and float). $\endgroup$ – user8718165 Mar 26 at 18:31
  • $\begingroup$ Also could you please tell me why does that online conversion site not give an error on inputting ABDX/-+? Both the postfix expressions return the same infix. $\endgroup$ – user8718165 Mar 26 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.