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. • At the h[r]
(1)Lecture 17
(2)Semantics
• How do we describe the “meaning” of a program?
• Dynamic semantics or semantics is concerned with accurately describing the execution behaviour of a language
• Why do we care?
– English descriptions are often incomplete and ambiguous
– Compiler writers must implement the language description accurately – Programmers want the same behaviour on different platforms
• There is no single widely acceptable notation or formalism for describing semantics
(3)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
• At the highest level, we’re interested in the final result (natural operational semantics)
• At the lowest level, look at a translated version to determine precise meaning of a single
(4)Operational Semantics Example
• C Statement
for (expr1; expr2; expr3) {
}
• Operational Statements expr1;
loop: if expr2 == goto out
expr3;
goto loop out:
(5)Operational Semantics (continued)
• A better alternative: A complete computer simulation
• The process:
– Build a translator (translates source code to the machine code of an idealized computer)
(6)Evaluation of Operational Semantics
• Good if used informally (language manuals, etc.) • Extremely complex if used formally (e.g., Vienna
Definition Language), it was used for describing semantics of PL/I
• Can lead to circularities, because statements of high level language are described in statements of lower level language
(7)Axiomatic Semantics
• Based on formal logic (predicate calculus) • Original purpose: formal program verification • Correctness proofs specify constraints on
program variables
(8)Axiomatic Semantics (continued)
• Logical expressions used in axiomatic semantics are called assertions
• 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
(9)Axiomatic Semantics Form
• Pre, post form: {P} statement {Q}
• An example
– a = b + {a > 1}
– One possible precondition: {b > 10}
– Weakest precondition: {b > 0}
(10)Program Proof Process
• The postcondition for the entire program is the desired result
– 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
• An axiom is a logical statement that is assumed to be true.