Operational Semantics - PowerPoint PPT Presentation

About This Presentation
Title:

Operational Semantics

Description:

Automatic compiler generation. Alternative Formal Semantics. Operational Semantics ... { int z, y; z = 1; y= x. while (y 0) { z = z * y ; y = y 1; return z [x ... – PowerPoint PPT presentation

Number of Views:268
Avg rating:3.0/5.0
Slides: 66
Provided by: Dor103
Category:

less

Transcript and Presenter's Notes

Title: Operational Semantics


1
Operational Semantics
  • Mooly Sagiv
  • http//www.math.tau.ac.il/sagiv/courses/pa.html
  • Tel Aviv University
  • 640-6706
  • Textbook Semantics with Applications
  • Chapter 2
  • H. Nielson and F. Nielsonhttp//www.daimi.au.dk/
    bra8130/Wiley_book/wiley.html

2
Outline
  • Why formal semantics?
  • Possible formal semantics
  • A Simple programming language While
  • Natural Operational Semantics for While
  • Structural Operational Semantics for While
  • Equivalence Result
  • Extensions to While
  • Abort
  • Non determinism
  • Parallel constructs
  • Blocks and procedures

3
Syntax vs. Semantics
  • The pattern of formation of sentences or phrases
    in a language
  • Examples
  • Regular expressions
  • Context free grammars
  • The study or science of meaning in language
  • Examples
  • Interpreter
  • Compiler
  • Better mechanisms will be given today

4
Benefits of Formal Semantics
  • Programming language design
  • hard- to-define hard-to-implementhard-to-use
  • resolve ambiguities
  • Programming language implementation
  • Programming language understanding
  • Program correctness
  • Program equivalence
  • Compiler Correctness
  • Correctness of Static Analysis
  • Design of Static Analysis
  • Automatic generation of interpreter
  • But probably not
  • Automatic compiler generation

5
Alternative Formal Semantics
  • Operational Semantics
  • The meaning of the program is described
    operationally
  • Natural Operational Semantics
  • Structural Operational Semantics
  • Denotational Semantics
  • The meaning of the program is an input/output
    relation
  • Mathematically challenging but complicated
  • Axiomatic Semantics
  • Logical axioms
  • The meaning of the program are observed properties

6
int fact(int x) int z, y z 1 y x
while (ygt0) z z y y
y 1 return z
7
int fact(int x) int z, y z 1 y x
while (ygt0) z z y y
y 1 return z
8
int fact(int x) int z, y z 1 y x
while (ygt0) z z y y
y 1 return z
9
int fact(int x) int z, y z 1 y x
while (ygt0) z z y y
y 1 return z
10
Denotational Semantics
int fact(int x) int z, y z 1 y x
while (ygt0) z z y y
y 1 return z
?x. if x 0 then 1 else x f(x -1)
11
Axiomatic Semantics
xn int fact(int x) int z, y z
1 xn ? z1 y x xn ? z1 ? yn while
(ygt0) xn ? y ?0 ? zn! / y! xn
? y gt0 ? zn! / y! z z y
xn ? ygt0 ? zn!/(y-1)! y y 1
xn ? y ?0 ? zn!/(y-1)! return
z xn ? zn!
12
Static Analysis
  • Automatic derivation of static properties which
    hold on every execution leading to a
    programlocation

13
Example Static Analysis Problem
  • Find variables with constant value at a given
    program location
  • Example program

int p(int x) return x x void main() int
z if (getc()) z p(6) 8 else z p(5)
7 printf (z)
14
Abstract (Conservative) interpretation
abstract representation
15
Example rule of signs
  • Safely identify the sign of variables at every
    program location
  • Abstract representation P, N, ?
  • Abstract (conservative) semantics of

16
Abstract (conservative) interpretation
ltN, Ngt
17
Example rule of signs (cont)
  • Safely identify the sign of variables at every
    program location
  • Abstract representation P, N, ?
  • ?(C) if all elements in C are positive
    then return P
    else if all elements in C are negative
    then return N
    else return ?
  • ?(a) if (aP) then
    return0, 1, 2,
    else if (aN) return -1, -2, -3, ,
    else return Z

18
Benefits of Operational Semanticsfor Static
Analysis
  • Correctness (soundness) of the analysis
  • The compiler will never change the meaning of the
    program
  • Establish the right mindset
  • Design the analysis
  • Becomes familiar with mathematical notations used
    in programming languages

19
The While Programming Language
  • Abstract syntaxS x a skip S1 S2
    if b then S1 else S2 while b do S
  • Use parenthesizes for precedence
  • Informal Semantics
  • skip behaves like no-operation
  • Import meaning of arithmetic and Boolean
    operations

20
Example While Program
y 1 while ?(x1) do ( y y x x x -
1 )
21
General Notations
  • Syntactic categories
  • Var the set of program variables
  • Aexp the set of arithmetic expressions
  • Bexp the set of Boolean expressions
  • Stm set of program statements
  • Semantic categories
  • Natural values N0, 1, 2,
  • Truth values Tff, tt
  • States State Var ? N
  • Lookup in a state s s x
  • Update of a state s s x ? 5

22
Example State Manipulations
  • x?1, y?7, z?16 y
  • x?1, y?7, z?16 t
  • x?1, y?7, z?16x?5
  • x?1, y?7, z?16x?5 x
  • x?1, y?7, z?16x?5 y

23
Semantics of arithmetic expressions
  • Assume that arithmetic expressions are
    side-effect free
  • A? Aexp ? State ? N
  • Defined by induction on the syntax tree
  • A? n ? s n
  • A? x ? s s x
  • A? e1 e2 ? s A? e1 ? s A ? e2 ? s
  • A? e1 e2 ? s A? e1 ? s A ? e2 ? s
  • A? ( e1 ) ? s A? e1 ? s --- not needed
  • A? - e1 ? s -A ? e1 ? s

24
Semantics of Boolean expressions
  • Assume that Boolean expressions are side-effect
    free
  • B? Bexp ? State ? T
  • Defined by induction on the syntax tree
  • B? true ? s tt
  • B? false ? s ff
  • B? x ? s s x
  • B? e1 e2 ? s
  • B? e1 ? e2 ? s
  • B? e1 ?e2 ? s

25
Natural Operational Semantics
  • Describe the overall effect of program
    constructs
  • Ignore non terminating computations

26
Natural Semantics
  • Notations
  • ltS, sgt - the program statement S is executed on
    input state s
  • s representing a terminal (final) state
  • For every statement S, write meaning rulesltS, igt
    ? oIf the statement S is executed on an input
    state i, it terminates and yields an output state
    o
  • The meaning of a program P on an input state s is
    the set of outputs states o such that ltP, igt ? o
  • The meaning of compound statements is defined
    using the meaning of immediate constituent
    statements

27
Natural Semantics for While
assns ltx a, sgt ? sx ?A?a?s skipns ltskip,
sgt ? s
axioms
rules
28
Natural Semantics for While(More rules)
whilettns ltS , sgt ? s, ltwhile b do S, sgt ?
s ltwhile b do S, sgt ? s
if B?b?stt
29
Simple Examples
  • Let s0 be the state which assigns zero to all
    program variables
  • Assignments assns ltx x1, s0gt ? s0x ?1
  • Skip statementskipns ltskip, s0gt ? s0
  • Composition

compns ltskip ,s0gt ? s0, ltx x1, s0gt ? s0x
?1 ltskip x x 1, s0gt ?s0x ?1
30
Simple Examples (Cont)
  • Let s0 be the state which assigns zero to all
    program variables
  • if-construct

ifttns ltskip ,s0gt ? s0 ltif x0
then skip else x x 1, s0gt ?s0
31
A Derivation Tree
  • A proof that ltS, sgt ?s
  • The root of tree is ltS, sgt ?s
  • Leaves are instances of axioms
  • Internal nodes rules
  • Immediate children match rule premises
  • Simple Example

ltskip x x 1, s0gt ?s0x ?1gt
ltskip, s0gt ?s0
lt x x 1, s0gt ?s0x ?1gt
32
An Example Derivation Tree
lt(x x1 y x1) z y), s0gt ?s0x ?1y
?2z ?2
33
Top Down Evaluation of Derivation Trees
  • Given a program S and an input state s
  • Find an output state s such that ltS, sgt ?s
  • Start with the root and repeatedly apply rules
    until the axioms are reached
  • Inspect different alternatives in order
  • In While s and the derivation tree is unique

34
Example of Top Down Tree Construction
  • Input state s such that s x 3
  • Factorial program

y 1 while ?(x1) do (y y x x x - 1)
35
Program Termination
  • Given a statement S and input s
  • S terminates on s if there exists a state s such
    thatltS, sgt ? s
  • S loops on s if there is no state s such that
    ltS, sgt ? s
  • Given a statement S
  • S always terminates if for every input state s, S
    terminates on s
  • S always loops if for every input state s, S
    loops on s

36
Properties of Natural Semantics
  • Equivalence of program constructs
  • skip skip is semantically equivalent to
    skip
  • ((S1 S2) S3) is semantically equivalent to
    (S1 ( S2 S3))
  • (x 5 y x 8) is semantically
    equivalent to(x 5 y 40)
  • Deterministic
  • If ltS, sgt ? s1 and ltS, sgt ? s2 then s1s2

37
Semantic Equivalence
  • S1 and S2 are semantically equivalent if for all
    s and sltS1, sgt ? s if and only if ltS2, sgt ? s
  • Simple examplewhile b do Sis semantically
    equivalent toif b then (S while b do S) else
    skip

38
Deterministic Semantics for While
  • If ltS, sgt ? s1 and ltS, sgt ? s2 then s1s2
  • The proof uses induction on the shape of
    derivation trees
  • Prove that the property holds for all simple
    derivation trees by showing it holds for axioms
  • Prove that the property holds for all composite
    trees
  • For each rule assume that the property holds for
    its premises (induction hypothesis) and prove it
    holds for the conclusion of the rule

39
The Semantic Function Sns
  • The meaning of a statement S is defined as a
    partial function from State to State
  • Sns Stm ? (State ? State)
  • Sns ?S?s s if ltS, sgt ?s and otherwise Sns
    ?S?s is undefined
  • Examples
  • Sns ?skip?s s
  • Sns ?x 1?s s x ?1
  • Sns ?while true do skip?s undefined

40
Structural Operational Semantics
  • Emphasizes the individual steps
  • Usually more suitable for analysis
  • For every statement S, write meaning rules ltS, igt
    ? ?If the first step of executing the statement
    S on an input state i leads to ?
  • Two possibilities for ?
  • ? ltS, sgt The execution of S is not completed,
    S is the remaining computation which need to be
    performed on s
  • ? o The execution of S has terminated with a
    final state o
  • ? is a stuck configuration when there are no
    transitions
  • The meaning of a program P on an input state s is
    the set of final states that can be executed in
    arbitrary finite steps

41
Structural Semantics for While
asssos ltx a, sgt ? sx ?A?a?s skipsos
ltskip, sgt ? s
axioms
rules
42
Structural Semantics for Whileif construct
43
Structural Semantics for Whilewhile construct
whilesos ltwhile b do S, sgt ?
ltif b then (S while b do S) else skip, sgt

44
Derivation Sequences
  • A finite derivation sequence starting at ltS,
    sgt?0, ?1, ?2 , ?k such that
  • ?0ltS, sgt
  • ?i ? ?i1
  • ?k is either stuck configuration or a final state
  • An infinite derivation sequence starting at ltS,
    sgt?0, ?1, ?2 such that
  • ?0ltS, sgt
  • ?i ? ?i1
  • ?0 ?i ?i in i steps
  • ?0 ? ?i in finite number of steps
  • For each step there is a derivation tree

45
Example
  • Let s0 such that s0 x 5 and s0 y 7
  • S (zx x y) y z

46
Factorial Program
  • Input state s such that s x 3

y 1 while ?(x1) do (y y x x x - 1)
47
Program Termination
  • Given a statement S and input s
  • S terminates on s if there exists a finite
    derivation sequence starting at ltS, sgt
  • S terminates successfully on s if there exists a
    finite derivation sequence starting at ltS, sgt
    leading to a final state
  • S loops on s if there exists an infinite
    derivation sequence starting at ltS, sgt

48
Properties of the Semantics
  • S1 and S2 are semantically equivalent if
  • for all s and sltS1, sgt ? s if and only if
    ltS2, sgt ?s
  • there is an infinite derivation sequence starting
    at ltS1, sgt if and only if there is an infinite
    derivation sequence starting at ltS2, sgt
  • Deterministic
  • If ltS, sgt ? s1 and ltS, sgt ? s2 then s1s2
  • The execution of S1 S2 on an input can be split
    into two parts
  • execute S1 on s yielding a state s
  • execute S2 on s

49
Sequential Composition
  • If ltS1 S2, sgt ?k s then there exists a state
    s and numbers k1 and k2 such that
  • ltS1, sgt ?k1 s
  • ltS2, sgt ?k2 s
  • and k k1 k2
  • The proof uses induction on the length of
    derivation sequences
  • Prove that the property holds for all derivation
    sequences of length 0
  • Prove that the property holds for all other
    derivation sequences
  • Show that the property holds for sequences of
    length k1 using the fact it holds on all
    sequences of length k (induction hypothesis)

50
The Semantic Function Ssos
  • The meaning of a statement S is defined as a
    partial function from State to State
  • Ssos Stm ? (State ? State)
  • Ssos?S?s s if ltS, sgt ?s and otherwise Ssos
    ?S?s is undefined

51
An Equivalence Result
  • For every statement S of the While language
  • Snat?S? Ssos?S?

52
Extensions to While
  • Abort statement (like C exit)
  • Non determinism
  • Parallelism
  • Local Variables
  • Procedures
  • Static Scope
  • Dynamic scope

53
The While Programming Language with Abort
  • Abstract syntaxS x a skip S1 S2
    if b then S1 else S2 while b do S
    abort
  • Abort terminates the execution
  • No new rules are needed in natural and
    structural operational semantics
  • Statements
  • skip
  • abort
  • while true do skip

54
Conclusion
  • The natural semantics cannot distinguish between
    looping and abnormal termination (unless the
    states are modified)
  • In the structural operational semantics looping
    is reflected by infinite derivations and abnormal
    termination is reflected by stuck configuration

55
The Programming Language with Non-Determinism
While
  • Abstract syntaxS x a skip S1 S2
    if b then S1 else S2 while b do S S1
    or S2
  • Either S1 or S2 is executed
  • Example
  • x 1 or (x 2 x x2)

56
The While Programming Language with
Non-DeterminismNatural Semantics
57
The While Programming Language with
Non-DeterminismStructural Semantics
58
The While Programming Language with
Non-DeterminismExamples
  • x 1 or (x 2 x x2)
  • (while true do skip) or (x 2 x x2)

59
Conclusion
  • In the natural semantics non-determinism will
    suppress looping if possible (mnemonic)
  • In the structural operational semantics
    non-determinism does not suppress looping

60
The While Programming Language with Parallel
Constructs
  • Abstract syntaxS x a skip S1 S2
    if b then S1 else S2 while b do S S1
    par S2
  • All the interleaving of S1 or S2 are executed
  • Example
  • x 1 par (x 2 x x2)

61
The While Programming Language with Parallel
ConstructsStructural Semantics
62
The While Programming Language with Parallel
ConstructsNatural Semantics
63
Conclusion
  • In the natural semantics immediate constituent is
    an atomic entity so we cannot express
    interleaving of computations
  • In the structural operational semantics we
    concentrate on small steps so interleaving of
    computations can be easily expressed

64
The While Programming Language with local
variables and procedures
  • Abstract syntaxS x a skip S1 S2
    if b then S1 else S2 while b do S
    begin Dv Dp S end call pDv var x a
    Dv ?Dp proc p is S Dp ?

65
Conclusions Local Variables
  • The natural semantics can remember local states
  • Need to introduce stack or heap into state of the
    structural semantics

66
Conclusions
  • Structural operational semantics allows us to
    simulate low level computations without getting
    bugged into too many details
  • Natural semantics allows to abstract more
  • Local memory
  • Non termination
  • Thinking in concrete semantics is essential for a
    compiler writer
Write a Comment
User Comments (0)
About PowerShow.com