Help needed to understand the SymbolTable module

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help needed to understand the SymbolTable module

reflectionalist
OK, my take is a bit weird.  After finishing the hardware part, I have read from 6 up through to 11 before programming any part of the software hierarchy (planned for a second go).  So far so good, until 11.3.3 about the SymbolTable module.  I don't get the two paragraphs on p. 238.  In particular, I do not understand the way of using running numbers associated to symbols to implement the abstraction of scope.  I understand well the classic way of using "a list of hash tables" to record an identifier's scope mentioned in 11.1 [p. 226].  But I have difficulty to see how the way proposed on p. 238 compares to the classic.  Can someone give me an example in comparison?

The last sentence of the first paragraph and the following four lines right below [p. 238] are also very confusing.  I can not see how they are related to the first paragraph.  Should there be a paragraph break before the last sentence of the paragraph?  I can not properly parse the four lines, confused by the double colons.  Should the content be put in a table?  Should some text be in another typographical style, say typewriter?  After all, I have accumulated a lot of style errors in the book and will post after I finish.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

cadet1620
Administrator
I'm on vacation and my book is about 1500km from me at the moment so I can't make sense of your section number / page number references, or anything about the typography.

The proposed design--using two hash tables--does not support the classic nested scope design. This is a simplified design that supports exactly two scopes: class scope and subroutine scope.  The hash table for class scope is created before parsing the file. The hash table for subroutine scope is created when a subroutine is found, before parsing the argument list. The hash table for the subroutine scope is destroyed when the end of the subroutine is encountered.

(Since nested subroutines are not allowed, the deletion of the hash table for the current subroutine can be delayed until the next subroutine is found.)

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

reflectionalist
Thanks for the reply even though on vacation. I will quote the text in question [p. 238] below (with typographical style reproduced in HTML):

This module provides services for creating and using a symbol table. Recall that each symbol has a scope from which it is visible in the source code. The symbol table implements this abstraction by giving each symbol a running number (index) within the scope. The index starts at 0, increments by 1 each time an identifier is added to the table, and resets to 0 when starting a new scope. The following kinds of identi- fiers may appear in the symbol table:

Static: Scope: class.

Field: Scope: class.

Argument: Scope: subroutine (method/function/constructor).

Var: Scope: subroutine (method/function/constructor).

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

ybakos
Reflectionalist, I recommend diving in and starting to build the software stack. Many elements are revealed concretely through the act of building, and I personally loved this part of the discovery.

Regarding the text in question, the author is simply stating that every symbol is going to be given a number, and these numbers should start at zero and simply increase. Second, this sequence of numbers should be independent for every class scope and function scope.

If a Jack class has two static variables (fields), a and b; and one function (subroutine) that has two arguments, x and y, and one local variable z; then we have five symbols (a, b, x, y and z) within two different scopes (the class and the function). During compilation, you have a symbol table like this:

ClassName
a -> 0
b -> 1

FunctionName
x -> 0
y -> 1
z -> 2


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

reflectionalist
ybakos:

Thank you for the recommendation and clarification.  According to your description, the running number assigned to each symbol in a scope seems to be just a counter.  But I still do not see what what it is used for, in particular, what it has to do with scope, given that the two separate hash tables deal with the class scope and the subroutine scope respectively.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

cadet1620
Administrator
reflectionalist wrote
According to your description, the running number assigned to each symbol in a scope seems to be just a counter.  But I still do not see what what it is used for, in particular, what it has to do with scope, given that the two separate hash tables deal with the class scope and the subroutine scope respectively.
The running number is the "index" for the symbol, returned by SymbolTable.IndexOf(). For example, for subroutine arguments, this index is the offset from ARG to the argument on the stack.
    function void foo(int num, String str)
will be called with the value of num at ARG+0 and the address of str at ARG+1.

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

ybakos
In reply to this post by reflectionalist
reflectionalist wrote
But I still do not see what what it is used for, in particular, what it has to do with scope, given that the two separate hash tables deal with the class scope and the subroutine scope respectively.
If you implement the VM layer you will see exactly why. :)

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help needed to understand the SymbolTable module

reflectionalist
In reply to this post by cadet1620
cadet1620 wrote
The running number is the "index" for the symbol, returned by SymbolTable.IndexOf(). For example, for subroutine arguments, this index is the offset from ARG to the argument on the stack.
    function void foo(int num, String str)
will be called with the value of num at ARG+0 and the address of str at ARG+1.

Thanks for the elaboration. I infer from the example on Page 226 (reproduced below) and Figure 11.6 [p. 236] that the symbol table keeps four running numbers respectively for static, fields, argument and local.

Name Type Kind #
nAccounts int static 0
id int field 0
name String field 1
balance int field 2
sum int argument 0
status boolean local 0

However, the text I quoted above on Page 238 and ybakos' and your answers suggests there are only two running numbers, respectively for class scope and subroutine scope. I am confused.

Loading...