AttributeGrammar

AttributeGrammar - Context-sensitive Analysis Attribute...

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

View Full Document Right Arrow Icon
cs5363 1 Context-sensitive Analysis Attribute Grammar And Type Checking
Background image of page 1

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

View Full DocumentRight Arrow Icon
cs5363 2 Context-Sensitive Analysis To understand the input computation, a compiler/interpreter need to discover The types of values stored in each variable The types of argument and return values for each function The representation/interpretation of each value The memory space allocated for each variable The scope and live range of each variable Static definition of variables: variable declarations Compilers need properties of variables before translation Use symbol tables to keep track of variable information Context-sensitive analysis Determine properties of program constructs E.g., CFG cannot enforce all variables are declared before used
Background image of page 2
cs5363 3 Syntax-Directed Translation Compilers translate language constructs Need to keep track of relevant information Attributes: relevant information associated with a construct Attribute grammar (syntax-directed definition) Associate a collection of attributes with each grammar symbol Define actions to evaluate attribute values during parsing e ::= n | e+e | e e | e * e | e / e Attributes for expressions: type of value: int, float, double, char, string,… type of construct: variable, constant, operations, … Attributes for constants: values Attributes for variables: name, scope Attributes for operations: arity, operands, operator,…
Background image of page 3

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

View Full DocumentRight Arrow Icon
cs5363 4 Attribute Grammar Associate a set of attributes with each grammar symbol Associate a set of semantic rules with each production Specify how to compute attribute values of symbols Systematic evaluation of context information through traversal of parse tree (or abstract syntax tree) e ::= n | e+e | e e | e * e | e / e e.val=305 e.val=5 e.val=300 e.val=15 5 + * e.val=20 15 20 Annotated parse tree for 5+15*20: e.val = e1.val [/] e2.val e ::= e1 / e2 e.val = e1.val [*] e2.val e ::= e1 * e2 e.val = e1.val [-] e2.val e ::= e1 - e2 e.val = e1.val [+] e2.val e ::= e1 + e2 e.val = n.val e ::= n Semantic rules production
Background image of page 4
cs5363 5 e ::= n | e+e | e e | e * e | e / e e.val = e1.val [/] e2.val e ::= e1 / e2 e.val = e1.val [*] e2.val e ::= e1 * e2 e.val = e1.val [-] e2.val e ::= e1 - e2 e.val = e1.val [+] e2.val e ::= e1 + e2 e.val = n.val e ::= n Semantic rules production e.val=305 e.val=5 e.val=300 e.val=15 5 + * e.val=20 15 20 Synthesized Attribute Definition An attribute is synthesized if in the parse tree, Attributes of parents are determined from those of children S-attributed definitions Syntax-directed definitions with only synthesized attributes Can be evaluated through post-order traversal of parse tree
Background image of page 5

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

View Full DocumentRight Arrow Icon
cs5363 6 Inherited Attribute Definition An attribute is inherited if The attribute value of a parse-tree node is determined from attribute values of its parent and siblings Addtype(id.entry,L.in) L::= id L1.in := L.in Addtype(id.entry,L.in) L::=L1 ,id T.type:=real T::= real
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 39

AttributeGrammar - Context-sensitive Analysis Attribute...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online