2009-12-07 147 views
1

我在这段代码中遇到了很难匹配大括号的时间。它始终指示没有“if”错误的“else”,但不确定语法应如何阅读。如果任何人都可以帮助它,将不胜感激。这里是代码:匹配卷曲括号

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


     } 
+3

为什么你不能使用文本编辑器,简单的语法的理解来跟踪所有相应的打开和关闭括号? – artdanil 2009-12-07 02:21:11

+0

请说这是哪种语言。 – 2009-12-07 04:14:24

回答

5

else if一个else后发生的情况:

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

编辑:固定和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(")"); 
    } 
} 
+0

**否则如果(isNumber(theToken))**可能用于** if(isIdent(theToken)){**,否则如果在“else”之后没有意义。 – YOU 2009-12-07 02:45:19

+0

我猜测它没有意义;它在上面的其他部分缺少一个右括号。 – Tordek 2009-12-07 02:55:05

+0

是的,事实上它需要右括号 – YOU 2009-12-07 03:03:56

0

我怀疑这里是你的问题。

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


      else { 
       cs.emitLOADI(theNumber); 
      } 

编辑应该是这样?

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

即使将if部分用作内联语句,也可能在if-else子句之间有多行。大部分空白并不重要。 – 2009-12-07 02:43:48

+0

是的,但基于缩进,它看起来很像,如果是要加入其他。 – 2009-12-08 07:59:49

2

建议,如果您有匹配花括号问题,启动一个新的函数定义,只是首先添加控制流和分支(ifs,elses,...),并在每个if/else if/else块中使用大括号 - 即使是一个内衬。

当你得到这个权利,添加功能的其余部分。
当大括号变得毫不费力时,然后开始跳过一个衬垫。

+4

......或者从不开始跳过大括号。如果代码块周围总是有大括号,有些人会发现代码更清晰。 – Guffa 2009-12-07 02:34:06

+0

是 - 亲自总是离开他们,但不想开始一场神圣的战争:) – seanb 2009-12-07 02:36:55

0

几个检查站

我想你会需要ISNUMBER(theToken)之前关闭括号

}} else if (isNumber(theToken)) { 

,并可你不需要这行后支架?

IdentInfo theInfo = symTable.lookup(ident); 
} 

可能是整个代码是这样的吗?

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

我不想开始支架放置神圣的战争,但这就是为什么我更喜欢“开放大括号下一行”成语。即使我没有IDE来帮助我,我也可以直观地排列开始和结束大括号。

就这样说,IDE是你最好的朋友。

我也喜欢的立即添加开口和填充它之前右括号的块。

成功避开一个行块,而不括号将帮助的建议。添加并不是很费劲。

+0

+! “我不想开始支架放置圣战”哦,你是做... – 2009-12-07 02:41:41

+0

只是说明我的理由,就这样。 – duffymo 2009-12-07 02:45:06

1

这是结构的样子:

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

在哪里我已经把*你有一个else以下的人。在这一点之前简单地添加另一个括号并不能解决它,因为这会在别的之后放入其他的。

所以,你的情况有一些根本性的错误,你几乎不得不从一开始就重新考虑它。

在所有if语句中添加括号,以确保它们确实在您期望的位置开始和结束。

0

如果您使用的是Visual Studio,您可以使用CTRL + “]” 键相匹配的括号

0

尝试评论这样的代码:

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

     //do something  

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

我在一些langueages做在Flash中,虽然它让事情陷入困境。

祝你好运!

0

发布的代码有6'}'和7'{'。

最后的“{”以下摘录的语句,似乎没有匹配的一个“}”

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