CSC 4181 Compiler Construction Scope and Symbol Table.

Slides:



Advertisements
Similar presentations
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 5 Basic Semantics
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Run time vs. Compile time
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 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
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.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
Runtime Organization.
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 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
COMPILERS Semantic Analysis hussein suleman uct csc3003s 2009.
Advanced Programming in C
Implementing Subprograms
Chapter 3:: Names, Scopes, and Bindings
Chapter 10 : Implementing Subprograms
Name Spaces: ALL versus OOL
Implementing Subprograms Chapter 10
Implementing Subprograms
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Names.
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
Scope, Visibility, and Lifetime
Implementing Subprograms
UNIT V Run Time Environments.
COMPILERS Semantic Analysis
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
Implementing Subprograms
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

CSC 4181 Compiler Construction Scope and Symbol Table

Symbol Table2 Scope 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 Table3 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.

Symbol Table4 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 Table5 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.

Symbol Table6 Nested subroutines in Pascal

Symbol Table7 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.

Symbol Table8 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 Table9 Static Links and Static Chains

Symbol Table10 Scope in OOP In OOP, scope extends beyond functions and the main program. Each class defines a scope that cover every functions scope in that class. Inheritance and access modifiers also make some variables and functions visible outside their scope.

Symbol Table11 Dynamic Scope The bindings between names and objects depend 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 tend to be interpreted rather than compiled.

Symbol Table12 Dynamic Links A dynamic link point to its caller activation record.

Symbol Table13 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.

Symbol Table14 Example: Static vs. Dynamic Scope var a : integer; procedure first a := 1; procedure second var a : integer; first(); begin a := 2; second(); write_integer(a); end; Static Scope: 1 Dynamic Scope: 2

Symbol Table15 Example: Static Scope var a : integer; procedure first a := 1; procedure second var a : integer; first(); begin a := 2; second(); write_integer(a); end; var a : integer; main() a := 2; second() var a : integer; first() a := 1; write_integer(a); The program prints 1

Symbol Table16 Example: Dynamic Scope var a : integer; procedure first a := 1; procedure second var a : integer; first(); begin a := 2; second(); write_integer(a); end; var a : integer; main() a := 2; second() var a : integer; first() a := 1; write_integer(a); The program prints 2

Symbol Table17 Referencing Environment A referencing environment is a set of active bindings at any point during programs execution. It corresponds to a sequence of scopes that can be examined in order to find the current binding for a given name.

Symbol Table18 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 Table19 Example: Shallow vs. Deep Bindings (Dynamically Scoped Language) var thres : integer; function older(p : person) : boolean return p.age > thres procedure show(p : person, c : function) begin var thres : integer; thres := 20; if c(p) write(p) end procedure main(p) begin thres := 35; show(p, older); end Deep binding: prints person p if older than 35 Shallow binding: prints person p if older than 20

Symbol Table20 Example: Deep Binding (Dynamically Scoped Language) var thres : integer; function older(p : person) : boolean return p.age > thres procedure show(p : person, c : function) begin var thres : integer; thres := 20; if c(p) write(p) end procedure main(p) begin thres := 35; show(p, older); end main(p) thres := 35 show(p, older) var thres : integer thres := 20 older(p) return p.age > thres if write(p)

Symbol Table21 Example: Shallow Binding (Dynamically Scoped Language) var thres : integer; function older(p : person) : boolean return p.age > thres procedure show(p : person, c : function) begin var thres : integer; thres := 20; if c(p) write(p) end procedure main(p) begin thres := 35; show(p, older); end main(p) thres := 35 show(p, older) var thres : integer thres := 20 older(p) return p.age > thres if write(p)

Symbol Table22 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 Table23 Example: Shallow vs. Deep Bindings (Statically Scoped Language) var thres : integer; function older(p : person) : boolean return p.age > thres procedure show(p : person, c : function) begin var thres : integer; thres := 20; if c(p) write(p) end procedure main(p) begin thres := 35; show(p, older); end Shallow binding: Doesnt make sense

Symbol Table24 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 Table25 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 beginning of the scope and removed at the end.

Symbol Table26 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 Table27 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.

Symbol Table28 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.