Example 2 is a more practical one; it might be used by a symbol table routine to attach type information to the variables in a declaration.
EXAMPLE 2 Assigning declaration types to variables
Consider the following attribute grammar:
Real A,B,Q Integer X,Y,Q
and the following grammar:
DeclarationType List List
Variable List
List , Variable Type
Real | Integer
Adding Attributes,
DeclarationType List List.Class := Type.Class
ListVariable Variable.Class := List.Class
List0List1, Variable List1.Class := List0.Class
Variable.Class := List0.Class
TypeReal | Integer Type.Class := LexValue(Type)
where LexValue is the value of the token (Real or Integer) found by the lexical analyzer. This attribute, Class, for the nonterminal Type in the last production, is said to be intrinsic because its value is given, not computed. There is again only one attribute, Class. It is an inherited attribute since its value is computed while going down the tree. This can be seen by noticing that the semantic functions compute the attribute for the grammar symbols on the right-hand side of production in terms of the values on the left-hand side. For example, in the semantic function:
Variable.Class := List.Class
The nonterminal "Variable" appears on the right-hand side of the production:
List Variable
while the nonterminal "List" appears on the left-hand side.
The parse tree before attribute evaluation for the first line above is:
Send
questions and comments to: Karen Lemone