CSC 533: Organization of Programming Languages. Spring 2005

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

Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes

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

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

Programming Languages, Summary CSC419; Odelia Schwartz

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

Type Bindings. Static Type Binding

Imperative Programming

Names, Bindings, Scopes

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

NOTE: Answer ANY FOUR of the following 6 sections:

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

CSCI312 Principles of Programming Languages!

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

Introduction. A. Bellaachia Page: 1

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

Informatica 3 Syntax and Semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics

Concepts Introduced in Chapter 7

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

CMSC 331 Final Exam Section 0201 December 18, 2000

G Programming Languages - Fall 2012

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Concepts of Programming Languages

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Concepts of Programming Languages

Binding and Variables

Weeks 6&7: Procedures and Parameter Passing

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

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

G Programming Languages - Fall 2012

Programmiersprachen (Programming Languages)

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

Lecture 4 Memory Management

6. Names, Scopes, and Bindings

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

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

CPS 506 Comparative Programming Languages. Programming Language

Chapter 6. Data Types ISBN

10. Abstract Data Types

References and pointers

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

Software II: Principles of Programming Languages. Why Expressions?

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Short Notes of CS201

Programming Languages

CSCE 314 Programming Languages. Type System

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS201 - Introduction to Programming Glossary By

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

The role of semantic analysis in a compiler

Implementing Subprograms

Fundamentals of Programming Languages

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

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

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

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Topic IV. Block-structured procedural languages Algol and Pascal. References:

MIDTERM EXAMINATION - CS130 - Spring 2003

HANDLING NONLOCAL REFERENCES

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

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

TYPES, VALUES AND DECLARATIONS

1 Lexical Considerations

CS558 Programming Languages

Names, Scopes, and Bindings II. Hwansoo Han

LECTURE 18. Control Flow

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

Chapter 1. Preliminaries

CSC 533: Programming Languages. Spring 2015

Chapter 7 Expressions and Assignment statements

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

CMSC 330: Organization of Programming Languages

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CS558 Programming Languages

G Programming Languages - Fall 2012

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Advanced Programming & C++ Language

Data Types In Text: Ch C apter 6 1

CSE 307: Principles of Programming Languages

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Lexical Considerations

Lecture 6 Introduction to Objects and Classes

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

Types. What is a type?

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Chapter 7. Expressions and Assignment Statements ISBN

Name, Scope, and Binding. Outline [1]

Contents. 8-1 Copyright (c) N. Afshartous

Programming Languages 2nd edition Tucker and Noonan"

Chapter 7. Expressions and Assignment Statements

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Lecture 16: Static Semantics Overview 1

CSE 307: Principles of Programming Languages

Transcription:

CSC 533: Organization of Programming Languages Spring 2005 Language features and issues variables & bindings data types primitive complex/structured expressions & assignments control structures subprograms We will focus on C++ and Java as example languages 1 Variables imperative programming languages tend to be abstractions of the underlying von Neumann architecture variables correspond to memory cells variable attributes name type determines the range of values and operations address (memory location) value scope range of statements in which the variable is accessible lifetime time during which the variable is bound to an address 2

Static vs. dynamic the time & manner in which variables are bound to attributes is key to the behavior/implementation of the language static binding: prior to run-time, remains fixed usually more efficient dynamic binding: occurs or can change during run-time usually more flexible 3 Binding name (What names can refer to variables?) names are used to identify entities in programs length originally, 1 char names only FORTRAN 6 chars COBOL 30 chars C arbitrary length, only first 8 chars are significant C++ & Java arbitrary length, all chars significant (???) connectors case-sensitive C, C++, Java, COBOL, Ada all allow underscores Scheme allows hyphens,?,!, debate about desirability studies have shown: maxscoreinclass best max_score_in_class ok maxscoreinclass worst 4

Binding name (cont.) special words are words with preset meanings in the language keyword: has predefined meaning in context can be used freely in other contexts, can be overridden e.g., in FORTRAN REAL X REAL = 3.5 declaration assignment reserved word: cannot be reused or overridden in most languages (incl. C++ & Java), special words are reserved In ALGOL 60, special words had to be written in a distinct font 5 Binding type (When is the type of a variable decided?) static (compile-time) explicit declarations (most modern languages) num : integer; Pascal int num; C++/Java implicit declarations In FORTRAN, if not declared then type is assumed starts with I-N INTEGER, else REAL TRADEOFFS? dynamic (run-time) variable is given type on assignment, can change e.g., JavaScript, ISETL, APL, SNOBOL ADVANTAGE: DISADVANTAGE: flexible can write generic code costly type checking must be done during run error-detection abilities are diminished binding greatly determines implementation static compilation dynamic interpretation 6

Binding type (cont.) type checking: ensuring that operands in expressions are compatible a compatible type is either 1. legal for that operator, or 2. can be coerced (automatically converted) into a legal type coercion affects type checking, hence reliability most languages coerce numeric values X = 3.5 + 2; C++ makes extensive use of coercion char ch = A + 1; int * ptr = array[]; ptr++; can define coercion paths for new classes Java allows C++-like coercion of primitive types must explicitly convert reference types (objects) 7 Binding type (cont.) a strongly typed language is one in which all type incompatibilities are caught (both compile-time & run-time) C++ Java somewhat weakly typed (mostly due to backward compatibility) loopholes exist: = vs. == bool vs. int union unchecked parameters much better closes many of C-style loopholes 8

Binding address (When is memory allocated & assigned?) static: bound before execution, stays same e.g., early versions of FORTRAN, constants, global variables in C++ static binding no recursion WHY? stack-dynamic: bound when declaration is reached, but type bound statically e.g., could specify in FORTRAN 77, locals in C++, primitives in Java can save space over static, but slower WHY? heap-dynamic: bound during execution, changeable can be explicit: user allocates/deallocates objects e.g., new/delete in C++, new in Java efficient, but tricky (garbage collection helps) can be implicit: transparent to the user e.g., JavaScript, Scheme As with dynamic type binding: flexible, inefficient, error detection weakened 9 Binding address (cont.) the three binding options correspond to common memory partitions code segment: contains program instructions and static memory (note: compile-time) Heap segment (dynamic) stack-segment: contains stack-dynamic memory (note: run-time, LIFO) memory is allocated from one end (top) must be freed in reverse order (popped) stack ptr FREE USED Stack segment (semi-dynamic) Code segment (static) heap-segment: contains heap-dynamic memory (note: run-time, unpredictable) since lifetime is not predictable, must store as unstructured (linked) memory Note: LISP/Scheme is entirely heap based boundary between the stack and heap can be flexible share same block of memory grow from different ends advantages? disadvantages? 10

Binding value (How are values assigned to variables?) consider the assignment: X = Y when on the left-hand side, variable refers to an address (l-value) when on the right-hand side, variable refers to the value stored there (r-value) In C++/Java, an assignment returns the value being assigned cout << x = 3 << endl; x = y = 5; Note: assignment is right-associative Some languages automatically initialize variables on declaration e.g., JavaScript initialize to undefined C++ initializes global primitives, Java initializes primitive fields can specify initial values for user-defined types (class constructors) Not all languages treat assignment the same X = 2 + 3 In Prolog, right-hand side is not evaluated X is assigned the operation 2+3 can t change variables! 11 Scope & lifetime lifetime is a temporal property; scope is a spatial property a variable is visible in a statement if it can be referenced there the scope of a variable is the range in which it is visible scope rules determine how variables are mapped to their attributes scoping can be static: based on program structure (scope rules determined at compile-time) dynamic: based on calling sequence (must be determined during run-time) 12

Static vs. dynamic scoping program MAIN; procedure P1; print a; end; {of P1} static (lexical) non-local variables are bound based on program structure if not local, go out a level example prints 7 P1 MAIN P2 procedure P2; a := 0; P1; end; {of P2} a := 7; P2; end. {of MAIN} dynamic non-local variables are bound based on calling sequence if not local, go to calling point example prints 0 MAIN P2 P1 13 Nested scopes program MAIN; many languages allow nested procedures procedure P1(x : integer); procedure P3; print x, a; end; {of P3} P3; end; {of P1} static scoping example prints 1, 7 P1 P3 MAIN P2 procedure P2; a := 0; P1(a+1); end; {of P2} dynamic scoping example prints 1, 0 MAIN P2 a := 7; P2; end. {of MAIN} P1 P3 14

In-class exercise output using static scoping? output using dynamic scoping? program MAIN; procedure P1(x : integer); procedure P3; print x, a; end; {of P3} P3; end; {of P1} procedure P2; a := 0; P1(a+1); end; {of P2} a := 7; P1(10); P2; end. {of MAIN} 15 Nested scopes in C++/Java in C++/Java, can t have nested functions but can nest blocks: new environments in { } void foo() public void foo() { { int x = 3; int x = 3; if (x > 0) { if (x > 0) { int x = 5; int x = 5; cout << x << endl; System.out.println(x); } } cout << x << endl; System.out.println(x); } } Note: in C, variables can only be declared at the start of a block in C++/Java, can declare variables anywhere tradeoffs? 16

Tradeoffs virtually all modern languages are statically scoped LISP was originally dynamic, as were APL and SNOBOL later variants, Scheme and Common LISP, are statically scoped serious drawbacks of dynamic scoping cannot understand a routine out of context cannot hide variables cannot type-check at compile time static scoping is more intuitive & readable can lead to lots of parameter passing moving code within a program can change its meaning 17 Static scoping and modularity structured programming movement of the 70 s (e.g., Pascal) stressed the independence of modules don t use global variables (and so don t rely on scope rules) result: all input to subprogram is clearly delineated as parameters note: OOP methodology weakens modularity somewhat member functions within a class have direct access to data fields class foo { public: // MEMBER FUNCTIONS private: // DATA FIELDS }; is this desirable? avoidable? safe? 18