Technology1 min read

Pratt parsing is a black box

By · Published by Everything Blog

In short

The approach categorizes tokens based on what appears to their left, dividing elements into null denotations called nuds and left denotations called leds. These classifications are mapped to semantic actions and binding power tables that use positive integers. Unlike standard methods that bind actions to grammar production rules, this system attaches actions directly to terminal symbols. Consequently, actions execute immediately after tokens are consumed from the stream, effectively circumventing standard left-recursion issues present in LL(k) systems.

Key points

  • Tokens are divided into nuds and leds depending on whether they expect preceding tokens i…: Tokens are divided into nuds and leds depending on whether they expect preceding tokens in the stream.
  • Binding power tables assigned with positive integers dictate the behavior and priority of…: Binding power tables assigned with positive integers dictate the behavior and priority of token parsing.
  • Dispatching semantic actions after tokens are consumed eliminates the left-recursion pitf…: Dispatching semantic actions after tokens are consumed eliminates the left-recursion pitfalls commonly found in LL(k) parsers.

Sources checked by our analysis pipeline: 1

Introduction

It caught my attention that most of Pratt parsing tutorials that I've stumbled on spend almost all of their time explaining Pratt's algorithm and none explaining the properties behind Pratt that actually matter. Pratt fits into just 9 lines of code if you're programming in a high-level language such as Rhombus:

And here's a secret: that's the least important thing you need to understand about Pratt's. In fact, you don't need to understand this piece at all! Imagine if you were only able to use quicksort if you understood its implementation. Imagine if you were only able to use files if you knew how to implement a filesystem from scratch. It's the same logic here. If you lack the power to abstract, you'll arrive nowhere in computer science because that's how the whole field works. In fact, I wrote the backbone of a heavily Pratt-based parsing machinery for a compiler that I've been writing since last year w/o even bothering to understand the above snippet in any detail.

Not convinced? Think for a second on how they do in…

Technology