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

Similar documents
LECTURE 14. Names, Scopes, and Bindings: Scopes

6. Names, Scopes, and Bindings

G Programming Languages - Fall 2012

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

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

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

Programming Languages

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

Names and Abstractions: What s in a Name?

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

CS 314 Principles of Programming Languages

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

Names, Scopes, and Bindings II. Hwansoo Han

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Today's Topics. CISC 458 Winter J.R. Cordy

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

CSE 307: Principles of Programming Languages

Syntax Errors; Static Semantics

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

The role of semantic analysis in a compiler

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

MIDTERM EXAM (Solutions)

CSCI312 Principles of Programming Languages!

Semantic Analysis. Compiler Architecture

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

Chapter 3:: Names, Scopes, and Bindings

Implementing Subprograms

CA Compiler Construction

G Programming Languages - Fall 2012

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Scope. Chapter Ten Modern Programming Languages 1

Type Bindings. Static Type Binding

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Compiler construction

Principles of Programming Languages

Chapter 10. Implementing Subprograms ISBN

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Names, Bindings, Scopes

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

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CS 314 Principles of Programming Languages. Lecture 13

CSC 533: Organization of Programming Languages. Spring 2005

Chapter 5. Names, Bindings, and Scopes

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Programming Languages: Lecture 12

Special Topics: Programming Languages

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Iterative Languages. Scoping

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

Imperative Programming

Concepts Introduced in Chapter 7

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

Names, Scope, and Bindings

Implementing Subprograms

UNIT-4 (COMPILER DESIGN)

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Lecture08: Scope and Lexical Address

Procedure and Object- Oriented Abstraction

Chapter 5 Names, Binding, Type Checking and Scopes

names names identifiers variables subroutines constants

CS 415 Midterm Exam Spring SOLUTION

Run Time Environments

CSCE 314 Programming Languages. Type System

Chapter 10 Implementing Subprograms

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Informatica 3 Marcello Restelli

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.


What about Object-Oriented Languages?

System Software Assignment 1 Runtime Support for Procedures

Principles of Compiler Design

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Special Topics: Programming Languages

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

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

Scope & Symbol Table. l Most modern programming languages have some notion of scope.

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

Memory Management and Run-Time Systems

The Procedure Abstraction

Programmiersprachen (Programming Languages)

Chapter 9. Subprograms

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Compiler Construction

Names, Scope, and Bindings

Programming Languages: Lecture 11

Run-time Environments - 2

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

Transcription:

Scope CSC 4181 Compiler Construction Scope and Symbol Table A scope is a textual region of the program in which a (name-to-object) binding is active. There are two types of scope: Static scope Dynamic scope Most modern languages implement static scope (i.e., the scope of binding is determined at compile-time). Symbol Table 2 Static Scope Static scope is also called lexical scope because the bindings between name and objects can be determined by examining the program text. Typically, the current binding for a given name is the one encountered most recently in a top-to-bottom scan of the program. Static Scope Rules The simplest static scope rule has only a single, global scope (e.g., early Basic). A more complex scope rule distinguishes between global and local variables (e.g., Fortran). Languages that support nested functions (e.g., Pascal, Algol) require an even more complicated scope rule. Symbol Table 3 Symbol Table 4 Closest Nested Scope Rule A name that is introduced in a declaration is known in the scope in which it is declared, and in each internally nested scope, unless it is hidden by another declaration of the same name in one or more nested scopes. Nested subroutines in Pascal Symbol Table 5 Symbol Table 6 1

Hole and Qualifier A name-to-object binding that is hidden by a nested declaration of the same name is said to have a hole in its scope. In most languages, the object whose name is hidden is inaccessible in the nested scope. Some languages allow accesses to the outer meaning of a name by applying a qualifier or scope resolution operator. Static Links and Static Chains A static link points to the activation record of its lexically-scoped parent. Static chain is a chain of static links connecting certain activation record instances in the stack. Symbol Table 7 Symbol Table 8 Static Links and Static Chains Scope in OOP In OOP, scope exts beyond functions and the main program. Each class defines a scope that cover every function s scope in that class. Inheritance and access modifiers also make some variables and functions visible outside their scope. Symbol Table 9 Symbol Table 10 Dynamic Scope The bindings between names and objects dep on the flow of control at run time. In general, the flow of control cannot be predicted in advance by the compiler Languages with dynamic scoping t to be interpreted rather than compiled. Dynamic Links A dynamic link point to its caller activation record. Symbol Table 11 Symbol Table 12 2

Static vs. Dynamic Scope Example: Static vs. Dynamic Scope Static scope rules match the reference (use of variable) to the closest lexically enclosing declaration. Dynamic scope rules choose the most recent active declaration at runtime. ; Static Scope: 1 Dynamic Scope: 2 Symbol Table 13 Symbol Table 14 Example: Static Scope Example: Dynamic Scope ; main() second() first() The program prints 1 ; main() second() first() The program prints 2 Symbol Table 15 Symbol Table 16 Referencing Environment A referencing environment is a set of active bindings at any point during program s execution. It corresponds to a sequence of scopes that can be examined in order to find the current binding for a given name. Shallow and Deep Bindings When the referencing environment of a routine is not created until the routine is usually called, it is late binding. The late binding of the referencing environment is known as shallow binding. If the environment is bound at the time the reference is first created, it is early binding. The early binding of the referencing environment is called deep binding. Symbol Table 17 Symbol Table 18 3

Example: Shallow vs. Deep Bindings Deep binding: prints person p if older than 35 Shallow binding: prints person p if older than 20 Symbol Table 19 Example: Deep Binding main(p) thres := 35 show(p, older) var thres : integer thres := 20 older(p) if <return value is true> Symbol Table 20 Example: Shallow Binding main(p) thres := 35 show(p, older) var thres : integer thres := 20 older(p) if <return value is true> Symbol Table 21 Shallow and Deep Bindings in Statically Scoped Language Shallow binding has never been implemented in any statically scoped language. Shallow bindings require more work by a compiler. Deep binding in a statically scoped languages is an obvious choice. Symbol Table 22 Example: Shallow vs. Deep Bindings (Statically Scoped Language) Shallow binding: Doesn t make sense Symbol Table 23 Symbol Table A symbol table is a dictionary that maps names to the information the compiler knows about them. It is used to keep track of the names in statically scoped program. Its most basic operations are insert (to put a new mapping) and lookup (to retrieve the binding information for a given name). Symbol Table 24 4

Symbol Table Static scope rules in most languages require that the referencing environment be different in different parts of the program. It is possible to implement a semantic analyzer such that new mappings are inserted at the ning of the scope and removed at the. The Problems The straightforward approach to maintaining a referencing environment is not practical due to: Nested scope: an inner binding must hide its outer binding. Forward reference: names are sometimes used before they are declared. Symbol Table 25 Symbol Table 26 Multilevel Symbol Table Most static scope rules can be handled by augmenting a simple symbol table to allow embedding symbol tables. When an inner scope is entered, the compiler executes the enter_scope operation. It executes the leave_scope operation when exits. Lookup Operation When a lookup operation is initiated, the current symbol table is examined. If a given name is not found, an immediate outer symbol table is examined. This process is repeated until a binding is found for the name or an outermost symbol table is reached. Symbol Table 27 Symbol Table 28 5