Attribute Grammars

Attribute Grammars
Attribute Grammar is a
Framework for specifying semantics
and enables Modular specification.
http://knoesis.wright.edu/tkprasad/papers/Attribute-Grammars.pdf
CS7100 (Prasad)
L16-7AG
1
Hierarchy of
approximations
S*
Regular (lexer)
Context-free (parser)
Context-sensitive
(type-checker)
“Correct” Programs
(no run-time errors)
CS7100 (Prasad)
L16-7AG
2
Expresssive
power
Context-sensitive
(type-checker)
Languages
Regular (lexer)
Context-free (parser)
“Correct” Programs
(no run-time errors)
CS7100 (Prasad)
L16-7AG
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”
CS7100 (Prasad)
L16-7AG
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.
CS7100 (Prasad)
L16-7AG
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
CS7100 (Prasad)
L16-7AG
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
CS7100 (Prasad)
L16-7AG
7
Derivation Tree
N
N
0
N
1
110 ~> 6
1
CS7100 (Prasad)
L16-7AG
8
Derivation Tree
N
N
0
N
1
110 ~> 6
1
CS7100 (Prasad)
L16-7AG
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
CS7100 (Prasad)
N . val := N. val + 2^ N. len
N . len := N. len + 1
L16-7AG
10
Grammar Syntax Variations
N -> 0
N -> 1
N ::=
N -> 0 N
N -> 1 N
CS7100 (Prasad)
L16-7AG
0
| 1
| 0N
| 1N
11
Synthesized Attributes
• Evaluation of a constant expression
• Type Inference
5.0 + 2
• Determination of non-terminals that derive
null string or terminal strings
CS7100 (Prasad)
L16-7AG
12
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
CS7100 (Prasad)
L16-7AG
13
Inherited Attributes
• Coercion (automatic type conversion) 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
CS7100 (Prasad)
L16-7AG
14
• Attributes
– Synthesized
– Inherited
A(X)
S(X)
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) )
CS7100 (Prasad)
L16-7AG
15
Information Flow
inherited
computed
available
synthesized
...
...
CS7100 (Prasad)
L16-7AG
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.
CS7100 (Prasad)
L16-7AG
17
Static Semantics
E
->
n
|
m
E.type := int
E
->
x
|
y
E.type := real
E
->
E1
+
E2
E
->
E1
*
E2
CS7100 (Prasad)
E.type :=
if
E1.type = E2.type
then E1.type
else real
L16-7AG
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,T1), type(F,T2),
T1 \= T2.
• Type Checking ?- type(+(i,x),real).
• Type Inference ?- type(+(x,x),T).
CS7100 (Prasad)
L16-7AG
20
Static Semantics
E
E
E
->
->
->
CS7100 (Prasad)
n
p
|
|
if
then
else
E.type := int
m
E.type := bool
q
E.type :=
if ( E0.type = bool )
 ( E1.type = E2.type )
then E1.type
else type error
E0
E1
E2
L16-7AG
21
Fractions
F -> . N
F.val := N.val
N.pow := 1
N -> 0
N -> 1
N.val
N.val
N -> 0 N
N.pow := 1 + N.pow
N.val
:= N.val
N.pow := 1 + N.pow
N.val
:= N.val +
(1/2^N.pow)
N -> 1 N
CS7100 (Prasad)
:= 0
:= (1/2^N.pow)
L16-7AG
23
Fractions (Alternate solution)
F -> . N
F.val
:= N.val / 2
N -> 0
N -> 1
N.val
N.val
:= 0
:= 1
N -> 0 N
N.val
:= N.val / 2
N -> 1 N
N.val
:= N.val / 2
+ 1
CS7100 (Prasad)
L16-7AG
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
CS7100 (Prasad)
L16-7AG
25
An Extended Example
• Number of distinct identifiers in a straight-line program.
BNF
<exp> ::=
<var>
| <exp> + <exp>
<stm> ::= <var> := <exp> | <stm> ; <stm>
Attributes
<var>
<exp>
<stm>
 id
 ids
 ids
 num
• Semantics specified in terms of sets (of identifiers).
CS7100 (Prasad)
L16-7AG
26
<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>
<stm>.ids =
::=
<stm1> ; <stm2>
<stm1>.ids
U <stm2>.ids
<stm>.num = | <stm>.ids |
CS7100 (Prasad)
L16-7AG
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
else
CS7100 (Prasad)
<exp>.envi
cons(<var>.id,<exp>.envi)
L16-7AG
28
Attribute Computation Rules
<exp>



::=
envi
envo
dnum
<exp1>.envi
<exp2>.envi
<exp>.envo
<exp>.dnum
CS7100 (Prasad)
<exp1> + <exp2>



=
=
=
=
envi
envo
dnum



envi
envo
dnum
<exp>.envi
<exp1>.envo
<exp2>.envo
length(<exp>.envo)
L16-7AG
29