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 ProblemLookahead 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsLogic Gates FundamentalsImplementing Boolean Functions with GatesKarnaugh Map SimplificationCombinational Circuit DesignFlip-Flops and LatchesFinite State Machines (FSMs)Deterministic Finite Automata (DFA)Nondeterministic Finite Automata (NFA)Two-Way Finite AutomataNFA to DFA Conversion (Subset Construction)DFA Properties and Minimization AlgorithmsRegular Languages: Definition and CharacterizationContext-Free Grammars (CFGs)Context-Free Grammar Properties and AmbiguityParse Trees, Derivations, and Ambiguity in CFGsContext-Free Grammars in Compiler DesignThe Parsing ProblemLL Parsing and Predictive Parsing

Longest path: 91 steps · 413 total prerequisite topics

Prerequisites (2)

Leads To (2)