Names. 2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association.

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

CSC 4181 Compiler Construction Scope and Symbol Table.
Basic Semantics.
Implementing Subprograms
Chapter 5 Basic Semantics
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 5K. Louden, Programming Languages1 Chapter 5 Basic Semantics.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 4: Names Fall 2009 Marco Valtorta.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
The Concept of Variables
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
CSC321: Programming Languages Names Chapter 4: Names 4.1 Syntactic Issues 4.2 Variables 4.3 Scope 4.4 Symbol Table 4.5 Resolving References 4.6 Dynamic.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Scope.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
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.
CSI 3125, Names etc., page 1 Names, binding, scope, type checking Names, variables Binding Scope Constants. Variable initialization Type checking Plan.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
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 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
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.
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.
Dr. Philip Cannata 1 Names, Types, and Functions standard hue names.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Design issues for Object-Oriented Languages
Advanced Programming in C
Name Spaces: ALL versus OOL
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Names.
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 10: Implementing Subprograms Sangho Ha
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
Scope of Variables.
Scope, Visibility, and Lifetime
Implementing Subprograms
Implementing Subprograms
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Names, Types, and Functions
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
Presentation transcript:

Names

2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association occurs before run-time. A binding is dynamic if the association occurs at run- time.  Variable Basic bindings Name Address Type Value Lifetime

3 L-value and R-value of a variable name  L-value - use of a variable name to denote its address. Ex: x = …  R-value - use of a variable name to denote its value. Ex: … = … x …  Some languages support/require explicit dereferencing. Ex: x := !y + 1

4 Another example: (Pointers) int x,y; int *p; x = *p; *p = y;

5 Scope  The scope of a name is the collection of statements which can access the name binding. Static scoping, a name is bound to a collection of statements according to its position in the source program. Most modern languages use static (or lexical) scoping.

6 More than on Scope  Two different scopes are either nested or disjoint. In disjoint scopes, same name can be bound to different entities without interference.  The scope in which a name is defined or declared is called its defining scope.  A reference to a name is nonlocal if it occurs in a nested scope of the defining scope; otherwise, it is local.

7 What Constitute a Scope  Simplest Scope (Fortran) is a compilation unit. C Java For Loop no yes Blocknestednested Function yes yes Class n/anested

8 Example  void sort (float a[ ], int size) {  int i, j;  for (i = 0; i < size; i++) // i, size local  for (j = i + 1; j < size; j++)  if (a[j] < a[i]) { // a, i, j local  float t;  t = a[i]; // t local; a, i nonlocal  a[i] = a[j];  a[j] = t;  }  }

9 Symbol Table  A symbol table is a data structure kept by a translator that allows it to keep track of each declared name and its binding.  Assume for now that each name is unique within its local scope.  The data structure of the symbol table can be any implementation of a dictionary, where the name is the key.

10 Symbol Table and Scopes 1.Each time a scope is entered, push a new dictionary onto the stack. 2.Each time a scope is exited, pop a dictionary off the top of the stack. 3.For each name declared, generate an appropriate binding and enter the name-binding pair into the dictionary on the top of the stack. 4.Given a name reference, search the dictionary on top of the stack: a)If found, return the binding. b)Otherwise, repeat the process on the next dictionary down in the stack. c)If the name is not found in any dictionary, report an error.

11 Example  stack of dictionaries At line 7: At line 4 and 11:

12 Resolving References  For static scoping, the referencing environment for a name is its defining scope and all nested subscopes.  The referencing environment defines the set of statements which can validly reference a name.

13 Example (1)  Outer scope:  Function B:  Function A:  Function main:

14 Example (2)  Symbol Table Stack for Function B:  Symbol Table Stack for Function A:  Symbol Table Stack for Function main:

15 Dynamic Scoping  In dynamic scoping, a name is bound to its most recent declaration based on the program’s call history.  Symbol table for each scope built at compile time, but managed at run time.  Scope pushed/popped on stack when entered/exited.

16 Dynamic Scoping Example:  Referencing i on line 4  Assume a call history: main(line 17)  A(line 10) B FunctionDictionary B A Main  The reference to i on line 4 resolves to the one declared in A on line 9 ( 1 st i encountered )

17 Visibility  A name is visible if its referencing environment includes the reference and the name is not redeclared in an inner scope.  A name redeclared in an inner scope effectively hides the outer declaration.  Some languages provide a mechanism for referencing a hidden name

18 Java Example 1 public class Student { 2 private String name; 3 public Student (String name,...) { 4 this.name = name; } 7 }

19 Ada Example  P3 and main access the integer x  P1 and p2 access the float x procedure Main is x : Integer; procedure p1 is x : Float; procedure p2 is begin... x... end p2; begin... x... end p1; procedure p3 is begin... x... end p3; begin... x... end Main;

20 Overloading  Overloading uses the number or type of parameters to distinguish among identical function names or operators.  Examples: +, -, *, / can be float or int + can be float or int addition or string concatenation in Java

21 Java Example  Java allows overloading methods names within the same class public class PrintStream extends FilterOutputStream {... public void print(boolean b); public void print(char c); public void print(int i); public void print(long l); public void print(float f); public void print(double d); public void print(char[ ] s); public void print(String s); public void print(Object obj); }

22 Lifetime  The lifetime of a variable is the time interval during which the variable has been allocated a block of memory.  Earliest languages used static allocation.  Algol introduced the notion that memory should be allocated/deallocated at scope entry/exit.