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]
Trang 1Describing Syntax
and Semantics
Trang 2• 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
• Entire books have been dedicated to various semantic notations!
Trang 3• 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
statement ( structural operational semantics )
Trang 4• C Statement
for (expr1; expr2; expr3)
{
.
}
• Operational Statements
expr1;
loop: if expr2 == 0 goto out
expr3;
goto loop out:
Human reader is virtual computer, assumed to be able to
correctly “execute” the instructions and recognize the
effects. Note that language is intermediate level, not
Trang 5• A better alternative: A complete computer
simulation
• The process:
– Build a translator (translates source code to the
machine code of an idealized computer)
– Build a simulator for the idealized computer
Trang 6• 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
• These problems can be avoided with formalisms based
on logic or mathematics
Trang 7• Based on formal logic (predicate calculus)
• Original purpose: formal program verification
• Correctness proofs specify constraints on
program variables
• When proofs can be constructed, they show that
a program performs the computation described
by its specification
Trang 8• 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
precondition that will guarantee the postcondition
Trang 9• Pre, post form: {P} statement {Q}
• An example
– a = b + 1 {a > 1}
– One possible precondition: {b > 10}
– Weakest precondition: {b > 0}
Trang 10• 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.
• An inference rule is a method of inferring the
truth of one assertion on the basis of the value