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

The Parsing Problem

Graduate Depth 89 in the knowledge graph I know this Set as goal
9topics build on this
405prerequisites beneath it
See this on the map →
Context-Free Grammars in Compiler DesignTokenization and Lexemes+1 moreLL Parsing and Predictive ParsingLR Parsing Fundamentals+1 more
parsing syntax-analysis problem-formulation

Core Idea

Syntax analysis (parsing) determines whether a token stream is valid according to a grammar and builds a parse tree or AST. The problem is: given a CFG and input tokens, construct a derivation tree. Not all grammars admit efficient parsing; ambiguous grammars have multiple derivations. Practical parsers require restrictive grammar classes (LL, LR) or disambiguating rules.

Explainer

The lexical analyzer you already built breaks source code into tokens — identifiers, keywords, operators, literals. But a flat list of tokens says nothing about structure. The expression `3 + 4 * 5` is five tokens, but its meaning depends entirely on how those tokens group: does the multiplication bind tighter than the addition? Parsing is the phase that recovers this hierarchical structure from the linear token stream, guided by the rules of a context-free grammar.

Recall that a context-free grammar defines a language through production rules: a nonterminal on the left can be replaced by a sequence of terminals and nonterminals on the right. Parsing is the inverse problem — given the terminals (tokens), find a sequence of production applications (a derivation) that produces them. The result is a parse tree (or its compressed form, an abstract syntax tree) that makes the grammatical structure explicit. For `3 + 4 * 5`, the parse tree shows multiplication nested deeper than addition, capturing the precedence rule encoded in the grammar.

The core difficulty is that not all grammars can be parsed efficiently. A grammar is ambiguous if some input has more than one valid parse tree — meaning the grammar assigns two different structures (and potentially two different meanings) to the same program. The classic example is the dangling-else problem: `if a then if b then s1 else s2` can associate the `else` with either `if`. Ambiguity must be resolved, either by rewriting the grammar or by adding disambiguating rules (such as "else binds to the nearest if").

Even unambiguous grammars may require exponential time to parse with a naive algorithm. Practical compilers restrict themselves to grammar subclasses that guarantee linear-time parsing. LL grammars are parsed top-down by reading input left-to-right and choosing productions by looking ahead a fixed number of tokens. LR grammars are parsed bottom-up by shifting tokens onto a stack and reducing them when a complete right-hand side is recognized. These two families cover nearly all programming language constructs, and the choice between them shapes the entire front end of a compiler. Understanding the parsing problem — what makes it hard, what makes it tractable — is the foundation for studying both approaches.

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 Problem

Longest path: 90 steps · 405 total prerequisite topics

Prerequisites (3)

Leads To (3)