Docstoc

Attribute Grammars

Document Sample
Attribute Grammars Powered By Docstoc
					                  Attribute Grammars
                    Attribute Grammar is a
             Framework for specifying semantics
              and enables Modular specification.
             http://knoesis.wright.edu/tkprasad/papers/Attribute-Grammars.pdf




CS784 (Prasad)                           L167AG                                 1
                                         S*
                   Regular (lexer)

                 Context-free (parser)

                  Context-sensitive
                   (type-checker)




                   “Correct” Programs
                  (no run-time errors)




CS784 (Prasad)          L167AG                2
      Semantics of Bit Pattern : 0101110


• Number                         • ASCII Character “ ”.
     – 46     (if base 2)        • Switch positions
     – 101110 (if base 10)            – ON-OFF-ON-…
• Fraction                       • Binary string
     – 23/64                       “0101110”
• Unary encoding “13”


CS784 (Prasad)               L167AG                       3
      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.


CS784 (Prasad)           L167AG                4
                       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
CS784 (Prasad)                     L167AG                          5
                 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



CS784 (Prasad)            L167AG                              6
                 Derivation Tree
                              N

                     N
                                      0
                 N
                         1
                                          110 ~> 6
                 1




CS784 (Prasad)               L167AG                  7
                 Synthesized Attributes
                                   N . val := 0
       N -> 0                     N . len := 1

       N -> 1                     N . val := 1
                                   N . len := 1

                                   N . val := N. val
       N -> 0 N
                                   N . len := N. len + 1

       N -> 1 N                   N . val := N. val + 2^ N. len
                                   N . len := N. len + 1


CS784 (Prasad)            L167AG                             8
                    Inherited Attributes

    • Declaration and Use
                 { int i, j, k;
                    i := i + j + j; }


       <assign-stm> -> <var> := <expr>
          <var>.env := <assign-stm>.env
         <expr>.env := <assign-stm>.env


CS784 (Prasad)              L167AG         9
                 Inherited Attributes
• Coercion Code Generation
     5.0 + 2
             coerce_int_to_real
• Determination of un-initialized variables
• Determination of reachable non-terminals
• Evaluation of an expression containing
  variables

CS784 (Prasad)           L167AG               10
• Attributes           A(X)
    – Synthesized      S(X)
    – Inherited         I(X)
• Attribute computation rules (Semantic functions)
                 X0   ->   X1       X2   …   Xn

    S(X0) = f( I(X0), A(X1), A(X2), …, A(Xn) )
    I(Xj) = Gj( I(X0), A(X1), A(X2), …, A(Xj-1))
              for all j in 1..n
          P( A(X0), A(X1), A(X2), …, A(Xn) )

CS784 (Prasad)             L167AG                    11
                   Information Flow
             inherited                         computed

                                               available
            synthesized




                          ...            ...




CS784 (Prasad)                  L167AG                     12
• 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.
CS784 (Prasad)         L167AG                      13
                      Static Semantics
      E     ->    n    |   m             E.type := int

      E     ->    x    |   y             E.type := real

      E     ->   E1    +   E2

      E     ->   E1    *   E2
                                         if   E1.type = E2.type
                                         then E.type := E1.type
                                         else E.type := real


CS784 (Prasad)                  L167AG                        14
       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).

CS784 (Prasad)        L167AG                   15
                     Static Semantics

                                        E.type := int
     E      ->   n    |    m
                                        E.type := bool
     E      ->   p    |    q
                                        if    ( E0.type = bool )
     E      ->    if      E0                ( E1.type = E2.type )
                 then     E1            then E.type := E1.type
                 else     E2            else type error


CS784 (Prasad)                 L167AG                            16
                    Fractions
                               F.val := N.val
         F -> . N
                               N.pow := 1

         N -> 0                N.val := 0
         N -> 1                N.val := (1/2^N.pow)

                               N.pow := 1 + N.pow
        N -> 0 N               N.val := N.val


        N -> 1 N               N.pow := 1 + N.pow
                               N.val := N.val +
                                       (1/2^N.pow)
CS784 (Prasad)        L167AG                        17
                 Fractions (Alternate solution)
                                              := N.val / 2
         F -> . N                     F.val


         N -> 0                       N.val   := 0
         N -> 1                       N.val   := 1


        N -> 0 N                      N.val   := N.val / 2


        N -> 1 N                      N.val   := N.val / 2
                                                   + 1

CS784 (Prasad)               L167AG                          18
     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
CS784 (Prasad)                  L167AG                 19
                 An Extended Example
• Distinct identifiers in a straight-line program.
BNF
<exp> ::=      <var>     | <exp> + <exp>
<stm> ::= <var> := <exp> | <stm> ; <stm>

Attributes
    <var>                id
   <exp>                 ids
   <stm>                 ids       num

• Semantics specified in terms of sets (of identifiers).
CS784 (Prasad)           L167AG                        20
   <exp>   ::= <var>
<exp>.ids = { <var>.id }
   <exp>   ::= <exp1> + <exp2>
<exp>.ids = <exp>.ids U <exp>.ids
         <stm>    ::=       <var> := <exp>
<stm>.ids ={ <var>.id } U <exp>.ids
<stm>.num = | <stm>.ids |
         <stm>    ::=       <stm1> ; <stm2>
<stm>.ids =      <stm1>.ids      U <stm2>.ids
<stm>.num = | <stm>.ids |
CS784 (Prasad)          L167AG                  21
                 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)
CS784 (Prasad)                L167AG                  22
                 Attribute Computation Rules
        <exp>          ::=        <exp1> + <exp2>

        envi                       envi       envi
        envo                       envo       envo
        dnum                       dnum       dnum

        <exp1>.envi        =        <exp>.envi
        <exp2>.envi        =        <exp1>.envo
        <exp>.envo         =        <exp2>.envo
        <exp>.dnum         =      length(<exp>.envo)

CS784 (Prasad)                 L167AG                   23

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:9/22/2012
language:English
pages:23