Chapter 4

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 X.aX.a for attribute aa of occurrence XX. Subscripts such as E1,E2E_1,E_2 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 A→X1⋯XnA\to X_1\cdots X_n, an inherited attribute of XjX_j may depend on inherited attributes of AA and attributes of earlier siblings X1,…,Xj−1X_1,\ldots,X_{j-1}. 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.

DefinitionEvaluation routeMain restriction
S-attributedPostorder; LR reduction actionsNo inherited attributes
L-attributedLeft-to-right depth-first traversalCompute inherited inputs before visiting a child; synthesized outputs on return
General acyclic SDD instanceTopological order of its dependency graphNo 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 treeTraversal/action placement
Prefix notationOperator before both children
Postfix notationOperator after both children
Value for an S-attributed arithmetic expressionCombine already evaluated child values
Infix with unambiguous groupingParenthesise 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.

ConstructWhat to verify
Arithmetic a+bBoth operands meet the language's numeric rules; apply specified conversions
Logical negationOperand has an allowed truth-value type; a toy language may insist on Boolean
Function callCallee is callable; argument count and types agree with its signature
Dereference *pOperand has an appropriate pointer type
Array accessBase is indexable and index type is allowed; compute element type
AssignmentLeft operand is assignable; right value is compatible with its type
ReturnReturned 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.

Revise offline: the whole Compiler Design handbook as one PDF

43 pages · 2.0 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections