Differences btw S and L attributes.pptxbjtekh

23211a0533 0 views 12 slides Oct 07, 2025
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

rnbghidltfkn,k


Slide Content

Differences between s and l attributes B. Adbhutha 23211A0533

contents Introduction to SDT Attributes in SDD Synthesized attribute Inherited attribute Differences between S and L attributes Summary

Syntax directed translation Syntax-Directed Translation (SDT) attaches semantic actions to grammar productions to compute attributes for tasks like type checking and code generation during parsing. SDT is built on context-free grammars; attribute values flow along the parse tree according to rules, enabling systematic evaluation during top-down or bottom-up parsing.

Attributes in SDD An attribute grammar associates attributes with grammar symbols and rules that compute them; evaluation order follows the parse tree and dependency constraints. Two attribute kinds are central: synthesized attributes (flow up from children) and inherited attributes (flow from parent/left siblings), forming the basis of S- and L-attributed SDTs.

Synthesized attribute A synthesized attribute at node n is computed solely from attributes at n’s children and thus naturally evaluates bottom-up from leaves to root. Synthesized attributes may belong to terminals or nonterminals and are compatible with both S-attributed and L-attributed SDTs.

Inherited attribute An inherited attribute at node n is computed from attributes of n’s parent and/or n’s siblings; in L-attributed SDDs it is restricted to parent and left siblings for evaluability. Inherited attributes are only for nonterminals and are typically passed top-down or sideways (from left sibling) during a left-to-right traversal.

S attribute An SDT that uses only synthesized attributes is S-attributed; no inherited attributes appear in any production’s actions. Evaluation is bottom-up and works cleanly with shift–reduce/bottom-up parsers; any S-attributed SDD is also L-attributed as a subset. Example: For grammar E → E1 + T, define E.val = E1.val + T.val , where E1.val and T.val come from child nodes; values rise to the parent E. Because only synthesized attributes appear, semantic actions can be placed at the right end and evaluated during reduce actions in bottom-up parsing.

L attribute An L-attributed SDT allows both synthesized and inherited attributes with the restriction that each inherited attribute of Xj in A → X1 X2 … Xn may depend only on attributes of X1…Xj−1 and inherited attributes of A. This constraint ensures attributes can be evaluated in one depth-first, left-to-right traversal, fitting top-down parsing such as recursive descent. Example: For S → A B, pass context with A.inh = f( S.inh ) and compute B.synth = g( A.synth ), mixing inherited and synthesized flows while respecting left-to-right dependency. No symbol may inherit from a right sibling; e.g., in S → A B C, C can depend on S, A, and B, but B cannot depend on C for inherited attributes.

Differences between s and l attributes Aspect S-Attributed SDT L-Attributed SDT Attribute kinds Only synthesized; no inherited Synthesized + inherited under left-to-right restriction Typical parsing Bottom-up/shift–reduce friendly Top-down/recursive-descent friendly Evaluation order Bottom-up from leaves to root One depth-first, left-to-right traversal Action placement Often rightmost on RHS for reduce steps Actions may be anywhere on RHS if dependencies respected Dependency limits Synthesized from children only Inherited from parent and left siblings; not right sibling Inclusion relation Subset of L-attributed Superset; includes all S-attributed

example Arithmetic evaluation (S-attributed): E → E1 + T with E.val = E1.val + T.val ; T.val derives from factors and digits, all synthesized upward. Context-sensitive passing (L-attributed): For declarations S → T L, pass T.type down as L.inh , and compute L.synth as a list of typed identifiers; inherited flows from parent to left sibling to rightward symbols.

summary Use S-attributed when semantics can be computed purely bottom-up from children and integration with bottom-up parsing is desired. Use L-attributed when semantic context must flow from parent/left siblings to a symbol, enabling top-down evaluation while forbidding right-sibling dependencies.

Thankyou 