ELI5: pratt parser
// explanation
What is a Pratt parser?
A Pratt parser is a way to teach a computer to read and understand mathematical expressions like "2 + 3 * 4" [2]. Instead of checking every possible rule for how operations should be ordered, it looks at each piece (called a token) one at a time and figures out what to do based on what it sees [3].
Why do we need it?
When you write math or code, some operations need to happen before others - like multiplication before addition [2]. A Pratt parser is really good at handling these rules, and it's much simpler to write than other methods [1].
How is it different from other parsers?
Most parsers check every level of importance for operations, like checking for addition, then multiplication, then parentheses [3]. A Pratt parser is smarter - it just looks at the next piece of information and decides what to do based on that one piece [3].
When would you use it?
Programmers use Pratt parsers when they're building interpreters or compilers - basically tools that read code or expressions and understand them [6].
// sources
Apr 13, 2020 ... The simplest technique for hand-writing a parser is recursive descent, which models the grammar as a set of mutually recursive functions.
Mar 19, 2011 ... Pratt parsing is the jelly. When you mix the two together, you get a simple, terse, readable parser that can handle any grammar you throw at it.
Feb 9, 2022 ... A Pratt parser dispatches on the next token, instead of recursing down the N precedence levels. In the end it's not all that different.
Operator-precedence parser ยท Contents ยท Relationship to other parsers ยท Precedence climbing method ยท Pratt parsing ยท Alternative methods ยท References ยท External links.
Aug 11, 2017 ... Pratt parsing works by scanning the input tokens, and classifying them into two categories ... operators that operate to the right, with no left-ย ...
Video by Core Dumped

Video by Colin James

Video by Computerphile
