-
Book Overview & Buying
-
Table Of Contents
Practical Systems Programming in Go
By :
A parser is a core component of a compiler or interpreter that analyzes source code according to the language's grammar and transforms a linear sequence of tokens into a structured representation, typically an AST. While the lexer is responsible for identifying individual tokens, the parser determines how those tokens relate to one another syntactically, capturing the hierarchical structure of the program.
In this implementation, we use a Pratt parser — a compact, top-down parsing technique particularly well suited to expression-heavy languages. Rather than encoding operator precedence through an extensive set of grammar rules, Pratt parsing associates parsing behavior directly with tokens. Each token may define a prefix function (describing how it starts an expression) and/or an infix function (describing how it combines with a left-hand expression). The parser advances through the token stream by recursively parsing expressions while comparing the precedence,...