cs160-lec12

cs160-lec12 - CMPSC 160 Translation of Programming...

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

View Full Document Right Arrow Icon
1 CMPSC 160 Translation of Programming Languages Lecture 12: Attribute Grammars Attribute Grammars What is an attribute grammar? A context-free grammar augmented with a set of semantic rules Each symbol in the derivation has a set of values, or attributes The semantic rules specify how to compute a value for each attribute Example grammar: S E E + T | | T * F | / | F num We want to write an expression interpreter One way to do this is to augment the expression grammar with semantic rules that compute the value of each valid expression
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Expression Interpreter Productions Semantic Rules S E S.val E.val 0 1 + T .val E 1 .val + T.val | E 1 .val T.val | T T.val * F .val * F.val | 1 / / F.val | F F.val num num .val Notice that: Semantic rules use context information In this attribute grammar, attributes of grammar symbols on the lhs are computed using the attributes of grammar symbols on the rhs (such attributes are called synthesized attributes ) The token num has a value attribute returned by the lexer To Evaluate an Expression S E E – T E – T * num E – F * num num * num T – num num F – num num num num num - num For “10 – 2 * 3” E * num num val = 2 val = 3 val = 10 val = 3 val = 2 * 3 = 6 – 6 = 4 Annotated parse tree (values of the attributes are evaluated) dependency arcs
Background image of page 2
3 Attribute Grammars Attributes are associated with nodes in parse tree (terminals and non- terminals) Productions are associated with semantic rules which define how to assign values to attributes An attribute is defined (computed) once, using local information Identical terms in a production are labeled for uniqueness E E + T becomes 0 1 + Rules and parse tree define an attribute dependence graph Dependence graph must be non-circular, otherwise it has no meaning This produces a high-level, functional specification (no side-effects) Synthesized attribute Depends on values from children Inherited attribute Depends on values from siblings and parent Attribute Grammars A X 1 X 2 ... X n Synthesized attributes : An attribute of A computed using attributes of X 1 , X 2 , . .. , X n Example: E 0 E 1 + T { E 0 .val E 1 .val + T.val } Inherited attributes : An attribute of a symbol on the rhs computed using attributes of A , X 1 , X 2 , . .. , X n Example:
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

cs160-lec12 - CMPSC 160 Translation of Programming...

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

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