Programming Lecture: Scope and Symbol Tables, Assignments of Programming Languages

A part of programming language course notes, focusing on the concepts of scope and symbol tables. The differences between static and dynamic scoping, the role of symbol tables in keeping track of names and their bindings, and the handling of nested scopes. Dr. Amie souter's announcements include information about term projects, midterms, and homework assignments.

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-4jo
koofers-user-4jo 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
4/18/2005
1
Programming Languages
Dr. Amie Souter
Announcements
Term project
partners
questions?
Midterm 1
Next week
Chapters 1, 2, 3, 4, 5, 11
Homework 2
–posted
Any questions??
4/18/2005
3
Semantics
Syntax: structure
Semantics: meaning
Attributes
Bindings
Symbol Tables
Lifetimes
–Environments
Aliases, Dangling References, Garbage
4
Attributes
Attributes
zProperties of language entities, especially
identifiers used in a program.
int myattrex(int a, int b, int c){
int x, y, z;
z = a * b + c – d + e;
z = d;
}
zDeclarations ("definitions") bind attributes
to identifiers.
pf3
pf4
pf5

Partial preview of the text

Download Programming Lecture: Scope and Symbol Tables and more Assignments Programming Languages in PDF only on Docsity!

4/18/2005 1

Programming Languages

Dr. Amie Souter

Announcements

  • Term project
    • partners
    • questions?
  • Midterm 1
    • Next week
    • Chapters 1, 2, 3, 4, 5, 11
  • Homework 2
    • posted
  • Any questions??

4/18/2005 3

Semantics

• Syntax: structure

• Semantics: meaning

  • Attributes
  • Bindings
  • Symbol Tables
  • Lifetimes
  • Environments
  • Aliases, Dangling References, Garbage

4

AttributesAttributes

z Properties of language entities, especially

identifiers used in a program.

int myattrex(int a, int b, int c){

int x, y, z;

z = a * b + c – d + e; … z = d; }

z Declarations ("definitions") bind attributes

to identifiers.

5

Binding times can vary widelyBinding times can vary widely

int myattrex(int a, int b, int c){

int x, y, z;

const int m = 200;

z = a * b + c – d + e;

z = d;

6

ScopeScope

z The scope of a declaration is the region of the program to which the bindings established by the declaration apply. (If individual bindings apply over different regions: scope of a binding.) z Scope is typically indicated implicitly by the position of the declaration in the code, though keywords can modify it. z In a block-structured language, the scope is typically the code from the end of the declaration to the end of the "block" (indicated by braces {…} in C and Java) in which the declaration occurs. z Scope can extend backwards to the beginning of the block in certain cases (class declarations in Java and C++, top-level declarations in Scheme).

4/18/2005 7

Questions about Scope Rules

  • When is a new scope encountered?
    • new scope is encountered on subroutine entry
  • What occurs when new scope is opened?
    • create bindings for new local variables (process also calledelaboration)
    • deactivate bindings for global variables that are hidden by local ones with same name (these global variable are said to have a "hole" in their scope)
  • What occurs on subroutine exit?
    • destroy bindings for local variables and reactivate bindings for global variables that were deactivated (hidden) 8

Scope ExampleScope Example

int x; void p(int a){ int y; char z; z = y + z; { int m; z = m + z; } }

int main(){ int x; char z;

z = x * x; }

14

Dynamic scope evaluatedDynamic scope evaluated

z Almost all languages use lexical scope: with dynamic scope the meaning of a variable cannot be known until execution time, thus there cannot be any static checking. z In particular, no static type checking. z Originally used in Lisp. Scheme could still use it, but doesn't. Some languages still use it: VBScript, Javascript, Perl (older versions). z Lisp inventor (McCarthy) now calls it a bug. z Still useful as a pedagogical tool to understand the workings of scope. In some ways a lot like dynamic binding of methods.

4/18/2005 15

Symbol Tables

  • What is a Symbol Table?
    • Used to keep track of names (and what they refer to) in a statically scoped language
    • Built and used during compilation
  • Basic idea
    • Implement as a dictionary - maps names to the information the compiler knows about them (type, etc)
    • Operations - insert and lookup
  • How to handle nested scopes?
    • Problem: a local declaration can hide a global one with the same name
    • Cannot remove the global one – because it becomes visible again outside the local scope

4/18/2005 16

Symbol Tables

  • Solution
  • Use scope labels and a separate stack of active scopes
    • When a new scope is encountered (at compilation)
      • assign a label to it
      • push an entry for that scope on the stack (enter_scope)
    • When a declaration is encountered
      • insert the name in the table together with the label of current scope
    • When a name is referenced
      • lookup for the name (in the table), that has the label of the current or outer scopes (as shown in the stack)
    • When leaving a scope
      • pop the scope from the stack (leave_scope)
    • All names are kept in the table, nothing is ever deleted
    • Only entries on the stack are pushed and popped (^17)

Symbol table structureSymbol table structure

z A table of little stacks of declarations under each name (using lexical scope):

int local to x main

public static f void method

name bindings

main publicvoid method static

public static int

args (^) parameter of String[] main

public class Scope { public static int x = 2; public static void f() { System.out.println(x); } public static void main(String[] args) { int x = 3; f(); } }

18

Symbol Table ExampleSymbol Table Example

int x; void p(int a){ int y; char z; z = y+x; { int y; z = y + 2; // what does the symbol table look like at //this point? } }

int main(){ int x; char z; p(); z = x * x; } (^19)

OverloadingOverloading

z To what extent can the same name be

used to refer to different things in a

program?

20

Symbol table and environmentSymbol table and environment

z A dictionary or table is used to maintain the identifier/attribute bindings.

z It can be maintained either during translation or execution or both. (Pre-translation entities are entered into the initial or default table.)

z During translation this table is called the symbol table.

z During execution this table is called the environment.

z If both are maintained, the environment can usually dispense with names, keeping track only of locations (names are maintained implicitly).

21

The EnvironmentThe Environment

z Can be constructed entirely statically (Fortran): all vars and functions have fixed locations for the duration of execution. z Can also be entirely dynamic: functional languages like Scheme and ML. z Most language use a mix: C, C++, Java, Ada. z Consists of three components:

  • A fixed area for static allocation
  • A stack area for lifo allocation (usually the processor stack)
  • A "heap" area for on-demand dynamic allocation (with or without garbage collection)

26

Stack at Point #2:Stack at Point #2:

© 2003 Brooks/Cole - Thomson Learning™ 30

Lifetime/ExtentLifetime/Extent

z The lifetime or extent of a program entity is the duration of its allocation in the environment. z Allocation is static when the lifetime is the duration of the entire program execution.

z Lifetime is related to but not identical to scope. With scope holes, lifetime can extend to regions of the program where the program entity is not accessible. z It is also possible for scope to exceed lifetime when a language allows locations to be manipulated directly (as for example manual deallocation). This is of course very dangerous!

31

Variables and ConstantsVariables and Constants

z A variable is an object whose stored value

can change during execution.

z A constant is an object whose value does

not change throughout its lifetime.

z Constants are often confused with literals:

constants have names, literals do not.

z Constants may be:

  • compile-time static (may not ever be allocated)
  • load-time static
  • dynamic

32

Aliases, Dangling References, andAliases, Dangling References, and

GarbageGarbage

z An alias occurs when the same object is bound to two different names at the same time. This is fairly common with Java objects. z A dangling reference is a location that has been deallocated from the environment, but is still accessible within the program. Dangling references are impossible in a garbage-collected environment with no direct access to addresses. z Garbage is memory that is still allocated in the environment but has become inaccessible to the program. Garbage can be a problem in a non- garbage collected environment, but is much less serious than dangling references.