Compiler Design and Construction - Old Questions
6. Describe the L- attributed definitions. How L-attributed definitions are evaluated?
An syntax directed definition that uses both synthesized and inherited attribute with restriction of not taking values from right siblings is called L-attributed definition.
Mathematically,
A syntax-directed definition is L-attributed if each inherited attribute of Xj on the right side of A → X1 X2 … Xn depends only on
- The attributes of the symbols X1, X2, …, Xj-1
- The inherited attributes of A
Consider a production: S → ABC
Here, S can take values from A, B, and C (synthesized). 'A' can take values from S only. B can take values from S and A. C can get values from S, A, and B.
L – attributed definitions allow for a natural order of evaluating attributes: depth-first and left to right. Every S-attributed syntax-directed definition is also L-attributed and the above rule applied only for inherited attributes.
Procedure for Depth First Evaluation of L – Attributed Definitions:
Procedure dfvisit(n: node); //depth-first evaluation
for each child m of n, from left to right do
evaluate inherited attributes of m;
dfvisit(m);
evaluate synthesized attributes of n
Example:
A → XY
X.i = A.i
Y.i = X.s
A.s = Y.s