Attribute Grammars - PowerPoint PPT Presentation

About This Presentation
Title:

Attribute Grammars

Description:

var id exp ids stm ids num. Semantics specified in terms of sets (of identifiers) ... else cons( var .id, exp .envi) CS784 (Prasad) L167AG. 23 exp ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 24
Provided by: csWr
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Attribute Grammars


1
Attribute Grammars
  • Attribute Grammar is a
  • Framework for specifying semantics
  • and enables Modular specification.
  • http//knoesis.wright.edu/tkprasad/papers/Attribut
    e-Grammars.pdf

2
S
Regular (lexer)
Context-free (parser)
Context-sensitive (type-checker)
3
Semantics of Bit Pattern 0101110
  • Number
  • 46 (if base 2)
  • 101110 (if base 10)
  • Fraction
  • 23/64
  • Unary encoding 13
  • ASCII Character .
  • Switch positions
  • ON-OFF-ON-
  • Binary string 0101110

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.

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

6
Synthesized Attributes
  • N -gt 0
  • N -gt 1
  • N -gt N 0
  • N -gt N 1
  • N . val 0
  • N . val 1
  • N . val 2 N. val
  • N . val 2 N. val 1

7
Derivation Tree
N
N
0
N
1
110 gt 6
1
8
Synthesized Attributes
  • N . val 0
  • N . len 1
  • N . val 1
  • N . len 1
  • N . val N. val
  • N . len N. len 1
  • N . val N. val 2 N. len
  • N . len N. len 1
  • N -gt 0
  • N -gt 1
  • N -gt 0 N
  • N -gt 1 N

9
Inherited Attributes
  • Declaration and Use
  • int i, j, k
  • i i j j
  • ltassign-stmgt -gt ltvargt ltexprgt
  • ltvargt.env ltassign-stmgt.env
  • ltexprgt.env ltassign-stmgt.env

10
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

11
  • Attributes A(X)
  • Synthesized S(X)
  • Inherited I(X)
  • Attribute computation rules (Semantic functions)
  • X0 -gt 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) )

12
Information Flow
inherited
computed
available
synthesized
...
...
13
  • 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.

14
Static Semantics
  • E -gt n m
  • E -gt x y
  • E -gt E1 E2
  • E -gt E1 E2
  • E.type int
  • E.type real
  • if E1.type E2.type
  • then E.type E1.type
  • else E.type real

15
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).

16
Static Semantics
  • E -gt n m
  • E -gt p q
  • E -gt 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

17
Fractions
  • F.val N.val
  • N.pow 1
  • N.val 0
  • N.val (1/2N.pow)
  • N.pow 1 N.pow
  • N.val N.val
  • N.pow 1 N.pow
  • N.val N.val (1/2N.pow)
  • F -gt . N
  • N -gt 0
  • N -gt 1
  • N -gt 0 N
  • N -gt 1 N

18
Fractions (Alternate solution)
  • F.val N.val / 2
  • N.val 0
  • N.val 1
  • N.val N.val / 2
  • N.val N.val / 2
  • 1
  • F -gt . N
  • N -gt 0
  • N -gt 1
  • N -gt 0 N
  • N -gt 1 N

19
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

20
An Extended Example
  • Distinct identifiers in a straight-line
    program.
  • BNF
  • ltexpgt ltvargt ltexpgt ltexpgt
  • ltstmgt ltvargt ltexpgt ltstmgt ltstmgt
  • Attributes
  • ltvargt ? id
  • ltexpgt ? ids
  • ltstmgt ? ids ? num
  • Semantics specified in terms of sets (of
    identifiers).

21
  • ltexpgt ltvargt
  • ltexpgt.ids ltvargt.id
  • ltexpgt ltexp1gt ltexp2gt
  • ltexpgt.ids ltexpgt.ids U ltexpgt.ids
  • ltstmgt ltvargt ltexpgt
  • ltstmgt.ids ltvargt.id U ltexpgt.ids
  • ltstmgt.num ltstmgt.ids
  • ltstmgt ltstm1gt ltstm2gt
  • ltstmgt.ids ltstm1gt.ids U ltstm2gt.ids
  • ltstmgt.num ltstmgt.ids

22
Alternate approach Using lists
  • Attributes
  • ? envi list of vars in preceding context
  • ? envo list of vars for following context
  • ? dnum number of new variables
  • ltexpgt ltvargt
  • ltexpgt.envo
  • if member(ltvargt.id,ltexpgt.envi)
  • then ltexpgt.envi
  • else cons(ltvargt.id,ltexpgt.envi)

23
Attribute Computation Rules
  • ltexpgt ltexp1gt ltexp2gt
  • ? envi ? envi ? envi
  • ? envo ? envo ? envo
  • ? dnum ? dnum ? dnum
  • ltexp1gt.envi ltexpgt.envi
  • ltexp2gt.envi ltexp1gt.envo
  • ltexpgt.envo ltexp2gt.envo
  • ltexpgt.dnum length(ltexpgt.envo)
Write a Comment
User Comments (0)
About PowerShow.com