Take a look at the algorithm at the bottom of this wikipedia page, ostensibly used in early Fortran compilers. In order to resolve all conflicts between binary operators of different precedence, it inserts a certain number of exactly-backwards-balanced parentheses around every binary operator, (like mapping "+" to ")))+(((") the fewer parentheses the higher precedence the operator is.
While I wouldn't call this elegant since it relies on mashing together ill-formed textual fragments of programs and doesn't obviously extend well to custom fixity declarations, it is adorable.
While I wouldn't call this elegant since it relies on mashing together ill-formed textual fragments of programs and doesn't obviously extend well to custom fixity declarations, it is adorable.