S and L Attribute in Compiler
In this class, We discuss S and L Attribute in Compiler.
For Complete YouTube Video: Click Here
The reader should have prior knowledge of synthesized and inherited attributes. Click Here.
The reader should know our previous class failure of attribute evaluation.
First, We refresh the concept of bottom-up and top-down parsing.
In bottom-up, we find the string and reduce it to the matching production.
T – *FT’
*FT’ is reduced to T.
In the top-down approach, we expand the production T and find the input matching string.
The top-down follows left most derivation. Ie. Top-down left to right.
S Attribute:
We said it as S Attribute when all the attributes synthesized attributes.
Synthesized attributes take values from their child or themselves.
So S attribute is best suited for bottom-up parsing.
We were not forming a loop when all attributes synthesized attributes.
L Attribute:
The L attribute can have synthesized or inherited attributes. But follow the below conditions.
1) Inherited attributes should take values from parent attributes or left sibling attributes.
L Attribute is suitable for top-down parsing.
In top-down, we follow left-right evaluation.
So to avoid cycles in evaluation, we can take values from the left sibling.
Right sibling is not allowed to use in L Attribute.
Example:
T – FT’ {T’.inh = F.val}
T’ – *FT1′ { T1.inh = T’.inh * F.val}
T’.inh is using from a left sibling.