The Fortran I compiler would expand each operator with a sequence of parentheses. In a simplified form of the algorithm, it would
replace + and – with ))+(( and ))-((, respectively;
replace * and / with )*( and )/(, respectively;
add (( at the beginning of each expression and after each left parenthesis in the original expression; and
add )) at the end of the expression and before each right parenthesis in the original expression.
Although not obvious, the algorithm was correct, and, in the words of Knuth, “The resulting formula is properly parenthesized, believe it or not.”
Code4Food
2020-5-17 08:46:24
Yes, you can generalize it to + - * / and ^.
+ -> )))+((( ditto for -
* -> ))*((
^ -> )^(
enclose the whole expression with ((( and )))
If you have n different precedence level, use one level of () for the operators with the highest precedence and add one () for each level going down.