Chapter 4 Fundamentals Semantics of Programming Languages - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 4 Fundamentals Semantics of Programming Languages

Description:

... tree like program, block, assignment, expression, conditional, loop, and others ... Denotational Semantics of Assignment Statement. Assignment Statements ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 25
Provided by: itc125
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Fundamentals Semantics of Programming Languages


1
Chapter 4Fundamentals (Semantics of
Programming Languages)
2
Outline
  • Compilers A review
  • Syntax specification
  • BNF (EBNF)
  • Semantics specification
  • Static semantics
  • Attribute grammar
  • Dynamic semantics
  • Operational semantics
  • Denotational semantics
  • Axiomatic semantics
  • Lambda Calculus

3
References
  • Concepts in Programming Languages by J. Mitchel
    textbook Chapter 4
  • Programming Languages Principles and Paradigms
    by Allan Tucker and R. Noonan, Chapter 3
    Handout
  • Concepts of Programming Languages by R.
    Sebesta, 6th Edition, Chapter 3.

4
Denotational Semantics
  • Denotational semantics of a language defines the
    meanings of abstract language elements as a
    collection of environment- and state-transforming
    functions
  • M Class ? ? ? ?
  • ? Is the set of all program states ?
  • Environment of a program is the set of objects
    and types that are active at each step during its
    execution
  • State of a program is the set of all active
    objects and their current values.

5
Denotational Semantics
  • Denotational semantics defines the meaning of a
    program as a series of state transformations
    resulting from the application of a series of
    functions M
  • State-transforming functions individually define
    the meaning of every class of element that can
    occur in the programs abstract syntax tree like
    program, block, assignment, expression,
    conditional, loop, and others
  • State-transforming functions are necessarily
    partial functions

6
Denotational Semantics
  • Denotational semantics
  • The meaning or denotation is associated with
    each program or program phrase like expression,
    statement, declaration, etc.
  • Describe meaning of programs by specifying the
    mathematical
  • Function
  • Function on functions
  • Value, such as natural numbers or strings
  • defined for each construct

7
Motivation
  • Precision
  • Use mathematics instead of English
  • Avoid details of specific machines
  • Aim to capture pure meaning apart from
    implementation details
  • Basis for program analysis
  • Justify program proof methods
  • Soundness of type system, control flow analysis
  • Proof of compiler correctness
  • Language comparisons

8
Basic Principle of Denotational Semantics
  • Compositionality
  • The meaning of a compound program must be
    defined from the meanings of its parts (not the
    syntax of its parts).
  • Example
  • P Q
  • composition of two functions, state ?
    state
  • Object language and metalanguage

9
Trivial Example Binary Numbers
  • Syntax
  • b ? 0 1
  • n ? b nb
  • e ? n ee
  • Semantics value function E exp -gt numbers
  • E 0 0
  • E 1 1
  • E nb 2E n E b
  • E e1e2 E e1 E e2
  • Obvious, but different from compiler evaluation
    using registers, etc.
  • This is a simple machine-independent
    characterization ...

10
Second Example Expressions w/vars
  • Syntax
  • d ? 0 1 2 9
  • n ? d nd
  • e ? x n e e
  • Semantics value E exp ? state ? numbers
  • state s vars ?
    numbers
  • E x s s(x)
  • E 0 s 0
  • E 1 s 1
  • E nd s 10E n s E d s
  • E e1 e2 s E e1 s E e2 s

11
Semantics of Imperative Programs
  • Syntax
  • P xe if B then P else P PP
    while B do P
  • Semantics
  • C Programs (State ? State)
  • State (Variables ? Values)
  • would be locations ? values if we wanted to
    model aliasing

Every imperative program can be translated into a
functional program in a relatively simple,
syntax-directed way.
12
Semantics of Assignment
  • C x e
  • is a function states ? states
  • C x e s s
  • where s variables ? values is identical to s
    except
  • s(x) E e s gives the value of e in
    state s

13
Semantics of Conditional
  • C if B then P else Q
  • is a function states ? states
  • C if B then P else Q s
  • C P s if E B s is true
  • C Q s if E B s is false
  • Simplification assume B cannot diverge or have
    side effects

14
Semantics of Iteration
  • C while B do P
  • is a function states ? states
  • C while B do P
  • the function f such that
  • f(s) s if E B s
    is false
  • f(s) f( C P (s) ) if E B s
    is true

15
Denotational Semantics
  • Using denotational semantics, the meaning of a
    program can be defined as
  • M Program ? ?
  • M (Program p) M (P.body, ltv1, undefgt, ltv2,
    undef gt, ltvn, undef gt
  • Normally abstract definition of a program
    consists of two parts declaration and body
  • Denotational semantics is straight forward to
    implement

16
Denotational Semantics
  • State M (Program p)
  • return M (p.body,initialState(p.decpart))
  • State initialState (Declarations d)
  • State sigma new State()
  • Value undef new Value()
  • for (int i 0 i lt d.size() i)
  • sigma.put(((Declaration)(d.elementAt(i))).v,
    undef)
  • return sigma

17
Denotational Semantics
  • Some state-transforming functions
  • Expression
  • M expression ? state ? value
  • M (expression e, state ?)
  • e if e is a value
  • ?(e) if e is a variable
  • ApplyBinary (e.op, M(e.term1, ?), M(e.term2,
    ?) ) if e is a binary

18
Denotational Semantics
  • Some state-transforming functions
  • Conditional
  • M conditional ? state ? state
  • M (conditional c, state ?)
  • M (c.thenpart, ?) if M (c.test, ?) is true
  • M (c.elsepart, ?) if M (c.test, ?) is false

19
Operational vs Denotational Semantics
  • The difference between denotational and
    operational semantics
  • In operational semantics, the state changes are
    defined by coded algorithms
  • In denotational semantics, they are defined by
    rigorous mathematical functions

20
Denotational Semantics of Expressions
  • Me(ltexprgt, s) ?
  • case ltexprgt of
  • ltdec_numgt gt Mdec(ltdec_numgt, s)
  • ltvargt gt
  • if VARMAP(ltvargt, s) undef
  • then error
  • else VARMAP(ltvargt, s)
  • ltbinary_exprgt gt
  • if (Me(ltbinary_exprgt.ltleft_exprgt, s)
    undef
  • OR Me(ltbinary_exprgt.ltright_exprgt,
    s)
  • undef)
  • then error
  • else
  • if (ltbinary_exprgt.ltoperatorgt then
  • Me(ltbinary_exprgt.ltleft_exprgt, s)
  • Me(ltbinary_exprgt.ltright_exprgt, s)
  • else Me(ltbinary_exprgt.ltleft_exprgt, s)
  • Me(ltbinary_exprgt.ltright_exprgt, s)
  • ...

21
Denotational Semantics of Assignment Statement
  • Assignment Statements
  • Maps state sets to state sets
  • Ma(x E, s) ?
  • if Me(E, s) error
  • then error
  • else s lti1,v1gt,lti2,v2gt,...,ltin,vn
    gt,
  • where for j 1, 2, ..., n,
  • vj VARMAP(ij, s) if ij ltgt x
  • Me(E, s) if ij x

22
Denotational Semantics of Loops
  • Logical Pretest Loops
  • Maps state sets to state sets
  • 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))

23
Denotational Semantics of Loops
  • 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

24
Evaluation of Denotational Semantics
  • 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, they are of little use
    to language users
Write a Comment
User Comments (0)
About PowerShow.com