Download presentation
Presentation is loading. Please wait.
Published byLoreen Lynch Modified over 9 years ago
1
Compiler Construction Sohail Aslam Lecture 31
2
2 Beyond Syntax These questions are part of context-sensitive analysis Answers depend on values, not parts of speech Answers may involve computation
3
3 Beyond Syntax These questions are part of context-sensitive analysis Answers depend on values, not parts of speech Answers may involve computation
4
4 Beyond Syntax These questions are part of context-sensitive analysis Answers depend on values, not parts of speech Answers may involve computation
5
5 Beyond Syntax How can we answer these questions? Use formal methods Context-sensitive grammars Attribute grammars
6
6 Beyond Syntax How can we answer these questions? Use formal methods Context-sensitive grammars Attribute grammars
7
7 Beyond Syntax How can we answer these questions? Use formal methods Context-sensitive grammars Attribute grammars
8
8 Beyond Syntax Use ad-hoc techniques Symbol tables ad-hoc code
9
9 Attribute Grammars A CFG is augmented with a set of rules Each symbol in the derivation has a set of values or attributes Rules specify how to compute a value for each attribute
10
10 Attribute Grammars A CFG is augmented with a set of rules Each symbol in the derivation has a set of values or attributes Rules specify how to compute a value for each attribute
11
11 Attribute Grammars A CFG is augmented with a set of rules Each symbol in the derivation has a set of values or attributes Rules specify how to compute a value for each attribute
12
12 Attribute Grammars grammar for signed binary numbers (SBN) Number → Sign List Sign → + – List → List Bit | Bit Bit → 0 1
13
13 Attribute Grammars derivation for “ –1 ” Number → Sign List → – List → – Bit → – 1
14
14 Attribute Grammars For “-101” Number → Sign List → Sign List Bit → Sign List 1 → Sign List Bit 1 → Sign List 0 1 → Sign Bit 0 1 → Sign 1 0 1 → – 1 0 1
15
15 Attribute Grammars For an attributed version of SBN, the following attributes are needed SymbolAttributes Number val Sign neg List pos, val Bit pos, val
16
16 Attribute Grammars We will add rules to compute decimal value of a signed binary number
17
17 Attribute Grammars ProductionsAttribution Rules Number → Sign List List.pos 0 if Sign.neg then Number.val – List.val else Number.val List.val Sign → + Sign.neg false Sign → – Sign.neg true
18
18 Attribute Grammars ProductionsAttribution Rules Number → Sign List List.pos 0 if Sign.neg then Number.val – List.val else Number.val List.val Sign → + Sign.neg false Sign → – Sign.neg true
19
19 Attribute Grammars ProductionsAttribution Rules List 0 → List 1 Bit List 1.pos List 0.pos + 1 Bit.pos List 0.pos List 0.val List 1.val + Bit.val List → Bit Bit.pos List.pos List.val Bit.val Bit → 0 Bit.val 0 Bit → 1 Bit.val 2 Bit.pos
20
20 Attribute Grammars Attributes are associated with nodes in parse tree Rules are value assignments associated with productions
21
21 Attribute Grammars Attributes are associated with nodes in parse tree Rules are value assignments associated with productions
22
22 Attribute Grammars Rules and parse tree define an attribute dependence graph Graph must be acyclic
23
23 Attribute Grammars Number Sign List Bit – 1 List.pos 0 List.val Bit.val = 1 Number.val – List.val = –1 Bit.pos 0 Bit.val 2 Bit.pos = 1 Sign.neg true
24
24 AttributesAttributes Attributes are distinguished based on the direction of value flow 1.Synthesized attributes 2.Inherited attributes
25
25 Synthesized Attributes Attributes of a node whose values are defined wholly in terms of attributes of node’s children and from constants are called synthesized attributes
26
26 Synthesized Attributes Values used to compute synthesized attributes flow bottom-up in the parse tree Good match to LR parsing
27
27 Inherited Attributes Attributes whose values are defined in terms of node’s own attributes, node’s siblings node’s parent Values flow top-down and laterally in the parse tree
28
28 Inherited Attributes Attributes whose values are defined in terms of node’s own attributes, node’s siblings node’s parent Values flow top-down and laterally in the parse tree
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.