Syntax-directed translation and semantic checks
A grammar fixes the structure; semantic rules attach values and computations to that structure. Track each occurrence of a grammar symbol separately, even when several occurrences share the same name.
4.1 Attributes and dependency graphs
A syntax-directed definition (SDD) associates attributes with grammar symbols and semantic rules with productions. Write for attribute of occurrence . Subscripts such as distinguish occurrences; they do not introduce new nonterminals.
A synthesized attribute is defined for the production's head, using permitted child attributes and inherited information at that head. An inherited attribute of a body occurrence is defined in its parent's production, using permitted parent, sibling or same-node information. Token attributes such as num.lexval come from lexical analysis. Classify an attribute by where it is defined, not by a suffix such as .i or .s.
An attribute grammar is commonly defined as an SDD whose semantic functions have no side effects. Printing or changing a global symbol table requires an explicit action order as well as ordinary value dependencies.
4.2 S-attributed and L-attributed definitions
Past papers · CS 2014–2026 (23 papers): 5 questions, 8 marks
An S-attributed definition uses only synthesized attributes. Compute children before their parent: a postorder tree traversal, or actions at reductions in a bottom-up parser, supplies the needed values.
An L-attributed definition allows synthesized attributes and restricted inherited dependencies. For a production , an inherited attribute of may depend on inherited attributes of and attributes of earlier siblings . Some definitions also permit same-node dependencies provided a separate acyclicity condition is established. It must not require a later sibling's result or the parent's as-yet-uncomputed synthesized result.
| Definition | Evaluation route | Main restriction |
|---|---|---|
| S-attributed | Postorder; LR reduction actions | No inherited attributes |
| L-attributed | Left-to-right depth-first traversal | Compute inherited inputs before visiting a child; synthesized outputs on return |
| General acyclic SDD instance | Topological order of its dependency graph | No fixed traversal is guaranteed for every instance |
Every S-attributed definition is L-attributed. An L-attributed definition may contain both kinds of attributes. Being based on an LR(1) grammar does not ensure that arbitrary inherited attributes can be evaluated directly during an ordinary bottom-up parse.
For declarations, a type nonterminal can synthesize a type which is passed as an inherited input to a list of identifiers. A left-recursive list can instead synthesize and carry a common type from its earlier occurrence. The same declaration language and symbol-table updates can therefore have different attribute classifications.
4.3 Translation actions and expression values
Past papers · CS 2014–2026 (23 papers): 4 questions, 8 marks
An SDT embeds actions at positions in a production body. Execute an action when traversal/parsing reaches that position. If every action is at the end of a production, a bottom-up parser executes it at reduction. Reversing a rightmost derivation gives the reduction order; reversing printed output is generally wrong.
| Desired output from an expression tree | Traversal/action placement |
|---|---|
| Prefix notation | Operator before both children |
| Postfix notation | Operator after both children |
| Value for an S-attributed arithmetic expression | Combine already evaluated child values |
| Infix with unambiguous grouping | Parenthesise each binary node |
4.4 Type checks and declaration information
Past papers · CS 2014–2026 (23 papers): 2 questions, 3 marks
Type checking verifies that uses agree with the language's type rules. A declaration inserts a binding; a use looks it up in the visible scope. Identifier-expression types must come from the declaration, not from a constant assumption that every identifier is an integer.
| Construct | What to verify |
|---|---|
Arithmetic a+b | Both operands meet the language's numeric rules; apply specified conversions |
| Logical negation | Operand has an allowed truth-value type; a toy language may insist on Boolean |
| Function call | Callee is callable; argument count and types agree with its signature |
Dereference *p | Operand has an appropriate pointer type |
| Array access | Base is indexable and index type is allowed; compute element type |
| Assignment | Left operand is assignable; right value is compatible with its type |
| Return | Returned value agrees with the declared result type |
Coercion is an implicit conversion inserted by the compiler; a cast requests a conversion explicitly. Neither permits every arbitrary operation. Numeric widening and structure/name equivalence rules are language-specific. A structural comparison recursively compares type constructors and components; name equivalence distinguishes declared type identities as defined by the language.
Last-minute recall
- Attribute occurrences, not just attribute names, are graph nodes.
- Dependency arrows point from prerequisite to result.
- S-attributed evaluation is postorder.
- L-attributed evaluation supplies inherited values before entering a child.
- A later sibling is not an allowed L-attributed source.
- Execute epsilon-rule actions and base values.
- The grammar determines grouping; the semantic rule determines the operation.
- Symbol-table lookup supplies an identifier's type.