# What is a Pratt parser?

> A Pratt parser handles operator precedence with a table of prefix and infix functions instead of one grammar rule per level. How it works, simply.

**Pratt parser**: A Pratt parser, or top-down operator precedence parser, parses expressions using a table that gives each token a prefix function, an infix function and a precedence.

A **Pratt parser** parses expressions by giving each kind of token up to two parsing functions (one for when it starts an expression, one for when it appears between two) and a **precedence**. It is named after Vaughan Pratt, who described it in 1973 as "top down operator precedence".

## How it works

The core is one function, `parsePrecedence(level)`:

1. Read a token and call its **prefix** function (a number literal, a unary minus, a parenthesis).
2. While the next token's precedence is at least `level`, read it and call its **infix** function (a binary `+`, a call `(`, a dot), passing what was parsed so far as the left operand.

An infix function for `+` parses its right side with `parsePrecedence` one level higher, which gives left associativity. Adding an operator is one row in a table.

## Pratt vs recursive descent

A [recursive descent parser](https://cracked.guide/glossary/recursive-descent-parser) writes one function per precedence level (equality, comparison, term, factor, unary). That is clear but repetitive, and every expression walks through all the levels. A Pratt parser collapses those levels into a table. Crafting Interpreters uses recursive descent for jlox ([chapter 6](https://cracked.guide/guide/ci-06)) and a Pratt parser for clox ([chapter 17](https://cracked.guide/guide/ci-17)), so you build both and can compare.

## Questions

### Is a Pratt parser recursive descent?

It is a top-down recursive parser too, but it drives precedence from a table instead of from a chain of grammar rules.

### What is binding power?

Another name for precedence in Pratt parsing: how strongly an operator holds on to the operands around it.

---

Source: https://cracked.guide/glossary/pratt-parser
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
