开发者

Matching up Curly Brackets

开发者 https://www.devze.com 2022-12-13 14:18 出处:网络
I am having a difficult time matching up curly brackets in this code. It keeps indicating a \"else\" without an \"if\" error but not sure开发者_运维问答 how the syntax should read. If anyone can help

I am having a difficult time matching up curly brackets in this code. It keeps indicating a "else" without an "if" error but not sure开发者_运维问答 how the syntax should read. If anyone can help it would be most appreciated. Here is the code:

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(")");
            }


        }


This else if happens after an else:

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

Edit: fixed and reindented

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(")");
    }
}


I don't want to start a brace placement holy war, but this is why I prefer the "opening brace on the next line" idiom. I can line up the opening and closing brace visually, even if I don't have an IDE to help me.

With that said, an IDE is your best friend here.

I also like the recommendation of immediately adding the opening and closing brace to a block before filling it in.

Eschewing one-line blocks without braces will help. It's not really that much effort to add.


Would suggest if you are having problems matching braces, start a new function definition, just start by adding the control flow and branching (ifs, elses,...), and use braces on every if / else if / else block - even the one liners.

When you get that right, add the rest of the function.
When the braces become a no brainer, then start skipping them for one liners.


This is what the structure looks like:

private void compileFactor() {
  if (...) {
    ...
  }
  if (...) {
    ...
    if (...) ...
    if (...) ...
    else ...
  } else {
    if (...) ...
    else {
      ...
*   } else if (...) {
      ...
    } else if (...) {
      ...
    }
  }

Where I have put the * you have an else following an else. Simply adding another bracket before that point doesn't fix it either, as that would put the else after a different else.

So, there is something fundamentally wrong with your conditions, and you pretty much have to rethink it from the start.

Add brackets in all the if statements, so that you are sure that they are really starting and ending where you expect them to.


Few checkpoints

I think you will need to close brackets before isNumber(theToken)

 }} else if (isNumber(theToken)) {

and may be you dont need bracket after this line?

    IdentInfo theInfo = symTable.lookup(ident);
}

May be the whole code is like this?

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(")");
    }
}


If you are using Visual Studio, you can use CTRL+ "]" keys to match the braces


Try commenting code like this:

function blah(){  //start function
  if(...){        //open if 1
   for(...){   //open for

       //do something    

    }//close for  
  } //close if 1  
}//close function

I do it in Flash, although in some langueages it bogs things down.

Good luck!


The posted code has 6 '}' and 7 '{'.

The last '{' statement of the following excerpt is the one that appears to not have a matching '}'

            else cs.emit(Machine.LOADF, theAddr);
    } else {


I suspect here is your problem.

            if (constValue = null) t.error("undeclared identifier used in expr: "+ident); 


            else { 
                cs.emitLOADI(theNumber); 
            } 

EDIT Should this be?

            if (constValue = null) {
                 t.error("undeclared identifier used in expr: "+ident);  
            } else { 
                cs.emitLOADI(theNumber); 
            } 
0

精彩评论

暂无评论...
验证码 换一张
取 消