CSC 533: Programming Languages Spring 2015

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
CSC 533: Organization of Programming Languages Spring 2005
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
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.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
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.
Names, Bindings, Type Checking, and Scopes
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)
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.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
ISBN Chapter 5 Names, Bindings, and Scopes.
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.
1 CSC 533: Programming Languages Spring 2014 Language features and issues  variables & bindings  data types primitive complex/structured  expressions.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Names, Bindings, Type Checking, and Scopes
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Structure of Programming Languages
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding Imperative programming has instructions that manipulate the “state” of the process the “state” is the collection of variables and their.
Names, Bindings, Type Checking, and Scopes
Scope, Visibility, and Lifetime
CSC 533: Programming Languages Spring 2015
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
CSC 533: Organization of Programming Languages Spring 2007
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Presentation transcript:

CSC 533: Programming Languages Spring 2015 Language features and issues variables & bindings data types primitive complex/structured expressions & assignments control structures subprograms We will focus on C, C++, and Java as example languages

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

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

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 C, C++, Java, COBOL, Ada all allow underscores Scheme allows hyphens, ?, !, … case-sensitive debate about desirability studies have shown: maxScoreInClass best max_score_in_class ok maxscoreinclass worst

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 declaration REAL = 3.5 assignment reserved word: cannot be reused or overridden in most languages (incl. C, C++ & Java), special words are reserved In ALGOL 60, special words had to be written in a distinct font

Binding type (When is the type of a variable decided?) static (compile-time) explicit declarations (most modern languages) num : integer; Pascal int num; C/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, PHP, Python, Perl ADVANTAGE: flexible – can write generic code DISADVANTAGE: costly – type checking must be done during run error-detection abilities are diminished binding greatly determines implementation static  compilation dynamic  interpretation

Binding type (cont.) type checking: ensuring that operands in expressions are compatible a compatible type is either legal for that operator, or 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/C++ make extensive use of coercion char ch = ’A’ + 1; int * ptr = new int[10]; ptr++; can define coercion paths for new classes Java allows some C++-like coercion of primitive types but must explicitly convert realint & reference types (objects)

Binding type (cont.) a strongly typed language is one in which all type incompatibilities are caught (both compile-time & run-time) C somewhat strongly typed, but numerous loopholes exist e.g., = vs. == bool vs. int union unchecked parameters C++ slightly better, but retains most weaknesses for backward compatibility Java much better closes many C-style loopholes

Binding address (When is memory allocated & assigned?) static: bound before execution, stays same e.g., early versions of FORTRAN, constants, global variables in C/C++ & Java 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/C++, primitives & references 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/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

Binding address (cont.) the three binding options correspond to common memory partitions code segment: contains program instructions and static memory (note: compile-time) stack-segment: contains stack-dynamic memory (note: run-time, LIFO) memory is allocated from one end (top) must be freed in reverse order (popped) 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?

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/C++/Java, an assignment returns the value being assigned cout << x = 3 << endl; System.out.println(x = 3); x = y = 5; Note: assignment is right-associative some languages automatically initialize variables on declaration e.g., JavaScript initialize to undefined C/C++ initialize 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!

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)

Static vs. dynamic scoping static (lexical) non-local variables are bound based on program structure if not local, go “out” a level program MAIN; var a : integer; procedure P1; begin print a; end; {of P1} procedure P2; a := 0; P1; end; {of P2} a := 7; P2; end. {of MAIN}  example prints 7 dynamic non-local variables are bound based on calling sequence if not local, go to calling point  example prints 0

Nested scopes many languages allow nested procedures static scoping program MAIN; var a : integer; procedure P1(x : integer); procedure P3; begin print x, a; end; {of P3} P3; end; {of P1} procedure P2; a := 0; P1(a+1); end; {of P2} a := 7; P2; end. {of MAIN} many languages allow nested procedures static scoping  example prints 1, 7 dynamic scoping  example prints 1, 0

In-class exercise output using static scoping? program MAIN; var a : integer; procedure P1(x : integer); procedure P3; begin 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} output using static scoping? output using dynamic scoping?

Nested scopes in C/C++/Java in C/C++/Java, can’t have nested functions/methods but can nest blocks: new environments in { } void foo() { public void foo() { int x = 3; int y = 3; if (x > 0) { if (y > 0) { int x = 5; int x = 5; cout << x << endl; System.out.println(x); } } int x = 9; 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?

Scoping tradeoffs virtually all modern languages are statically scoped LISP was originally dynamic, as were APL and SNOBOL Scheme is statically scoped, as is Common LISP (by default) dynamic scoping is an option in Common LISP & Perl 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 but can lead to lots of parameter passing also, moving code within a program can change its meaning

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 public class Die { private int numSides; private int numRolls; public Die() { this.numSides = 6; this.numRolls = 0; } public Die(int sides) { this.numSides = sides; public int getNumberOfSides() { return this.numSides; public int getNumberOfRolls() { return this.numRolls; public int roll() { this.numRolls++; return (int)(Math.random()*this.numSides + 1); note: OOP methodology weakens modularity somewhat methods within a class have direct access to data fields is this desirable? avoidable? safe? note: optional "this." prefix helps