A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

LR Parsing Fundamentals

Graduate Depth 90 in the knowledge graph ☐ I know this ☆ Set as goal
7topics build on this
413prerequisites beneath it
See this on the map →
StacksThe Parsing Problem→→LALR Grammar ConstructionLookahead in Parsing and Grammar Classes+1 more
bottom-up-parsing shift-reduce parsing-tables

Core Idea

LR parsing is bottom-up, deterministic parsing that constructs a parse tree by reducing input tokens to the start symbol. An LR parser maintains a stack of states and a lookahead token; each state encodes all possible actions (shift or reduce). LR parsers handle a much larger grammar class than LL, including left-recursive grammars, making them suitable for real programming languages.

Explainer

If you understand the parsing problem — turning a flat sequence of tokens into a structured tree — then LR parsing is the most powerful deterministic approach to solving it. Where top-down parsers (like recursive descent) try to guess which grammar rule to apply by looking at the first few tokens, LR parsing works bottom-up: it reads tokens left to right, accumulates them, and waits until it has enough evidence to recognize which rule produced them. The name "LR" stands for Left-to-right scan, Rightmost derivation — it builds the parse tree from the leaves up to the root.

The core mechanism relies on the stack data structure you already know. An LR parser maintains a stack of states, where each state represents a set of partially-recognized grammar rules. At each step, the parser consults a parsing table indexed by the current state and the next input token (the lookahead). The table tells it to do one of two things: shift (push the current token and a new state onto the stack, then advance to the next token) or reduce (pop several items off the stack that match the right-hand side of a grammar rule, and push the left-hand side nonterminal back on). Think of it like assembling a puzzle from individual pieces — you keep picking up pieces (shifting) until you notice a group that forms a recognizable sub-picture (reducing).

Consider parsing the expression `3 + 4 * 5`. The parser shifts `3`, reduces it to an Expression, shifts `+`, shifts `4`, reduces to Expression, shifts `*`, shifts `5`, reduces to Expression. Now it sees `Expression * Expression` on the stack and reduces that to Expression (because `*` binds tighter). Then it sees `Expression + Expression` and reduces that. The parsing table encodes operator precedence and associativity — the parser never has to guess, because the table was precomputed from the grammar to resolve every possible ambiguity deterministically.

The power of LR parsing comes from what it can handle. Top-down parsers choke on left-recursive grammars (rules like `Expr → Expr + Term`), which naturally describe left-associative operators. LR parsers handle left recursion effortlessly because they build from the bottom up — they've already parsed the left operand before they need to decide about the rule. This is why parser generators like yacc and bison produce LR parsers: real programming language grammars are full of left recursion and operator precedence rules that would require awkward rewriting for a top-down parser. The tradeoff is that LR parsing tables can be large and the algorithm is harder to implement by hand, which is exactly why we use parser generators to build them automatically.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → The Parsing Problem → LR Parsing Fundamentals

Longest path: 91 steps · 413 total prerequisite topics

Prerequisites (2)

Leads To (3)