Synthesized Attributes for Evaluating Arithmetic Expressions (E→E+T|T, T→T*F|F)

Synthesized Attributes for Evaluating Arithmetic Expressions (E→E+T|T, T→T*F|F)

Verified Sources
Nov 3, 2026

Synthesized Attributes (Semantic Actions) — Syntax-Directed Translation

We are given the expression grammar:

E→E+T∣T,T→T∗F∣F,F→digit.E \to E + T \mid T,\quad T \to T * F \mid F,\quad F \to \text{digit}.

A standard attribute-grammar solution is to attach a synthesized numeric attribute to each nonterminal—typically E.valE.val, T.valT.val, and F.valF.val—so the value of an expression is computed bottom-up in the parse tree. Synthesized attributes are computed from children and flow upward. 2

Key idea: represent the usual operator precedence/associativity:

  • ∗* binds tighter than ++ because it is defined at the TT level, not at EE.
  • Left recursion (E→E+TE\to E+T, T→T∗FT\to T*F) corresponds to left-associative chaining in the parse tree.

Footnotes

  1. Attribute grammar - Wikipedia - Defines synthesized attributes as computed from child values (bottom-up). ↩

  2. 6.0 Semantic Analysis Translation and Attribute Grammars - Lecture notes explaining synthesized attributes and bottom-up propagation in attribute grammars. ↩

Attribute grammar (S-attributed)

Use a single synthesized attribute:

  • val on each nonterminal E,TE, T.
  • lexval on terminal digit.

Semantic rules for each production:

For EE

  1. E→E+TE \to E + T
    • E.val:=E1.val+T.valE.val := E_1.val + T.val
  2. E→TE \to T
    • E.val:=T.valE.val := T.val

For TT

  1. T→T∗FT \to T * F
    • T.val:=T1.val∗F.valT.val := T_1.val * F.val
  2. T→FT \to F
    • T.val:=F.valT.val := F.val

For FF

  1. F→digitF \to digit
    • F.val:=digit.lexvalF.val := digit.lexval

This is an S-attributed translation scheme: every attribute on the LHS depends only on RHS children attributes, so it can be evaluated bottom-up. 2

Footnotes

  1. Attribute grammar - Wikipedia - Defines synthesized attributes as computed from child values (bottom-up). ↩

  2. 6.0 Semantic Analysis Translation and Attribute Grammars - Lecture notes explaining synthesized attributes and bottom-up propagation in attribute grammars. ↩

Annotated evaluation of $3 + 5 * 2$ using synthesized attributes

  1. 1
    Step 1

    Tokens: digit(3), '+', digit(5), '*', digit(2). Set F.valF.val for each digit: F3.val=3F_3.val=3, F5.val=5F_5.val=5, F2.val=2F_2.val=2.

  2. 2
    Step 2

    The 5∗25 * 2 portion forms a TT, and the whole becomes E=E+TE = E + T.

  3. 3
    Step 3

    Using T→T∗FT \to T * F: compute T52.val:=T5.val∗F2.val=5∗2=10T_{52}.val := T_5.val * F_2.val = 5 * 2 = 10.

  4. 4
    Step 4

    Using E→TE \to T and T→FT \to F: compute E3.val:=3E_3.val := 3.

  5. 5
    Step 5

    Using E→E+TE \to E + T: Eroot.val:=E3.val+T52.val=3+10=13E_{root}.val := E_3.val + T_{52}.val = 3 + 10 = 13.

  6. 6
    Step 6

    The value of the expression is E.val=13E.val = 13.

Annotated syntax tree / parse tree (with computed attributes)

One syntax-tree shape that matches the grammar’s precedence is:

  • Root: EE
  • Root production: E→E+TE \to E + T
  • Left child: E→T→F→digit(3)E \to T \to F \to digit(3)
  • Right child: T→T∗FT \to T * F
    • left of ∗*: T→F→digit(5)T \to F \to digit(5)
    • right of ∗*: F→digit(2)F \to digit(2)

Bottom-up attribute computation corresponds exactly to:

  • F.val(3)=3,  F.val(5)=5,  F.val(2)=2F.val(3)=3,\;F.val(5)=5,\;F.val(2)=2
  • T.val(5∗2)=5∗2=10T.val(5*2)=5*2=10
  • E.val(3+(5∗2))=3+10=13E.val(3+(5*2))=3+10=13

Synthesized attributes flow upward from FF to TT to EE. 2

Footnotes

  1. Attribute grammar - Wikipedia - Defines synthesized attributes as computed from child values (bottom-up). ↩

  2. 6.0 Semantic Analysis Translation and Attribute Grammars - Lecture notes explaining synthesized attributes and bottom-up propagation in attribute grammars. ↩

type="tip" title="Pro Tip: Check precedence structurally" content="If an expression has both ++ and ∗*, ensure the parse tree groups ∗* under TT nodes, and ++ under EE nodes. This guarantees you evaluate 5∗25*2 before adding to the left EE value."

type="warning" title="Warning: Don’t compute with a flat left-to-right scan" content="Do not evaluate strictly left-to-right as (3+5)∗2(3+5)*2. The grammar’s nonterminal layering (EE vs TT) encodes precedence; the attribute evaluation must follow the parse tree."

Synthesized attribute flow for 3 + 5 * 2

Values computed bottom-up at each nonterminal/root node.

Common questions about this attribute-grammar setup

Knowledge Check

Question 1 of 4
Q1Single choice

Which synthesized attribute(s) should be used to compute the value of the expression from this grammar?