I'm (trying) to make my own code parser that evaluates expressions. I've done some reading about this topic and seen the various mnemonics that school children are taught, such as:
P(arentheses)
E(xponents)
M(ultiplication)
D(ivision)
A(ddition)
S(ubtraction)
B(rackets)
O(rders) / sometimes I(ndices)
D(ivision)
M(ultiplication)
A(ddition)
S(ubtraction)
In PEMDAS multiplication comes before division, and in BODMAS/BIDMAS division comes before multiplication. However I've realised this doesn't matter because multiplication and division using this convention have the same level of precedence, but it only works if an extra convention is followed, that is, that you evaluate the expressions from left to right. The same goes for subtraction and addition, in some cases if you do addition before subtraction you'll get the wrong answer. The two operations add and minus have the same precedence, however there's still the extra requirement that you evaluate from left to right. This seems to have confused many students on the Khan Academy comments section.
So to take an example:
$8 \div 4 \times 2 - 2 + 4$
If you follow PEMDAS without taking into account left to right evaluation we get the wrong answer:
Multiplication:
$ 4 \times 2 = 8$
$Division: 8 \div 8 = 1$
Result: $ 1 - 2 + 4$
Correct result: $4 - 2 + 4$
then
Addition:
$ 2 + 4 = 6 $
Subtraction:
$ 1 - 6 = -1$
Result: $-1$
Correct result: $6$
So obviously this error happened because I didn't evaluate left to right. But I was thinking, originally when I was trying to figure this out I thought about giving division a higher precedence than multiplication, multiplication a higher precedence than subtraction, and subtraction a higher precedence than addition, so the precedence order would look like:
$\div$
$\times$
$-$
$+$
This goes against what math teaches that division and multiplication have the same precedence, and addition and subtraction have the same precedence. However I noticed that if I order the operations this way, not only does the above example work correctly, but all the other examples do too. For example:
$ 12 \div 3 \times 2 + 4 \div 4 - 1 \times 10 $
If I use my own precedence table I get the result reading this forwards or backwards. And it's the same result as using PEMDAS or BODMAS with the left-to-right rule.
A simpler case:
$8 - 4 + 2 - 2$
PEMDAS and BODMAS teaches us that plus and minus have the same precedence, so we can just evaluate this from left to right. Or if we take minus operator to have a higher precedence than addition, then we can forget about both the fact that plus and minus have equal precedence and also forget the left-to-right rule and picture something like:
$(8 - 4) + (2 - 2)$
So would having my own operator precedence in the way I've shown above, ignoring the left-to-right rule, work out in all cases, or am I missing something. I'd like to hear if this slightly modified operator precedence doesn't work, because then I'll just implement a PEMDAS type function.
Also, if it does work, would arranging the operator precedence to be, from highest to lowest $\div \times - +$ and forgetting about the left-to-right rule be a valid math order of operations equivalent to PEMDAS or BODMAS? Obviously I'd maintain the parentheses and exponentiation as higher.