scope
play

Scope A scope is a textual region of the program in which a - PDF document

Scope A scope is a textual region of the program in which a (name-to-object) binding is CSC 4181 active. Compiler Construction There are two types of scope: Static scope Scope and Symbol Table Dynamic scope Most modern


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

  2. Hole and Qualifier Static Links and Static Chains • A name-to-object binding that is hidden by • A static link points to the activation record a nested declaration of the same name is of its lexically-scoped parent. said to have a hole in its scope. • Static chain is a chain of static links • In most languages, the object whose name connecting certain activation record is hidden is inaccessible in the nested instances in the stack. scope. • Some languages allow accesses to the outer meaning of a name by applying a qualifier or scope resolution operator. Symbol Table 7 Symbol Table 8 7 8 Static Links and Static Chains Scope in OOP • In OOP, scope extends 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 9 10 Dynamic Scope Dynamic Links • The bindings between names and objects • A dynamic link point to its caller activation depend on the flow of control at run time. record. • 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 Table 11 Symbol Table 12 11 12 2

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend