-1

If yes then why is it so? Isnt right associativity valid for postfix expression?

Deleted User
  • 2,551
  • 1
  • 11
  • 18
user3202188
  • 131
  • 1
  • 7
  • 1
    Poorly chosen duplicate with incorrect answer. Cannot see any reason to downvote or delete this question. – user207421 Feb 11 '20 at 00:38

1 Answers1

7

Associativity is irrelevant for postfix expressions.

Compare infix x^y^z (right-associative):

x y z ^ ^

with infix x + y + z (left-associative):

x y + z +

One of the benefits of postfix (and prefix) notation is that it eliminates the ambiguities raised by infix notation that require associativity rules to resolve.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • How are the ambiguities eliminated in case of postfix or prefix? – user3202188 Jan 28 '14 at 14:10
  • There are none. `(x+y)+z` becomes postfix `x y + z +`, while `x+(y+z)` becomes postfix `x y z + +`. An intuitive explanation is that while infix operators float between two arguments, pre- and post-fix operators are "anchored" at the end of their arguments. – chepner Jan 28 '14 at 14:16
  • @user3202188 Ambiguities such as negating a number? It is resolved by using another token to represent the otherwise ambiguous notation. For example, instead of `3 -` which is an invalid postfix expression because `-` is for subtraction, you might use `n`, or you might pop `3`, push `0`, and push `-`, which will result in `0 3 -`. The latter is used when no notation for negating a number exists. –  Jan 28 '14 at 14:20
  • While conversion of infix to post fix using stack, do we have to use left associativity always or even right associativity is valid in the case of operators with equal precedence? – user3202188 Jan 28 '14 at 14:25
  • Using the same character `-` for both subtraction and unary negation is a different type of ambiguity, unrelated to associativity. Unary negation `-x` (two tokens, `-` and `3`) should also be distinguished from `-3` (one token). – chepner Jan 28 '14 at 14:25
  • @user3202188 Associativity is a property of individual operators. Some (`+`, `-`, `*`, `/`) are left-associative. Others (exponents, whether `^` or `**`, and... function application?) are right associative. You need to use both as appropriate for parsing an arbitrary expression. – chepner Jan 28 '14 at 14:28