Note:- I'm not talking about Increment & Decrement Operator. I'm talking about prefix & postfix expressions.

Example in python:-

def calculate(a, b, c, d):
    return a * ( b + c ) / d 

calculate(1,2,3,4)

I know that python doesn't support prefix or postfix expressions. But imagine if python supports postfix expression, I can write the function like this

def calculate(a, b, c, d):
    return a b c + * d /

calculate(1,2,3,4)

I read in this link that evaluating the postfix expressions are faster than infix expressions

  1. The time required for evaluating an infix expression is O(n^2) -> reason - Need to find the operators having the highest precedence.
  2. The time required for evaluation of postfix expression is O(n) -> reason - No need to find the precedence of operators. We just need to evaluate from left to right.

If this is the case then why don't popular programming languages like C, C++, Java, Python, Ruby, or PHP support postfix expressions natively?

1

There are 1 answers

2
John Kugelman On BEST ANSWER

I read in this link that evaluating the postfix expressions are faster than infix expressions. The time required for evaluating an infix expression is O(n^2) -> reason - Need to find the operators having the highest precedence.

That answer is wrong. Its purported description of how compilers parse expressions is nonsense. But heck, even if it were right, the n here is miniscule. Typical expressions have, what, 1-5 operators? That's child's play even for a quadratic algorithm.

Also, languages aren't designed to squeak out microperformance gains from their parsers. They're designed to help humans express their ideas with maximum readability, concision, expressivity, or whatever other goals each language has. I can't even imagine someone writing a parser so slow that speedups would actually be noticeable.

If this is the case then why don't popular programming languages like C, C++, Java, Python, Ruby, or PHP support postfix expressions natively?

People are familiar with infix notation. Since the performance argument is mistaken, there is no compelling reason for a popular language to use prefix or postfix notation. Using either of those pretty much guarantees a language won't be mainstream (sorry, Lisp fans!).