hn.today

Pratt parsing is a black box

vinipsmaker.github.io53 points26 comments
Screenshot of Pratt parsing is a black box

Pratt parsing is presented not merely as a compact parsing algorithm but as a different way to think about grammar. Instead of attaching semantic actions to production rules, as traditional parsers do, it attaches them to terminal tokens. A token’s “nud” action handles it when nothing precedes it; its “led” action handles it when it follows an existing expression. Binding powers determine how expressions group. The parser repeatedly consumes tokens and dispatches to these actions, reducing expression parsing to asking what a token expects on its left. This perspective can make constructs that seem to require separate grammar rules - such as function calls - natural: an opening parenthesis is a led because it follows the function expression.

The author argues that this organization avoids left recursion, backtracking, and the need to undo semantic actions, since each action runs once for a valid match. It also permits actions to look ahead or invoke other parsing techniques, and leaves unused token-table entries available as extension points for macros or user-defined operators. Examples from a compiler show dispatch tables for nud, led, and binding-power handlers, plus a unary-operator action and array-indexing logic. These advantages do not make Pratt a universal ambiguity detector: when grammars or parsers are combined, conflicts can remain, and Pratt generally takes the first match. The key shift is to design parsing around the expectations created by each token, rather than around production rules.

Read on vinipsmaker.github.io26 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.