Identifiers & Lifetimes

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Names and Bindings.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7: User-Defined Functions II
Chapter 5 Names, Bindings, and Scopes
Basic Semantics.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Scope.
Names, Bindings, and Scopes
1 Introduction to Semantics The meaning of a language.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
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.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
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.
Programming Languages and Paradigms Imperative Programming.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
1 Review. 2 Creating a Runnable Program  What is the function of the compiler?  What is the function of the linker?  Java doesn't have a linker. If.
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.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CPS120: Introduction to Computer Science Variables and Constants.
Chapter 5. Outline Some Exercises in C++ Names Variables Scope Lifetime.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Names, Scope, and Bindings Programming Languages and Paradigms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
Advanced Programming in C
Test 2 Review Outline.
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.
Multiple variables can be created in one declaration
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Advanced Programming Basics
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Group Status Project Status.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Seoul National University
Programming Language C Language.
Names, Bindings, and Scopes
COMPILERS Semantic Analysis
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Seoul National University
Types and Related Issues
Presentation transcript:

Identifiers & Lifetimes 7/27/2019 Identifiers & Lifetimes Named entities and their binding to storage.

Use of Identifiers What parts of a program have identifiers ("names")? variables constants subprograms labels formal parameters classes whole programs? const int BUFSIZE = 1024; static long total; long count( int value ) { sum: { while(value>0) total += value--; } return total;

Naming of Identifiers Do identifier names need to unique? depends on scope of identifier, and syntax of language: use must be unambiguous Can an identifier use the name of a keyword? yes, in some (older) languages in current language design, keywords are reserved for clarity (* In Pascal, identifier can be a keyword *) var integer: real; real: integer; begin real := 2 + 3; (* integer arithmetic *)

What kind of identifier is "A"? (Java) Is the meaning of each identifier unambiguous? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class A { /* Stop waste! Conserve letters. */ A A; A ( ) { } A (A A) { this.A = A; } A A ( ) { return A; } A A (A A) { A: if ( A.A( ) == A ) return A; else break A; return this.A; } public static void main(String [] a) { A A = new A( ); A.A = new A( A.A( A.A( ) ) );

Lifetime Lifetime is the duration of time that an entity (variable, constant, ...) is bound to memory. Lifetime can be... duration of process execution (static variables) duration of object existence (object attributes) duration of function activation (local variables) duration of scope activation (variable defined in a scope). A function also defines a scope. Lifetime applies to entities that have values, not names. "Lifetime of an identifier" doesn't make sense... identifiers have "scope" rather than "lifetime".

Lifetime and Scope Scope of identifiers and lifetime of entity it refers to are not the same. Lifetime: time when entity exists in memory scope: the parts of a program where a name is known or "visible" void add( int count ) { static long SUM = 0; float x = 0.5; while(count>0) { int x = 2; SUM += x*count; count--; } printf("%f", x); count: scope is function add( ), lifetime unknown. SUM: scope is function body, lifetime is process execution time. float x: scope is part of the function excluding while loop, lifetime is function activation time. int x: scope is while loop, lifetime is duration of while loop.

Lifetime and Scope (2) BUFSIZE has global scope (any file in this program can refer to its value). Its lifetime is the process's lifetime. buf has file scope. Its lifetime is the program's lifetime. length , k: scope is the function, lifetime is duration of function. c: scope is the for loop. Life is duration of "for" loop execution. int BUFSIZE = 1024; static char buf[BUFSIZE]; void read( int length ) { int k; for(k=0; k<length; k++) { int c = getchar(); if (c<0) break; buf[k] = c; } buf[k] = '\0';

Constants C "const" can be compile time, load time, or run time constants: const int MaxSize = 80; /* compile time */ void mysub( const int n ) { static const NUMBER = 8*sizeof(int); const time_t now = time(0); /* dynamic */ const int LastN = n; /* dynamic */ In Java, "final" merely means a variable cannot be changed after the first assignment. final InputStream in = System.in; void mysub ( int n ) { final int LastN = n;

Types of Constants There are several classes of constants, depending on when their value is known compile time: value can be computed at compile time, includes: manifest constants (literals): const int MAX = 1024; computable expressions: const int MAXBYTES = 8*MAX; elaboration time: value is determined when the program is executed. sometimes this simply means a variable whose value cannot be changed (this is enforced by the compiler).