[antlr-interest] Problem in BaseAST.equals

richardhensley99 richard.hensley at mckesson.com
Fri Aug 30 17:50:36 PDT 2002


I was just chasing down an NPE problem in my code, and I chased it 
down to the following method of BaseAST:

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

I think this code should be changed to read as follows

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

The problem is that I was trying to compare two nodes of type EOF. 
Apparently an EOF type node has a null text component, and thus this 
code blows with an NPE.

I was writing code to compare two AST trees for testing before I 
discovered equalsTree. What I can't figure out is why equalsTree 
doesn't seem to have an NPE.

Richard


 

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



More information about the antlr-interest mailing list