Attribute Grammars Attribute Grammar is a Framework for
Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification. http: //cecs. wright. edu/~tkprasad/papers/Attribute-Grammars. pdf CS 7100 (Prasad) L 16 -7 AG 1
Hierarchy of approximations S* Regular (lexer) Context-free (parser) Context-sensitive (type-checker) “Correct” Programs (no run-time errors) CS 7100 (Prasad) L 16 -7 AG 2
Expresssive power Context-sensitive (type-checker) Languages Regular (lexer) Context-free (parser) CS 7100 (Prasad) L 16 -7 AG 3
Semantics of Bit Pattern : 0101110 . • ASCII Character “ ” • Number • Switch positions – 46 (if base 2) – 101110 (if base 10) – ON-OFF-ON-… • Fraction • Binary string “ 0101110” – 23/64 • Unary encoding “ 13” CS 7100 (Prasad) L 16 -7 AG 4
Motivation for Precise Specification • Capture subtly different semantics for the same syntax in various languages. § Arrays and strings. § Parameter passing mechanisms. § Scoping rules. § Primitive types vs Composite types. § Type equivalence. CS 7100 (Prasad) L 16 -7 AG 5
Attribute Grammars Formalism for specifying semantics based on context-free grammars (BNF) © Static semantics (context-sensitive aspects) • Type checking and type inference • Compatibility between procedure definition and call • Scope rules © Dynamic semantics • Associate attributes with terminals and non-terminals • Associate attribute computation rules with productions CS 7100 (Prasad) L 16 -7 AG 6
Synthesized Attributes N -> 0 N. val : = 0 N -> 1 N. val : = 1 N -> N 0 N. val : = 2* N. val N -> N 1 N. val : = 2* N. val + 1 CS 7100 (Prasad) L 16 -7 AG 7
Derivation Tree N N N 0 1 110 ~> 6 1 CS 7100 (Prasad) L 16 -7 AG 8
Derivation Tree N N N 0 1 110 ~> 6 1 CS 7100 (Prasad) L 16 -7 AG 9
Synthesized Attributes N. val : = 0 N. len : = 1 N -> 0 N -> 1 N. val : = 1 N. len : = 1 N -> 0 N N. val : = N. val N. len : = N. len + 1 N -> 1 N CS 7100 (Prasad) N. val : = N. val + 2^ N. len : = N. len + 1 L 16 -7 AG 10
Grammar Syntax Variations N -> 0 N -> 1 N -> 0 N N -> 1 N CS 7100 (Prasad) N : : = L 16 -7 AG 0 | 1 | 0 N | 1 N 11
Synthesized Attributes • Evaluation of a constant expression • Type Inference 5. 0 + 2 • Meta-level processing for compilergeneration: Determination of non-terminals that derive null string or terminal strings CS 7100 (Prasad) L 16 -7 AG 12
Inherited Attributes • Declaration and Use { int i, j, k; i : = i + j; } <assign-stm> -> <var> : = <expr> <var>. env : = <assign-stm>. env <expr>. env : = <assign-stm>. env CS 7100 (Prasad) L 16 -7 AG 13
Inherited Attributes • Coercion (automatic type conversion) Code Generation 5. 0 + 2 x + i coerce_int_to_real • Determination of un-initialized variables • Evaluation of an expression containing variables • Meta: Determination of reachable non-terminals CS 7100 (Prasad) L 16 -7 AG 14
• Attributes – Synthesized – Inherited A(X) S(X) I(X) • Attribute computation rules (Semantic functions) X 0 -> X 1 X 2 … Xn S(X 0) = f( I(X 0), A(X 1), A(X 2), …, A(Xn) ) I(Xj) = Gj( I(X 0), A(X 1), A(X 2), …, A(Xj-1)) for all j in 1. . n P( A(X 0), A(X 1), A(X 2), …, A(Xn) ) CS 7100 (Prasad) L 16 -7 AG 15
Information Flow inherited computed available synthesized . . . CS 7100 (Prasad) L 16 -7 AG 16
• Synthesized Attributes Pass information up the parse tree • Inherited Attributes Pass information down the parse tree or from left siblings to the right siblings • Attribute values assumed to be available from the context. • Attribute values computed using the semantic rules provided. The constraints on the attribute evaluation rules permit top-down left-to-right (one-pass) traversal of the parse tree to compute the meaning. CS 7100 (Prasad) L 16 -7 AG 17
Static Semantics E -> n | m E. type : = int E -> x | y E. type : = real E -> E 1 + E 2 E -> E 1 * E 2 CS 7100 (Prasad) E. type : = if E 1. type = E 2. type then E 1. type else real L 16 -7 AG 19
Executable Specification in Prolog type(i, int). type(x, real). type(+(E, F), T) : - type(E, T), type(F, T). type(+(E, F), real) : - type(E, T 1), type(F, T 2), T 1 = T 2. • Type Checking ? - type(+(i, x), real). • Type Inference ? - type(+(x, x), T). CS 7100 (Prasad) L 16 -7 AG 20
Static Semantics E E E -> -> -> CS 7100 (Prasad) n p | if then else | m q E. type : = int E. type : = bool E. type : = if ( E 0. type = bool ) Ù ( E 1. type = E 2. type ) then E 1. type else type error E 0 E 1 E 2 L 16 -7 AG 21
Fractions F ->. N F. val : = N. val N. pow : = 1 N -> 0 N -> 1 N. val : = 0 : = (1/2^N. pow) N -> 0 N N. pow N. val : = 1 + N. pow : = N. val N -> 1 N N. pow N. val : = 1 + N. pow : = N. val + (1/2^N. pow) CS 7100 (Prasad) L 16 -7 AG 23
Fractions (Alternate solution) F ->. N F. val : = N. val / 2 N -> 0 N -> 1 N. val : = 0 : = 1 N -> 0 N N. val : = N. val / 2 N -> 1 N N. val : = N. val / 2 + 1 CS 7100 (Prasad) L 16 -7 AG 24
Applications of Attribute Grammars • Compiler Generation – Top-down Parsers (LL(1)) • FIRST sets, FOLLOW sets, etc – Code Generation Computations • Type, Storage determination, etc • Databases – Optimizing Bottom-up Query Evaluation (Magic Sets) • Programming and Definitions CS 7100 (Prasad) L 16 -7 AG 25
An Extended Example • Distinct identifiers in a straight-line program. BNF <exp> : : = <var> | <exp> + <exp> <stm> : : = <var> : = <exp> | <stm> ; <stm> Attributes <var> <exp> <stm> symbol id set-of-symbols ids int num • Semantics specified in terms of sets (of identifiers). CS 7100 (Prasad) L 16 -7 AG 26
<exp> : : = <var> <exp>. ids = { <var>. id } <exp> : : = <exp 1> + <exp 2> <exp>. ids = <exp 1>. ids U <exp 2>. ids <stm> : : = <var> : = <exp> <stm>. ids ={ <var>. id } U <exp>. ids <stm>. num = | <stm>. ids = : : = <stm 1> ; <stm 2> <stm 1>. ids U <stm 2>. ids <stm>. num = | <stm>. ids | CS 7100 (Prasad) L 16 -7 AG 27
Alternate approach : Using lists • Attributes envi : list of vars in preceding context envo : list of vars for following context dnum : number of new variables <exp> : : = <var> <exp>. envo = if member(<var>. id, <exp>. envi) then <exp>. envi else cons(<var>. id, <exp>. envi) CS 7100 (Prasad) L 16 -7 AG 28
Attribute Computation Rules <exp> : : = envi envo dnum <exp 1>. envi <exp 2>. envi <exp>. envo <exp>. dnum CS 7100 (Prasad) <exp 1> + <exp 2> = = envi envo dnum <exp>. envi <exp 1>. envo <exp 2>. envo length(<exp>. envo) L 16 -7 AG 29
- Slides: 27