Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
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.
CSC 533: Organization of Programming Languages Spring 2005
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Variables Six properties: Binding times of properties:
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
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
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.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
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.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
COME Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Runtime Environments Compiler Construction Chapter 7.
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
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.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 5 Names, Bindings, and Scopes.
Programming Languages and Paradigms Imperative Programming.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS 363 Comparative Programming Languages Names, Type Checking, 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.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
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.
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 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
Data Types In Text: Chapter 6.
Chapter 4 Variables & Binding
Type Checking, and Scopes
CSC 533: Programming Languages Spring 2015
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Names and Binding In Text: Chapter 5.
CSC 533: Organization of Programming Languages Spring 2007
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Variables Names Bindings Type Scope

L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address Right hand side represents a value A B C D Statement L-Value R-Value C = B D = A + B10315 A = location Memory location Name

Managing Names Global Data -> everyone sees it i,j,k,l,m,n, o,p,r,s,t,u,v, w,x,y,z,… Others accessing the same data -knowingly different expectations -unknowingly colliding with other programs forget to declare, …no warning

Tools for Name Management Declaring in blocks (ifs and loops) Declaring in procedures (Declaring in files/c/c++) Declaring in classes Declaring in namespaces Declaring as global

C++ namespaces namespace myvars { int a, b; } namespace yourvars { int a,b; } cout << myvars::a << endl; cout << yourvars::b << endl; {using namespace myvars; cout << b << endl; }

C++ iostream #include int main () { std::cout << "ANSI-C++\n"; return 0; } #include using namespace std; int main () { cout << "ANSI-C++\n"; return 0; }

Aliases Makes readability and verification difficult Two ways to represent a single value –reference parameters –pointers c unions : used to provide dual interpretation, tricky fortran equivalence INTEGER A(1000), b(400) EQUIVALANCE (A(1), B(1)) A(1) = 23 B(1) has the SAME value! Usually for conserving memory required of large tables. union { int a; int b; } twoval; twoval x; x.a=7; cout << x.b; // OUTPUTS 7

Binding Times Language design : types of data Language implementation : representation of integer Compile : initialization of some data values (constants) Load : real address of global data Link : which version of the library is linked in Execute –beginning of program –beginning of procedure –beginning of block –in statement execution

Binding Times Understanding of the language depends upon knowing WHEN things happen STATIC : before run-time DYNAMIC : during run-time There are more specific times but these are commonly used

Type Binding Static –Explicit Declaration - most languages –Implicit Declaration - Fortran (I-N are int) Dynamic –Usually interpreted languages –APL L < L <- “abc” Inference –exclusively in some languages such as ML (p.166) –routinely in expression evaluation

Dynamic Typing Requires some type of descriptor Expensive run-time effort, difficult to find errors Integer array x[10] x integer array 10 (values) …..Name …..Type …..array or scalar …..size …..data

Memory Allocation Global data : static Stack Dynamic : local data for procedures Heap Dynamic –c++ new and delete operators same for java, pascal, etc –some languages such as APL allocate only when assigned data –efficient memory management –run-time expense

Examples in c++ #include int i; // i is static global int double(int x) { int temp; // temp is stack dynamic temp = 2 * x; return temp; } void main() { int *j; // i is explicit heap-dynamic j = new(int); *j=6; *j=*j + 4; i=double (i); cout << i; }

C static Most local data in a procedure is stack- based On return, the activation record is “popped” and memory is reused on subsequent calls Can local values be retained between calls? In C, declare the variable static to make it retain one memory location between calls and/or share one location for recursive calls

Type Checking One of the more subtle issues of programming expression evaluation and assignment passing parameters runtime check - difficult and expensive but part of interpreters compile-time check - more typical

c is WEAKLY typed #include int test(x) int *x; { *x =*x + 7; return 0; } main() { float j; j = 4.3; test(&j); printf("%f\n",j); } Output -> Look for example programs: ~dgame/www/cs310/examples files named typetest.c and typetest2.c

c++ is STRONGLY typed #include int test(int & x) { x =x + 7; return 0; } main() { float j; j = 4.3; test(j); cout << j << endl; } Output -> 4.3 doesn’t pass as ref! Look for example program: ~dgame/www/cs310/examples file named typetest2.cpp

Name Type Compatability Pascal –type celsius = real; – fahrenheit = real; –These two are NOT the same type That IS the intended effect. You do not want the two temperatures mixed. Application dictates what is compatible C++ uses name compatibility

Structure Type Compatability Basically asks are the implementations the same Not easy to determine for the compiler when there are ways to equivalence types Some languages (pascal) don’t specify which so it becomes implementation dependent –example below shows that point Type type1 = array [1..10] of integer; type2 = array [1..10] of integer; type3 = type2; type1 and type2 are not compatible, but type2 and type3 are … not strictly name equivalence

Scope Rules How does one determine which data is visible to a procedure? Static rules –c++ class members have a set of rules –c/c++ have other data with a unique set of rules –pascal/algol/pl1 block structured rules –others Dynamic rules –reverse through the calling chain

Block Structured Program t(input,output); var x,y:integer; procedure one() var a,x:integer; begin..end; (* one *) procedure two() var b,c:integer; procedure twoa() var d,x : integer; begin..end; (* twoa *) begin..end; (* two *) procedure three() var d,x:integer; begin..end; (* three *) begin..end. (* main routine *) t x t y t one a one,x one y t two b two, c two, x t y t three d three,x three y t twoa d twoa, x twoa, b two, c two, y t

C and C++ files Any variables declared outside of a function are global. Those variables are available to all other files In order to access them, you must declare the variable extern The sum of all files compiled can not have any global names the same or the declaration is ambiguous

#include … int i; void main() { } #include … int i; void one() { } ILLEGAL #include … int i; void main() { } #include … int j; void one() { } #include … extern int j; void two() { } LEGAL : Third file accesses j in second file

Dynamic scope (assume pascal does) Use the same example for block structured pascal: If calls are made as follows: main calls three three calls two A reference by procedure two to x will access the x from three main three two Activation stack Access determined by calling sequence NOT program structure! main twothree

Dynamic Scoping Interpreted languages –APL –LISP Extremely difficult to debug Can’t type check parameters All data potentially accessible by other procedures Slow to find parameters Even later dialects of LISP converted

Data Initialization When does a variable become initialized when you do something like – int i=0; Generally compilers will store zeros in memory reserved for static variables and copy it into memory when running, so the data is initialized before execution begins Local data in procedures obviously can not do this: must accomplish at run-time

Variables Names Bindings Type Scope