Binding and Variables

Similar documents
Fundamentals of Programming Languages

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Informatica 3 Syntax and Semantics

Chapter 5 Names, Binding, Type Checking and Scopes

Programming Languages, Summary CSC419; Odelia Schwartz

Type Bindings. Static Type Binding

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

NOTE: Answer ANY FOUR of the following 6 sections:

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Programmiersprachen (Programming Languages)

Imperative Programming

CSC 533: Organization of Programming Languages. Spring 2005

Organization of Programming Languages CS 3200/5200N. Lecture 09

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

CS201 - Introduction to Programming Glossary By

Short Notes of CS201

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

Names, Bindings, Scopes

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Names, Scopes, and Bindings II. Hwansoo Han

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Programming Languages Third Edition. Chapter 7 Basic Semantics

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

9. Subprograms. 9.2 Fundamentals of Subprograms

Programming Languages

Chapter 9 Subprograms

CPSC 3740 Programming Languages University of Lethbridge. Data Types

0. Overview of this standard Design entities and configurations... 5

Basic Types & User Defined Types

References and pointers

CSE 307: Principles of Programming Languages

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Chapter 9. Subprograms

CSE 307: Principles of Programming Languages

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Name, Scope, and Binding. Outline [1]

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Implementing Subprograms

Programming Languages: Lecture 11

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CS 314 Principles of Programming Languages

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

UNIT V Sub u P b ro r g o r g a r m a s

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 9. Subprograms

CS 314 Principles of Programming Languages. Lecture 11

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Computers Programming Course 5. Iulian Năstac

Properties of an identifier (and the object it represents) may be set at

Homework #3 CS2255 Fall 2012

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Chapter 5 Names, Bindings, Type Checking, and Scopes

Informatica 3 Marcello Restelli

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 7 Expressions and Assignment statements

MIDTERM EXAM (Solutions)

10. Abstract Data Types

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Chapter 9 Subprograms

Lecture 16: Static Semantics Overview 1

Types and Type Inference

Fundamental Concepts and Definitions

EC 413 Computer Organization

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Evolution of Programming Languages

Computers Programming Course 6. Iulian Năstac

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Functions. (transfer of parameters, returned values, recursion, function pointers).

Final-Term Papers Solved MCQS with Reference

CS 230 Programming Languages

Classes and Objects. Class scope: - private members are only accessible by the class methods.

CS 314 Principles of Programming Languages

G Programming Languages - Fall 2012

Concepts Introduced in Chapter 7

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Memory Management and Run-Time Systems

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lab 3. Pointers Programming Lab (Using C) XU Silei

TYPES, VALUES AND DECLARATIONS

Types. What is a type?

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Chapter 11 Introduction to Programming in C

Abstract Data Types and Encapsulation Concepts

Special Topics: Programming Languages

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Implementing Subprograms

Transcription:

Binding and Variables 1. DEFINITIONS... 2 2. VARIABLES... 3 3. TYPE... 4 4. SCOPE... 4 5. REFERENCES... 7 6. ROUTINES... 9 7. ALIASING AND OVERLOADING... 10 8. GENERICS AND TEMPLATES... 12 A. Bellaachia Page: 1

1. Definitions Attributes: o It is a set of properties used to describe a program entity, e.g., variable and function. o Example: Array: Name, Element type, Index type, Index lower bound, Index upper bound, and address. Variable: Name, Type, and Value. Descriptors: It is where the values of the attributes of an element are stored. Binding: o It is the process of assigning a value to the attribute of an element. o Binding Time: At what time a value is assigned an attribute. There are two types of binding: Static: o A binding is static if it first occurs before run time and remains unchanged throughout program execution. o Static binding occurs at: Compile time: Implementation time: Range of values for an integer. Language definition: A. Bellaachia Page: 2

Possible operators on strings: and +. Dynamic: o A binding is dynamic if it first occurs during execution or can change during execution of the program. o Dynamic binding occurs at: Run time: Assign a value to a variable. o Stability: Is the assignment of a value fixed or modifiable? 2. Variables Most conventional programming languages can be viewed as abstraction of an underlying Von Neumann architecture: Memory cell with and address and a value. Four Semantic Attributes of Variables In general, the semantics of variables in programming languages is often described in terms of four attributes. Name: It is the name used to refer to the variable. Type: A description of the set of permissible values for a variable. Scope: The region of program text over which a variable is known. l-value (or location): A location in memory associated with the variable. A. Bellaachia Page: 3

r-value: Typically, an indirect attribute of a variable; the value stored in the memory location associated with a variable. 3. Type Definitions: Type checking is the activity of ensuring that the operands of an operator are of compatible types Casting or Coercion: A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. A type error is the application of an operator to an operand of an inappropriate type. Static type checking: If all type bindings are static. Dynamic type checking: If all type bindings are dynamic. Strongly Typed Languages: A Language is strongly typed if type errors are always detected.some languages are strongly type: Ada, C, C++, Java Others are not: LISP Most allow you to create complex: ADTs, Records, Structures, Classes 4. Scope Definitions: A. Bellaachia Page: 4

The scope of a variable is the range of statements over which it is visible The scope rules of a language determine how references to names are associated with variables. Example: #include <stdio.h> main( ) { int x, y; printf("please enter a value for x: "); scanf( "%d", &x ); printf("please enter a value for y: "); scanf( "%d", &y ); { // This block used to swap x and y int temp; temp = x; x = y; y = temp; printf( "x:%d and y:%d\n", x, y ); // printf( "temp:%d\n", temp ); If you try to execute the last statement in Visual C++, you will get the following error: error C2065: 'temp' : undeclared identifier Non-local variables: The non-local variables of a program unit are those that are visible but not declared in the program. Example: #include <stdio.h> A. Bellaachia Page: 5

float increase_factor = 10.5; main() { float x; printf("please enter a value for x: "); scanf( "%f", &x ); printf( "The input value is: %f\n",x); // increase_factor variable is used but not declared in // main function. printf( "The increased value is: %f\n",x * increase_factor); Searching: Search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name Main A C D B E Scoping types: Static scope binding: The scope of a variable is defined by examining the program code. You do not need to run the program. A. Bellaachia Page: 6

Dynamic scope binding: o The scope of a variable is defined at runtime. o Example: MAIN { - declaration of x SUB1 { - declaration of x -... call SUB2... SUB2 {... - reference to x -...... call SUB1 MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x Static scooping: reference to x is to MAIN's x Dynamic scooping: reference to x is to SUB1's x 5. References Definition: A pointer or reference: it is when the r-value of a variable is used to access another variable. Used for dynamic storage management and addressing Example: #include <stdio.h> A. Bellaachia Page: 7

main() { int x = 5; int *x_pointer; // Pointer to an integer. printf( "The value of x before is: %d\n", x ); x_pointer = &x; // Now x_pointer can access x. *x_pointer = 6; // Indirectly change x. printf( "The value of x is after: %d\n", x ); printf( "The memory cell pointed to by x_pointer: %d\n", *x_pointer ); A C/C++ example: #include <stdio.h> #define size 10 main() { int choices[size]; int *choices_ptr; // Points to the array. int i=0; choices_ptr = choices; //initialize the array for (i=0;i<size;++i) // the following is equivalent to choices[i]=i*i; *(choices_ptr+i) = i*i; // Print the array for (i=0;i<size;++i) printf("choices[%d]= %d\n", i, choices[i]); Java: No pointer arithmetic Can only point at objects A. Bellaachia Page: 8

No explicit deallocator (garbage collection is used) 6. Routines We will use the term routine to mean: Subprograms: Assembler Subroutines: FORTRAN Procedures: Pascal, Ada Functions: C, LISP Methods: Java, C++ Routine Parts: Declaration: o The Specification of the name, list of formal parameters, and any return type. Body o The list of statements within the definition of the routine. Invocation: o Statement used to call the routine. Routine Attributes: Name Scope: It is similar to variable scope. Type: It is defined by the routine header: Name of the routine, the types of the parameters, and the type of the returned type. L-value: It is the memory area where the body of the routine is stored. R-value: It is the body of the routine. A. Bellaachia Page: 9

Routine Parameters: Formal parameters: o It is the set of parameters that appear in the routine s definition. Actual parameters: o It is the set of parameters that appear in the routine s call. Some programming languages have positional method and named for binding actual parameters to formal parameters in routine calls. Routine Signature: This specifies the types of the parameters and the return type. Activation record: o Data objects associated with local variables (including any parameters) o The relative position (offset) of the data object in the activation record. o Return pointer: It is the address where execution must resume in the calling routine. 7. Aliasing and Overloading Overloading: Method overloading is commonly used to create several methods with the same name that perform similar tasks: public int square (int side); public double square(double side) A. Bellaachia Page: 10

Java enables methods of the same name to be defined as long as they have different signatures. A C++ Example: #include <iostream.h> int max(int x, int y){ return (x>y?x:y); float max(float x, float y){ return (x>y?x:y); void main(){ float a = 4.5; float b = 3.4; cout << max(3,6) << endl; cout << max(a,b) << endl; Aliasing: Two names are aliases if they refer to the same entity at the same program points. It is related to variables. Example: #include <iostream.h> void main(){ int i =4; int * p_i = &i; int * p_ii= &i; cout << "The value of i: " << i << endl << "The value of &p_i: " << * p_i << endl << "The value of &p_ii: " << * p_ii << endl ; A. Bellaachia Page: 11

8. Generics and Templates Generic routines allow the same code to be used for multiple data types: ADT Stack, Sorting, searching, etc. Called templates in C++ Generic types bound to actual types by instantiation at compile time A. Bellaachia Page: 12