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

LL Parsing and Predictive Parsing

Graduate Depth 90 in the knowledge graph ☐ I know this ☆ Set as goal
4topics build on this
413prerequisites beneath it
See this on the map →
StacksThe Parsing Problem→→Lookahead in Parsing and Grammar ClassesParser Generators and Yacc/Bison
top-down-parsing recursive-descent predictive-parsing

Core Idea

LL(k) parsing is top-down, deterministic parsing using k lookahead tokens. An LL(1) parser uses a single lookahead token to predict which production rule to apply. It can be implemented as a recursive descent parser (function per nonterminal) or via a table-driven parser. LL grammars must be non-left-recursive and free of ambiguity, limiting expressiveness but enabling simple implementation.

Explainer

You already know from the parsing problem overview that a parser's job is to take a flat sequence of tokens and recover the tree structure implied by a grammar. LL parsing is the most intuitive approach: it reads the input Left-to-right and constructs a Leftmost derivation, building the parse tree from the root down toward the leaves. Think of it as making predictions — you look at the next token and decide which grammar rule to expand, then commit to that choice and move forward.

The "k" in LL(k) tells you how many tokens ahead the parser peeks before making its prediction. In practice, LL(1) — one token of lookahead — is the workhorse. Consider parsing an if-statement: when the parser sees the token `if`, it knows immediately to apply the if-statement production rule. No other rule could start with `if`. This works because LL(1) grammars are designed so that for every nonterminal, the possible productions have disjoint FIRST sets — the set of tokens that can begin each alternative. When FIRST sets overlap, the parser cannot decide which rule to apply with a single token, and the grammar is not LL(1).

There are two common implementation strategies, both of which use the stack data structure you already know. A recursive descent parser turns each grammar nonterminal into a function. The function for `Expression` calls the function for `Term`, which calls the function for `Factor`, and so on — the call stack itself acts as the parsing stack. Alternatively, a table-driven parser uses an explicit stack and a parsing table indexed by (current nonterminal, lookahead token). Each table entry tells the parser which production to apply. Both approaches are equivalent in power; recursive descent is easier to write by hand, while table-driven parsers are easier to generate automatically.

The main limitation of LL parsing is that the grammar must be non-left-recursive. A rule like `Expr → Expr + Term` sends a recursive descent parser into an infinite loop — it keeps calling the `Expr` function without consuming any input. The standard fix is left-factoring and rewriting left recursion into right recursion (e.g., `Expr → Term Expr'`, `Expr' → + Term Expr' | ε`). This transformation preserves the language but changes the parse tree shape, which matters when you later attach semantic actions. Despite these restrictions, LL parsing remains widely used because it is simple to implement, produces clear error messages (you always know what the parser expected), and maps naturally to hand-written parsers for languages designed with top-down parsing in mind.

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 → LL Parsing and Predictive Parsing

Longest path: 91 steps · 413 total prerequisite topics

Prerequisites (2)

Leads To (2)