Thread with 7 posts
jump to expanded postThe 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.β[8]
@hikari huh, yes this makes sense if you count the effect on the parens level of each of the )/( etc subexpressions. extremely cute.
@leftpaddotpy @hikari I used this trick before for Advent of Code exercises.
@hikari this is the hackiest way to do operator precedence I've ever seen wow
@hikari Holy poop, now this is something we can actually /understand!/
@hikari yeah that will work.
I have done *far* worse hacks in service of parsing. You really don't want to know what an undergraduate's first attempt (before even hearing the terms LL and (LA)LR) at an expression parser looks like, even if it works.
@phenidone I'm writing a terrible expression parser right now⦠I really ought to learn proper parsing algorithms but I tend to prefer an okay application of things I already understand over learning a new approach