You are on page 1of 3

CS 403: Programming Outline

Languages n Reading & Questions from Last Class


n Scopes
Lecture 3 n Symbol Tables
Fall 2004 n Reading for Next Class
Department of
Computer Science
University of Alabama
Joel Jones
©2004 Joel Jones Lecture 3 ©2004 Joel Jones
1 2

Question from Last Class Scope rules


• How many different scopes does C++ have? • The region of the program in which a
binding is active is its scope.
• Most languages today are lexically
scoped

• Pair Up:
• Decide on answers to the questions and the justification

Lecture 3 ©2004 Joel Jones Lecture 3 ©2004 Joel Jones


3 4

Static (Lexical) Scopes Understanding Scopes in C++


• Global scope (basic, original awk) Pair Up:
• Program unit (main program, subroutines, • Given the following code, what is the sequence of scopes that are
searched to find the declaration of “x”?
functions) in FORTRAN, C, C++.
Foo.c++:
• Blocks (C, C++, Java, Pascal, etc.)

• Objects (structs, unions) (C++, Java, etc.) class Foo::Bar {
• Enumerations (C, C++) …
void f() {… int i; if (aBool) {… i = x; } };
• File scope (C, C++)
}
Lecture 3 ©2004 Joel Jones Lecture 3 ©2004 Joel Jones
5 6
Static Scope in FORTRAN Static Scope (Cont.)
• We will discuss two classes of Fortran • Common block example
subroutine first
objects real b(2)
logical flag
• variables complex c
type coordinates
b(1) b(2) c flag x y z_0
• common blocks sequence
real x, y i(1) i(2) i(3) i(4) i(5) i(6) i(7) i(8)
logical z_0
• Common blocks are blocks of storage end type coordinates
type (coordinates) p
that can be shared by several program common /reuse/ b,c,flag,p

units. subroutine second
integer I(8)
common /reuse/ i

Lecture 3 ©2004 Joel Jones Lecture 3 ©2004 Joel Jones


7 8

Static Scope (Cont.) Static Scope (Cont.)


• Lifetime of statically allocated variables and common • The default is that common blocks can go away
blocks is the duration of the program.
when there are no active program units that
• Most Fortran 77 implementations do all allocations
statically. access them.
• If default allocation is not static, variables and common • Saved variables and saved common blocks
blocks ca be saved (I.e. declared as save: save / may cause collisions in parallel executions, but
reuse/,w,r) to force their static allocation.
private entities enable the creation of new
• Variables can only be saved in the program unit where
they are declared. copies with each invocation.
• If a common block is saved, it has to be saved in all
program units where it appears.

Lecture 3 ©2004 Joel Jones Lecture 3 ©2004 Joel Jones


9 10

Symbol Tables Symbol Tables (Cont.)


• Symbol tables are used to keep track of scope • Each symbol table entry contains
and binding information about names. • the symbol name,
• its category (scalar variable, array, constant, type, procedure,
• The symbol table is searched every time a field name, parameter, etc.)
name is encountered in the source text. • scope number,
• type (a pointer to another symbol table entry),
• Changes occur when a new name or new
• and additional, category specific fields (e.g. rank and shape for
information about a name is discovered. arrays)
• The abstract syntax tree will contain pointers to • To keep symbol table records uniform, it may be
the symbol table rather than the actual names convenient for some of the information about a name to
used for objects in the source text. be kept outside the table entry, with only a pointer to
this information stored in the entry.

Lecture 3 ©2004 Joel Jones Lecture 3 ©2004 Joel Jones


11 12
Symbol Tables (Cont.) Symbol Tables (Cont.)
• The symbol table may contain the keywords at • One of the important issues is handling static
the beginning if the lexical scanner searches the scope.
symbol table for each name . • A simple solution is to create a symbol table for
• Alternatively, the lexical scanner can identify each scope and attach it to the node in the
keywords using a separate table or by creating abstract syntax tree corresponding to the scope.
a separate final state for each keyword. • An alter native is to use a additional data
structure to keep track of the scope. This
structure would resemble a stack:

Lecture 3 ©2004 Joel Jones Lecture 3 ©2004 Joel Jones


13 14

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
Symbol Tables for C++
error()
k = new_symbol_table_index()
symbol_table[k].name = id Pair Up:
additional[top++] = k
• Given the number of different scopes in C++, does the previous
procedure old_id(id)
for index= top to 0 by -1 4
representation provide much guidance for implementing a C++
top
if id == compiler? Why or why not? What problems are involved?
symbol_table[additional[index]].name
then
return additional[index] 2 LL
A
error()
2
C 0
procedure scope_entry ()
additional
scope_marker[LL++] = top scope_marker
B

procedure scope_exit() A
top = scope_marker[--LL]
symbol table
Lecture 3 ©2004 Joel Jones
16

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

Lecture 3 ©2004 Joel Jones


17

You might also like