Upload
emmeline-newman
View
220
Download
0
Embed Size (px)
Citation preview
SemanticsSemantics (1)(1)
2
Symantec (1) To provide an authoritative definition
of the meaning of all language constructs for:1. Programmers2. Compiler writers3. Standards developers
A programming language is complete only when its syntax, type system, and semantics are well-defined
3
Symantec (2)
Semantics is a precise definition of the meaning of a syntactically and type-wise correct program
4
Approaches to define Semantics (1)
Operational Semantics Whatever happened when the program
compiled is compiled by a compiler C and run on a machine M
Axiomatic Semantics Axiomatize the meaning of each
statement of the language Give the formal specification of what a
program is supposed to do.
5
Denotation Semantics Define the meaning of each type of
statement that occurs in the abstract syntax as a state-transformation mathematical function.
The meaning can be expressed as a collection of functions operating on a program state
Approaches to define Semantics (2)
6
Expression Semantics
Expression Semantics includes Operators Operators associativity and precedence Role of different evaluation order Importance of precision
7
Infix Notation
Binary operator is written between its operands
Ambiguous expression Associativity and Precedence is one
way to eliminate the ambiguity
8
Eliminating infix Expression Ambiguity (1) Polish prefix notations
Binary operator is written in front of the two operands
Semantics is inherently unambiguous Prefix can be generated by using a prefix walk
(preorder traversal) Limitation: some symbol cannot be used for an
operation with deferent number of operands - is cannot be used for both unary and binary
minus One solution using different symbols
Example: the infix: a+b-c*d the prefix: -+ab*cd
9
Eliminating infix Expression Ambiguity (2)
Polish postfix notations Binary operator follows the two operands Semantics is inherently unambiguous postfix can be generated by using a
postfix walk (postorder traversal) Example:
the infix: a+b-c*d the postfix: ab+cd*-
10
Expression Sort-Circuit Evaluation (1)
Evaluating a Boolean expression from left to right and stop as soon as the truth of the expression can be determined.
The short-circuit definition of A and B if A then B else false
The short-circuit definition of A or B if A then true else B
11
Expression Sort-Circuit Evaluation (2)
Advantages: shorter and clear code
Disadvantages: A && B is not the same as B && A A false and B undefined
A && B false B && A undefined
12
ExampleNode p = head;while (p != null &&
p.info != key) p = p.next;if (p == null)
// not in list ...else // found it ...
boolean found = false;while (p != null &&
! found) { if (p.info == key) found = true; else p = p.next; }
while (p !=null){ If (p.inf == key) break; P=p.next; }
13
The Meaning of an Expression
Meaning of expression should depend only on the values of its sub-expressions and the meaning of its operator
Fixed size representation of numbers in computer makes numbers have smallest and largest values Mathematics has unlimited values
14
Example: (a+b)+c ≠ a+(b+c) for some values
of a, b, c Assume:
a = largest possible integer b = 3 c =-5
Left-hand side= error (integer overflow)
Right-hand side = largest integer -2
15
Example a is semantically undefined a = 14
Increment i after the 2nd referencing
a=19 Increment i before the 2nd referencing
In many languages sub-expression in separate sub-trees may be evaluated in any order
i = 2; b = 2; c=5;a = b * i++ + c * i
16
Program State (1)
The state of a program is the collection of all active objects and their current values.
Maps: The pairing of active objects with specific
memory locations, and the pairing of active memory locations
with their current values
17
Program State (2)
The current statement (portion of an abstract syntax tree) to be executed in a program is interpreted relative to the current state.
The individual steps that occur during a program run can be viewed as a series of state transformations
18
Example
Suppose variables i and j have the values 13 and -1 at some time during the execution
i and j are associated with memory locations 154 and 155 at that time
The current state: Environment {i,154, j,155} Memory {0,undef, … 154,13, 155,-1, …}
19
Program State (3)
The state of a program is a product of its active objects, Their memory locations, and Associative values
Function composed of two maps environment and memory
State = environment x memory State is like watch widow
20
Assignment Semantics
Assignment = Variable target; Expression source
The semantic is as follows Source expression is evaluated in the
current state, resulting in a value The resulting value replaces the value of
the target variable, resulting in a new state
21
Assignment Semantics (1)Assignment Statement vs. Expression
In most languages, assignment is a statement; cannot appear in an expression.
In C-like languages, assignment is an expression. Example: if (a = 0) ... // an error while (*p++ = *q++) ; // strcpy while (ch = getc(fp)) ...
22
Assignment Semantics (2)Copy vs. Reference Semantics
Copy: a = b; a, b have same value. Changes to either have no effect on other. Used in imperative languages.
Reference a, b point to the same object. A change in object state affects both Used by many object-oriented languages.