Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.

Similar presentations


Presentation on theme: "CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification."— Presentation transcript:

1 CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.

2 CS7100 (Prasad)L16-7AG2 “Correct” Programs (no run-time errors)  Regular (lexer) Context-free (parser) Context-sensitive (type-checker) Hierarchy of approximations

3 CS7100 (Prasad)L16-7AG3 Languages “Correct” Programs (no run-time errors) Context-sensitive (type-checker) Context-free (parser) Regular (lexer) Expresssive power

4 CS7100 (Prasad)L16-7AG4 Semantics of Bit Pattern : Number –46 (if base 2) – (if base 10) Fraction –23/64 Unary encoding “13” ASCII Character “. ” Switch positions –ON-OFF-ON-… Binary string “ ”

5 CS7100 (Prasad)L16-7AG5 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.

6 CS7100 (Prasad)L16-7AG6 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

7 CS7100 (Prasad)L16-7AG7 Synthesized Attributes    N   N   val   val   val  N  val  val  N  val 

8 CS7100 (Prasad)L16-7AG8 Derivation Tree N N N ~> 6

9 CS7100 (Prasad)L16-7AG9 Synthesized Attributes    N  N  val   len   val   len   val  N  val  len  N  len   val  N  val  ^ N  len  len  N  len + 1

10 CS7100 (Prasad)L16-7AG10 Inherited Attributes Declaration and Use { int i, j, k; i := i + j + j; }    :=. env :=. env

11 CS7100 (Prasad)L16-7AG11 Inherited Attributes Coercion Code Generation  coerce_int_to_real Determination of un-initialized variables Determination of reachable non-terminals Evaluation of an expression containing variables

12 CS7100 (Prasad)L16-7AG12 AttributesAttributes A( X ) –Synthesized S( X ) –Inherited I( X ) Attribute computation rulesAttribute computation rules (Semantic functions) X0 -> X1 X2 … Xn S( X0 ) = f ( I( X0 ), A( X1 ), A( X2 ), …, A( Xn ) ) I( Xj ) = G j ( I( X0 ), A( X1 ), A( X2 ), …, A( Xj-1 )) for all j in 1..n P ( A( X0 ), A( X1 ), A( X2 ), …, A( Xn ) )

13 CS7100 (Prasad)L16-7AG13 Information Flow inherited synthesized... computed available available

14 CS7100 (Prasad)L16-7AG14 Synthesized AttributesSynthesized Attributes Pass information up the parse tree Inherited AttributesInherited 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.

15 CS7100 (Prasad)L16-7AG15 Static Semantics E -> n | m E -> x | y E -> E1 + E2 E -> E1 * E2  E.type  int E.type  real if E1.type  E2.type then E.type  E1.type else E.type  real

16 CS7100 (Prasad)L16-7AG16 Static Semantics E -> n | m E -> x | y E -> E1 + E2 E -> E1 * E2  E.type  int E.type  real E.type  if E1.type  E2.type then E1.type else real

17 CS7100 (Prasad)L16-7AG17 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,T1), type(F,T2), T1 \= T2. Type Checking ?- type(+(i,x),real). Type Inference ?- type(+(x,x),T).

18 CS7100 (Prasad)L16-7AG18 Static Semantics E -> n | m E -> p | q E -> if E0 then E1 else E2 E.type  int E.type  bool E.type  if ( E0.type  bool )  ( E1.type = E2.type ) then E1.type else type error

19 CS7100 (Prasad)L16-7AG19 Static Semantics E -> n | m E -> p | q E -> if E0 then E1 else E2 E.type  int E.type  bool if ( E0.type  bool )  ( E1.type = E2.type ) then E.type  E1.type else type error

20 CS7100 (Prasad)L16-7AG20 Fractions F       N  N F.val  N.val N.pow  1 N.val  N.val  1/2^N.pow)  N.pow  1 + N.pow N.val  N  val N.pow  1 + N.pow N.val  N  val + (1/2^N.pow)

21 CS7100 (Prasad)L16-7AG21 Fractions (Alternate solution) F       N  N F.val  N.val  2 N.val  N.val  N.val  N  val  2 + 1

22 CS7100 (Prasad)L16-7AG22 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

23 CS7100 (Prasad)L16-7AG23 An Extended Example Distinct identifiers in a straight-line program. BNF ::= | + ::= := | ; Attributes  id  ids  ids  num Semantics specified in terms of sets (of identifiers).

24 CS7100 (Prasad)L16-7AG24 ::=.ids = {.id } ::= +.ids =.ids U.ids ::= :=.ids ={.id } U.ids.num = |.ids | ::= ;.ids =.ids U.ids.num = |.ids |

25 CS7100 (Prasad)L16-7AG25 Alternate approach : Using lists Attributes  envi : list of vars in preceding context  envo : list of vars for following context  dnum : number of new variables ::=.envo = if member(.id,.envi) then.envi else cons(.id,.envi)

26 CS7100 (Prasad)L16-7AG26 ::= +  envi  envi  envi  envo  envo  envo  dnum  dnum  dnum.envi =.envi.envi =.envo.envo =.envo.dnum = length(.envo) Attribute Computation Rules


Download ppt "CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification."

Similar presentations


Ads by Google