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

Attribute Grammar Framework

Graduate Depth 90 in the knowledge graph ☐ I know this ☆ Set as goal
464prerequisites beneath it
See this on the map →
Abstract Syntax Trees (ASTs)Parse Trees, Derivations, and Ambiguity in CFGs→
semantic-analysis attributes grammars

Core Idea

Attribute grammars associate attributes (semantic values) with grammar symbols and define rules for computing attributes. Synthesized attributes are computed from children; inherited attributes from parents. This framework elegantly separates parsing from semantic analysis.

How It's Best Learned

Write attribute grammars for a small language using tools like Antlr. Implement both bottom-up and top-down attribute evaluators.

Common Misconceptions

Attribute grammars are the only way to do semantic analysis (they are one useful approach; ad-hoc traversal is simpler for many tasks). All attributes must be computed in one pass (multiple passes can be clearer).

Explainer

You know from parse trees and ASTs that parsing gives you the syntactic structure of a program — which tokens group together and how. But syntax alone cannot answer questions like "is this variable declared?" or "do the types in this expression match?" These are semantic questions, and attribute grammars provide a formal framework for computing semantic information directly on the parse tree. The idea is to attach attributes — named values like types, scope levels, or computed results — to grammar symbols, and define rules that specify how to compute each attribute from other attributes in the tree.

There are two kinds of attributes, and the distinction matters for evaluation order. Synthesized attributes flow upward: a parent node's attribute is computed from its children's attributes. Think of evaluating an arithmetic expression tree — the value of an addition node is synthesized from the values of its left and right children. Inherited attributes flow downward or sideways: a child's attribute is computed from its parent or siblings. For example, a declaration like `int x, y, z;` might have the type `int` as an inherited attribute that flows from the type specifier down to each variable in the list. A grammar where every attribute is synthesized is called an S-attributed grammar and can be evaluated in a single bottom-up pass. When inherited attributes are involved, you need an L-attributed grammar (where inherited attributes depend only on left siblings and the parent), which can still be evaluated left-to-right in a single pass.

Consider a concrete example: type checking in a simple expression language. You might attach a `type` synthesized attribute to every expression node. For a rule like `Expr → Expr₁ + Expr₂`, the semantic rule says: if both children have type `int`, the result has type `int`; if both are `float`, the result is `float`; if one is `int` and the other `float`, insert a coercion and the result is `float`; otherwise, it is a type error. Each production in the grammar gets its own set of semantic rules, and together they define how type information propagates through the entire tree. The attribute grammar framework makes these rules explicit and composable rather than scattered through ad-hoc visitor code.

In practice, many compilers use ad-hoc AST traversals instead of formal attribute grammar tools, because the framework can feel heavyweight for simple analyses. But the conceptual model remains valuable even when the implementation is informal. Thinking in terms of synthesized and inherited attributes clarifies the information flow in any semantic analysis pass: what information do you need from children (synthesize it), and what information must come from context (inherit it). This mental model helps you design clean, predictable compiler passes whether or not you use a formal attribute grammar evaluator.

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 → Abstract Syntax Trees (ASTs) → Attribute Grammar Framework

Longest path: 91 steps · 464 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.