AST with Scope Re: [antlr-interest] Re: Can Resolvers etc be written as ANTLR tree parsers?

Matthew Ford Matthew.Ford at forward.com.au
Tue Jan 14 18:28:47 PST 2003


See below for an example of carrying around the scope with the AST node.

(Sorry the code has some other specific stuff in it also)


/* AD V1.0
 * (c)Forward Computing and Control Pty. Ltd.
 */
package au.com.forward.gaussAD.translators;

import antlr.CommonAST;
import antlr.Token;
import antlr.collections.AST;

import au.com.forward.gaussAD.symbolTable.*;

/** A CommonAST which also stores scope
 * also adds flag to indicate if
 * result of this node is optimized diagonal matrix
 *
 */
public class CommonASTWithScope extends CommonAST
       implements GAUSS_AD_PARSERTokenTypes {

    protected Scope scope=null; // save the scope

    protected SymbolTableEntry tableEntry = null;

    static private GaussADWalkerPrinter printer = new
GaussADWalkerPrinter();

    private int line = 0;  // for line numbers

    public int getLine(){
         return line;
    }


    public CommonASTWithScope() {
   super();
    }

    public CommonASTWithScope(Token tok) {
   super(tok);
   tableEntry = new SymbolTableEntry(tok.getText());
   setTokenType(tok.getType());
    }

    public void initialize(AST t) {
     super.initialize(t);
     if (t instanceof CommonASTWithScope) {
      // set scope and table entry
       Scope tmpScope = ((CommonASTWithScope)t).getScope();
        line=((CommonASTWithScope)t).getLine();
       if (tmpScope != null) {
        //scope = tmpScope.cloneScope();
        scope = tmpScope;  // should clone here but need to sort out
problems
       }
      // clone tableEntry
      tableEntry =
((CommonASTWithScope)t).getSymbolTableEntry().symbolTableEntryClone();
     }
    }


    public void initialize(int t, String txt) {
     super.initialize(t,txt);
     tableEntry = new SymbolTableEntry(txt);
     setTokenType(t);
    }

    // over rides entry text with this txt
    public void initialize(int t, String txt, SymbolTableEntry entry) {
     super.initialize(t,txt);
     tableEntry = new SymbolTableEntry(txt,entry);
     setTokenType(t);
    }

    private void setTokenType(int t) {
     if (t == ONE) {
      tableEntry.setONE();
     } else if (t==ZERO) {
      tableEntry.setZERO();
     } else if (t==NUM_FLOAT) {
      tableEntry.setNumber();
     }
    }

    public void initialize(Token t) {
     super.initialize(t);
     line=t.getLine();
     tableEntry = new SymbolTableEntry(t.getText());
     setTokenType(t.getType());
    }

    /**
    * Set the token text for this node
    * This clears the symbolTableEntry
    */
    public void setText(String text) {
     super.setText(text);
     tableEntry = new SymbolTableEntry(text);
  }



   /** Get the first child of this node; null if not children */
    public CommonASTWithScope getChild() {
    return (CommonASTWithScope) super.getFirstChild();
    }

    /** Get the next sibling in line after this one */
    public CommonASTWithScope getSibling() {
       return (CommonASTWithScope) super.getNextSibling();
    }

   /** Is node t equal to this in terms of token type and text?
    * use Scope identsEqual to control ignore Case
    */
    public boolean equals(AST t) {
   if ( t==null ) return false;
   return Scope.identsEqual(this.getText(),t.getText()) &&
         this.getType() == t.getType();
    }

   /** Is node t equal to this in terms of token type and text?
    * use Scope identsEqual to control ignore Case
    */
    public boolean identEquals(String str) {
   if ( str==null ) return false;
   return Scope.identsEqual(this.getText(),str);
    }

    public void setScope(Scope scope) {
     this.scope = scope;
    }

    public Scope getScope() {
     return(scope);
    }

    public void setSymbolTableEntry(SymbolTableEntry entry) {
      tableEntry = entry;  // don't clone
    }

    public SymbolTableEntry getSymbolTableEntry() {
     return tableEntry;
    }


    /**
     * return true if this node represents
     * a diagional matrix by only saving the
     * diagional vector.
     */
    public boolean isOptimizedDiag() {
     if (tableEntry == null) {
      return false;
     } // else {
     return tableEntry.isOptimizedDiag();
    }


  // walk along the siblings and for each sibling do all children
 public CommonASTWithScope replaceAll(CommonASTWithScope t,
CommonASTWithScope target, CommonASTWithScope replacement) {
  CommonASTWithScope result = null;
  if (t == null) {
   return t;
  }
//   System.out.println("input: " +t.toStringList());
//   System.out.println("target " +target.toStringList());
//   System.out.println("replacement " + replacement.toStringList());

    if (t.equalsTree(target)) {
     result = (CommonASTWithScope)AST_Utils.astFactory.dupTree(replacement);
//System.out.println(" root equals target result="+result.toStringList());
    } else {
     result = (CommonASTWithScope)AST_Utils.astFactory.dup(t);
//System.out.println(" root not equal check children of " +
t.toStringList());
    if ( t.getFirstChild()!=null ) {

result.setFirstChild(replaceAll(((CommonASTWithScope)t.getFirstChild()),
target, replacement));
     }
  }
//System.out.println(" finished root now do siblings");

  CommonASTWithScope nt = result; // now do siblings
   while (t != null) {      // for each sibling of the root
   t = (CommonASTWithScope)t.getNextSibling();
   if (t!= null) {
    nt.setNextSibling(replaceAll((CommonASTWithScope)t, target,
replacement));
    nt = (CommonASTWithScope)nt.getNextSibling();
   }
    }

// System.out.println("result "+ result.toStringList());
   return result;
  }

}
----- Original Message -----
From: "John D. Mitchell" <johnm-antlr at non.net>
To: <antlr-interest at yahoogroups.com>
Sent: Wednesday, January 15, 2003 5:45 AM
Subject: [antlr-interest] Re: Can Resolvers etc be written as ANTLR tree
parsers?


> >>>>> "micheal" == micheal jor <open zone at virgin net>
<open.zone at virgin.net> writes:
>
> [...]
>
> >> The trick is to build the initial AST with sufficient information. :-)
A
> >> canonical example seems to be mapping back to the source with position
> >> information.
>
> > Isn't this a Lexer issue?. Ensuring the info is captured by the lexer in
> > the first place?
>
> There's two primary facets to the problem:
>
> * Gathering the information in the first place
>
> * Propagating the information through the intervening stages
> to where you need it.
>
> [...]
>
> >> * create auxiliary structues that you thread through the tree
>
> > An example would be?
>
> Use-def/def-use chains.  [I.e., chaining together all of the uses of e.g.,
> a variable with the definition of that variable.]
>
>
> >>> If there are examples of resolvers etc written as ANTLR tree parsers,
> >>> I'd appreciate a link/sample.
>
> >> In this context, what do you mean by "resolvers"?
>
> > A processing entity that traverses an AST and links ID nodes to their
> > defining occurences, resolving overloads and expression types. [Might
> > make mutiple traversals.]
>
> Use an auxiliarly stack to track scoping.  Of course, that can become more
> complicated depending on your source language.
>
> For use before definition, you can create a lookaside list per scope where
> you save the references to those uses and resolve them either when you
come
> to the definition or when you leave the scope or when you've completed the
> traversal -- again, it depends a lot on the semantics of your language.
>
> Take care,
> John
>
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list