# Shunting-yard algorithm and unary minus

I am attempting to implement a shunting-yard algorithm for a calculator following the rules laid out in https://en.wikipedia.org/wiki/Shunting-yard_algorithm .

When programming the unary minus, however, I do not know which precedence method to follow due to the ideas raised in following question https://math.stackexchange.com/questions/1299236/why-does-unary-minus-operator-sometimes-take-precedence-over-exponentiation-and

As this is a key component of a functioning calculator, which precedence is the 'industry standard' and is there an algorithm which would perform both of these precedence methods at the same time to solve this problem?

Bonus points if the algorithm (if it exists) retains the O(n) linear time increase.

• If I understand what you are saying, you want to parse the expression first according to a grammar that has unary - lower precedence than binary ^, and second, also parse the expression according to a grammar where unary - is higher precedence than binary ^, and still be O(n). Well, that is easy, you just parse it twice because O(2n) = O(n). – Erik Eidt Sep 4 '16 at 0:30

In most languages which have an exponentiation operator like PHP and Python and others, according to this source, the exp operator is defined to have a higher precedence than unary operators. The article mentions also a few exceptions like shell programming in Bash. The same source tells us the upcoming Javascript/ECMAScript version 2016 (ES7) will introduce the `**` operator in Javascript, but ambigouus usage will be forbidden.