[antlr-interest] Complaints about BaseAST implementation

Andy Tripp atripp at jazillian.com
Tue Oct 11 08:14:15 PDT 2005


 

>Hi Terence
>
>Please please tell me you didn't code BaseAST class.
>
>
>Ok, now my grudge against that class. After perfectly
>fine declarations in AST interface of tree walking
>methods, this class goes and implements what seems to
>be common 'algorithms'. But instead of using available
>interface, it defines fields ('down' and 'right') and
>uses them liberally throughout the code.
>
>And, now, here I am. I tried making a node class that
>dissociates the logic of keeping track of
>child/sibling /nodes (in short, I implemented a
>mechanism for demand paging the tree or fragments
>thereof), which, (no prizes for guessing), failed
>miserably because in this implementation BaseAST.right
>and BaseAST.down are always null (the magic of
>figuring out and fetching child/sibling is elsewhere).
>
>Now I am crying in pain after debugging for two days
>why the parse tree after my magic is missing about 10%
>of nodes. Tell me what I should do to make it right.
>Please don't tell me to fork the ANTLR code base.
>
>The only option I have right now is to drag myself to
>the bar next door, get drunk and forget for the
>meanwhile that this issue exists :=(
>
>*sob* *sob*
>
>- Akhilesh
>
 
Looks to me that the only places where the "right" and "down" fields are 
misused are in
the addChild() and getNumberOfChildren() methods of BaseAST.
Here is what they should look like:

 /**Add a node to the end of the child list for this node */
    public void addChild(AST node) {
        if (node == null) return;
        BaseAST t = getFirstChild();
        if (t != null) {
            while (t.right != null) {
                t = t.getNextSibling();
            }
            t.right = (BaseAST)node;
        }
        else {
            this.down = (BaseAST)node;
        }
    }

    /** How many children does this node have? */
    public int getNumberOfChildren() {
        BaseAST t = getFirstChild();
        int n = 0;
        if (t != null) {
            n = 1;
            while (t.getNextSibling() != null) {
                t = t.getNextSibling();
                n++;
            }
            return n;
        }
        return n;
    }



More information about the antlr-interest mailing list