Semantics
Semantics
Static semantics
attribute grammars
examples
computing attribute values
status
Dynamic semantics
operational semantics
axiomatic semantics
examples
loop invariants
evaluation
denotational semantics
examples
evaluation
(3.1)
Static Semantics
(3.2)
Attribute Grammars
(3.3)
(3.4)
Synthesized attributes
are determined from nodes of children in
parse tree
if X0 -> X1 ... Xn is a rule, then S(X0) =
f(A(X1), ..., A(Xn))
Inherited attributes
are determined from parent and siblings
I(Xj) = f(A(X0), ..., A(Xn))
often, just X0 ... Xj-1
siblings to left in parse tree
(3.5)
(3.6)
(3.7)
Example attributes
actual_type
expected_type
for <expr>, determined by type of variable on
LHS of assignment statement, for example
env
pointer to correct symbol table environment, to
be sure semantic information used is correct set
think of different variable scopes
(3.8)
Attribute Grammar:
1. syntax rule: <expr> -> <var>[1] + <var>[2]
semantic rules:
<var>[1].env <- <expr>.env
<var>[2].env <- <expr>.env
<expr>.actual_type <<var>[1].actual_type
predicate:
<var>[1].actual_type =
var>[2].actual_type
<expr>.expected_type = <expr>.actual_type
2. syntax rule: <var> -> id
semantic rule:
<var>.actual_type <- lookup
(3.9)
(3.10)
(3.11)
Well-defined, well-understood
formalism
used for several practical compilers
Dynamic Semantics
(3.12)
Operational Semantics
(3.13)
(3.14)
Evaluation
Axiomatic Semantics
(3.15)
(3.16)
(3.17)
(3.18)
(3.19)
(3.20)
(3.21)
(3.22)
{I} B {I}
evaluation of the Boolean must not
change the validity of I
{I and B} S {I}
I is not changed by executing the body of
the loop
(3.23)
Denotational Semantics
(3.24)
(3.25)
(the value of ij in
(3.26)
Assignment Statements
Ma(x:=E, s): if Me(E, s) = error
then error
else s = {<i1,v1>,...,<in,vn>},
where for j = 1, 2, ..., n,
vj= VARMAP(ij, s) if ij <> x
= Me(E, s) if ij = x
(3.27)
(3.28)