Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.

Similar presentations


Presentation on theme: "Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type."— Presentation transcript:

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

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

3 Copyright © 2006 Addison-Wesley. All rights reserved. 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 machine- dependent 3-49

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

5 Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics (continued) L interpreter low-level code virtual machine hardware 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. 3-51

6 Copyright © 2006 Addison-Wesley. All rights reserved. Operational Semantics (continued) E.g. C StatementOperational Semantics for (expr; expr2; expr3) expr1; {loop: if expr2 ==0 goto out… }expr3; goto loop out: … 3-52

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

8 Copyright © 2006 Addison-Wesley. All rights reserved. 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} 3-54

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

10 Copyright © 2006 Addison-Wesley. All rights reserved. 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) 3-56

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

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

13 Copyright © 2006 Addison-Wesley. All rights reserved. Axiomatic Semantics: Assignment Statements An axiom for assignment statements {Q x->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. 3-59

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


Download ppt "Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type."

Similar presentations


Ads by Google