匹配Curly Brackets

Alp*_*g14 1 if-statement

我很难在这段代码中匹配大括号.它一直指示"else"而没有"if"错误,但不确定语法应该如何读取.如果有人可以提供帮助,那将非常感激.这是代码:

private void compileFactor() {
        boolean its_a_variable = theInfo.isVar();
        if (isIdent(theToken)) {
            String ident = theToken;
            theToken = t.token();       // handles var and const cases!

            IdentInfo theInfo = symTable.lookup(ident);
        }


            boolean its_a_variable = theInfo.isVar();
            int theAddr = theInfo.getAddr(); 
            boolean isGlobal = theInfo.getIsGlobal();
            int constValue = theInfo.getValue();

            if (its_a_variable) {   // pld12: CHANGE THIS!!
                int theAddr = theInfo.getAddr(); 
                boolean 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 {
                int constValue = theInfo.getValue();
                if (constValue = null) t.error("undeclared identifier used in expr: "+ident);


                else {
                    cs.emitLOADI(theNumber);
                }


            else if (isNumber(theToken)) {
                int theNumber = new Integer(theToken).intValue();
                cs.emitLOADINT(theNumber);
                theToken = t.token();
            }
            else if (equals(theToken, "(")) {  // nothing to do to generate code!
                accept("(");
                compileExpr();
                accept(")");
            }


        }
Run Code Online (Sandbox Code Playgroud)

Tor*_*dek 5

else if发生在else:

        else if (isNumber(theToken)) {
            int theNumber = new Integer(theToken).intValue();
            cs.emitLOADINT(theNumber);
            theToken = t.token();
        }
Run Code Online (Sandbox Code Playgroud)

编辑:修复并重新缩进

private void compileFactor() {
    if (isIdent(theToken)) {
        String ident = theToken;
        theToken = t.token();       // handles var and const cases!

        IdentInfo theInfo = symTable.lookup(ident);

        boolean its_a_variable = theInfo.isVar();
        int theAddr = theInfo.getAddr(); 
        boolean isGlobal = theInfo.getIsGlobal();
        int constValue = theInfo.getValue();

        if (its_a_variable) {   // pld12: CHANGE THIS!!
            int theAddr = theInfo.getAddr(); 
            boolean 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 {
            int constValue = theInfo.getValue();
            if (constValue = null) {
                t.error("undeclared identifier used in expr: "+ident);
            } else {
                cs.emitLOADI(theNumber);
            }
        }
    } else if (isNumber(theToken)) {
        int theNumber = new Integer(theToken).intValue();
        cs.emitLOADINT(theNumber);
        theToken = t.token();
    } else if (equals(theToken, "(")) {  // nothing to do to generate code!
        accept("(");
        compileExpr();
        accept(")");
    }
}
Run Code Online (Sandbox Code Playgroud)