Pratt parsing is a black box vinipsmaker.github.io · 20 points · 11 comments · H...

@best

Pratt parsing is a black box vinipsmaker.github.io · 20 points · 11 comments · HN thread

TL;DR The author argues that Pratt parsing should be learned as a black box, focusing on its mindset rather than its 9-line implementation. The core idea is that each token registers a nud (no left operand) or led (expects a left operand) action, which removes left-recursion, backtracking and memoization concerns. The post also covers right-associativity tricks, macros, and modal lexing, drawn from the author's own compiler project.

  • Pratt parsing registers semantic actions to terminal tokens (nuds and leds) instead of to grammar production rules as in Yacc/Bison.
  • Because the action runs after its token is consumed, left recursion does not arise and no backtracking or memoization is needed; semantic actions run exactly once.
  • Right-associative operators are obtained by using bp - 1 instead of bp in the led's binding power.
  • Pratt macros are nuds and leds in the language itself, so macros can invoke both the lexer and the parser, unifying textual and AST macros.
  • Modal lexing, where the parser passes a mode to the lexer, lets sublanguages such as type annotations coexist with conflicting tokens.
  • Pratt cannot parse statements alone, needs a separate lexer, and cannot do incremental parsing as GLR does.

Discussion The thread is short and mostly focused on terminology. Early commenters asked what 'semantic actions' and 'production rules' mean, and several explained them in the context of Yacc/Bison. A substantial side exchange questioned whether the article was AI-generated, which others rejected on the grounds of its precise parser-theory content. The author replied in the thread, saying the code is currently closed to avoid LLM training. Other comments raised a tangent on using brackets rather than angle brackets for generics and asked how Pratt compares to PEG and Ohm grammars. The mood is mostly curious and civil, with one dismissive comment.

  • Terminology is standard parser theory, not slop (many): Several commenters said the vocabulary of production rules and semantic actions is standard in compiler courses and that the article is clearly written by a knowledgeable human.
  • Suspected the article was AI-generated (a few): One commenter asked whether it was written by an LLM because the terminology was unfamiliar to them; another dismissed it as slop, and was rebuked for not looking the terms up.
  • Interest in comparing Pratt with PEG and Ohm (some): A commenter wondered whether Pratt gives up the readability of a declarative grammar as documentation, and asked for a side-by-side calculator example against PEG-style tools.
  • Brackets for types instead of angle brackets (some): A commenter endorsed the article's point against angle brackets for generics and suggested making types first-class values passed in ordinary parentheses, with erased static types costing nothing at runtime.
  • Author offers to answer questions (a few): The author said they used Pratt parsing for about a year in a macro-heavy language, that the code is closed until version 0.1, and that they will answer questions.

#parsing #compilers #pratt #prattParsing #programmingLanguages