Are building a symbol table and code generation done in two seperate passes?

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

Are building a symbol table and code generation done in two seperate passes?

kraftwerk1611
Hi,

I have created a Symbol Table class following the given API.

The next question that I am facing is if there are two passes required through the Jack file to do this project?

1- first to populate the symbol table with information about all the given identifiers and then

2-making a 2nd pass through jack file, generating code referring to information in symbol tables. But I am not sure how to handle subroutine scope in this case as the book says that we should have only one symbol table for the subroutines at a time. So if I create a S.T for one subroutine and then when the next subroutine starts what to do with the first table.

How are these two tasks of populating the symbol table and generating xml/vm code related? Are they done one after another with a kind of finish-to-start relationship  or are they done simultaneously?

Thanks for any replies that clears this up.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Are building a symbol table and code generation done in two seperate passes?

cadet1620
Administrator
The jack language was carefully designed so that it can be compiled in a single pass.

class FooBar {
    class variable declaration

    function void foo(argument declaration) {
        foo's local variable declaration
        code that references class and foo variables
    }
    // discard foo's symbols

    function void bar(argument declaration) {
        bar's local variable declaration
        code that references class and bar variables
    }
    // discard bar's symbols
}

The only identifiers that need to be put into the SymbolTable are the class fields and statics, and the functions' arguments and vars.  The syntax enforces that they be declared before any references to them.

Any identifier that is encountered that is not in the symbol table can be assumed to be a class name or function name. The syntax of function calls and identifier presence/absence in the SymbolTable enables you to tell the difference:
    function(...)
    variable.function(...)
    class.function(...)


If you keep your XML output code from project 11 intact and just add the appropriate VmWriter calls, the XML is quite handy for debugging code generation problems. XML output isn't required for the final compiler. My compiler doesn't generate the XML file unless the -x command line option is used.

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

Re: Are building a symbol table and code generation done in two seperate passes?

kraftwerk1611
Thanks.

Should the xml tag like

<keyword> field </keyword>

be replaced by the following

<kind>field</kind>

Or should we add an extra <kind> tag under <keyword>

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

Re: Are building a symbol table and code generation done in two seperate passes?

cadet1620
Administrator
There isn't "correct" answer for compiler XML output since there is no test XML supplied for project 11.

My compiler generates the same XML as the final JackAnalyzer from project 10. This allows regression testing. If some changes I've made to my compiler break something, I can run my compiler on the files in project 10 and maybe get some additional clues if I screwed up the syntax analysis.

--Mark
Loading...