CS420 Lecture18 Semantics
CS420 Lecture18 Semantics
Programming
Languages
Dr. Mary Pourebadi
San Diego State University
Lecture 18
Describing Semantics
Book: Chapter 3
ISBN 0-321-49362-1
Semantics
• Operational Semantics
– Describe the meaning of a program by
executing its statements on a machine, either
simulated or actual. The change in the state of
the machine (memory, registers, etc.) defines
the meaning of the statement
• To use operational semantics for a high-
level language, a virtual machine is needed
Two-digit number:
number Is 42.
We break it down as follows :M_dec('42') = 10 * M_dec('4') + M_dec('2’)
So:M_dec('42') = 10 * 4 + 2 = 40 + 2 = 42
Three-digit number:
number is 315.
Using the rule:M_dec('315') = 10 * M_dec('31') + M_dec('5')And,
M_dec('31') = 10 * M_dec('3') + M_dec('1’)
So:M_dec('315') = 10 * (10 * 3 + 1) + 5 = 10 * 31 + 5 = 310 + 5 = 315
1-14
Expressions
Me(<expr>, s) =
case <expr> of
<dec_num> => Mdec(<dec_num>, s)
<var> =>
if VARMAP(<var>, s) == undef
then error
else VARMAP(<var>, s)
<binary_expr> =>
if (Me(<binary_expr>.<left_expr>, s) == undef
OR Me(<binary_expr>.<right_expr>, s) =
undef)
then error
else
if (<binary_expr>.<operator> == '+' then
Me(<binary_expr>.<left_expr>, s) +
Me(<binary_expr>.<right_expr>, s)
else Me(<binary_expr>.<left_expr>, s) *
Me(<binary_expr>.<right_expr>, s)
...
Ma(x := E, s) =
if Me(E, s) == error
then error
else s’ =
{<i1,v1’>,<i2,v2’>,...,<in,vn’>},
where for j = 1, 2, ..., n,
if ij == x
then vj’ = Me(E, s)
else vj’ = VARMAP(ij, s)
Ml(while B do L, s) =
if Mb(B, s) == undef
then error
else if Mb(B, s) == false
then s
else if Msl(L, s) == error
then error
else Ml(while B do L, Msl(L, s))
• An example
– a = b + 1 {a > 1}
– One possible precondition: {b > 10}
– Weakest precondition: {b > 0}
{P1} S1 {P2}
{P2} S2 {P3}
(I and B) S {I}
{I} while B do S {I and (not B)}