3 5 Dynamic Semantics Meanings of expressions statements

  • Slides: 14
Download presentation
3. 5 Dynamic Semantics • Meanings of expressions, statements, and program units • Static

3. 5 Dynamic Semantics • Meanings of expressions, statements, and program units • Static semantics – type specifications • Dynamic semantics – briefed as semantics, meaning • There is no single widely acceptable notation or formalism for describing semantics • Will describe several of these methods that have been developed – Operational semantics – Axiomatic semantics – Denotational semantics Copyright © 2006 Addison-Wesley. All rights reserved. 3 -47

Operational Semantics • Describe the meaning of a program by executing its statements on

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 highlevel language, a real or a virtual machine is needed Copyright © 2006 Addison-Wesley. All rights reserved. 3 -48

Operational Semantics • A pure interpreter for any programming language can be constructed in

Operational Semantics • A pure interpreter for any programming language can be constructed in software. It has problems – The detailed characteristics of the particular computer would make actions difficult to understand – Such a semantic definition would be machinedependent Copyright © 2006 Addison-Wesley. All rights reserved. 3 -49

Operational Semantics (continued) • A better alternative: A complete computer simulation – a low-level

Operational Semantics (continued) • A better alternative: A complete computer simulation – a low-level virtual machine • The process: – Build a translator (translates source code to the machine code of an idealized computer) – Build a simulator for the idealized computer Copyright © 2006 Addison-Wesley. All rights reserved. 3 -50

Operational Semantics (continued) L interpreter low-level code virtual machine The state changes in the

Operational Semantics (continued) L interpreter low-level code virtual machine The state changes in the virtual machine brought about by executing the code that results from translating a given statement in the high-level language defines the meaning of that statement. In effect, this approach describes the meaning of high-level language statements in terms of statements in a simpler, lower-level language. hardware Copyright © 2006 Addison-Wesley. All rights reserved. 3 -51

Operational Semantics (continued) • E. g. C Statement for (expr; expr 2; expr 3)

Operational Semantics (continued) • E. g. C Statement for (expr; expr 2; expr 3) { … } Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics expr 1; loop: if expr 2 ==0 goto out … expr 3; goto loop out: … 3 -52

3. 5 Axiomatic Semantics • Used to prove the correctness of programs • In

3. 5 Axiomatic Semantics • Used to prove the correctness of programs • In a proof, each statement of a program is both preceded and followed by a logical expression that specifies constraints on program variables • The logical expressions are called assertions • E. g. {x>10} sum = 2*x + 1 {sum>1} Copyright © 2006 Addison-Wesley. All rights reserved. 3 -53

Axiomatic Semantics (continued) • An assertion before a statement (a precondition) states the relationships

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. E. g. {x>10}, {x>50}, {x>100} are all valid preconditions. The weakest of all is {x>0} Copyright © 2006 Addison-Wesley. All rights reserved. 3 -54

Axiomatic Semantics (continued) • If the weakest precondition can be computed from the given

Axiomatic Semantics (continued) • If the weakest precondition can be computed from the given postcondition for each statement of a language, then correctness proofs can be constructed for programs in that language. The proof is begun by using the desired results of the program’s execution as the postcondition of the last statement of the program. This postcondition, along with the last statement is used to compute the weakest precondition for the last statement. This precondition is then used as the postcondition for the second last statement. This process continues until the beginning of the program is reached. At that point, the precondition of the first statement states the condition under which the program will compute the desired results. If the condition is implied by the input specification of the program, the program has been verified to be correct. Copyright © 2006 Addison-Wesley. All rights reserved. 3 -55

Axiomatic Semantics (continued) (precondition) – match with input? 1 st statement (postcondition/precondition) 2 nd

Axiomatic Semantics (continued) (precondition) – match with input? 1 st statement (postcondition/precondition) 2 nd statement (postcondition/precondition) 3 rd statement (postcondition/precondition) … last statement (postcondition/result) Copyright © 2006 Addison-Wesley. All rights reserved. 3 -56

Axiomatic Semantics Form • Pre-, post form: {P} statement {Q} • An example –

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 © 2006 Addison-Wesley. All rights reserved. 3 -57

Axiomatic Semantics: Assignment Statements • Let x = E be a general assignment statement

Axiomatic Semantics: Assignment Statements • Let x = E be a general assignment statement and Q be its postcondition. Its precondition P is defined by the axiom P = Q x->E which means that P is computed as Q with all instances of x replaced by E • E. g. a = b/2 – 1 {a<10} The weakest precondition is computed by b / 2 – 1 < 10 b < 22 Copyright © 2006 Addison-Wesley. All rights reserved. 3 -58

Axiomatic Semantics: Assignment Statements • An axiom for assignment statements {Qx->E} x = E

Axiomatic Semantics: Assignment Statements • An axiom for assignment statements {Qx->E} x = E {Q} • Prove the following statement is correct: {x>5} x = x – 3 { x>0} The computed preconditon is x > 3. x > 5 implies x > 3. Copyright © 2006 Addison-Wesley. All rights reserved. 3 -59

Axiomatic Semantics: Assignment Statements • An inference rule called “The Rule of Consequence” •

Axiomatic Semantics: Assignment Statements • An inference rule called “The Rule of Consequence” • General form: If S 1, S 2, …, and Sn are true, then the S 1, S 2, …, Sn truth of S can be inferred. S • The form here: (=> Means “implies”) • E. g. {x>3}x=x-3{x>0}, {x>5}=>{x>3}, {x>0}=>{x>0} {x>5}x=x-3{x>0} Copyright © 2006 Addison-Wesley. All rights reserved. 3 -60