{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec17mod

# lec17mod - CS415 Compilers Context-sensitive Analysis...

This preview shows pages 1–7. Sign up to view the full content.

CS415 Compilers Context-sensitive Analysis Attribute Grammars, Syntax-Directed Translation Schemes These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 17 2 cs415, spring 10 Attribute Grammars What is an attribute grammar? A context-free grammar augmented with a set of rules Each symbol in the derivation has a set of values, or attributes The rules specify how to compute a value for each attribute Number Sign List Sign + |– List List Bit |B i t Bit 0 |1 Example grammar This grammar describes signed binary numbers We would like to augment it with rules that compute the decimal value of each valid input string
Lecture 17 3 cs415, spring 10 Attribute Grammars Add rules to compute the decimal value of a signed binary number Productions Attribution Rules Numbe r Sign List List.pos 0 If Sign.neg then Number.val – List.val else Number.val List.val Sign + Sign.neg false |– Sign.neg true 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 | Bit.pos List.pos List.val Bit.val 0 Bit.val 0 |1 Bit.val 2 Bit.pos Symbol Attri butes Number val neg List pos, val

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 17 4 cs415, spring 10 Attribute Grammars Productions Attribution 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 pos val pos val pos val LIST 0 LIST 1 BIT • semantic rules define partial dependency graph • value flow top down or across: inherited attributes • value flow bottom-up: synthesized attributes
Lecture 17 5 cs415, spring 10 Attribute Grammars pos val pos val pos val LIST 0 LIST 1 BIT • semantic rules associated with production A →α have to specify the values for all - synthesized attributes for A (root) - inherited attributes for grammar symbols in α (children) rules must specify local value flow! • terminals can be associated with values returned by the scanner. These input values are associated with a synthesized attribute. • Starting symbol cannot have inherited attributes. Note:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 17 6 cs415, spring 10 Using Attribute Grammars Attribute grammars can specify context-sensitive actions Take values from syntax Perform computations with values Insert tests, logic, … Synthesized Attributes Use values from children & from constants S-attributed grammars: synthesized attributes only Evaluate in a single bottom-up pass Good match to LR parsing Inherited Attributes Use values from parent, constants, & siblings L-attributed grammars: A X 1 X 2 … X n and each inherited attribute of X i depends on - attributes of X 1 X 2 … X i-1 , and - inherited attributes of A Evaluate in a single top-down pass (left to right) Good match for LL parsing S-attributed L-attributed
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 21

lec17mod - CS415 Compilers Context-sensitive Analysis...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online