semantics(1). 2 symantec(1) to provide an authoritative definition of the meaning of all language...

22
Semantics Semantics (1) (1)

Upload: emmeline-newman

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

SemanticsSemantics (1)(1)

Page 2: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 3: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

3

Symantec (2)

Semantics is a precise definition of the meaning of a syntactically and type-wise correct program

Page 4: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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.

Page 5: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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)

Page 6: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

6

Expression Semantics

Expression Semantics includes Operators Operators associativity and precedence Role of different evaluation order Importance of precision

Page 7: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

7

Infix Notation

Binary operator is written between its operands

Ambiguous expression Associativity and Precedence is one

way to eliminate the ambiguity

Page 8: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 9: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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*-

Page 10: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 11: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 12: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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; }

Page 13: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 14: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 15: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 16: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 17: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 18: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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, …}

Page 19: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 20: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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

Page 21: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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)) ...

Page 22: Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards

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.