CSC 3315 Languages Compilers Hamid Harroud School of

  • Slides: 25
Download presentation
CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University

CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University h. harroud@aui. ma http: //www. aui. ma/~H. Harroud/csc 3315/ CSC 3315 (Spring 2009) 1

Semantics n n There is no single widely acceptable notation or formalism for describing

Semantics n n There is no single widely acceptable notation or formalism for describing semantics Operational Semantics n n 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 “Program testing can be used to show the presence of bugs, but never to show their absence!” Dijkstra

Operational Semantics n n n To use operational semantics for a high-level language, a

Operational Semantics n n n To use operational semantics for a high-level language, a virtual machine is needed A hardware pure interpreter would be too expensive A software pure interpreter also has problems n n The detailed characteristics of the particular computer would make actions difficult to understand Such a semantic definition would be machinedependent

Axiomatic Semantics n n Based on formal logic (predicate calculus) Original purpose: formal program

Axiomatic Semantics n n Based on formal logic (predicate calculus) Original purpose: formal program verification Axioms or inference rules are defined for each statement type in the language (to allow transformations of expressions to other expressions) The expressions are called assertions

Axiomatic Semantics (cont. ) n “Thus the practice of proving programs would seem to

Axiomatic Semantics (cont. ) n “Thus the practice of proving programs would seem to lead to solution of three of the most pressing problems in software and programming, namely, reliability, documentation, and compatibility. However, program proving, certainly at present, will be difficult even for programmers of high caliber; and may be applicable only to quite simple program designs. ” C. A. R Hoare, 1969

Axiomatic Semantics (cont. ) n n n An assertion before a statement (a precondition)

Axiomatic Semantics (cont. ) n n n 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

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

Axiomatic Semantics Form n Pre-, post form: {P} statement {Q} n An example n n n a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0}

Program Proof Process n The postcondition for the entire program is the desired result

Program Proof Process n The postcondition for the entire program is the desired result n Work back through the program to the first statement. If the precondition on the first statement is the same as the program specification, the program is correct.

Axiomatic Semantics: Assignment n n An axiom for assignment statements {Qx e} x =

Axiomatic Semantics: Assignment n n An axiom for assignment statements {Qx e} x = e {Q} Example: { 0 >= 0 & 0 <= n & n > 0 } x = 0; { x >= 0 & x <= n & n > 0 } n Exercise: { ? } z = z + 1; { z <= N } { a > b } a = a – b; { ? }

Axiomatic Semantics: Inference n An inference rule for sequences {P 1} S 1 {P

Axiomatic Semantics: Inference n An inference rule for sequences {P 1} S 1 {P 2} S 2 {P 3}

Inference Rule Example x = 0; f = 1; while (x != n) {

Inference Rule Example x = 0; f = 1; while (x != n) { x = x + 1; f = f * x; } We want to prove: { f = x! } x = x + 1; f = f * x; { f = x! } P 1 S 2 P 3

Inference Rule Example n Looking for P 2 such that: { f = x!

Inference Rule Example n Looking for P 2 such that: { f = x! } x = x + 1; { P 2 } f = f * x; { f = x! } n We have: So: f = x! f = ((x + 1) – 1)! f = (x – 1)! x x + 1 f = x! P 1 S 1 P 2 { f = x! } x = x + 1; {f = (x – 1)! }

Inference Rule Example n Now, we can see that: f = (x – 1)!

Inference Rule Example n Now, we can see that: f = (x – 1)! f * x = (x – 1)! * x = x! So, f = x! f f * x f = (x – 1)! And therefore: {f = (x – 1)! } f = f * x; P 2 S 2 {f = x! } P 3

Axiomatic Semantics: if-else Assume {P & B } S 1 {R} and {P &

Axiomatic Semantics: if-else Assume {P & B } S 1 {R} and {P & B } S 2 {R} Then, we conclude {P} if ( B ) S 1 else S 2 {R}

If-else Example n if ( a < 0 ) b = -a; else b

If-else Example n if ( a < 0 ) b = -a; else b = a; n We want to prove that R {b = abs(a)} is true: {true} if (a<0) b = -a; else b = a; {b =abs(a) } P B S 1 S 2 R

If-else Example n Assume B is true & a < 0 – a =

If-else Example n Assume B is true & a < 0 – a = abs(a) Using the axiom for the assignment: {– a = abs(a)} b = -a; {b = abs(a)} n Assume B is false: true & a < 0 a = abs(a) then: {a = abs(a)} b = a; {b = abs(a)}

Axiomatic Semantics: Loops n An inference rule for logical pretest loops {P} while (B)

Axiomatic Semantics: Loops n An inference rule for logical pretest loops {P} while (B) S {Q} where I is the loop invariant (the inductive hypothesis) A loop invariant is a condition which is satisfied immediately before a loop, remain true during its execution, and is alaways satisfied at the end fo the loop.

Axiomatic Semantics: Loops n Characteristics of the loop invariant: I must meet the following

Axiomatic Semantics: Loops n Characteristics of the loop invariant: I must meet the following conditions: n n n P => I -- the loop invariant must be true initially {I} B {I} -- evaluation of the Boolean must not change the validity of I {I and B} S {I} -- I is not changed by executing the body of the loop (I and (not B)) => Q -- if I is true and B is false, Q is implied The loop terminates

Loop Invariant n n The loop invariant I is a weakened version of the

Loop Invariant n n The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition

Loop Invariant Example x = 0; while ( x = x f = f

Loop Invariant Example x = 0; while ( x = x f = f } f x + * = 1; != n ) { 1; x; { f = x! } while ( x = x f = f } { f = x! & We know that: { f = x! } x = x + 1; f = f * x; { f = x! } x != n ) { + 1; * x; x = n} I B S I& B

Loop Invariant Example (2) {n>0} x = 1; while ( x = p =

Loop Invariant Example (2) {n>0} x = 1; while ( x = p = } {? } p x x p = A; != n ) { + 1; * A;

Evaluation of Axiomatic Semantics n n n Developing axioms or inference rules for all

Evaluation of Axiomatic Semantics n n n Developing axioms or inference rules for all of the statements in a language is difficult It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers Its usefulness in describing the meaning of a programming language is limited for language users or compiler writers

Denotational Semantics n n n Based on recursive function theory The most abstract semantics

Denotational Semantics n n n Based on recursive function theory The most abstract semantics description method Originally developed by Scott and Strachey (1970)

Denotational Semantics (continued) n The process of building a denotational specification for a language

Denotational Semantics (continued) n The process of building a denotational specification for a language n n n Define a mathematical object for each language entity Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects The meaning of language constructs are defined by only the values of the program's variables

Summary n BNF and context-free grammars are equivalent meta -languages n n Well-suited for

Summary n BNF and context-free grammars are equivalent meta -languages n n Well-suited for describing the syntax of programming languages Three primary methods of semantics description n Operational, Axiomatic, Denotational