Thread with 3 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 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