CS 403 Programming Languages Lecture 3 Fall 2003

  • Slides: 18
Download presentation
CS 403: Programming Languages Lecture 3 Fall 2003 Department of Computer Science University of

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

Outline Reading & Questions from Last Class n Scopes n Symbol Tables n Reading

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

Question from Last Class n How many different scopes does C++ have? • Pair

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

Scope rules The region of the program in which a binding is active is

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

Static (Lexical) Scopes Global scope (basic, original awk) n Program unit (main program, subroutines,

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

Understanding Scopes in C++ • Pair Up: • • Given the following code, what

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 (a. Bool) {… i = x; } }; • } Lecture 3 © 2003 Joel Jones 6

Static Scope in FORTRAN n We will discuss two classes of Fortran objects variables

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

Static Scope (Cont. ) n Common block example subroutine first real b(2) logical flag

Static Scope (Cont. ) n 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 Lecture 3 b(1) b(2) i(1) i(2) c i(3) © 2003 Joel Jones i(4) flag x y z_0 i(5) i(6) i(7) i(8) 8

Static Scope (Cont. ) n n n Lifetime of statically allocated variables and common

Static Scope (Cont. ) n n n Lifetime of statically allocated variables and common blocks is the duration of the program. Most Fortran 77 implementations do 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. Lecture 3 © 2003 Joel Jones 9

Static Scope (Cont. ) n n The default is that common blocks can go

Static Scope (Cont. ) n n 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. Lecture 3 © 2003 Joel Jones 10

Symbol Tables n n Symbol tables are used to keep track of scope and

Symbol Tables n n 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. Lecture 3 © 2003 Joel Jones 11

Symbol Tables (Cont. ) n Each symbol table entry contains n n n the

Symbol Tables (Cont. ) n Each symbol table entry contains n n n 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. Lecture 3 © 2003 Joel Jones 12

Symbol Tables (Cont. ) n n The symbol table may contain the keywords at

Symbol Tables (Cont. ) n n 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. Lecture 3 © 2003 Joel Jones 13

Symbol Tables (Cont. ) n n n One of the important issues is handling

Symbol Tables (Cont. ) n n n 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: Lecture 3 © 2003 Joel Jones 14

Symbol Tables (Cont. ) procedure new_id(id) for index = top to scope_marker[LL - 1]

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() 4 top LL 2 A 2 C procedure scope_entry () scope_marker[LL++] = top 0 additional B procedure scope_exit() top = scope_marker[--LL] scope_marker A symbol table Lecture 3 © 2003 Joel Jones 15

Symbol Tables for C++ • Pair Up: • • Given the number of different

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? Lecture 3 © 2003 Joel Jones 16

Announcements n n ACM initial meeting of the year. Thursday, August 28 th, 5:

Announcements n n ACM initial meeting of the year. Thursday, August 28 th, 5: 00 pm, EE 119. Industrial Colloquium Series. Tuesday, September 2 nd, 5: 00 pm in EE 119. Herschel Chandler will talk about "Being an Independent Computer Consultant. " The talk will cover: What is it like to be an independent computer consultant? How to you prepare for such a career and how do you get started? How do you get clients, and what is it like working with them? Lecture 3 © 2003 Joel Jones 17

Reading for Next Class n Skim the following, paying particular attention to how to

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