2015-06-17 59 views
0

我正在處理一個問題,我有一個字符串列表,這只是一個Java代碼。所以如果代碼在方法體之前或之後有任何聲明,那麼這個腳本應該給我一個錯誤。如果變量聲明在方法體內,那麼它應該可以正常工作。
爲前:如何檢查聲明?

int abcs; // This should generate error. 
double cad; //This should generate error. 

Void main(){ 
    int test; //This should not generate error. 

    void test1(){ 
    int test3; //This should not generate error. 
    } 
} 

我已經寫了一個小程序,這一點,但能不能夠得到想要的結果,我的代碼是:

private void check(String line) 

{詮釋計數= 0; int flag = 0;

// is there an open brace on the current line 
    int bracketpos = line.indexOf('{'); 
    // is there a close brace on the current line 
    int closebracketpos = line.indexOf('}'); 
    // is there a semicolon on the current line 
    int charpos = line.indexOf(';'); 

    // increment count if there is an open brace 
    if(bracketpos>=0) 
     ++count; 

    // decrement count if there is a close brace 
    if(closebracketpos>=0) 
     --count; 

    // if there is an open brace on the current line 
    // ...and there's a close brace on the current line 
    // ...and there's a semicolon on the current line 
    // ...and the number of brackets are balanced (count==0) 
    if(bracketpos>=0 && closebracketpos>=0 && charpos>=0 && count==0){ 
     // if the first semicolon appears after the first close brace 
     if(charpos>closebracketpos){ 
      flag =2; 
     } 
     else 
      // the close brace appears after the semicolon, or neither exist 
      flag =0; 
    } 

    else if (charpos >= 0 && count ==0) { 

     flag= 1; 
    } 
    else { 
    flag = 0; 
     // the line does not have a ;,{,} 
    } 

if(flag!=0) 
{ 
     //ERROR 
} 
} 

我正在逐行傳遞我的例子,所以count記錄着「{」和「}」。

問題: 我正在將我的示例逐行添加到check()中。我想查看示例中是否有任何聲明。但是,如果方法體內有任何聲明,那麼它很好,但是如果方法體外面有聲明,那麼我的check()會提示/或給我一個錯誤。

+2

昨天我在一個*顯着*類似的問題的評論中添加了:找到一個Java解析器來使用。不要開始編寫你自己的原始解析器,它將非常脆弱。 –

+0

如果你的家庭作業確實是在這裏構建一種編譯器,那麼至少你必須跟蹤應用程序的結構。你可以把它看作一棵樹:它的根節點是方法聲明,然後在它的子元素中它可以有幾個語句,但不包含另一個方法的定義(只是爲了檢查這個特定的情況)。 –

+0

@LuiggiMendoza這不是一項家庭作業,我正在創建一個腳本,我將應用於檢查所有這些文件中的聲明的一千個文件的目錄。 –

回答

0

我嘗試終於得到它的工作,唯一的問題是計數初始化爲0,每次檢查()被調用。

所以我想出了兩個選擇,要麼計數靜態全局int。 或者用check()傳遞計數值,並且應該返回更新的計數以便保留「{」和「}」的記錄。

1

你的邏輯錯誤。您應該添加註釋,說明代碼在每個階段執行的操作。這裏有讓你開始...

private void check(String line) 
{  int count = 0; 
     int flag = 0; 

     // is there an open brace on the current line 
     int bracketpos = line.indexOf('{'); 
     // is there a close brace on the current line 
     int closebracketpos = line.indexOf('}'); 
     // is there a semicolon on the current line 
     int charpos = line.indexOf(';'); 

     // increment count if there is an open brace 
     if(bracketpos>=0) 
      ++count; 

     // decrement count if there is a close brace 
     if(closebracketpos>=0) 
      --count; 

     // if there is an open brace on the current line 
     // ...and there's a close brace on the current line 
     // ...and there's a semicolon on the current line 
     // ...and the number of brackets are balanced (count==0) 
     if(bracketpos>=0 && closebracketpos>=0 && charpos>=0 && count==0){ 
      // if the first semicolon appears after the first close brace 
      if(charpos>closebracketpos){ 
       flag =2; 
      } 
      else 
       // the close brace appears after the semicolon, or neither exist 
       flag =0; 
     } 

     else if (charpos >= 0 && count ==0) { 

      flag= 1; 
     } 
     else { 
     flag = 0; 
      // the line does not have a ;,{,} 
     } 

    if(flag!=0) 
{ 
     //ERROR 
    } 
} 
+0

感謝您的更改,但是您能否解釋邏輯錯誤的原因。我可以保留「{和}」的記錄。 同樣在第三個IF語句中,我可以檢查像這些「{int a;} double test」這樣的語句。 - 所以這會產生錯誤。 –

+0

我不認爲你瞭解Java如何工作。每次調用函數時都使用局部變量。此外,沒有一行在一行上有左括號,右括號和分號,因此if語句毫無意義。 – adelphus

+0

就局部變量而言,如果使用靜態int計數,怎麼樣?我想這應該工作。 帶開放大括號,大括號和分號的行只是處理最壞的情況。因爲我不確定文件內容,所以這個文件是最糟糕的情況。 –