Basic Semantics Attributes, Bindings, and Semantic Functions

Slides:



Advertisements
Similar presentations
Chapter 3:: Names, Scopes, and Bindings
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Names and Bindings.
Chapter 7: User-Defined Functions II
Chapter 5 Names, Bindings, and Scopes
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Basic Semantics.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
Chapter 5 Basic Semantics
Programming Languages Third Edition
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
Chapter 5K. Louden, Programming Languages1 Chapter 5 Basic Semantics.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Variables Six properties: Binding times of properties:
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Chapter 9: Subprogram Control
Names, Bindings, and Scopes
Chapter 5 - Basic Semantics
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
Programming Languages and Paradigms Imperative Programming.
COMP3190: Principle of Programming Languages
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
© Kenneth C. Louden, Chapter 5 - Basic Semantics Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Names and Binding In Text: Chapter 4.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Names, Scope, and Bindings Programming Languages and Paradigms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Advanced Programming in C
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Names and Attributes Names are a key programming language feature
Type Checking, and Scopes
CSE 3302 Programming Languages
Names, Bindings, and Scopes
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Chapter 10: Implementing Subprograms Sangho Ha
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
Names and Binding In Text: Chapter 5.
CSE 3302 Programming Languages
Names, Bindings, and Scopes
Presentation transcript:

Basic Semantics Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, and the Environment Variables and Constants When you print a handout, you may select one of the pages 40-44.

Programming Languages Attributes Attributes The properties of language entities, typically identifiers For other language entities, say operator symbols, the attributes of which are often predetermined. Examples of Attributes the location of a variable: the place holder the value of an expression: the storable quantity the types of identifiers: determine the operations applicable the code of a procedure: the operation of the procedure the size of a certain type: determines the value range Programming Languages

Programming Languages Binding Binding The process of associating an attribute to a name Declarations (including definitions) bind the attributes to identifiers Example of Binding // C++ example int x, *y; x = 2; y = new int(3); x Type: integer Value: 2 y Type: integer pointer Value: ∙ → 3 Programming Languages

Programming Languages Binding Time The time when a binding occurs Large Categories of Binding Times Static binding: the binding occurs prior to the execution Dynamic binding: the binding occurs during the execution Further Refined Binding Time Categories Language definition time Language implementation time Program translation time Link time Load time Execution time (run time) Programming Languages

Programming Languages Binding Time Examples The value of an expression execution time if it contains variables translation time if it is a constant expression The type of an identifier translation time in a compilation system (say, Java) execution time in an interpreter (say, LISP) The maximum number of digits of an integer language definition time for some languages (say, Java) language implementation time for others (say, C) The location of a variable load time for static variables (static in C) execution time for dynamic variables (auto in C) Programming Languages

Programming Languages Declarations A principal method for establishing bindings implicit binding: implicitly assumed by a declaration explicit binding: explicitly specified by a declaration example) In the following C declaration, int x; the type binding is an explicit binding the location binding is an implicit binding Programming Languages

Different Terms for Declarations Some languages differentiate definitions from declarations Definition: a declaration that binds all potential attributes Declaration: a declaration that binds only a part of attributes C language example a function declaration (prototype) binds only the type (the interface) of the function Programming Languages

Examples of C Declarations int x = 0; Explicitly specifies data type and initial value. Implicitly specifies scope (explained later) and location in memory. int f(double); Explicitly specifies type (double  int) Implicitly specifies nothing else: needs another declaration specifying code The former is called a definition in C, the latter is simply a declaration. Programming Languages

Programming Languages Block and Locality Block a standard language construct which may contain declarations unit of allocations Locality of the Declarations or the References Local: the declaration and the reference for a name are in the same block Non-Local: the declaration of a name is not in the block which contains the reference for the name Note that we need some rules to locate corresponding declarations for non-local references. Programming Languages

Programming Languages Block-Structured Block-Structured Program The program consists of blocks, which may be nested Most Algol descendants exploit this structure Kinds of Blocks procedural block: Pascal non-procedural block: Ada, C -- Ada example declare x: integer; begin … end (* Pascal example *) program ex; var x: integer begin … end Programming Languages

Programming Languages Scope Scope of a Binding the region of the program over which the binding is maintained Scope and Block Declaration before Use Rule: The scope is typically extends to the end of the block which contains the declaration In some constructs, the scope may extend backwards to the beginning of the block (classes in Java and C++, top-level declarations in Scheme) Programming Languages

Programming Languages Scope Rules Lexical Scope (Static Scope) Rule the scope of a binding is the inside of the block which contains the corresponding declaration the standard scope rule of most block-structured languages Dynamic Scope Rule the scope of a binding is determined according to the execution path the symbol table (or the environment) should be managed dynamically Programming Languages

Lexical Scope Example (C) int x; void p(void) { char y; ... } /* p */ void q(void) { double z; } /* q */ main() { int w[10]; } x In C, the declaration before use rule apply. p y q z main w Programming Languages

Programming Languages Scope Holes What is a scope hole? a local declaration of a name can mask a prior declaration of the same name in this case, the masked declaration has a scope hole over the corresponding block Visibility and Scope Visibility: the region where the declared name is visible (excluding scope holes) Scope: the region where the binding exists (including scope holes) Programming Languages

Scope Resolution Operator // C++ example int x; void p(void) { char x; x = 'a'; // local x ::x = 42; // global x ... } /* p */ main() { x = 2; // global x } The global integer variable x has a scope hole in the body of p. In C, the global x cannot be referenced in p. In C++, the global x can be referenced in p using a scope resolution operator ::. Ada also has a scope resolution operator .. Programming Languages

Programming Languages File Scope in C File Scope In C, a global name can be turned into a file scope name by attaching the keyword static. A file scope name can only be referenced in that file. Example File 1 File 2 File 3 extern int x; ... x int x; ... x static int x; ... x Programming Languages

Recursive Declaration Recursive functions are generally well-defined int factorial(int n) { ... factorial(n – 1) ... } How about recursive variables? int x = x + 1; Not allowed in Ada or Java Allowed in C/C++ for local variables but meaningless Dealing with mutual recursions in the context of declaration before use rule. forward declarations in Pascal prototype declarations in C/C++ Programming Languages

Programming Languages Java Scope Example public class Scope { public static void f() { System.out.println(x); } public static void main(String[] args) { int x = 3; f(); } public static int x = 2; In Java classes, the declaration before use rule does not apply. In a class declaration, the scope of a declaration is the entire class. Note the underlined declaration. The result may differ according to the scope rules. The above code prints 2. (Java adopts lexical scope rule) Under dynamic scope rule, the code would print 3. Programming Languages

Dynamic Scope Evaluated Disadvantages of the Dynamic Scope The scope of a declaration cannot be determined statically. (Hand-simulation is needed to find the applicable declaration.) The types of identifiers cannot be determined statically. (A static type-checking is impossible) Historical Note Originally used in Lisp. Scheme could still use it, but doesn't. Some languages still use it: VBScript, Javascript, Perl (older versions). Lisp inventor (McCarthy) now calls it a bug. Programming Languages

Symbol Table Maintenance In a lexically scoped languages, The symbol table is maintained like a stack. The symbol table is maintained statically, i.e. regardless to the execution path. In a dynamically scoped languages, All the bindings of the outermost names are constructed. The bindings are maintained according to the execution path. Programming Languages

Symbol Table under Lexical Scope int x; char y; void p(void) { double x; ... { /* block b */ int y[10]; } void q(void) { int y; main() { char x; p void function y char global x int double local to p p void function y char global x int p void function y char global x int q p void function y char global x int q p void function y char global q main int x local to main x int global p void function y char global x int q main p void function x int global q y char local to q p void function y char global x int q main p void function x int global double local to p y char int[10] local to b y char global x int p void function y char global x int p void function y char global x int double local to p Programming Languages

Symbol Table under Dynamic Scope #include <stdio.h> int x = 1; char y = 'a'; void p(void) { double x = 2.5; printf("%c\n",y) { /* block b */ int y[10]; } void q(void) { int y = 42; printf("%d\n",x); p(); main() { char x = 'b'; q(); return 0; p void function y char='a' global q main int x … char int=42 local to q 98 Output double=2.5 local to p p void function y char='a' global q main int x … char int=42 local to q double=2.5 local to p 98 * Output ASCII 42 = 'b' p void function y char='a' global q main int x int=1 char='b' local to main int=42 local to q 98 Output ASCII value of 'b' p void function y char='a' global q main int x int=1 char='b' local to main p void function y char='a' global q main int x int=1 char='b' local to main int=42 local to q p void function y char='a' global q main int x int=1 Programming Languages

Programming Languages Overloading What is overloading? reusing names for different entities of a kind within the same scope entity1/name1, entity2/name2 (entity1, entity2)/name the name is overloaded in the above case operator overloading, function overloading Overload Resolution choosing the appropriate entity for the given usage of the overloaded name the calling context (the information contained in a call) is generally used for overload resolution Programming Languages

Programming Languages Overloading Example In most languages, the operator + is overloaded integer addition (say, ADDI) floating point number addition (say, ADDF) Disambiguating Clue: data types of operands How about mixed-type expression? 2 + 3.2 C/C++ adopts promotion (implicit type conversion). Ada treats the above expression error. Programming Languages

Programming Languages Function Overloading int max(int x, int y) // max #1 { return x > y ? x : y; } double max(double x, double y) // max #2 int max(int x, int y, int z) // max #3 { return x > y ? (x > z ? x : z) : (y > z ? y : z); } Name resolution max(2,3) calls max #1 max(2.1,3.2) calls max #2 max(1,3,2) calls max #3 Programming Languages

Overload Resolution Issues Implicit conversions may cause ambiguous calls max(2.1, 3) C++: too many candidates (max #1 or max #2) Ada: no candidates Java: implicit conversions are used only for the cases of no information loss Whether the return type is included in the calling context or not C++, Java: not included Ada: included Programming Languages

Operator Overloading in C++ #include <iostream> using namespace std; 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; Programming Languages

Other Kinds of Reuse of Names Reusing names for different kinds of entities Separate name space for each kind is needed. These kinds of reusing is not an overloading. Which is which? class name method name parameter name label name typedef struct A A; struct A {  int data;    A * next; }; C example class A { A A(A A)   { A:     for(;;)   { if (A.A(A) == A) break A; }     return A;   } Java example structure tag name type name Programming Languages

Programming Languages Environment Environment Construction Time static environment: FORTRAN dynamic environment: LISP mixture: most Algol-style languages Variable Allocation Time in a Algol-style Language global variables static allocation allocated in load time local variables mostly dynamic allocation allocated in the declaration elaboration time (i.e. when the control flow passing the declaration) Programming Languages

Programming Languages Typical Environment Components of Typical Algol-style Languages static area for static allocation stack for LIFO-style dynamic allocation heap for on-demand dynamic allocation Programming Languages

Programming Languages Activation Record Activation an invocation of a subprogram the subprogram environment should be constructed for each activation Activation Record the region of memory allocated for an activation subprogram environment + bookkeeping information Run-Time Stack block enters and exits are LIFO-style procedure calls and returns are LIFO-style activation records are stored in the run-time stack Programming Languages

Run-Time Stack Manipulation 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 */ point 1 Programming Languages

Run-Time Stack Manipulation 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 */ point 2 Programming Languages

Run-Time Stack Manipulation 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 */ point 3 Programming Languages

Run-Time Stack Manipulation 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 */ point 4 Programming Languages

Run-Time Stack Manipulation 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 */ point 5 Programming Languages

Programming Languages Heap Manipulation Heap (Free Store) the memory pool for the objects allocated manually Heap Deallocation manual deallocation: special functions or operators are used for deallocation (free in C, delete in C++) automatic deallocation: garbage collector is used (more safe but somewhat slow, Java) Ada Approach Ada does not provide delete operation but allows a user-defined deallocation (Unchecked_Deallocation) Programming Languages

Pointer and Dereferencing an object whose value is a reference to an object Dereferencing referencing an object via a pointer value In order to manipulate the heap objects, pointers are mandatory (either implicitly or explicitly) /* C example */ int *x; // pointer declaration x = (int*)malloc(sizeof(int)); // memory allocation *x = 5; // dereferencing free(x); // deallocation Programming Languages

Programming Languages Lifetime Storable Object a chunk of memory cells an area of storage that is allocated in the environment The Lifetime (or Extent) of an Object the duration of its allocation in the environment Lifetime vs. Scope the lifetime and the scope of variables are closely related but not identical (cf. local static variables in C/C++) according to the scope: local, global according to the lifetime: static, dynamic Programming Languages

Local Static Variable Example (C) int p(void) { static int p_count = 0; /* initialized only once - not each call! */ p_count += 1; return p_count; } main() { int i; for (i = 0; i < 10; i++) { if (p() % 3) printf("%d\n",p()); return 0; The variable p_count counts the number of calls of the function p. Accordingly, p is history sensitive. Guess the output ! Programming Languages

Variables and Constants an object whose value may change during execution Constants an object whose value does not change for its lifetime Literals a language entity whose value is explicit from its name a kind of constants but may never be allocated Programming Languages

Diagrams for Variables Schematic Representation Box-Circle Diagram Programming Languages

Programming Languages L-value and R-value L-value and R-value of a Variable l-value (LHS value): the location r-value (RHS value): the value stored Programming Languages

Programming Languages Assignment General Syntax infix notation variable assingmentOpertor expression Semantics storage semantics assignment by value-copying pointer semantics assignment by sharing (shallow copying) assignment by cloning (deep copying) Programming Languages

Assignment by Value-Copying The value of the variable is copied. x = y Programming Languages

Programming Languages Assignment by Sharing The location of the variable is copied. x = y Programming Languages

Programming Languages Assignment by Cloning The location and the value of the variable is duplicated. x = y Programming Languages

Programming Languages Java Example Java supports all the kinds of assignment semantics assignment of object variables: assignment by sharing assignment of simple data: assignment by value-copying object cloning is supported by the method clone. A closer view of object assignment in Java x = y Programming Languages

Programming Languages Constant Semantics Schematic Diagram for Constants Constant has Value Semantics Once the value binding is constructed, the value cannot be changed The location of a constant cannot be referred to. Programming Languages

Classification of Constants Literals and Named Constants literals: names denote the exact value named constants: names for the meaning of the value Classification of Named Constants static constants (manifest constants) compile-time static constants (may never be allocated) load-time static constants dynamic constants Programming Languages

Programming Languages Aliases Aliases two or more different names for the same object at the same time bad for readability (may cause potentially harmful side effects; see the next slide) Side Effect any change persists beyond the execution of a statement Potentially Harmful Side Effect the side effect cannot be determined from the written statement the previous code should also be read Programming Languages

An Example of Harmful Aliases main() { int *x, *y; x = (int *) malloc(sizeof(int)); *x = 1; y = x; /* *x and *y now aliases */ *y = 2; printf("%d\n",*x); return 0; } Programming Languages

Programming Languages What makes aliases? What makes aliases? pointer assignment call-by-reference parameters assignment by sharing explicit-mechanism for aliasing: EQUIVALENCE and COMMON in FORTRAN variant records Why explicit-mechanism for aliasing in FORTRAN? in order to save memory the memory was a valuable resource at that time Programming Languages

Programming Languages Dangling References Dangling References locations accessible but deallocated the locations are deallocated too early dangerous! What makes dangling references? pointer assignment and explicit deallocation pointer assignment and implicit deallocation by block exit by function exit Programming Languages

An Example of Dangling References main() { int *x, *y; x = (int *) malloc(sizeof(int)); *x = 1; y = x; /* *x and *y now aliases */ free(x); /* *y now a dangling reference */ printf("%d\n",*y); /* illegal reference */ return 0; } Programming Languages

Programming Languages Garbage Garbage (Dangling Objects) inaccessible memory locations that are allocated the locations are deallocated too late a waste of memory but not harmful What makes garbage? explicit allocation and the access point is lost due to assignment deallocation of the access point Programming Languages

Programming Languages An Example of Garbage main() { int *x; x = (int *) malloc(sizeof(int)); x = 1; /* OOPS! */ ... return 0; } Programming Languages

Programming Languages Garbage Collection A language subsystem that automatically reclaims garbage. Most functional language implementations and some object-oriented language implementations are using garbage collectors. (See Section 8.5) Programming Languages