23
CS784 (Prasad) L167AG 1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification. http://knoesis.wright.edu/tkprasad/papers/ Attribute-Grammars.pdf

Attribute Grammars

  • Upload
    garret

  • View
    70

  • Download
    0

Embed Size (px)

DESCRIPTION

Attribute Grammars. Attribute Grammar is a Framework for specifying semantics and enables Modular specification. http://knoesis.wright.edu/tkprasad/papers/Attribute-Grammars.pdf. “Correct” Programs ( no run-time errors ). S*. Regular ( lexer ). Context-free ( parser ). - PowerPoint PPT Presentation

Citation preview

Page 1: Attribute Grammars

CS784 (Prasad) L167AG 1

Attribute Grammars

Attribute Grammar is a

Framework for specifying semantics

and enables Modular specification.http://knoesis.wright.edu/tkprasad/papers/Attribute-Grammars.pdf

Page 2: Attribute Grammars

CS784 (Prasad) L167AG 2

“Correct” Programs(no run-time errors)

Regular (lexer)

Context-free (parser)

Context-sensitive (type-checker)

Page 3: Attribute Grammars

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

Page 4: Attribute Grammars

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

Page 5: Attribute Grammars

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

Page 6: Attribute Grammars

CS784 (Prasad) L167AG 6

Synthesized Attributes

N

N

val

val

valNval

valNval

Page 7: Attribute Grammars

CS784 (Prasad) L167AG 7

Derivation Tree

N

N

N

1

1

0

110 ~> 6

Page 8: Attribute Grammars

CS784 (Prasad) L167AG 8

Synthesized Attributes

N

N

vallen

vallen

valNvallenNlen

val Nval^ Nlen

lenNlen + 1

Page 9: Attribute Grammars

CS784 (Prasad) L167AG 9

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

Page 10: Attribute Grammars

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

Page 11: Attribute Grammars

CS784 (Prasad) L167AG 11

• 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) = 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) )

Page 12: Attribute Grammars

CS784 (Prasad) L167AG 12

Information Flow

inherited

synthesized

... ...

computedcomputed

availableavailable

Page 13: Attribute Grammars

CS784 (Prasad) L167AG 13

• 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.

Page 14: Attribute Grammars

CS784 (Prasad) L167AG 14

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

Page 15: Attribute Grammars

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

Page 16: Attribute Grammars

CS784 (Prasad) L167AG 16

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

Page 17: Attribute Grammars

CS784 (Prasad) L167AG 17

Fractions

F

N

N

F.val N.valN.pow 1

N.val N.val 1/2^N.pow)N.pow 1 + N.powN.val Nval

N.pow 1 + N.powN.val Nval +

(1/2^N.pow)

Page 18: Attribute Grammars

CS784 (Prasad) L167AG 18

Fractions (Alternate solution)

F

N

N

F.val N.val 2

N.val N.val

N.val Nval 2

N.val Nval 2 + 1

Page 19: Attribute Grammars

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

Page 20: Attribute Grammars

CS784 (Prasad) L167AG 20

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

Page 21: Attribute Grammars

CS784 (Prasad) L167AG 21

<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 |

Page 22: Attribute Grammars

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

<exp> ::= <var><exp>.envo = if member(<var>.id,<exp>.envi) then <exp>.envi else cons(<var>.id,<exp>.envi)

Page 23: Attribute Grammars

CS784 (Prasad) L167AG 23

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

Attribute Computation Rules