再次出现空指针错误

发布于 2024-08-14 01:14:55 字数 4538 浏览 8 评论 0原文

所以我有这个编译器类,它编译一些 .mjava 文件,但其他文件却失败,想知道是否有人可以帮助我找出原因。我有两种方法可以破坏两个不同的文件。我尝试编译的第一个 consts.mjava 文件是:

// demo of true local and global variables
int glob0;
int glob1;
final int two = 2;
final int three = 3;
main() {
    int loc1;
    int loc2;
    int loc3;
    final int four = 4;
    glob0 = three;
    //print("glob0=", glob0, "\n");
    loc1 = glob0*two+1;
    glob1 = glob0*loc1;
    loc2  = glob1+1;
    loc3 = glob1*loc2/four;
    print("glob0=", glob0, " (should be 3)\n");
    print("glob1=", glob1, " (should be 21)\n");
    print("loc1=",  loc1,  " (should be 7)\n");
    print("loc2=",  loc2,  " (should be 22)\n");
    print("loc3=",  loc3,  " (should be 115)\n");
}

当我尝试用我的编译器类编译它时,它会在这里中断:

private void compileFactor() {

    if (isIdent(theToken)) {
        String ident = theToken;
        theToken = t.token();            
        IdentInfo theInfo = symTable.lookup(ident);


        boolean its_a_variable = theInfo.isVar();  ***//Breaks Here for consts.mjava Null Exception***
        int theAddr; 
        boolean isGlobal = theInfo.getIsGlobal();
        int constValue;
        int theNumber = 0;

        if (its_a_variable) {   // pld12: CHANGE THIS!!
                 theAddr = theInfo.getAddr(); 
                 isGlobal = theInfo.getIsGlobal();
                 if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
                 if (isGlobal) cs.emit(Machine.LOAD, theAddr);
                 else cs.emit(Machine.LOADF, theAddr);
        } else {
                 constValue = theInfo.getValue();
                 if (constValue == 0) 
                        t.error("undeclared identifier used in expr: "+ident);
                 else {
                          cs.emitLOADINT(theNumber);
                 }
                }
            } else if (isNumber(theToken)) {
                int theNumber = new Integer(theToken).intValue();
                cs.emitLOADINT(theNumber);
                theToken = t.token();
            } else if (equals(theToken, "(")) {  
                accept("(");
                compileExpr();
                accept(")");
            }
        }

下一个 locs.mjava 文件我尝试在此方法上运行中断:

private void compileIdentStmt() {
        String ident = theToken;
        boolean isGlobal = true;
        int location = 0;
        int entryPoint = 0;
        IdentInfo varInfo = null;
        if (!isIdent(ident)) t.error("expected identifier, got " + theToken);
        theToken = t.token();
        if (equals(theToken, "=")) {    
            accept("=");

            varInfo = symTable.lookup(ident);
            if (varInfo.isVar() == true) {      ***//Breaks Here on locs.mjava: Null Exception***
                location = varInfo.getAddr();        
                isGlobal = varInfo.getIsGlobal();    
            }

            /*
            if (varInfo==null) {
                location = GHack(ident);
                isGlobal = true;
            }
            if (location == -1) {
                location = LHack(ident);
                isGlobal = false;
            }
            /* */
            compileExpr();
            if (isGlobal) cs.emit(Machine.STOR, location);
            else cs.emit(Machine.STORF, location);      
            accept(";");
        } else if (equals(theToken, "(")) {     
                         varInfo = symTable.lookup(ident);

             if (varInfo.isProc() == true) {  
                entryPoint = varInfo.getEntryPoint();
                dprint("call to function " + ident + "; generating JSR to location " + entryPoint);
                accept("(");
            }
            /* 
            if (!equals(theToken, ")")) {   
                compileExpr();
                while (equals(theToken, ",")) {
                    accept(",");
                    compileExpr();
                }
            }
            /* */
            accept(")");
            accept(";");
            cs.emit(Machine.JSR, entryPoint);
        } else t.error("expected \"=\" or \"(\", got " + theToken);
    }

我什至会从我的方法中提供我的查找方法symTable() 来帮助:

public IdentInfo lookup(String ident) {
        IdentInfo ii;
        if (HMLocal != null) {
            ii = HMLocal.get(ident);
            if (ii != null) {
                return ii;
            }
            ii = HMGlobal.get(ident);
            if (ii != null) {
                return ii;
            }
        }
        return null;
    }

So I have this compiler class that compiles some .mjava files but others it fails on and wondering if anyone can help me figure out why. I have two methods that break for two different files. The first consts.mjava file I try to compile is:

// demo of true local and global variables
int glob0;
int glob1;
final int two = 2;
final int three = 3;
main() {
    int loc1;
    int loc2;
    int loc3;
    final int four = 4;
    glob0 = three;
    //print("glob0=", glob0, "\n");
    loc1 = glob0*two+1;
    glob1 = glob0*loc1;
    loc2  = glob1+1;
    loc3 = glob1*loc2/four;
    print("glob0=", glob0, " (should be 3)\n");
    print("glob1=", glob1, " (should be 21)\n");
    print("loc1=",  loc1,  " (should be 7)\n");
    print("loc2=",  loc2,  " (should be 22)\n");
    print("loc3=",  loc3,  " (should be 115)\n");
}

When I try to compile this with my compiler class it breaks here:

private void compileFactor() {

    if (isIdent(theToken)) {
        String ident = theToken;
        theToken = t.token();            
        IdentInfo theInfo = symTable.lookup(ident);


        boolean its_a_variable = theInfo.isVar();  ***//Breaks Here for consts.mjava Null Exception***
        int theAddr; 
        boolean isGlobal = theInfo.getIsGlobal();
        int constValue;
        int theNumber = 0;

        if (its_a_variable) {   // pld12: CHANGE THIS!!
                 theAddr = theInfo.getAddr(); 
                 isGlobal = theInfo.getIsGlobal();
                 if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
                 if (isGlobal) cs.emit(Machine.LOAD, theAddr);
                 else cs.emit(Machine.LOADF, theAddr);
        } else {
                 constValue = theInfo.getValue();
                 if (constValue == 0) 
                        t.error("undeclared identifier used in expr: "+ident);
                 else {
                          cs.emitLOADINT(theNumber);
                 }
                }
            } else if (isNumber(theToken)) {
                int theNumber = new Integer(theToken).intValue();
                cs.emitLOADINT(theNumber);
                theToken = t.token();
            } else if (equals(theToken, "(")) {  
                accept("(");
                compileExpr();
                accept(")");
            }
        }

The next locs.mjava file I try to run breaks on this method:

private void compileIdentStmt() {
        String ident = theToken;
        boolean isGlobal = true;
        int location = 0;
        int entryPoint = 0;
        IdentInfo varInfo = null;
        if (!isIdent(ident)) t.error("expected identifier, got " + theToken);
        theToken = t.token();
        if (equals(theToken, "=")) {    
            accept("=");

            varInfo = symTable.lookup(ident);
            if (varInfo.isVar() == true) {      ***//Breaks Here on locs.mjava: Null Exception***
                location = varInfo.getAddr();        
                isGlobal = varInfo.getIsGlobal();    
            }

            /*
            if (varInfo==null) {
                location = GHack(ident);
                isGlobal = true;
            }
            if (location == -1) {
                location = LHack(ident);
                isGlobal = false;
            }
            /* */
            compileExpr();
            if (isGlobal) cs.emit(Machine.STOR, location);
            else cs.emit(Machine.STORF, location);      
            accept(";");
        } else if (equals(theToken, "(")) {     
                         varInfo = symTable.lookup(ident);

             if (varInfo.isProc() == true) {  
                entryPoint = varInfo.getEntryPoint();
                dprint("call to function " + ident + "; generating JSR to location " + entryPoint);
                accept("(");
            }
            /* 
            if (!equals(theToken, ")")) {   
                compileExpr();
                while (equals(theToken, ",")) {
                    accept(",");
                    compileExpr();
                }
            }
            /* */
            accept(")");
            accept(";");
            cs.emit(Machine.JSR, entryPoint);
        } else t.error("expected \"=\" or \"(\", got " + theToken);
    }

I will even supply my lookup method from my symTable() to help:

public IdentInfo lookup(String ident) {
        IdentInfo ii;
        if (HMLocal != null) {
            ii = HMLocal.get(ident);
            if (ii != null) {
                return ii;
            }
            ii = HMGlobal.get(ident);
            if (ii != null) {
                return ii;
            }
        }
        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2024-08-21 01:14:55

如果您收到 NullPointerExceptions,那么这是因为示例中的 theInfovarInfo 为 null。

If you're getting NullPointerExceptions then it's because theInfo and varInfo are null in your examples.

以酷 2024-08-21 01:14:55

IdentInfo theInfo = symTable.lookup(ident);

在尝试使用它之前,您应该检查 theInfo 是否为 null,因为您的查找方法明确指出它可以返回 null

After

IdentInfo theInfo = symTable.lookup(ident);

you should check if theInfo is null before trying to work with it, since your lookup method clearly states it can return null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文