2009-12-07 145 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 {