[antlr-interest] Bug in ASTFactory class

Prashant Deva prashant.deva at gmail.com
Wed Jul 20 15:21:57 PDT 2005


There is a bug in the ASTFactory.addASTChild() method.
It is described below--

 public void addASTChild(ASTPair currentAST, AST child) {
        if (child != null) {
            if (currentAST.root == null) {
                // Make new child the current root
                currentAST.root = child;
            }
            else {
                if (currentAST.child == null) {
                    // Add new child to current root
                    currentAST.root.setFirstChild(child); //<--THIS
LINE CANT BE REACHED
                }
                else {
                    currentAST.child.setNextSibling(child);
                }
            }
            // Make new child the current child
            currentAST.child = child;              //  --|    THESE
LINES SHOULD BE IN THE
            currentAST.advanceChildToEnd(); //  --| ABOVE BRACE
        }
    }


The last 2 lines make the child *always* not null, so the root can
never set the first child.
They should be in the scope of the else statement.

Due to this bug, currently if you have a rule, with no tree symbols
(like ^), it will generate only 1 tree element, which is the root.

The correct code i belive should be- 

 public void addASTChild(ASTPair currentAST, AST child) {
        if (child != null) {
            if (currentAST.root == null) {
                // Make new child the current root
                currentAST.root = child;
            }
            else {
                if (currentAST.child == null) {
                    // Add new child to current root
                    currentAST.root.setFirstChild(child); 
                }
                else {
                    currentAST.child.setNextSibling(child);
                }
            
            // Make new child the current child
            currentAST.child = child;              
            currentAST.advanceChildToEnd(); 
          }
        }
    }

Thus by moving the last 2 lines in the 'else' scope , the
setFirstChild line can be reached.

PRASHANT


More information about the antlr-interest mailing list