Parsing: Words into Structure
The heart of a language: take the flat list of tokens and build a *tree* that captures the structure and meaning โ what groups with what, what happens before what. This is where grammar lives.
Tokens are just a flat list - they don't yet capture structure. In 2 + 3 * 4, which happens first? Parsing is the stage that answers this: it takes the flat token stream and builds a tree (an 'abstract syntax tree', or AST) that captures how things group and nest and what happens in what order. This is the heart of a language, where its grammar and precedence live - and it's more approachable than its reputation suggests.
From flat tokens to a tree
The parser arranges tokens into a tree that reflects structure. For 2 + 3 * 4, the tree captures that multiplication binds tighter, so 3 * 4 is one branch, added to 2.
# tokens: 2 + 3 * 4
#
# the tree the parser builds:
# (+)
# / \
# 2 (*)
# / \
# 3 4
#
# the shape encodes 'multiply first, then add'2 + 3 * 4 could mean (2+3)*4 or 2+(3*4). The parser's job is to resolve that using the language's grammar and precedence rules, producing a tree whose shape encodes the correct meaning: multiplication lower in the tree means it happens first. Once you have the tree, running the program is just walking it. The parser is where a flat sequence becomes structured meaning.Recursive descent: grammar as functions
The friendliest way to parse is recursive descent: write one function per grammar rule, and let them call each other, mirroring the structure of the language.
| Grammar level | Handles | Calls into... |
|---|---|---|
| expression | the whole thing | term |
| term (+ -) | addition/subtraction | factor |
| factor (* /) | multiplication (binds tighter) | primary |
| primary | numbers, names, ( ... ) | back up to expression |
Each function handles one precedence level and calls the next-tighter one - so the call structure naturally produces correct precedence (factor is 'below' term, so <code>*</code> binds tighter than <code>+</code>). This is exactly how <a href='/stack/larzcalc'>larzcalc</a> and <a href='/stack/larzscript'>larzscript</a> parse. Recursive descent is beginner-friendly precisely because the code looks like the grammar it implements.
Structure from a flat stream
- Parsing turns the flat token list into a tree (an AST) that captures structure.
- The tree's shape encodes meaning - precedence and grouping (3*4 first, then +2).
- Recursive descent writes one function per grammar rule, calling each other by precedence.
- The code mirrors the grammar - which makes it surprisingly readable.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Parsing turns the flat token stream into a tree (an AST) that captures structure and meaning.
- The tree's shape encodes precedence and grouping - what binds tighter, what happens first.
- Recursive descent is the friendly technique: one function per grammar rule, calling each other by precedence.
- The parser code mirrors the language's grammar, which is what makes it readable - as in larzcalc and larzscript.
Frequently asked
What is an 'abstract syntax tree' and why represent a program as a tree?
An abstract syntax tree (AST) is a tree-shaped representation of your program's structure - it captures how the parts of your code nest and relate, with each node representing a construct (an operation, a value, an if-statement, a function call) and its children representing the parts that make it up. It's called 'abstract' because it captures the essential structure and meaning while dropping incidental syntax details (like parentheses or exact whitespace) that were only there to guide the parsing - the tree encodes what the code means, not the literal characters. Why a tree? Because programs are fundamentally nested and hierarchical, and a tree captures that nesting perfectly. Consider '2 + 3 * 4': it's not really a flat sequence, it's 'add 2 to (the result of multiplying 3 and 4)' - a nested structure where the multiplication is a sub-part of the addition. A tree represents this naturally: an addition node with two children, one being the number 2 and the other being a multiplication node (with children 3 and 4). The tree's shape directly encodes the structure and precedence - the multiplication being deeper in the tree means it happens first. This nesting appears everywhere in code: an if-statement contains a condition and a body (which contains more statements); a function call has a function and arguments (which are themselves expressions); expressions contain sub-expressions. Trees represent all this cleanly, with parent-child relationships mirroring 'this construct contains these parts'. Once you have the program as a tree, running it becomes wonderfully simple: you walk the tree, and for each node you know what to do based on its type - for an addition node, evaluate its two children and add the results (evaluating a child might mean walking its subtree first); for an if-node, evaluate the condition child and then walk either the then-branch or else-branch. This 'walk the tree and act on each node' approach is exactly what a tree-walking interpreter does, and it's clean precisely because the tree already encodes the structure and order. Compare this to trying to run a flat list of tokens directly - you'd have to constantly figure out the structure on the fly, which is exactly what parsing into a tree does once, up front. So the tree is valuable because it captures the nested structure of programs in a form that's both a faithful representation of meaning and easy to process: build it once (parsing), then walk it to run or to compile. The AST is the central data structure connecting the front-end (lexing and parsing, which produce it) to the back-end (interpreting or compiling, which consume it), and its tree shape is what makes both sides manageable. You can actually see ASTs in the stack: larzcalc and larzscript both parse into tree structures and then walk them to evaluate - the tree is the bridge between text and execution.
Recursive descent and precedence sound complicated - is parsing really approachable for a beginner?
Parsing has a reputation for being hard, partly because formal treatments dive into dense theory, but the practical, approachable technique - recursive descent - is genuinely beginner-friendly, and precedence, while it sounds intimidating, has a neat trick that makes it fall out naturally. Here's why recursive descent is approachable: you write one function for each level of your grammar, and each function's code looks like the grammar rule it implements, so if you can describe your language's structure in plain terms, you can pretty directly translate that into parser functions. There's a pleasing correspondence between the grammar and the code, which means you're not inventing clever algorithms - you're transcribing structure into functions that call each other. The functions calling each other (recursion) mirrors how language constructs nest (an expression contains sub-expressions), which is natural once you see it. As for precedence - making '*' bind tighter than '+' so '2 + 3 * 4' means '2 + (3*4)' - the trick is elegant: you arrange your parser functions in layers by precedence, with each function handling one precedence level and calling the next-tighter level for its operands. So the 'addition' function handles '+' and '-' but gets its operands by calling the 'multiplication' function, which handles '*' and '/'. Because multiplication is 'below' addition in this call structure, it naturally binds tighter - the parser processes the tighter-binding operators deeper in the recursion, which puts them lower in the resulting tree, which means they happen first. You don't have to explicitly compute precedence; it emerges from how you layer the functions. This layered approach (sometimes called precedence climbing when generalised) is a standard, learnable pattern, and once you've seen it work, it clicks. The honest picture: parsing does have genuinely advanced corners - complex grammars, ambiguity, parser generators, and formal theory that gets deep - and those can be hard. But you don't need any of that to build a real, working parser for a reasonable language. Recursive descent handles a wide range of languages (many real, production languages use hand-written recursive-descent parsers), and the layered-by-precedence technique handles operator precedence cleanly. Both are within reach of a motivated beginner, especially with working examples to learn from. And you have those examples: the stack's larzcalc is a small, readable recursive-descent parser that correctly handles precedence for arithmetic (getting '2 + 3 * 4' right), and larzscript is a fuller one for a whole language - you can read both and see the technique in action, which demystifies it enormously. So: parsing's scary reputation comes from the theory-heavy and advanced end, but the practical technique of recursive descent, with precedence handled by layering functions, is approachable, standard, and learnable - it's mostly writing functions that mirror your grammar and calling them in precedence order. Start with a tiny grammar (like arithmetic), read larzcalc, and you'll find that building a parser is far more doable than the reputation suggests. It's one of those topics that feels impossible from the outside and surprisingly manageable once you're shown the friendly technique and a working example. Recursive descent is the friendly way in, and precedence-by-layering is the neat trick that makes it work.
Related lessons
Tokenizing: Text into Words
The first stage of any language: chop the raw stream of characters into meaningful chunks โฆ
How Programming Languages WorkInterpreting: Making the Tree Run
The payoff stage: take the tree the parser built and *run* it. Walk the tree, do what eachโฆ