Semantic Analysis — Attribute Grammars, Type Checking
674 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Semantic Analysis — Attribute Grammars, Type Checking ## 🎯 Learning Objectives - Define attributes for grammar symbols - Differentiate S-attributed and L-attributed grammars - Build a type checker using attribute grammars - Implement type inference for simple expressions * * * ## 1. Syntax-Directed Definitions (S...

Semantic Analysis — Attribute Grammars, Type Checking
🎯 Learning Objectives
- Define attributes for grammar symbols
- Differentiate S-attributed and L-attributed grammars
- Build a type checker using attribute grammars
- Implement type inference for simple expressions
1. Syntax-Directed Definitions (SDD)
1.1 Intuition
Syntax-directed definitions attach attributes to grammar symbols and semantic rules to productions. As the parser processes the input, it evaluates these rules to compute values (types, code, etc.).
1.2 Attributes
| Type | Meaning | Example |
|---|---|---|
| Synthesized | Computed from children's attributes | E.val = T.val + E'.val |
| Inherited | Passed from parent or siblings | T'.inh = E.val |
(Diagram)
2. S-Attributed Grammars
All attributes are synthesized — evaluation can be done bottom-up (LR parsing).
Example: Expression evaluation
pseudoProduction Semantic Rule E → E₁ + T E.val = E₁.val + T.val E → T E.val = T.val T → T₁ * F T.val = T₁.val * F.val T → F T.val = F.val F → ( E ) F.val = E.val F → num F.val = num.lexval
Evaluation for "2 + 3 * 4": Bottom-up: F(2)→T(2)→E(2), F(4)→T(4), F(3)→T(3)→T(3*4=12)→E(2+12=14)
3. L-Attributed Grammars
Attributes can be inherited but only from parents or left siblings — evaluation can be done top-down (LL parsing).
Example: Type checking
javascriptProduction Semantic Rule D → T L L.inh = T.type T → int T.type = integer T → float T.type = float L → L₁, id L₁.inh = L.inh id.type = L.inh L → id id.type = L.inh
4. Type Checking
4.1 Type Rules
c// Type checker for expressions if (E1.type == int && E2.type == int) E.type = int; // int + int = int else if (E1.type == float && E2.type == float) E.type = float; // float + float = float else if (E1.type == int && E2.type == float) E.type = float; // int + float = float (coercion) else ERROR("Type mismatch");
4.2 Type Inference
For
x = 5; y = x + 3.2;:5is int →xhas type intx + 3.2: int + float → float (coercion)ygets type float
5. 📝 Practice Questions
Q1: What is the difference between synthesized and inherited attributes?Answer: Synthesized attributes are computed from children (bottom-up). Inherited attributes are passed from parent or left sibling (top-down or left-to-right). SDDs with only synthesized attributes are S-attributed; those with inherited attributes but limited to left-to-right flow are L-attributed. Q2: Why can S-attributed SDDs be evaluated during LR parsing?Answer: LR parsers process input bottom-up, reducing productions from leaves to root. When a production is reduced, all children have been fully evaluated (their synthesized attributes are available). The parent's attributes can be computed immediately. Q3: Write an SDD for translating infix to postfix notation.Answer:pseudoE → E₁ + T E.code = E₁.code || T.code || "+" E → T E.code = T.code T → num T.code = num.lexval T → (E) T.code = E.codeQ4: How does a type checker handle type conversion between int and float?Answer: The type checker inserts implicit conversion (coercion). In an expression mixing int and float, the int is converted to float before the operation. The result type is float. Some languages (like C) also allow explicit casts to override default behavior. Q5: What is a symbol table and how is it used during semantic analysis?Answer: A symbol table maps identifier names to their attributes (type, scope, memory location). During semantic analysis, when an identifier is encountered, the type checker looks up its declared type in the symbol table to verify consistent usage.
6. 🔗 Cross-References
- Week 3 - LL Parsing: L-attributed during top-down parsing
- Week 4 - LR Parsing: S-attributed during bottom-up parsing
- Week 6 - Symbol Table: Storing attribute information Join Discord PreviousLR ParsingNextIntermediate Code