Writing a Pratt Parser in Go: Binding Powers & Lookup Tables

Added:

Pratt Parsing
AST Setup
Parser Core
Binding Powers
Expression Parsing
Binary Operators
Recursion Loop
Final Testing

Pratt Parsing

0:00
Playing Section
  • 1

    Explains Pratt parsing technique using binding powers and handler functions.

  • 2

    Introduces nud, led, and statement handlers for different token types.

  • 3

    Highlights lookup tables to simplify parser recursion and maintainability.

Familiarity with Go programming syntax and core paradigms, specifically structs, interfaces, methods, and map data structures.
Basic understanding of Lexical Analysis (Tokenization) and how source code is converted into a stream of tokens.
Conceptual knowledge of Abstract Syntax Trees (ASTs) and how hierarchical tree nodes represent structured code.
An understanding of operator precedence and associativity in mathematics and computer science (e.g., how '*' binds more tightly than '+').
Designing and implementing a tree-walking interpreter or evaluator to execute the AST generated by the Pratt parser.
Expanding the parser to support complex statement structures, such as control flow (if-else, loops), block scopes, and function definitions.
Implementing robust error handling and recovery strategies (like panic-mode recovery) to handle syntax errors gracefully.
Compiling the AST into custom bytecode and building a stack-based virtual machine to run it efficiently.
15.7K views405likes56:58@tylerlacebyOriginal Release: 2024-04-02

A Pratt parser is a parsing technique that uses binding power values and lookup tables to handle operator precedence and associativity automatically, where NUD (null denotation) handlers parse expressions without left operands (like unary operators), LED (left denotation) handlers parse binary expressions expecting left operands, and the parse_expression function recursively builds the AST by comparing current token binding power against a minimum threshold to determine when to continue parsing.