Synthesized Attributes for Evaluating Arithmetic Expressions (E→E+T|T, T→T*F|F)
Synthesized Attributes (Semantic Actions) — Syntax-Directed Translation
We are given the expression grammar:
A standard attribute-grammar solution is to attach a synthesized numeric attribute to each nonterminal—typically , , and —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 level, not at .
- Left recursion (, ) corresponds to left-associative chaining in the parse tree.
Footnotes
-
Attribute grammar - Wikipedia - Defines synthesized attributes as computed from child values (bottom-up). ↩
-
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 .
- lexval on terminal digit.
Semantic rules for each production:
For
For
For
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
-
Attribute grammar - Wikipedia - Defines synthesized attributes as computed from child values (bottom-up). ↩
-
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
- 1Step 1
Tokens: digit(3), '+', digit(5), '*', digit(2). Set for each digit: , , .
- 2Step 2
The portion forms a , and the whole becomes .
- 3Step 3
Using : compute .
- 4Step 4
Using and : compute .
- 5Step 5
Using : .
- 6Step 6
The value of the expression is .
Annotated syntax tree / parse tree (with computed attributes)
One syntax-tree shape that matches the grammar’s precedence is:
- Root:
- Root production:
- Left child:
- Right child:
- left of :
- right of :
Bottom-up attribute computation corresponds exactly to:
Synthesized attributes flow upward from to to . 2
Footnotes
-
Attribute grammar - Wikipedia - Defines synthesized attributes as computed from child values (bottom-up). ↩
-
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 nodes, and under nodes. This guarantees you evaluate before adding to the left value."
type="warning" title="Warning: Don’t compute with a flat left-to-right scan" content="Do not evaluate strictly left-to-right as . The grammar’s nonterminal layering ( vs ) 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
Which synthesized attribute(s) should be used to compute the value of the expression from this grammar?
Explore Related Topics
Constructing and Using an SLR(1) Parser for the Grammar $E \rightarrow E+T \mid T$, $T \rightarrow T*F \mid F$, $F \rightarrow (E) \mid id$
Attribute Evaluation in Syntax Trees: Dependency Graphs, Evaluation Order, and an Example
Ambiguity Analysis of an Expression Grammar with +, *, Parentheses, and id