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$
  • $\begingroup$ Are you aware of operator precedence and operator associativity? $\endgroup$ – Yuval Filmus Mar 26 '19 at 15:35
  • $\begingroup$ Yes, I'm aware of these stuff:-) $\endgroup$ – user8718165 Mar 26 '19 at 18:13
  • 1
    $\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 '19 at 18:48
  • 1
    $\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 '19 at 19:04
  • $\begingroup$ @YuvalFilmus Thank you very much for telling me. I finally fully understand it :) $\endgroup$ – user8718165 Oct 29 '19 at 10:30
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$

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.