Top Banner
Basic Semantics Basic Semantics
79

Basic Semantics

Jan 11, 2016

Download

Documents

jafari

Basic Semantics. Content. Names, attributes, and bindings Declarations, scope and the symbol table Overloading Allocation, lifetimes, and the environment Variables and constants Aliases, dangling references, and garbage. Names. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basic Semantics

Basic SemanticsBasic Semantics

Page 2: Basic Semantics

2

ContentContent

• Names, attributes, and bindings

• Declarations, scope and the symbol table

• Overloading

• Allocation, lifetimes, and the environment

• Variables and constants

• Aliases, dangling references, and garbage

Page 3: Basic Semantics

3

NamesNames

• A fundamental abstraction mechanism in a programming language is the use of names or identifiers to denote language entities or constructs

• In most languages, constants, variables, and procedures can have names assigned by the programmer

Page 4: Basic Semantics

4

AttributesAttributes

• The meaning of a name is determined by the attributes associated with the name

const int n = 5; /* n is a constant */

int x; /* x is a variable */

double f(int n) { … } /* f is a function */

Page 5: Basic Semantics

5

BindingBinding

• The process of associating an attribute to a name is called binding

const int n = 2; /* static binding */

int x; /* static binding */x = 2; /* dynamic binding */

int *y; /* static binding */y = new int; /* dynamic binding */

Page 6: Basic Semantics

6

Binding TimeBinding Time

• Language definition time: int, char, float

• Language implementation time: sizeof(int)

• Translation time: types of variables

• Link time: code body of external functions

• Load time: locations of global variables

• Execution time: values of variables, locations of local variables

Page 7: Basic Semantics

7

Symbol TableSymbol Table

• Bindings can be maintained by a data structure called the symbol table

• For an interpreter, the symbol table is a function that maps names to attributes

Symbol TableNames Attributes

Page 8: Basic Semantics

8

Symbol TableSymbol Table

• For a compiler, the symbol table is a function that maps names to static attributes

Symbol TableNames Static

attributes

Page 9: Basic Semantics

9

Environment & MemoryEnvironment & Memory

• The dynamic attributes locations and values can be maintained by two functions environment (memory allocation) and memory(assignment)

environmentNames Locations

memoryLocations Values

Page 10: Basic Semantics

10

DeclarationsDeclarations

• Declarations are a principal method for establishing bindings

• Declarations are commonly associated with a block. These declarations are called local declarations of that block.

• Declarations associated with an enclosing block are called nonlocal declarations

• Declarations can also be external to any block. These declarations are called global declarations

Page 11: Basic Semantics

11

An ExampleAn Exampleint x; /* global */

main(){ int i, n; /* nonlocal */ i = 1; n = 10; while (i < n) { int m; /* local */ m = i++; f(m); }}

Page 12: Basic Semantics

12

ScopeScope

• The scope of a binding is the region of the program over which the binding is maintained

• We can also refer to the scope of a declaration if all the bindings established by the declaration have identical scopes

• In a block-structured language, the scope of a binding is limited to the block in which its associated declaration appears. Such a scope rule is called static (or lexical) scope

Page 13: Basic Semantics

13

An ExampleAn Exampleint x;void p(void){ int i; …}void q(void){ int j; …}main() { int k; …}

i

j

kmain

q

p

x

Page 14: Basic Semantics

14

VisibilityVisibility

• The local binding of a name will take precedence over the global and nonlocal binding of the name. This causes a scope hole for the global and nonlocal binding of the name

• The visibility of a binding includes only those regions of a program where the binding applies

Page 15: Basic Semantics

15

An ExampleAn Exampleint i;void p(void){ int i; …}void q(void){ int j; …}main() { int i; …}

i

j

i

i

i

i

scope visibility

scope hole

scope hole

Page 16: Basic Semantics

16

Scope Resolution OperatorsScope Resolution Operators

• Scope resolution operators can be used to access the bindings hidden by scope holes

Page 17: Basic Semantics

17

An ExampleAn Example/* An example in C++ */int x;class ex { int x; void f() { int x; x = 1; /* local variable x */ ex::x = 2; /* x field of class ex */ ::x = 3; /* global variable x */ } void g() { x = 4; /* x field of class ex */ }};

Page 18: Basic Semantics

18

Scope Across FilesScope Across Files

• In C, global variable declarations can actually be accessed across files

File 1: extern int x;/* use x in other files */

File 2: int x; /* x can be used by other files */

File 2: static int x; /* x can only be used in this file */

Page 19: Basic Semantics

19

The Symbol TableThe Symbol Table

• A symbol table is like a dictionary for names: It must support insertion, lookup, and deletion of names with associated attributes

• The maintenance of scope information in a statically scoped language with block structure requires that declarations be processed in a fashion like stack

Page 20: Basic Semantics

20

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

Page 21: Basic Semantics

21

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

ychar

global

Page 22: Basic Semantics

22

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

ychar

global

pvoid

function

Page 23: Basic Semantics

23

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xdouble

local to pint

global

ychar

global

pvoid

function

Page 24: Basic Semantics

24

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xdouble

local to p1

intglobal

yint array

local to p2

pvoid

function

charglobal

Page 25: Basic Semantics

25

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

ychar

global

pvoid

function

Page 26: Basic Semantics

26

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

y

pvoid

function

qvoid

function

charglobal

Page 27: Basic Semantics

27

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

yint

local to q

pvoid

function

qvoid

function

charglobal

Page 28: Basic Semantics

28

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xint

global

y

pvoid

function

qvoid

function

charglobal

Page 29: Basic Semantics

29

An ExampleAn Example

int x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

x

y

pvoid

function

qvoid

function

charglobal

mainvoid

function

intglobal

Page 30: Basic Semantics

30

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

xchar

local to main

y

pvoid

function

qvoid

function

charglobal

mainvoid

function

intglobal

Page 31: Basic Semantics

31

An ExampleAn Exampleint x;char y;

void p(void){ double x; … { int y[10]; …}}

void q(void){ int y; … }

main() { char x; … }

x

y

pvoid

function

qvoid

function

charglobal

mainvoid

function

intglobal

Page 32: Basic Semantics

32

Symbol Tables for StructuresSymbol Tables for Structuresstruct { int a; char b; double c;} x = {1,'a',2.5};

void p(void){ struct { double a; int b; char c; } y = {1.2,2,'b'}; printf("%d, %c, %g\n",x.a,x.b,x.c); printf("%f, %d, %c\n",y.a,y.b,y.c);}

main(){ p(); return 0; }

x

y

pvoid

function

structlocal to p

structglobal

a int

b

c double

char

a double

b

c char

int

Page 33: Basic Semantics

33

Nested ProceduresNested Proceduresprocedure ex is x: integer := 1; y: character := ‘a’; procedure p is x: float := 2.5; begin put(y); new_line; A: declare y: array (1..10) of integer; begin y(1) := 2; put(y(1)); new_line; put(ex.y); new_line; end A; end p;

procedure q is y: integer := 42; begin put(x); new_line; p; end q;begin B: declare x: character := ‘b’; begin q; put(ex.x); new_line; end B;end ex;

Page 34: Basic Semantics

34

Nested ProceduresNested Procedures

x float

A

y array of integer

blocky character

p procedure

x integer

ex procedure

q procedure y integer

B block x character

Page 35: Basic Semantics

35

Dynamic ScopeDynamic Scope

• Using dynamic scope, the binding of a nonlocal name is determined according to the calling ancestors during the execution instead of the static program text

Page 36: Basic Semantics

36

An Example: Static ScopeAn Example: Static Scope

int x = 1;char y = ‘a’;void p(void){ double x = 2.5; printf(“%c\n”, y); { int y[10]; …}}void q(void){ int y = 42; printf(“%d\n”, x); p(); }main() { char x = ‘b’; q(); return 0; }

a

1

Page 37: Basic Semantics

37

An Example: Dynamic ScopeAn Example: Dynamic Scope

int x = 1;char y = ‘a’;void p(void){ double x = 2.5; printf(“%c\n”, y); { int y[10]; …}}void q(void){ int y = 42; printf(“%d\n”, x); p(); }main() { char x = ‘b’; q(); return 0; }

x

y

pvoid

function

qvoid

function

char = ‘a’global

mainvoid

function

int = 1global

Page 38: Basic Semantics

38

An ExampleAn Example

int x = 1;char y = ‘a’;void p(void){ double x = 2.5; printf(“%c\n”, y); { int y[10]; …}}void q(void){ int y = 42; printf(“%d\n”, x); p(); }main() { char x = ‘b’; q(); return 0; }

xchar = ‘b’in main

y

pvoid

function

qvoid

function

char = ‘a’global

mainvoid

function

int = 1global

Page 39: Basic Semantics

39

An ExampleAn Example

int x = 1;char y = ‘a’;void p(void){ double x = 2.5; printf(“%c\n”, y); { int y[10]; …}}void q(void){ int y = 42; printf(“%d\n”, x); p(); }main() { char x = ‘b’; q(); return 0; }

xchar = ‘b’in main

y

pvoid

function

qvoid

function

int = 42in q

mainvoid

function

int = 1global

char = ‘a’global

98

Page 40: Basic Semantics

40

An ExampleAn Example

int x = 1;char y = ‘a’;void p(void){ double x = 2.5; printf(“%c\n”, y); { int y[10]; …}}void q(void){ int y = 42; printf(“%d\n”, x); p(); }main() { char x = ‘b’; q(); return 0; }

xchar = ‘b’in main

y

pvoid

function

qvoid

function

int = 42in q

mainvoid

function

int = 1global

char = ‘a’global

double = 2.5in p

*

Page 41: Basic Semantics

41

Issue of Dynamic ScopeIssue of Dynamic Scope

• When a nonlocal name is used in an expression or statement, the declaration that applies to that name cannot be determined by simply reading the program

• Static typing and dynamic scoping are inherently incompatible

• Languages that use dynamic scope are Lisp, APL, Snobol, Perl

Page 42: Basic Semantics

42

OverloadingOverloading

• An operator or function name is overloaded if it is used to refer to two or more different things in the same scope

2 + 3 integer addition2.1 + 3.2 floating-point addition2 + 3.2 error or floating-point addition

by automatically converting2 to 2.0

Page 43: Basic Semantics

43

Overload ResolutionOverload Resolution

• Overloading of operators or function names can be resolved by checking the number of parameters (or operands) and the data types of the parameters (or operands)

int max(int x, int y); max(2, 3); double max(double x, double y); max(2.1, 3.2);int max(int x, int y, int z); max(1, 3, 2);

Page 44: Basic Semantics

44

Overloading & Automatic Overloading & Automatic ConversionConversion

• Automatic conversions complicate the process of overload resolution

max(2.1, 3);

C++: error, both conversions are allowed

Ada: error, no conversion is allowed

Java: only int to double is allowed

Page 45: Basic Semantics

45

Overloading & Automatic Overloading & Automatic ConversionConversion

• Since there are no automatic conversions in Ada, the return type of a function can also be used to resolve overloading

function max(x:integer; y:integer) return intfunction max(x:integer; y:integer) return floata: integer;b: float;a := max(2, 3); -- call to max #1b := max(2, 3); -- call to max #2

Page 46: Basic Semantics

46

Operator OverloadingOperator Overloading

• Ada and C++ also allow built-in operators to be extended by overloading

• We must accept the syntactic properties of the operator, i.e., we cannot change their associativity or precedence

Page 47: Basic Semantics

47

An ExampleAn Example

typedef struct { int i; double d; } IntDouble;

bool operator < (IntDouble x, IntDouble y){ return x.i < y.i && x.d < y.d; }

IntDouble operator + (IntDouble x, IntDouble y){ IntDouble z; z.i = x.i + y.i; z.d = x.d + y.d; return z;}

int main(){ IntDouble x = {1, 2.1}, y = {5, 3.4}; if (x < y) x = x + y; else y = x + y; cout << x.i << " " << x.d << endl; return 0;}

Page 48: Basic Semantics

48

Name OverloadingName Overloading

• A name can be used to refer to completely different things

class A {

A A(A A) {

A: for(;;)

{ if (A.A(A) == A) break A; }

return A;

}

}

Page 49: Basic Semantics

49

EnvironmentEnvironment

• Depending on the language, the environment may be constructed statically (at load time), dynamically (at execution time), or a mixture of the two

• FORTRAN uses a complete static environment

• LISP uses a complete dynamic environment

• C, C++, Ada, Java use both

Page 50: Basic Semantics

50

Location AllocationLocation Allocation

• Typically, global variables are allocated statically

• Local variables are usually allocated automatically and dynamically in stack-based fashion

• The lifetime or extent of an allocated location is the duration of its allocation in the environment

Page 51: Basic Semantics

51

An ExampleAn Examplemain() {

A: { int x; char y; /* ... */

B: { double x; int a; /* ... */ } /* end B */

C: { char y; int b; /* ... */

D: { int x; double y; /* ... */ } /* end D */

/* ... */

} /* end C */

/* ... */

} /* end A */

return 0;

}

Page 52: Basic Semantics

52

An ExampleAn Example

xy

xyxa

xyyb

xy

xyybxy

xyyb

xy

enterA

enterB

exitB

enterC

enterD

exitD

exitC

Page 53: Basic Semantics

53

Manually Location AllocationManually Location Allocation

• Many languages also provide mechanisms to manually allocate (or deallocate) memory for variables

C: malloc, free library functionsC++: new, delete built-in operatorsJava: new built-in operators

Page 54: Basic Semantics

54

Structure of an EnvironmentStructure of an Environment

static

stack

heap

Page 55: Basic Semantics

55

Static Local VariablesStatic Local Variables

• In C, the allocation of a local variable can be changed from stack-based to static

Page 56: Basic Semantics

56

An ExampleAn Exampleint p(void) { static int p_count = 0; /* initialized only once */ p_count += 1; return p_count;}

main() { int i; for (i = 0; i < 10; i++) if (p() % 3) printf("%d\n",p()); return 0;}

Page 57: Basic Semantics

57

VariablesVariables

• A variable is an object whose stored value can change during execution

• A variable is specified by its attributes, which include its name, its location, its value, and others

Name Value

Location

OtherAttributes

Page 58: Basic Semantics

58

AssignmentsAssignments

• The semantics of the assignment x = e are that e is evaluated to a value, which is then copied into the location of x

x = yy

x

Page 59: Basic Semantics

59

R-Value & L-ValueR-Value & L-Value

• The variable y on the right-hand side of x = y stands for the value of y, while the variable x on the left-hand side stands for the location of x

• We sometimes call the value stored in the location of a variable its r-value, while the location of a variable its l-value

Page 60: Basic Semantics

60

R-Value & L-ValueR-Value & L-Value

• ML, Algol68, and BLISS are languages that make r-values and l-values explicit. In ML,

val x = ref 0; val y = ref 1;

x := !y;x := !x + 1;

z : int -- not assignable (like a constant) x : int ref -- assignable reference cell

Page 61: Basic Semantics

61

Address-of & DereferencingAddress-of & Dereferencing

• In C, the address-of operator & explicitly returns the location of a variable, while the dereferencing operator * explicitly takes the value of a variable and returns it as a location

int x, y, *xptr;xptr = &x;*xptr = y;

Page 62: Basic Semantics

62

Semantics of AssignmentsSemantics of Assignments

• The usual semantics of an assignment, which copies the value of a location to another location, is called storage semantics

• In some languages, like Java and LISP, an assignment copies the location to another location. This semantics is called pointer semantics

Page 63: Basic Semantics

63

Pointer SemanticsPointer Semantics

• Pointer semantics are usually implemented using implicit pointers and implicit dereferencing

y

x

x = y

y

x

Java

Page 64: Basic Semantics

64

An ExampleAn Example

class ArrTest { public static void main(String[] args) { int[] x = {1, 2, 3}; int[] y = x; x[0] = 42; System.out.println(y[0]); }}

/* y[0] = 42 */

Page 65: Basic Semantics

65

ConstantsConstants

• A constant is a language entity that has a fixed value for the duration of its existence

• A constant can be a literal like 123 or ‘a’. Or it can be a name for a value, called symbolic constants. Once its value is computed, it cannot change

Page 66: Basic Semantics

66

Symbolic ConstantsSymbolic Constants

• A symbolic constant is like a variable, except that it has no location attribute, but a value only. The location of a symbolic constant cannot be explicitly referred to

Name ValueOther

Attributes

const int a = 2;int *x;x = &a; /* Illegal C code */

Page 67: Basic Semantics

67

Initialization of ConstantsInitialization of Constants

/* compile-time constant */const int a = 2; /* manifest constant */const int b = 27 + 2 * 2;const int b = 27 + a * a; /* C illegal, C++ ok *//* static (or load-time) constant */const int c = (int) time(0); /* C illegal, C++ ok *//* dynamic constant */int f(int x) { const int d = x + 1; return b + c;}

Page 68: Basic Semantics

68

Function ConstantsFunction Constants

• Function definitions are definitions of constants whose values are functions

int gcd(int u, int v) { if (v == 0) return u; else return gcd(v, u % v);}

Page 69: Basic Semantics

69

Function VariablesFunction Variables

• C has function variables, which must be defined as pointers. However, dereferencing is not required to access the value of function variables

int (*fun_var) (int, int);fun_var = gcd;main() { printf(“%d\n”, fun_var(15, 10)); return 0;}

A little inconsistant

Page 70: Basic Semantics

70

Functions in Functional LanguagesFunctions in Functional Languages

• Functional languages do a much better job of making clear the distinction between function constants, function variables, and function literals. In ML,

fn(x:int) => x * x (* literal *) (fn(x:int) => x * x) 2; val square = fn(x:int) => x * x; (* constant *) fun square (x:int) = x * x; (* constant *)

Page 71: Basic Semantics

71

AliasesAliases• An alias occurs when the same location is

bound to two different names at the same time

• An alias can occurs at call-by-reference parameter passing

• An alias can occurs at pointer assignment

• An alias can occurs at assignment with pointer semantics

• An alias can occurs in the EQUIVALENCE declaration of FORTRAN

Page 72: Basic Semantics

72

An Example: Call by ReferenceAn Example: Call by Reference

void incr(int &i){ i++;}

void f( ){ int x = 1; incr(x); printf(“%d\n”, x);}

/* x = 2 */

Page 73: Basic Semantics

73

An Example: Call by ReferenceAn Example: Call by Reference

x x

x

i

1

1

x

i

2

x 2

Page 74: Basic Semantics

74

An Example: Pointer AssignmentAn Example: Pointer Assignment

int *x, *y;x = (int *) malloc(sizeof(int));*x = 1;y = x;*y = 2;printf(“%d\n”, *x); /* x = 2 */

Page 75: Basic Semantics

75

An Example: Pointer AssignmentAn Example: Pointer Assignment

x

y

x

y

x

y

x

y

1

1 x

y

2

Page 76: Basic Semantics

76

An Example: Pointer SemanticsAn Example: Pointer Semantics

class ArrTest { public static void main(String[] args) { int[] x = {1, 2, 3}; int[] y = x; x[0] = 42; System.out.println(y[0]); }}

/* y[0] = 42 */

Page 77: Basic Semantics

77

Dangling ReferencesDangling References

• A dangling reference is a pointer that points to a location that has been deallocated.

int *x, *y;x = (int *) malloc(sizeof(int));*x = 2;y = x;free(x);printf(“%d\n”, *y);

int *dangle(void) { int x; return &x;}

Page 78: Basic Semantics

78

GarbageGarbage• Garbage is locations that have been allocated

but that have become inaccessible to the program

int *x;x = (int *) malloc(sizeof(int));x = 0;

void p(void) { int *x; x = (int *) malloc(sizeof(int)); *x = 2;}

Page 79: Basic Semantics

79

Garbage CollectionGarbage Collection

• It is useful to remove the need to deallocate memory explicitly from the programmer, while at the same time automatically reclaiming garbage for further use. This mechanism is called garbage collection

• Most functional. logic, and object-oriented languages provide garbage collection