Sub topic 20
Syntax-directed definitions: synthesized and inherited attributes
This chapter assumes you can read a context-free grammar, draw a parse tree, and trace a derivation. It also assumes you know what a symbol table is. Later chapters cover dependency graphs, S-attributed and L-attributed classes, and translation schemes. Here we only define attributes and learn to tell the two kinds apart.
What a syntax-directed definition is
A parse tree records structure but not meaning. A syntax-directed definition (SDD) attaches meaning to a grammar in two parts:
- Each grammar symbol gets a set of attributes. An attribute is a named value such as
val,type, orplace. - Each production gets semantic rules. A semantic rule computes an attribute of a symbol in that production from other attributes in the same production.
A parse tree whose nodes show attribute values is an annotated parse tree. Computing those values is called evaluating the SDD. An SDD with no side effects in its rules is an attribute grammar.
Synthesized attributes
An attribute of a nonterminal A at a parse-tree node N is synthesized if it is defined by a rule of the production at N, where A is the head. The rule may use only attributes of N's children and attributes of N itself. Information flows up the tree.
E -> E1 + T { E.val = E1.val + T.val }
E -> T { E.val = T.val }
T -> T1 * F { T.val = T1.val * F.val }
T -> F { T.val = F.val }
F -> ( E ) { F.val = E.val }
F -> digit { F.val = digit.lexval }Terminals have only synthesized attributes, and the lexer supplies them. digit.lexval is the numeric value of the lexeme. The parse tree has no rule that defines a terminal's attributes.
The annotated tree for 2 + 3 * 4 shows values moving up from the leaves:
Inherited attributes
An attribute of a nonterminal B at node N is inherited if it is defined by a rule of the production at N's parent, where B appears in the body. The rule may use attributes of N's parent, N's siblings, and N itself. Information flows down or sideways.
The classic example passes a declared type to every identifier in a list:
D -> T L { L.in = T.type }
T -> int { T.type = integer }
T -> float { T.type = float }
L -> L1 , id { L1.in = L.in; addType(id.entry, L.in) }
L -> id { addType(id.entry, L.in) }Here T.type is synthesized. L.in is inherited, because the rule L.in = T.type sits in the production D -> T L, and L appears in the body of that production. The rule in L -> L1 , id copies the inherited value one level down to L1.
The following Python code evaluates the same definition on float a, b, c. T_type computes a synthesized value. The inh argument of L carries the inherited attribute.
def T_type(node): # T -> int | float ; synthesized T.type
return node[1]
def L(node, inh, table): # inh is the inherited attribute L.in
if node[0] == "id": # L -> id
table[node[1]] = inh
return
_, sub, name = node # L -> L1 , id
L(sub, inh, table) # L1.in = L.in
table[name] = inh
def D(node, table): # D -> T L
_, t, lst = node
L(lst, T_type(t), table) # L.in = T.type
tree = ("D", ("T", "float"),
("list", ("list", ("id", "a"), "b"), "c"))
symtab = {}
D(tree, symtab)
print(symtab) # {'a': 'float', 'b': 'float', 'c': 'float'}Comparing the two kinds
| Property | Synthesized | Inherited |
|---|---|---|
| Defined by the rule of | The production where the symbol is the head | The production where the symbol is in the body |
| Computed from | Children and the node itself | Parent, siblings, and the node itself |
| Direction of flow | Up | Down or sideways |
| Example | `E.val`, `T.type` | `L.in`, the left operand passed into `E'` |
Rules and restrictions
- A terminal has no inherited attributes. Its attributes come from the lexer.
- The start symbol normally has no inherited attributes, because it has no parent to define them. If it does, the definition must say where the initial values come from.
- Each attribute occurrence in a production must be defined by exactly one rule. A synthesized attribute of the head and an inherited attribute of a body symbol are the ones the production must define.
- A rule may call a function with side effects, such as
addType. The SDD still has to fix an order in which these effects run. Attribute evaluation order is the subject of the next chapter.
Why inherited attributes matter
Inherited attributes appear when the grammar does not mirror the meaning. The grammar after left-recursion removal is the standard case:
E -> T E' { E'.inh = T.val; E.val = E'.syn }
E' -> + T E1' { E1'.inh = E'.inh + T.val; E'.syn = E1'.syn }
E' -> ε { E'.syn = E'.inh }The left operand has to travel to E' as an inherited value, because the structure no longer puts it under the +. The inh arguments of Ep and Tp in the recursive descent parser from earlier chapters are exactly these attributes.
Common errors
- Classifying by where the value is used instead of where the rule is written.
L.in = T.typemakesL.ininherited even though it uses a synthesized value. - Calling an attribute synthesized because a rule for a child uses it. The test is which symbol the rule defines, and in which production.
- Forgetting that a rule for a body symbol's inherited attribute may use a sibling to its left or right. Some definitions then become circular, so the dependency graph must be checked.
- Writing
E.val = E1.val + T.valbut leavingE -> Twithout a rule. Every production needs rules for the attributes it must define.
Exercise
In the production S -> A B with rules B.i = A.s and S.t = B.s, classify each of A.s, B.i, B.s, and S.t. Assume A.s is defined in A's own productions, and B.s in B's.
Show answer
A.s and B.s are synthesized, because each is defined by rules in the production where its symbol is the head. S.t is synthesized, because S is the head of S -> A B. B.i is inherited, because it is defined at the parent's production and B is in the body. It depends on the sibling A.
Exercise
Annotate 3 * 2 + 1 with val using the SDD in this chapter, and give the val at the root.
Show answer
T.val for 3 * 2 is 3 * 2 = 6. The left E is 6, the right T is 1, and the root E.val = 6 + 1 = 7.