0% found this document useful (0 votes)
11 views29 pages

CS420 Lecture18 Semantics

Uploaded by

rljitler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views29 pages

CS420 Lecture18 Semantics

Uploaded by

rljitler
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CS 420: Advanced

Programming
Languages
Dr. Mary Pourebadi
San Diego State University
Lecture 18

Describing Semantics

Book: Chapter 3

ISBN 0-321-49362-1
Semantics

• There is no single widely acceptable


notation or formalism for describing
semantics
• Several needs for a methodology and
notation for semantics:
– Programmers need to know what statements mean
– Compiler writers must know exactly what language
constructs do
– Correctness proofs would be possible
– Compiler generators would be possible
– Designers could detect ambiguities and inconsistencies

Copyright © 2018 Pearson. All rights reserved. 1-4


Operational 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

Copyright © 2018 Pearson. All rights reserved. 1-5


Operational Semantics

• A hardware pure interpreter would be too


expensive
• A software pure interpreter also has
problems
– The detailed characteristics of the particular
computer would make actions difficult to
understand
– Such a semantic definition would be machine-
dependent

Copyright © 2018 Pearson. All rights reserved. 1-6


Operational Semantics (continued)

• A better alternative: A complete computer


simulation
• The process:
– Build a translator (translates source code to the
machine code of an idealized computer)
– Build a simulator for the idealized computer

Copyright © 2018 Pearson. All rights reserved. 1-7


Operational Semantics (continued)

• Uses of operational semantics:


- Language manuals and textbooks
- Teaching programming languages

• Two different levels of uses of operational


semantics:
- Natural operational semantics
- Structural operational semantics

Copyright © 2018 Pearson. All rights reserved. 1-8


Denotational Semantics

• Based on recursive function theory


• The most abstract semantics description
method
• Originally developed by Scott and Strachey
(1970)

• Operational vs. Denotational Semantics ?

Copyright © 2018 Pearson. All rights reserved. 1-9


Denotation Semantics vs Operational
Semantics
• In operational semantics, the state changes
are defined by coded algorithms
• In denotational semantics, the state
changes are defined by rigorous
mathematical functions

Copyright © 2018 Pearson. All rights reserved. 1-10


Denotational Semantics - continued

• The process of building a denotational


specification for a language:
- Define a mathematical object for each language
entity
– Define a function that maps instances of the
language entities onto instances of the
corresponding mathematical objects
• The meaning of language constructs are
defined by only the values of the program's
variables

Copyright © 2018 Pearson. All rights reserved. 1-11


Denotational Semantics: program state

• The state of a program is the values of all


its current variables
s = {<i1, v1>, <i2, v2>, …, <in, vn>}

• Let VARMAP be a function that, when given


a variable name and a state, returns the
current value of the variable
VARMAP(ij, s) = vj

Copyright © 2018 Pearson. All rights reserved. 1-12


Decimal Numbers

<dec_num> → '0' | '1' | '2' | '3' | '4' | '5' |


'6' | '7' | '8' | '9' |
<dec_num> ('0' | '1' | '2' | '3' |
'4' | '5' | '6' | '7' |
'8' | '9')

Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9


Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>)
Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1

Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9

Copyright © 2018 Pearson. All rights reserved. 1-13


Decimal Numbers: Examples
Single-digit number:
number is 7.
The function M_dec('7') simply returns 7.

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

• Map expressions onto Z  {error}


• We assume expressions are decimal
numbers, variables, or binary expressions
having one arithmetic operator and two
operands, each of which can be an
expression

Copyright © 2018 Pearson. All rights reserved. 1-15


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

Copyright © 2018 Pearson. All rights reserved. 1-16


Assignment Statements

• Maps state sets to state sets U {error}

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)

Copyright © 2018 Pearson. All rights reserved. 1-17


Logical Pretest Loops

• Maps state sets to state sets U {error}

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

Copyright © 2018 Pearson. All rights reserved. 1-18


Loop Meaning

• The meaning of the loop is the value of the


program variables after the statements in the loop
have been executed the prescribed number of
times, assuming there have been no errors
• In essence, the loop has been converted from
iteration to recursion, where the recursive control
is mathematically defined by other recursive state
mapping functions

- Recursion, when compared to iteration, is easier


to describe with mathematical rigor

Copyright © 2018 Pearson. All rights reserved. 1-19


Evaluation of Denotational Semantics

• Can be used to prove the correctness of


programs
• Provides a rigorous way to think about
programs
• Can be an aid to language design
• Has been used in compiler generation
systems
• Because of its complexity, it are of little use
to language users

Copyright © 2018 Pearson. All rights reserved. 1-20


Axiomatic Semantics

• Based on formal logic (predicate calculus)


• Original purpose: formal program
verification
• Axioms or inference rules are defined for
each statement type in the language (to
allow transformations of logic expressions
into more formal logic expressions)
• The logic expressions are called assertions

Copyright © 2018 Pearson. All rights reserved. 1-21


Axiomatic Semantics (continued)
• An assertion before a statement (a
precondition) states the relationships and
constraints among variables that are true at
that point in execution
• An assertion following a statement is a
postcondition
• A weakest precondition is the least
restrictive precondition that will guarantee
the postcondition

Copyright © 2018 Pearson. All rights reserved. 1-22


Axiomatic Semantics Form

• Pre-, post form: {P} statement {Q}

• An example
– a = b + 1 {a > 1}
– One possible precondition: {b > 10}
– Weakest precondition: {b > 0}

Copyright © 2018 Pearson. All rights reserved. 1-23


Program Proof Process

• The postcondition for the entire program is


the desired result
– Work back through the program to the first
statement. If the precondition on the first
statement is the same as the program
specification, the program is correct.

Copyright © 2018 Pearson. All rights reserved. 1-24


Axiomatic Semantics: Assignment

• An axiom for assignment statements


(x = E): {Qx->E} x = E {Q}

• The Rule of Consequence:


{P} S {Q}, P'  P, Q  Q'
{P' } S {Q' }

Copyright © 2018 Pearson. All rights reserved. 1-25


Axiomatic Semantics: Sequences

• An inference rule for sequences of the form


S1; S2

{P1} S1 {P2}
{P2} S2 {P3}

{P1} S1{P2}, {P2} S2 {P3}


{P1} S1; S2 {P3}

Copyright © 2018 Pearson. All rights reserved. 1-26


Axiomatic Semantics: Loops
• An inference rule for logical pretest loops

{P} while B do S end {Q}

(I and B) S {I}
{I} while B do S {I and (not B)}

where I is the loop invariant (the inductive


hypothesis)

Copyright © 2018 Pearson. All rights reserved. 1-28


Loop Invariant

• The loop invariant I is a weakened version


of the loop postcondition, and it is also a
precondition.
• I must be weak enough to be satisfied prior
to the beginning of the loop, but when
combined with the loop exit condition, it
must be strong enough to force the truth of
the postcondition

Copyright © 2018 Pearson. All rights reserved. 1-30


Evaluation of Axiomatic Semantics

• Developing axioms or inference rules for all


of the statements in a language is difficult
• It is a good tool for correctness proofs, and
an excellent framework for reasoning about
programs, but it is not as useful for
language users and compiler writers
• Its usefulness in describing the meaning of
a programming language is limited for
language users or compiler writers

Copyright © 2018 Pearson. All rights reserved. 1-31


Summary

• BNF and context-free grammars are


equivalent meta-languages
– Well-suited for describing the syntax of
programming languages
• An attribute grammar is a descriptive
formalism that can describe both the
syntax and the semantics of a language
• Three primary methods of semantics
description
– Operation, axiomatic, denotational

Copyright © 2018 Pearson. All rights reserved. 1-32

You might also like