Sunday, December 16, 2007

Mynx Compiler Symbol Table as a Pattern and through Context

In implementing the Mynx compiler using a sentential-oriented, bounded-context parse algorithm I've had to backup and re-implement; often I realized the original implementation of a function or phase of the compiler was inefficient and/or ineffective. In writing the code for the symbol table I realize the original implementation uses the symbol table as it is designed--a singleton pattern.

Symbol Table as Singleton



The symbol table is implemented using the singleton pattern, and accessed with a method which either creates or returns the reference to the symbol table instance object. So it is a logical choice to use a singleton pattern to implement the symbol table.

The symbol table is the central nexus of information for the compiler. Information is written into the symbol table such as type, name, etc. and read from for semantic context checks. The symbol table acts like a database of information for the compiler.

Quantifying the Inefficiency with a Rationale



I discuss (in relation to my narrative writing) and exposit upon what I call QR Approach--to quantify a criticism and explain with a rationale or reason. Using the approach of quantify and rationale, inefficiency is quantified in the symbol table design and implementation.

  1. The quantification is that the symbol table has universal or global access through the singleton access method. The error reporting is mixed into the symbol table, but the symbol table does not report errors, it should return a status that propagates back to the original user of the symbol table.

  2. The rationale is that the symbol table should have a single point of access, not a global access like a global variable. Error reporting should be an outside function or operation--not done by the symbol table. For each of the criterion, a re-design with implementation is necessary.


Context Object



The Context object is the Mynx compiler functions as a nexus of information among each sentence, storing context information for semantic checks and to annotate to each sentence. For example in a nested loop, a unique label is generated in the code synthesis stage, but each sentence only knows the particular depth in such a statement sentences as:


exit for(3);
next while(2);
end loop(0);


The Context object contains the information for nesting depth, and also for context check of validity of a statement using depth is performed. The Context object acts as a single point of access for context information.

The Context object is implemented using the singleton pattern. The sentences are objects with a method "check" that takes a Context object, and then using the visitor pattern invokes methods on the Context object, passing the sentence itself to the Context object.


public void check(Context ctx)
{
//some other semantic checks

ctx.doContextCheck(this); //pass the sentence for semantic check in Context
}//end check


Symbol Table + Context Object



The symbol table is encapsulated within the Context object. It makes logical sense to access the symbol table via the context object--following the idea of a single access point.

Accessing the symbol table through the context object makes the symbol table more efficient by eliminating the global access via the singleton access method SymbolTable.getSymbolTable().

The symbol table returns via the context object method a Boolean logical value of true or false, and the error is reported in the sentence performing the symbol table operation. The sentence then reports the error in an error Reporter class. The Context object stores error information, and after the syntax and semantic analysis (both done in tandem on the fly), any contextual errors are reported (obviously, code synthesis cannot continue).

In re-implementation, the symbol table has two actions:


  1. Write - write information into the symbol table.

  2. Read - read information from the symbol table.



The first phase of re-implementation is to re-write the access to the symbol table through the Context object, to write data information during the compiler in the syntax and semantic analysis. The first phase handles declarative statements, the second phase handles executable statements. Now to do it without breaking any other code.

Labels: , , , , ,

Website Spy Software