title: Fixing an infix parser
body: <p>I have a mathematical expression parser that should handle <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>, <code>(-)</code>, functions, and of course atoms (such as <code>x</code>, <code>1</code>, <code>pi</code>, etc.).  The parser is basically designed according to <a href="https://en.wikipedia.org/wiki/Operator-precedence_parser" rel="nofollow">Wikipedia's operator precedence parser</a>, which I've reproduced below; <code>parse_primary()</code> is defined elsewhere.</p>
  
  <pre><code>parse_expression ()
      return parse_expression_1 (parse_primary (), 0)
  
  parse_expression_1 (lhs, min_precedence)
      while the next token is a binary operator whose precedence is &gt;= min_precedence
          op := next token
          rhs := parse_primary ()
          while the next token is a binary operator whose precedence is greater
                   than op's, or a right-associative operator
                   whose precedence is equal to op's
              lookahead := next token
              rhs := parse_expression_1 (rhs, lookahead's precedence)
          lhs := the result of applying op with operands lhs and rhs
      return lhs
  </code></pre>
  
  <p>How can I modify this parser to handle <code>(-)</code> correctly?  Better yet, how can I implement a parser with support for all infix and postfix operators (<code>!</code> for instance) that I might want?  Finally, how should functions be treated?</p>
  
  <p>I should note that <code>(-)</code> negation is distinguished in the lexer from <code>-</code> "subtraction", so it can be treated as a different token.</p>