Chapter 5 Names, Bindings, Type Checking, and Scopes

Similar documents
Chapter 5 Names, Binding, Type Checking and Scopes

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5. Names, Bindings, and Scopes

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

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

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

CSC 533: Organization of Programming Languages. Spring 2005

Introduction to Programming Using Java (98-388)

Informatica 3 Syntax and Semantics

Imperative Programming

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

Programming Languages, Summary CSC419; Odelia Schwartz

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

References and pointers

Type Bindings. Static Type Binding

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

NOTE: Answer ANY FOUR of the following 6 sections:

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

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

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

C++ (classes) Hwansoo Han

Chapter 9 Subprograms

Java Object Oriented Design. CSC207 Fall 2014

CMSC 331 Final Exam Section 0201 December 18, 2000

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

Lexical Considerations

Binding and Variables

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

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

Java Fundamentals (II)

1 Lexical Considerations

Semantic Analysis and Type Checking

Datatypes, Variables, and Operations

Lexical Considerations

G Programming Languages - Fall 2012

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Programming Languages

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Chapter 6 Introduction to Defining Classes

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

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

G Programming Languages - Fall 2012

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

The compilation process is driven by the syntactic structure of the program as discovered by the parser

TYPES, VALUES AND DECLARATIONS

CS 314 Principles of Programming Languages. Lecture 11

Lectures 5-6: Introduction to C

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Names, Bindings, Scopes

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Implementing Subprograms

Computational Expression

PROGRAMMING IN C++ COURSE CONTENT

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

Tokens, Expressions and Control Structures

Lecture 2: C Programm

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Chapter 2: Using Data

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Chapter 9. Subprograms

Programming Languages

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Name, Scope, and Binding. Outline [1]

Types. What is a type?

Final-Term Papers Solved MCQS with Reference

The Decaf Language. 1 Lexical considerations

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Programming Languages: Lecture 11

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Chapter 02: Using Data

Lecture 4 Memory Management

Chapter 10. Implementing Subprograms ISBN

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

Programmiersprachen (Programming Languages)

CSCI312 Principles of Programming Languages!

CSE 307: Principles of Programming Languages

Separate Compilation Model

3. Java - Language Constructs I

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

IC Language Specification

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

Chapter 2 Elementary Programming

Chapter 9. Subprograms

Chapter 6. Data Types ISBN

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

The Decaf language 1

Computer Components. Software{ User Programs. Operating System. Hardware

A Fast Review of C Essentials Part I

10. Abstract Data Types

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Transcription:

Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Scope and Lifetime Type Checking Referencing Environments Named Constants Names Used for variables, subprograms (or methods), types, classes, etc. Different languages have different rules for the formation of identifiers. Java: an unlimited-length sequence of letters and digits, the first one must be a letter. Fortran77: only be 1-6 letters or digits, the first of which must be a letter. Fortran90: up to 31 characters long, and allow them to include the _ character. 1

Prolog: starts with an upper-case letter is a variable, begins with a lower-case letter is a literal (constant) symbol. Perl: The most basic kind of variable in Perl is the scalar variable. Scalar variables hold both strings and numbers, and are remarkable in that strings and numbers are completely interchangable. -- The name of a scalar variable must start with a $, $priority = 9; $priority = 'high'; -- The name of a variable whose value is an array must start with an @, @monthtable 2

Case sensitivity Uppercase and lowercase letters in names are distinct. int var1, Var1; C, C++, JavaScript and Java names are case sensitive -- Visual Basic, Pascal and ASP are not case sensitive. Advantage: accurate and disambiguous. Disadvantage: readability (names that look alike are different) Keywords and Reserved Words A keyword is a word that is special only in certain contexts, e.g., in Fortran Real VarName (Real is a data type followed with a name) Real = 3.4 (Real is a variable) A reserved word is a special word that cannot be used as a user-defined name -- Reserved words are better than keywords because the ability to redefine keywords can be confusing. 3

Variables An abstraction of a memory cell. Variable attributes: Name Address Value Type Scope Lifetime Name More than one variable might have the same name. for (int i = 0; i < 10; i++) { a[i] = i;} for (int i = 0; i < 100; i++) { squares[i] = i*i; } 4

Address Address - the memory address with which a variable is associated A variable may have different addresses at different times during execution If subprogram has a local variable that is allocated when the subprogram is called, different calls may result in that variable have different addresses. If two variable names can be used to access the same memory location, they are called aliases Aliases can be created in different ways Reference variables, Pointers Pascal variant records C and C++ unions, FORTRAN EQUIVALENCE parameter passing 5

Referernce aliases String Name1= new String( Steve Jobs ); String Name2= new String( Steve Wozniak ); name1 name2 name2 = name1; name1 name2 Steve Jobs Steve Wozniak Steve Jobs Steve Wozniak 6

Pointer aliases. If several pointers point into the same block of memory, char *p = malloc(strlen( Java Software )); char *p2 = malloc(strlen( Solution )); p2 = p; Parameter passing by reference or pointer X Y null null Example 1: immutable string If the whole object cannot be extended by another class, the object is called immutable Object x = Java Solution ; givemeastring (x); System.out.println (x); [...] void givemeastring (Object y) { y = "This is a string"; } Result: Java Solution Object x = null; givemeastring (x); System.out.println (x); [...] void givemeastring (Object y) { y = "This is a string"; } Result: null 7

Example 2: mutable object class Car { String licenseplate; double speed; double maxspeed; void SetlicensePlate(String licenceplate1 ) { licenceplate = licenceplate1; } double GetSpeed () { return speed; } void SetSpeed (int speed1) { speed =speed1; } } class CarTest { public static void main(string args[]) { Car c = new Car(); Car d = c; d.setlicenseplate( Toronto66 ); d.setspeed (150); Aliases are harmful to readability It allows a variable to have its value changed by an assignment to a different variable. Program readers must remember all of them. 8

Value The contents of the memory cell at the variable's address. Are the two occurrences of a in this expression the same? a := a + 1; lvalue: The one on the left of the assignment refers to the location of the variable whose name is a; rvalue: The one on the right of the assignment refers to the value of the variable whose name is a; Most variables are allowed to change their value binding. #include <stdio.h> int main() { int x=10; x=5; printf("x = %d ", x); return 0; } 9

Constants are variables that are not allowed to do so. class CircleStuff { static final float PI = 3.1416;... } The final keyword final variables: a constant which will not and cannot change. final methods: it cannot be overridden. final arguments: the method will not directly change them final classes: this class will not be subclassed. Example: public final class String public final class FinalMethod { private final String name; protected FinalMethod(final String name) { this.name = name; } public final String getname() { return name; } } 10

Type the range of values the variable can have; the operations that are defined for those values; int signed integers 32 bits -2147483648 to 2147483647 Arithmetic operations + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The Concept of Binding A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Car c = new Car(); int speed1= c.getspeed(); 11

Binding time is the time at which a binding takes place. Possible Binding Times Static binding: before run time and remain unchanged. Language design time -- bind operator symbols to operations, bind + to addition operation Compile time Dynamic binding: occur during run time or can change Runtime int i; // i1... i = j+4; // i2 At compile time, i2 is bound to the i1 (its declaration). At run time, i is bound to the value computed for the expression j+4. 12

Shape draw () erase () Circle draw() erase () Triangle draw() erase () Line draw() erase () Shape s = new Circle(); s.draw(); Shape s = new Line(); s.draw(); Shape s = new Triangle(); s.draw(); 13

Bindings and Lifetime A variable may be bound to an address on the stack, or on the heap. Java: All data of primitive data type reside on stack All objects reside on heap int i = 6, int j ; int [ ] a = { 1,3,5,7,9}; int [ ] b = new int [3] ; String s = "abcdef String t = null; 14