In Aho et al's Compilers: Principles, Techniques, and Tools on page 305 it says "Terminals can have synthesized attributes, but not inherited attributes." Here's my hang up: if synthesized attributes are attributes that can be computed based on a node's children, and inherited attributes can be computed based on a node's parent and siblings, then this feels wrong to me because since the terminals would be the leaves of the parse tree they wouldn't have any children. If they don't have any children then they shouldn't be able to have synthesized attributes. Similarly it seems that since they're leaves it would be likely they would have parent nodes and, as a result, could have inherited attributes. If someone could point out where I'm going wrong here that would be awesome.
3 Answers
When I first read that in the dragon book, I was also confused. But if you think about it for a moment, it will become clear. The terminals synthesized attributes don't come from the parser; rather they come from the lexer. To give an example suppose you have a terminal digit (example taken from the dragon book). digit has the synthesized attribute lexval
. This synthesized attribute does not come from the parser. It comes from the lexer instead. It should be pretty clear why terminals can't have inherited attributes :)

- 3,097
- 2
- 14
- 45
-
If you want a terminal to have an inherited attribute, you can simulate that with a non-terminal whose only production is a right-hand side with the terminal as its only symbol. So the restriction is not very important in practice, but it is convenient for some of the theoretical statements the authors want to make. – rici Jun 10 '20 at 23:40
Most SDD (and so SDTS) computations are about attributes of Non Terminals, which is explicitly specified in the previous page of the Dragon book. We can think that the attributes of Terminals are synthesized from the symbol table maintained by the scanner and the other compiler phases.
For terminal values , it is coming directly from LEX . For example :
F -> DIGIT
DIGIT -> [0-9] /* defined in the lex file */
The terminal DIGIT , which has an attribute named lexval , depends on lex file for it's value and not it's parent F . As a result , lexval will be considered as synthesized attribute .

- 21
- 4