Pratt Parsing For Algebraic Expressions

Parsing algebraic expressions is always a pain. If you need to compute, say, 2+4*2, the answer should be the same as (2 + (4 *2)), not ((2 + 4) * 2) — in other words, the right answer is 10, not 12. The classic way to do this is to use two stacks and a table of precedences for the operators. However, [Martin Janiczek] prefers to use Pratt Parsers and wants to show you how they work.

The parser is named after [Vaughn Pratt]. The algorithm works with a table of precedence where operators with higher precedence have higher numbers. It then builds a left and right portion of a string, using recursion. So if you consider 2+4*2, you wind up, on the first pass, with (2+ parse(4*2)). The second parse returns a full expression to produce: (2+(4*2)).

If that was too fast, read the post which has a nice flowchart and an example step-by-step parse of 1+2-3*4+5/6^7-8*9.  Towards the bottom, there’s a nice animated flow chart that you can step through, almost like a debugger.

There are a few details left for the end. For example, there is a way to allow right-associative operators (e.g., 2^3^4 is actually ((2^(3^4)). You can also make some easy modifications to get things like unary negation and parenthesis.

Of course, there are other ways to go. You could stick with RPN. Or use the traditional method.

10 thoughts on “Pratt Parsing For Algebraic Expressions

  1. Even simpler to understand is to make a set of different functions, each responsible for different level. So you start with ‘parse_expr()’ which calls ‘parse_term()’, which calls ‘parse_factor()’. The parse_term() function will only accept expressions with ‘*’ and ‘/’ operators, but will return its result when it sees a ‘+’ or ‘-‘. For every precedence level you have a new function.

    1. It certainly is a type of recursive descent, although the fact that it splits out differently based on operator priority is the key difference. Earlier this week we looked at a different parsing method that is also a type of recursive descent technique but also had some nuances compared to what you normally see.

    1. The precedence function can have multiple following elements, just like a recursive descent parser. It can do the ternary operator and the if-then-else structure.

      Robert Nystrom uses Pratt parsing in the second half of Crafting Compilers, which is available for free online:

      Pratt’s original paper is pretty readable, if you want to go to the source:

  2. There is no immutable universal law that says 2+4*2 should be parsed as 2+(4*2). That’s an Earth-centric bias. I have it on good authority that on Omicron Persei 8, the expression parses as (2+4)*2.

  3. who cares? this is just a way to convert an implicit infix expression to a more explicit expression that is still infix

    evaluation is still done shunting yard style with prefix or post fix.

    I just don’t see anything special here

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.