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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CMPSC 160 Translation of Programming Languages Lecture 11: 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 → E + T | E – T | T T → T * F | T / F | 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 Expression Interpreter Productions Semantic Rules S → E S.val ← E.val E → E 1 + T E .val ← E 1 .val + T.val | E 1 – T E .val ← E 1 .val – T.val | T E .val ← T.val T → T 1 * F T .val ← T 1 .val * F.val | T 1 / F T .val ← T 1 .val / F.val | F T .val ← F.val F → num F.val ← 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 Expression Interpreter Productions Semantic Rules S → E S.val ← E.val E → E 1 + T E .val ← E 1 .val + T.val | E 1 – T E .val ← E 1 .val – T.val | T E .val ← T.val T → T 1 * F T .val ← T 1 .val * F.val | T 1 / F T .val ← T 1 .val / F.val | F T .val ← F.val F → num F.val ← 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 * F ⇒ E – T * num ⇒ E – F * num ⇒ E – num * num ⇒ T – num * num ⇒ F – num * num ⇒ num – num * num T E- F num T F F T For “10 – 2 * 3” E * num num val = 2 val = 3 val = 10 val = 10 val = 10 val = 10 val = 2 val = 3 val = 2 val = 2 * 3 = 6 val = 10 – 6 = 4 Annotated parse tree (values of the attributes are evaluated) dependency arcs Attribute Grammars • Attributes are associated with nodes in the 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) only once, using local information • Identical terms in a production are labeled for uniqueness – E → E + T becomes E → E 1 + T • 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 •...
View Full Document

This note was uploaded on 02/19/2012 for the course ENGR 361 taught by Professor Drexel during the Spring '12 term at Bloomsburg.

Page1 / 38

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

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