2015-10-25 43 views
0

我正在做單獨的一個字符串,當看到delimilator時,我添加了一個case,它對最後一個delimilator工作正常。例如我的字符串是"symbol control_line : std_logic:= '0' ; --example comment"當看到第一個定界符時,輸出是正確的:但是當它看第二個時:=失敗。我不知道爲什麼會發生?代碼應該罰款的兩個delimilator如何纔算出第一,但失敗了第二?count但小錯誤

這個prepareNextToken函數計算出第二個令牌的tokenLength是什麼。我可以使用這個函數來獲取當前的標記。

void Tokenizer::prepareNextToken() 
{ 
     string real=*str; 
     if(offset==real.size()) 
      complete=true; 
     else 
     { 
      if(ifcomment==false) 
      { 
       size_t length=0; 
       size_t index=offset; 
       size_t smallest=find_first_delimilater(vhdl_char); 
       while(index<real.size()) 
       { 
        length++; 
        if(index==smallest && real[index+1]==' ') 
        { 
         cout<<real[smallest]<<" "; 
         break; 
        } 
        else if(index==smallest && real[index+1]!=' ') 
        { 
         length++; 
         break; 
        } 
        else if(index==real.find(' ',offset)) 
        { 
         break; 
        } 
        else if(index==real.find("--",offset)) 
        { 
         length++; 
         break; 
        } 
        index++; 
       } 
       tokenLength=length; 
      } 
      else if(ifcomment==true) 
       tokenLength=real.size()-offset; 
     } 
     //cout<<tokenLength<<endl; 
} 

我的輸出是

signal   --which is correct 
    control_line  --the current offset 
    :     --which is right because I reach the first case in my  
         --prepareNextToken and ":" is first delimilator 
    std_logic:=  --that is the wrong output because it should be std_logic 
         -- and in a separate line comes out ";=" which is another 
         --delimilator, and is a multiple delimilator no empty case 
         -- so that means I go to the second cases 
    --     -- which is also right which go to fourth case 
    sample comment -- which is right 

我的問題是,爲什麼當「:」出來它自己的路線,但爲什麼「:=」出來它STD_LOGIC結束?

回答

1

substr的第二個參數是要提取的字符數,而不是結束位置(請參見http://www.cplusplus.com/reference/string/string/substr/)。 所以你的提取線應該是:

s=name.substr(offset,tokenLength); 
+0

非常感謝! – Lunayeah

+0

但現在我來談另一個問題。你能看看嗎? :) 提前致謝! – Lunayeah

+0

我不確定這裏的規則,但我很確定你必須開一個新的問題,而不是重複使用以前的問題......你可能會注意到我的答案完全是脫離主題了......你應該回到原來的問題,併發布這個新的......在一個新的線程:-)我會盡快給它看看它。 –