Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.

Similar presentations


Presentation on theme: "Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics."— Presentation transcript:

1 Formal Semantics of Programming Languages 虞慧群 yhq@ecust.edu.cn Topic 2: Operational Semantics

2 IMP: A Simple Imperative Language numbers N Positive and negative numbers n, m  N truth values T={true, false} locations Loc X, Y  Loc arithmetic Aexp a  Aexp boolean expressions Bexp b  Bexp commands Com c  Com

3 (3+5)  3 + 5 3 + 5  5+ 3 Abstract Syntax for IMP Aexp a ::= n | X | a 0 + a 1 | a 0 – a 1 | a 0  a 1 Bexp b ::= true | false | a 0 = a 1 | a 0  a 1 |  b | b 0  b 1 | b 0  b 1 Com c ::= skip | X := a | c 0 ; c 1 | if b then c 0 else c 1 | while b do c 2+3  4-5 (2+(3  4))-5 ((2+3)  4))-5

4 Example Program Y := 1; while  (X=1) do Y := Y * X; X := X - 1

5 But what about semantics

6 Expression Evaluation States Mapping locations to values  - The set of states  : Loc  N  (X)=  X=value of X in   = [ X  5, Y  7]  The value of X is 5  The value of Y is 7  The value of Z is undefined For a  Exp,   , n  N,  n  a is evaluated in  to n

7 Evaluating (a 0 + a 1 ) at  Evaluate a 0 to get a number n 0 at  Evaluate a 1 to get a number n 1 at  Add n 0 and n 1

8 Expression Evaluation Rules Numbers  n Locations   (X) Sums Subtractions Products Axioms

9 Derivations A rule instance Instantiating meta variables with corresponding values

10 Derivation (Tree) Axioms in the leafs Rule instances at internal nodes

11 Computing a derivation We write  n when there exists a derivation tree whose root is  n Can be computed in a top-down manner At every node try all derivations “in parallel” 5 16 21

12 Recap Operational Semantics The rules can be implemented easily Define interpreter Structural Operational Semantics Syntax directed Natural semantics

13 Equivalence of IMP expressions a0  a1a0  a1 iff

14 Boolean Expression Evaluation Rules  true  false

15 Boolean Expression Evaluation Rules(cont)

16 Equivalence of Boolean expressions b0 b1b0 b1 iff

17 Extensions Shortcut evaluation of Boolean expressions “Parallell” evaluation of Boolean expressions Other data types

18 The execution of commands   ’ c terminates on  in a final state  ’ Initial state  0  0 (X)=0 for all X Handling assignments   ’ A notation:   [5/X]

19 Rules for commands   Sequencing: Conditionals: Atomic commands

20 Rules for commands (while)

21 Example Program Y := 1; while  (X=1) do Y := Y * X; X := X - 1

22 Equivalence of commands c0 c1c0 c1 iff

23 Proposition 2.8 while b do c  if then (c; while b do c) else skip

24 Small Step Operational Semantics The natural semantics define evaluation in large steps Abstracts “computation time” It is possible to define a small step operational semantics  1 “one” step of executing a in a state  yields a’ in a state  ’

25 Small Step Semantics for Additions

26 Summary Operational semantics enables to naturally express program behavior Can handle Non determinism Concurrency Procedures Object oriented Pointers and dynamically allocated structures But remains very closed to the implementation Two programs which compute the same functions are not necessarily equivalent

27 Exercise 2 (1) Evaluate a  (X  4) + 3 in a state  s.t.  (X)=0. (2) Write down rules which express the “parallel” evaluation of b0 and b1 in b0 V b1 so that evaluates to true if either b0 evaluates to true, and b1 is unevaluated, or b1 evaluates to true, and b0 is unevaluated.


Download ppt "Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics."

Similar presentations


Ads by Google