1. Gyanastra
  2. Compiler Design
  3. UGC NET: Semantic Analysis and Type Checking
  4. Attribute Grammars and Evaluation Order
Free lesson, no account needed 10 min read 28 practice questions after it

Compiler Design · UGC NET: Semantic Analysis and Type Checking

Attribute Grammars and Evaluation Order

Overview

Semantic analysis checks the meaning of a syntactically valid program. An attribute grammar attaches values, called attributes, to grammar symbols and gives semantic rules that compute those values on a parse tree.
The central problem is evaluation order: which attribute must be computed before which other attribute.
The dependency graph, S-attributed definitions, L-attributed definitions, and annotated parse trees are the standard tools for solving this problem.
This lesson belongs to Semantic Analysis and Type Checking, and it assumes knowledge of context-free grammars, parse trees, top-down parsing such as LL, and bottom-up parsing such as LR.

Key concepts

  • A syntax-directed definition is a context-free grammar with attributes and semantic rules. In this lesson, attribute grammar means this standard textbook definition: grammar + attributes + semantic rules.
    If a book adds semantic predicates, treat them as extra conditions.
  • An attribute is a property of a grammar symbol at a parse-tree node: value, type, string, symbol-table entry, code fragment, etc.
  • A synthesized attribute of a non-terminal at a node is computed from attributes of that node’s children. It is naturally evaluated bottom-up.
  • An inherited attribute of a non-terminal at a node is computed from attributes of its parent and/or its left siblings. It is naturally passed downward or left-to-right.
  • An S-attributed definition uses only synthesized attributes. It can be evaluated by a postorder traversal of the parse tree and is natural for bottom-up parsing such as LR.
  • An L-attributed definition allows both synthesized and inherited attributes, but an inherited attribute may depend only on attributes to its left and on inherited attributes of the parent.
    It can be evaluated by one left-to-right depth-first traversal and is natural for top-down parsing such as LL.
  • Every S-attributed definition is L-attributed; the converse is false.
  • A dependency graph has one node for each attribute instance in the annotated parse tree. A directed edge u→vu \to v means the value of vv depends on the value of uu.
  • A topological sort of an acyclic dependency graph gives a valid evaluation order.
  • A cyclic dependency graph means no topological order exists, so the attributes in the cycle have no unique evaluation order.
  • An annotated parse tree is an ordinary parse tree with attribute values written at the nodes.
  • A semantic action is a code fragment attached to a production in a syntax-directed translation scheme. It is executed when the parser uses that production.

How it works

  1. Write a context-free grammar for the language construct.
  2. Decide which attributes are needed. Use synthesized attributes for values flowing upward, such as expression values.
    Use inherited attributes for information flowing downward or left-to-right, such as a declared type passed to identifiers.
  3. Attach semantic rules to each production. A rule may refer only to attributes of symbols appearing in that production.
  4. Build a parse tree for an input string.
  5. Create an attribute instance at every parse-tree node for every attribute declared on that grammar symbol.
  6. Add dependency edges: if attribute vv is computed using attribute uu, draw an edge u→vu \to v.
  7. If the graph is acyclic, topologically sort it: repeatedly remove a node with indegree zero and put it next in the evaluation order.
  8. Evaluate attributes in that order and write their values on the parse tree. The result is an annotated parse tree.
  9. For special cases, skip the general graph. For S-attributed definitions, evaluate children before parent.
    For L-attributed definitions, pass inherited attributes down, visit children left-to-right, and return synthesized attributes upward.

For a production A→X1X2⋯XnA \to X_1X_2\cdots X_n, an L-attributed definition requires every inherited attribute of XjX_j to depend only on inherited attributes of AA and attributes of X1,…,Xj−1X_1,\ldots,X_{j-1}.
It must not depend on a synthesized attribute of XjX_j itself or on any attribute of a symbol to its right. The leftmost symbol X1X_1 may use inherited attributes of AA and constants only.

Figure. Notice the branches: synthesized-only definitions follow bottom-up postorder, L-attributed definitions follow left-to-right depth-first, and all others build a dependency graph. A cycle means no unique evaluation order; otherwise topological sort gives the order.

Formulas

  • For a production A→X1X2⋯XnA \to X_1X_2\cdots X_n:

  • Synthesized attribute rule:
    A.s=f(X1.a1,…,Xn.an)A.s = f(X_1.a_1,\ldots,X_n.a_n) meaning the value of A.sA.s is computed from attributes of the children.

  • Inherited attribute rule:
    Xj.i=g(A.i,  X1.a1,…,Xj−1.aj−1)X_j.i = g(A.i,\; X_1.a_1,\ldots,X_{j-1}.a_{j-1}) meaning an inherited attribute of XjX_j is computed from the parent and left siblings.

  • Dependency edge:
    u→vmeans v=f(…,u,…)u \to v \quad \text{means } v = f(\ldots,u,\ldots) so uu must be evaluated before vv.

  • Topological order condition: if u→vu \to v is an edge, then uu appears before vv in the evaluation sequence.

  • S-attributed traversal: postorder, i.e., all children of a node before the node itself.

  • L-attributed traversal: inherited attributes on entering a node, children left-to-right, synthesized attributes on leaving the node.

Worked examples

Example 1: S-attributed expression evaluation.
Grammar: E→E+T∣T,T→T∗F∣F,F→numE \to E+T \mid T,\quad T \to T*F \mid F,\quad F \to \mathbf{num} Semantic rules: E.val=E1.val+T.val,E.val=T.val,E.val=E_1.val+T.val,\quad E.val=T.val, T.val=T1.val∗F.val,T.val=F.val,F.val=num.lexval.T.val=T_1.val*F.val,\quad T.val=F.val,\quad F.val=num.lexval. For input 2+3∗42+3*4, postorder evaluation gives: F(2)=2,T(2)=2,E(2)=2,F(2)=2,\quad T(2)=2,\quad E(2)=2, F(3)=3,T(3)=3,F(4)=4,F(3)=3,\quad T(3)=3,\quad F(4)=4, T(3∗4)=3∗4=12,E(2+12)=2+12=14.T(3*4)=3*4=12,\quad E(2+12)=2+12=14. The value is 1414. This works because only synthesized attributes are used.

Example 2: L-attributed type propagation.
Grammar: D→T L{L.in=T.type}D \to T\,L \quad \{L.in=T.type\} T→int{T.type=integer}T \to \mathbf{int} \quad \{T.type=\text{integer}\} L→L1,id{L1.in=L.in; addtype(id.entry,L.in)}L \to L_1,\mathbf{id} \quad \{L_1.in=L.in;\ \text{addtype}(\mathbf{id}.entry,L.in)\} L→id{addtype(id.entry,L.in)}L \to \mathbf{id} \quad \{\text{addtype}(\mathbf{id}.entry,L.in)\} For declaration int a, b, first T.type=integerT.type=\text{integer}, so L.in=integerL.in=\text{integer}. This inherited value is passed to the inner list: L1.in=integerL_1.in=\text{integer}, and both a and b are entered in the symbol table with type integer.
The attribute L.inL.in is inherited because it flows downward from the parent production.

Example 3: Dependency graph and topological order.
Production: A→B C DA \to B\,C\,D Rules: B.in=A.in,C.in=B.out,D.in=C.out,A.out=B.out+D.out.B.in=A.in,\quad C.in=B.out,\quad D.in=C.out,\quad A.out=B.out+D.out. Assume B.outB.out depends on B.inB.in, C.outC.out on C.inC.in, and D.outD.out on D.inD.in. A valid order is: A.in, B.in, B.out, C.in, C.out, D.in, D.out, A.out.A.in,\ B.in,\ B.out,\ C.in,\ C.out,\ D.in,\ D.out,\ A.out. Every edge points forward in this order: A.in→B.in→B.out→C.in→C.out→D.in→D.out→A.outA.in\to B.in\to B.out\to C.in\to C.out\to D.in\to D.out\to A.out, and B.out→A.outB.out\to A.out is also satisfied. Other topological orders may exist if they respect all edges.

Example 4: Numeric L-attributed evaluation.
Definitions: S→A B{A.in=1; B.in=A.out; S.val=B.out}S \to A\,B \quad \{A.in=1;\ B.in=A.out;\ S.val=B.out\} A→a{A.out=A.in+2}A \to a \quad \{A.out=A.in+2\} B→b{B.out=B.in∗3}B \to b \quad \{B.out=B.in*3\} Evaluation: A.in=1A.in=1, so A.out=1+2=3A.out=1+2=3. Then B.in=A.out=3B.in=A.out=3, so B.out=3∗3=9B.out=3*3=9. Finally S.val=B.out=9S.val=B.out=9.

Example 5: Statement-type check.
Statement I: An S-attributed definition can be evaluated by a postorder traversal.
Statement II: An L-attributed definition can be evaluated by a single left-to-right depth-first traversal.
Both statements are true. S-attributed definitions have only synthesized attributes, so children are evaluated before parents. L-attributed definitions satisfy the left-dependency condition, so inherited attributes can be passed downward and synthesized attributes returned upward in one left-to-right depth-first pass.

Comparison

ConceptMeaningEvaluation directionTypical parser
Synthesized attributeComputed from childrenBottom-upLR or postorder
Inherited attributeComputed from parent and/or left siblingsTop-down or left-to-rightLL or left-to-right DFS
S-attributed definitionOnly synthesized attributesPostorderBottom-up LR
L-attributed definitionInherited attributes depend only on left/parent informationOne left-to-right depth-first passTop-down LL
General SDDArbitrary dependenciesDependency graph and topological sortGeneral parser plus evaluator
Look-alike pairKey difference
S-attributed vs L-attributedS-attributed has only synthesized attributes; L-attributed may also have inherited attributes with restricted dependencies. Every S-attributed is L-attributed, not conversely.
Synthesized vs inheritedSynthesized comes from children; inherited comes from parent or left siblings.
Dependency graph edge u→vu\to vvv depends on uu, so uu is evaluated first.
Cyclic dependency graph vs DAGCyclic has no topological order; DAG always has at least one topological order.
Annotated parse tree vs ordinary parse treeAnnotated tree shows attribute values; ordinary tree shows only grammar symbols.

Revision summary

  • Synthesized: children to parent. Inherited: parent or left sibling to node.
  • S-attributed = only synthesized.
    L-attributed = inherited attributes depend only on left and parent information.
  • Every S-attributed definition is L-attributed; the reverse is not true.
  • An L-attributed definition is evaluated by one left-to-right depth-first pass.
  • An S-attributed definition is evaluated by postorder and fits bottom-up LR parsing.
  • Dependency edge u→vu\to v means uu before vv.
  • Cyclic dependency graph means no valid topological order and no unique attribute value.
  • Annotated parse tree = parse tree plus attribute values.
  • Semantic rule is a defining equation; semantic action is an executable code fragment.
  • For L-attributed definitions, an inherited attribute of a right-side symbol cannot depend on a synthesized attribute to its right.
  • The leftmost symbol of a production may use inherited attributes of the left side, not synthesized attributes of the left side.
  • Check attribute kind first, then check dependency direction, then check for cycles.

Previous-year exam insights

  • Direct concept questions ask whether a given attribute is synthesized or inherited, or whether a definition is S-attributed or L-attributed.
  • Statement I/II questions test the relation: every S-attributed definition is L-attributed, but an L-attributed definition may contain inherited attributes.
  • Match-the-list questions pair terms such as synthesized attribute, inherited attribute, dependency graph, topological sort, and S-attributed definition with their definitions.
  • Assertion-reason questions test cyclic dependency graphs: without a topological order, no unique evaluation order exists.
  • Numeric questions ask for a value after tracing inherited and synthesized attributes through a small production set; always evaluate left-to-right and downward first.
  • Order questions ask for a valid topological evaluation sequence; check every dependency edge before choosing an option.
  • A common trap is confusing inherited with synthesized; inherited does not flow upward from children in the same way.
  • Another trap is thinking every attribute grammar can be evaluated in one pass.
    Only S-attributed and L-attributed special cases guarantee simple traversal orders.
  • LR parsing is naturally suited to synthesized attributes because reductions process children before the parent; LL parsing is naturally suited to L-attributed definitions.

Practice at PARIKSHA

You are ready for the PARIKSHA tests of this sub-topic when you can:

  1. Define synthesized and inherited attributes without mixing their directions.
  2. Decide whether a given semantic rule is allowed in an S-attributed definition.
  3. Test an L-attributed definition by checking each inherited attribute against the left/parent rule.
  4. Draw a dependency graph for a small production and identify all edges correctly.
  5. Perform a topological sort of an acyclic dependency graph and write a valid evaluation order.
  6. Recognize a cyclic dependency graph and explain why no unique evaluation order exists.
  7. Trace a small S-attributed grammar on an expression and compute the final value.
  8. Trace a small L-attributed grammar with inherited type or numeric information and compute the final result.
  9. Distinguish an annotated parse tree from an ordinary parse tree.
  10. Explain why S-attributed fits bottom-up LR evaluation and L-attributed fits left-to-right top-down evaluation.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2