Download 9 - To Parent Directory

Transcript
73
within blocks within functions. At the present time, all of our identifiers are global.
We will not have local variables until we introduce functions in Lab 11.
When the program enters a new block (by encountering a left brace), a new level
is created in the symbol table. Any variables declared at that level are placed in this
level of the symbol table. When the program exits a block (by encountering a right
brace), the corresponding level of the symbol table is disposed of; those variables no
longer exist.
7.3
The IdEntry Class
Open the file IdEntry.java to see what the data members are. The member name is
the identifier value, i.e., the string that was found in the source file. The member type
is the data type. For the time being, this will be int. In Lab 10, we will introduce
the double type. The member blockLevel is the level at which the identifier was
defined: level 1 for reserved words, level 2 for globals, level 3 for locals, and so on. The
member scope is either GLOBAL, LOCAL, or PARAM. The member offset will be used
later when we introduce function calls. It represents the position of a local variable
or parameter in the activation record on the run-time stack.
The IdEntry class includes a toString() function. In Java, whenever an object
is sent to the println() function, that object’s toString() member function is
automatically invoked. We include the function here for debugging purposes. To
help follow what is happening, we may want to print an IdEntry object. If you want
less IdEntry information displayed, you may modify this function.
7.4
The Hashtable and LinkedList Classes
The various levels of the symbol table could be implemented individually as linked
lists. Instead, we will implement them as hash tables in order to speed up access
to the elements and to give ourselves come experience with using hash tables. Java
provides a complete Hashtable class. Go to the Java web page and read about the
Hashtable class. The class is found in the java.util package. Note the functions
put() and get(). These functions will allow us to put identifiers in the table and
later retrieve them. Hold on to that page; you will need it again in a few minutes.
You may want to read later the full description of hash tables in the introductory
part of the Hashtable web page.