1 Operational Semantics Mooly Sagiv Tel Aviv University 640-6706 Textbook: Semantics with Applications.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Models of Concurrency Manna, Pnueli.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Program Analysis and Verification
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
A Semantic Characterization of Unbounded-Nondeterministic Abstract State Machines Andreas Glausch and Wolfgang Reisig 1.
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
1 Iterative Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
4/17/2017 Section 3.6 Program Correctness ch3.6.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Describing Syntax and Semantics
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
A Seminar on Encapsulation Noam Rinetzky Mooly Sagiv Summary.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 2: Operational Semantics I Roman Manevich Ben-Gurion University.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Eran Yahav 1. Previously…  An algorithmic view  Abstract data types (ADT)  Correctness Conditions  Sequential consistency  Linearizability  Treiber’s.
Formal Semantics of Programming Languages 虞慧群 Topic 1: Introduction.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Chapter 3 Part II Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Program Analysis and Verification Noam Rinetzky Lecture 2: Operational Semantics 1 Slides credit: Tom Ball, Dawson Engler, Roman Manevich, Erik.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Compiler Principles Fall Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
Program Analysis and Verification
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Program Analysis and Verification
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Program Analysis and Verification Noam Rinetzky Lecture 2: Operational Semantics 1 Slides credit: Tom Ball, Dawson Engler, Roman Manevich, Erik.
Describing Syntax and Semantics
Introduction to Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Program Analysis and Verification
Syntax Questions 6. Define a left recursive grammar rule.
Spring 2017 Program Analysis and Verification Operational Semantics
Chapter 3 Describing Syntax and Semantics.
Program correctness Axiomatic semantics
Spring 2016 Program Analysis and Verification Operational Semantics
Presentation transcript:

1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter 2 H. Nielson and F. Nielson

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

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

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

5 Alternative Formal Semantics u Operational Semantics –The meaning of the program is described “operationally” –Natural Operational Semantics –Structural Operational Semantics u Denotational Semantics –The meaning of the program is an input/output relation –Mathematically challenging but complicated u 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 (y>0) { z = z * y ; y = y – 1; } return z } [x  3] [x  3, z , y  ] [x  3, z  1, y  ] [x  3, z  1, y  3] [x  3, z  3, y  3] [x  3, z  3, y  2]

7 int fact(int x) { int z, y; z = 1; y= x while (y>0) { z = z * y ; y = y – 1; } return z } [x  3, z  3, y  2] [x  3, z  6, y  2] [x  3, z  6, y  1] [x  3, z  3, y  2]

8 int fact(int x) { int z, y; z = 1; y= x while (y>0) { z = z * y ; y = y – 1; } return z } [x  3, z  6, y  1] [x  3, z  6, y  0] [x  3, z  6, y  1]

9 int fact(int x) { int z, y; z = 1; y= x while (y>0) { z = z * y ; y = y – 1; } return z } [x  3, z  6, y  0]

10 int fact(int x) { int z, y; z = 1; y= x; while (y>0) { z = z * y ; y = y – 1; } return 6 } [x  3, z  6, y  0]

11 x. if x = 0 then 1 else x * f(x -1) Denotational Semantics int fact(int x) { int z, y; z = 1; y= x ; while (y>0) { z = z * y ; y = y – 1; } return z; }

12 {x=n} int fact(int x) { int z, y; z = 1; {x=n  z=1} y= x {x=n  z=1  y=n} while {x=n  y  0  z=n! / y!} (y>0) { {x=n  y >0  z=n! / y!} z = z * y ; {x=n  y>0  z=n!/(y-1)!} y = y – 1; {x=n  y  0  z=n!/y!} } return z} {x=n  z=n!} Axiomatic Semantics

13 Static Analysis u Automatic derivation of static properties which hold on every execution leading to a program location

14 Example Static Analysis Problem u Find variables with constant value at a given program location u 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); }

15 Abstract (Conservative) interpretation abstract representation Set of states concretization Abstract semantics statement s abstract representation abstraction Operational semantics statement s Set of states

16 Benefits of Operational Semantics for Static Analysis u Correctness (soundness) of the analysis –The compiler will never change the meaning of the program u Establish the right mindset u Design the analysis u Becomes familiar with mathematical notations used in programming languages

17 The While Programming Language u Abstract syntax S::= x := a | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S u Use parenthesizes for precedence u Informal Semantics –skip behaves like no-operation –Import meaning of arithmetic and Boolean operations

18 Example While Program y := 1; while  (x=1) do ( y := y * x; x := x - 1 )

19 General Notations u 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 u Semantic categories –Natural values N={0, 1, 2, …} –Truth values T={ff, tt} –States State = Var  N –Lookup in a state s: s x –Update of a state s: s [ x  5]

20 Example State Manipulations u [x  1, y  7, z  16] y = u [x  1, y  7, z  16] t = u [x  1, y  7, z  16][x  5] = u [x  1, y  7, z  16][x  5] x = u [x  1, y  7, z  16][x  5] y =

21 Semantics of arithmetic expressions u Assume that arithmetic expressions are side-effect free u A  Aexp  : State  N u Defined by induction on the syntax tree –A  n  s = n –A  x  s = s x –A  e 1 + e 2  s = A  e 1  s + A  e 2  s –A  e 1 * e 2  s = A  e 1  s * A  e 2  s –A  ( e 1 )  s = A  e 1  s --- not needed –A  - e 1  s = -A  e 1  s u Compositional u Properties can be proved by structural induction

22 Semantics of Boolean expressions u Assume that Boolean expressions are side-effect free u B  Bexp  : State  T u Defined by induction on the syntax tree –B  true  s = tt –B  false  s = ff –B  x  s = s x –B  e 1 = e 2  s = –B  e 1  e 2  s = –B  e 1  e 2  s = tt if A  e 1  s = A  e 2  s ff if A  e 1  s  A  e 2  s tt if B  e 1  s = tt and B  e 2  =tt ff if B  e 1  s=ff or B  e 2  s=ff

23 Natural Operational Semantics u Describe the “overall” effect of program constructs u Ignore non terminating computations

24 Natural Semantics u Notations – - the program statement S is executed on input state s –s representing a terminal (final) state u For every statement S, write meaning rules  o “If the statement S is executed on an input state i, it terminates and yields an output state o” u The meaning of a program P on an input state i is the set of outputs states o such that  o u The meaning of compound statements is defined using the meaning of immediate constituent statements

25 Natural Semantics for While [ass ns ]  s[x  A  a  s] [skip ns ]  s [comp ns ]  s’,  s’’  s’’ [if tt ns ]  s’  s’ if B  b  s=tt [if ff ns ]  s’  s’ if B  b  s=ff axioms rules

26 Natural Semantics for While (Loop rules) [while tt ns ]  s’,  s’’  s’’ if B  b  s=tt [while ff ns ]  s if B  b  s=ff

27 Simple Examples [comp ns ]  s 0,  s 0 [x  1]  s 0 [x  1] u Let s 0 be the state which assigns zero to all program variables u Assignments [ass ns ]  s 0 [x  1] u Skip statement [skip ns ]  s 0 u Composition

28 Simple Examples (Cont) [if tt ns ]  s 0  s 0 u Let s 0 be the state which assigns zero to all program variables u if-construct

29 A Derivation Tree u A “proof” that  s’ u The root of tree is  s’ u Leaves are instances of axioms u Internal nodes rules –Immediate children match rule premises u Simple Example  s 0 [x  1]>  s 0  s 0 [x  1]>

30 An Example Derivation Tree  s0[x  1][y  2][z  2]

31 Top Down Evaluation of Derivation Trees u Given a program S and an input state s u Find an output state s’ such that  s’ u Start with the root and repeatedly apply rules until the axioms are reached u Inspect different alternatives in order u In While s’ and the derivation tree is unique

32 Example of Top Down Tree Construction u Input state s such that s x = 3 u Factorial program y := 1; while  (x=1) do (y := y * x; x := x - 1)

33 Program Termination u Given a statement S and input s –S terminates on s if there exists a state s’ such that  s’ –S loops on s if there is no state s’ such that  s’ u 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

34 Semantic Equivalence u S 1 and S 2 are semantically equivalent if for all s and s’  s’ if and only if  s’ u Simple example “while b do S” is semantically equivalent to: “if b then (S ; while b do S) else skip”

35 Properties of Natural Semantics u Equivalence of program constructs –“skip ; skip” is semantically equivalent to “skip” –“((S 1 ; S 2 ) ; S 3 )” is semantically equivalent to “(S 1 ;( S 2 ; S 3 ))” –“(x := 5 ; y := x * 8)” is semantically equivalent to “(x :=5; y := 40)” u Deterministic –If  s 1 and  s 2 then s 1 =s 2

36 Deterministic Semantics for While u If  s 1 and  s 2 then s 1 =s 2 u 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

37 The Semantic Function S ns u The meaning of a statement S is defined as a partial function from State to State u S ns : Stm  (State  State) u S ns  S  s = s’ if  s’ and otherwise S ns  S  s is undefined u Examples –S ns  skip  s =s –S ns  x :=1  s = s [x  1] –S ns  while true do skip  s = undefined

38 Structural Operational Semantics u Emphasizes the individual steps u Usually more suitable for analysis u For every statement S, write meaning rules   “If the first step of executing the statement S on an input state i leads to  ” u Two possibilities for  –  = »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 u 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

39 Structural Semantics for While [ass sos ]  s[x  A  a  s] [skip sos ]  s [comp 1 sos ]   axioms rules [comp 2 sos ]  s’ 

40 Structural Semantics for While if construct [if tt sos ]  if B  b  s=tt [if ff os ]  if B  b  s=ff

41 Structural Semantics for While while construct [while sos ] 

42 Derivation Sequences u A finite derivation sequence starting at  0,  1,  2 …,  k such that –  0 = –  i   i+1 –  k is either stuck configuration or a final state u An infinite derivation sequence starting at  0,  1,  2 … such that –  0 = –  i   i+1 u  0  i  i in i steps u  0  *  i in finite number of steps u For each step there is a derivation tree

43 Example u Let s 0 such that s 0 x = 5 and s 0 y = 7 u S = (z:=x; x := y); y := z

44 Factorial Program u Input state s such that s x = 3 u y := 1; while  (x=1) do (y := y * x; x := x - 1)

45 Program Termination u Given a statement S and input s –S terminates on s if there exists a finite derivation sequence starting at –S terminates successfully on s if there exists a finite derivation sequence starting at leading to a final state –S loops on s if there exists an infinite derivation sequence starting at

46 Properties of the Semantics u S 1 and S 2 are semantically equivalent if: – for all s and s’  * s’ if and only if  * s’ –there is an infinite derivation sequence starting at if and only if there is an infinite derivation sequence starting at u Deterministic –If  * s 1 and  * s 2 then s 1 =s 2 u The execution of S 1 ; S 2 on an input can be split into two parts: –execute S 1 on s yielding a state s’ –execute S 2 on s’

47 Sequential Composition u If  k s’’ then there exists a state s’ and numbers k 1 and k 2 such that –  k1 s’ –  k2 s’’ –and k = k 1 + k 2 u 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 k+1 using the fact it holds on all sequences of length k (induction hypothesis)

48 The Semantic Function S sos u The meaning of a statement S is defined as a partial function from State to State u S sos : Stm  (State  State) u S sos  S  s = s’ if  * s’ and otherwise S sos  S  s is undefined

49 An Equivalence Result u For every statement S of the While language –S nat  S  = S sos  S 

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

51 The While Programming Language with Abort u Abstract syntax S::= x := a | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S| abort u Abort terminates the execution u No new rules are needed in natural and structural operational semantics u Statements –skip –abort –while true do skip

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

53 The Programming Language with Non-Determinism While u Abstract syntax S::= x := a | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S| S 1 or S 2 u Either S 1 or S 2 is executed u Example –x := 1 or (x :=2 ; x := x+2)

54 [or 1 ns ]  s’  s’ The While Programming Language with Non-Determinism Natural Semantics [or 2 ns ]  s’  s’

55 The While Programming Language with Non-Determinism Structural Semantics

56 The While Programming Language with Non-Determinism Examples u x := 1 or (x :=2 ; x := x+2) u (while true do skip) or (x :=2 ; x := x+2)

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

58 The While Programming Language with Parallel Constructs u Abstract syntax S::= x := a | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S| S 1 par S 2 u All the interleaving of S 1 or S 2 are executed u Example –x := 1 par (x :=2 ; x := x+2)

59 The While Programming Language with Parallel Constructs Structural Semantics [par 1 sos ]   [par 2 sos ]  s’  [par 3 sos ]   [par 4 sos ]  s’ 

60 The While Programming Language with Parallel Constructs Natural Semantics

61 Conclusion u In the natural semantics immediate constituent is an atomic entity so we cannot express interleaving of computations u In the structural operational semantics we concentrate on small steps so interleaving of computations can be easily expressed

62 The While Programming Language with local variables and procedures u Abstract syntax S::= x := a | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S| begin D v D p S end | call p D v ::= var x := a ; D v |  D p ::= proc p is S ; D p | 

63 Conclusions Local Variables u The natural semantics can “remember” local states u Need to introduce stack or heap into state of the structural semantics

64 Conclusions u Structural operational semantics allows us to simulate low level computations without getting bugged into too many details u Natural semantics allows to abstract more –Local memory –Non termination u Thinking in concrete semantics is essential for a compiler writer