Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.

Similar presentations


Presentation on theme: "©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones."— Presentation transcript:

1 ©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones

2 Lecture 3©2004 Joel Jones 2 Outline Reading & Questions from Last Class Scopes Symbol Tables Reading for Next Class

3 Lecture 3©2004 Joel Jones 3 Question from Last Class How many different scopes does C++ have? Pair Up: Decide on answers to the questions and the justification

4 Lecture 3©2004 Joel Jones 4 Scope rules The region of the program in which a binding is active is its scope. Most languages today are lexically scoped

5 Lecture 3©2004 Joel Jones 5 Static (Lexical) Scopes Global scope (basic, original awk) Program unit (main program, subroutines, functions) in FORTRAN, C, C++. Blocks (C, C++, Java, Pascal, etc.) Objects (structs, unions) (C++, Java, etc.) Enumerations (C, C++) File scope (C, C++)

6 Lecture 3©2004 Joel Jones 6 Understanding Scopes in C++ Pair Up: Given the following code, what is the sequence of scopes that are searched to find the declaration of “x”? Foo.c++: … class Foo::Bar { … void f() {… int i; if (aBool) {… i = x; } }; }

7 Lecture 3©2004 Joel Jones 7 Static Scope in FORTRAN We will discuss two classes of Fortran objects variables common blocks Common blocks are blocks of storage that can be shared by several program units.

8 Lecture 3©2004 Joel Jones 8 Static Scope (Cont.) Common block example subroutine first real b(2) logical flag complex c type coordinates sequence real x, y logical z_0 end type coordinates type (coordinates) p common /reuse/ b,c,flag,p … subroutine second integer I(8) common /reuse/ i z_0yxflag i(8)i(7)i(6)i(5)i(4)i(3) c i(2) b(2) i(1) b(1)

9 Lecture 3©2004 Joel Jones 9 Static Scope (Cont.) Lifetime of statically allocated variables and common blocks is the duration of the program. Most Fortran 77 implementations do all allocations statically. If default allocation is not static, variables and common blocks ca be saved (I.e. declared as save: save /reuse/,w,r) to force their static allocation. Variables can only be saved in the program unit where they are declared. If a common block is saved, it has to be saved in all program units where it appears.

10 Lecture 3©2004 Joel Jones 10 Static Scope (Cont.) The default is that common blocks can go away when there are no active program units that access them. Saved variables and saved common blocks may cause collisions in parallel executions, but private entities enable the creation of new copies with each invocation.

11 Lecture 3©2004 Joel Jones 11 Symbol Tables Symbol tables are used to keep track of scope and binding information about names. The symbol table is searched every time a name is encountered in the source text. Changes occur when a new name or new information about a name is discovered. The abstract syntax tree will contain pointers to the symbol table rather than the actual names used for objects in the source text.

12 Lecture 3©2004 Joel Jones 12 Symbol Tables (Cont.) Each symbol table entry contains the symbol name, its category (scalar variable, array, constant, type, procedure, field name, parameter, etc.) scope number, type (a pointer to another symbol table entry), and additional, category specific fields (e.g. rank and shape for arrays) To keep symbol table records uniform, it may be convenient for some of the information about a name to be kept outside the table entry, with only a pointer to this information stored in the entry.

13 Lecture 3©2004 Joel Jones 13 Symbol Tables (Cont.) The symbol table may contain the keywords at the beginning if the lexical scanner searches the symbol table for each name. Alternatively, the lexical scanner can identify keywords using a separate table or by creating a separate final state for each keyword.

14 Lecture 3©2004 Joel Jones 14 Symbol Tables (Cont.) One of the important issues is handling static scope. A simple solution is to create a symbol table for each scope and attach it to the node in the abstract syntax tree corresponding to the scope. An alter native is to use a additional data structure to keep track of the scope. This structure would resemble a stack:

15 Symbol Tables (Cont.) procedure new_id(id) for index = top to scope_marker[LL - 1] by -1 if id == symbol_table[additional[index]].name then error() k = new_symbol_table_index() symbol_table[k].name = id additional[top++] = k procedure old_id(id) for index= top to 0 by -1 if id == symbol_table[additional[index]].name then return additional[index] error() procedure scope_entry () scope_marker[LL++] = top procedure scope_exit() top = scope_marker[--LL] A C A B additional 0 2 scope_marker 4 top 2 LL symbol table

16 Lecture 3©2004 Joel Jones 16 Symbol Tables for C++ Pair Up: Given the number of different scopes in C++, does the previous representation provide much guidance for implementing a C++ compiler? Why or why not? What problems are involved?

17 Lecture 3©2004 Joel Jones 17 Reading for Next Class Skim the following, paying particular attention to how to print the addresses of variables: Debugging with GDB: The GNU Source Level Debugger http://www.gnu.org/manual/gdb-5.1.1/gdb.html GDB Quick Reference Card http://www.refcards.com/download/gdb-refcard-letter.pdf


Download ppt "©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones."

Similar presentations


Ads by Google