[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implementing identification on SableCC ASTs

Hi Jens,

From what I can understand the problem you are having
is how to use the information stored in the symbol
table by your identification class to your code

Well, if that is the case, then this is how you solve
your problem: You will have your identificationg
class, which I'll call SemanticAnalyser and your code
generator class, which I'll call CodeGenerator. Each
of this classes will have a field of type SymbolTable
(or whatever your object is), then in your main class
you will have One as well. This object from your main
class is going to be shared by both your
SemanticAnalyser class and your CodeGenerator class.
You do this by passing the symbol table object from
your main class to the constructor of both the
SemanticAnalyser and the CodeGenerator. Here is how it
looks like:

public class SemanticAnalyser extends
DepthFirstAdapter {
  // this is the symbol table to be used by
  // by the methods that store the variables
  SymbolTable symbolTable; 
  // now you create a constructor for your 
  // SemanticAnalyser, which takes a SymbolTable
  // as parameter. 
  public SemanticAnalyser(SymbolTable symbolTable) {
    // since java passes objects by reference to  
    // methods, when we modify the symbolTable
    // object, we also modify the object passed
    // as parameter to the constructor
    // assign the symbolTable object parameter
    // to the symbolTable member variable
    this.symbolTable =  symbolTable;

  // here you code your inXXX and outXXX methods
  // that store your variables in the symbol table.
} // end class

// now your code generator looks the same
public class CodeGenerator extends DepthFirstAdapter {
  // your member variable symbolTable
  SymbolTable symbolTable;
  // your constructor
  public CodeGenerator(SymbolTable symbolTable) {
    // this has the same effect as the
    this.symbolTable = symbolTable;

  // your methods that generate your code
  // and access your symbol table go here
} // end class

Now your main class:

public class Main {
  public static void main(String[] args) {
    // create an instace of your symbol table
    // I am assuming that the constructor for
    // SymbolTable does not take any parameters
    SymbolTable symbolTable = new SymbolTable();

    // get the root node
    Node root = TreeBuilder.getNode(
                  new PushbackReader(
                    new BufferedReader(
                      new FileReader(args[0])),
    //  process the tree with the semantic
    // analyser and fill the symbolTable object
    // with the variables     
    root.apply(new SemanticAnalyser(symbolTable));

    // process code generation
    // your symbolTable object was filled
    // with variables by your SemanticAnalyser
    // now you can pass it to your codeGenerator
    // which will use it
    root.apply(new CodeGenerator(symbolTable));
  catch(Exception exc) {
    System.out.println(exc);  // output the exception 
} // end main class

Hope that makes sense to you.
If you have any other queries, just post it.

All the best


Yahoo! Plus
For a better Internet experience