A at a parse-tree node N is defined by a semantic rule associated with the production at N.
A synthesized attribute at node N is defined only in terms of attribute values at the children of N and at N itself.
The following attribute grammar translates expressions consisting of digits separated by plus or minus signs into postfix notation.
Each nonterminal has a string-valued attribute T that represents the postfix notation for the expression generated by that nonterminal in a parse tree.
The symbol ‘||’ in the semantic rule is the operator for string concatenation.
| Production | Semantic Rules | |
|---|---|---|
| 1. | <expr> ::= <expr1> + <term> |
T(<expr>) = T(<Eexpr1>) || T(<term>) || '+' |
| 2. | <expr> ::= <expr1> – <term> |
T(<expr>) = T(<expr1>) || T(<term>) || '–' |
| 3. | <expr> ::= <term> |
T(<expr>) = T(<term>) |
| 4. | <term> ::= 0 |
T(<term>) = '0' |
| 5. | <term> ::= 1 |
T(<term>) = '1' |
| … | … | … |
| 13. | <term> ::= 9 |
T(<term>) = '9' |
b at a node depends on at attribute c, then the semantic rule for b at that node must be evaluated after the semantic rule that defines c.
