Jason (jcreed) wrote,
Jason
jcreed

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.
Tags: parsing
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 10 comments