首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数但小虫

数但小虫
EN

Stack Overflow用户
提问于 2015-10-25 17:05:15
回答 1查看 77关注 0票数 0

我正在执行一个单独的字符串,当看到分隔符时,我确实添加了一个情况,对于最后一个分隔符,它工作得很好。例如,我的字符串是"symbol control_line : std_logic:= '0' ; --example comment",当看到第一个分隔符时,输出是正确的:但是当它查看第二个:=时,它失败了。我不知道为什么会这样?这两个分隔符的代码应该都很好,为什么只知道第一个,而第二个却失败了?

这个prepareNextToken函数计算出第二个令牌的tokenLength是什么。我可以使用这个函数得到当前的令牌。

代码语言:javascript
复制
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;
}

我的输出是

代码语言:javascript
复制
    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结尾?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-25 17:26:22

substr的第二个参数是要提取的字符数,而不是结束位置(参见http://www.cplusplus.com/reference/string/string/substr/)。所以你的提取线应该是:

代码语言:javascript
复制
s=name.substr(offset,tokenLength);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33332448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档