避免从字符串流中抓取任何东西

问题描述

| 我正在为一个非常基本的ISA编写汇编程序。目前,我正在实现解析器功能,并且正在使用字符串流从行中抓取单词。这是汇编代码的示例:
; This program counts from 10 to 0
        .ORIG x3000
        LEA R0,TEN     ; This instruction will be loaded into memory location x3000
        LDW R1,R0,#0
START   ADD R1,R1,#-1
        BRZ DONE
        BR  START
                        ; blank line
DONE    TRAP    x25     ; The last executable instruction
TEN     .FILL   x000A   ; This is 10 in 2\'s comp,hexadecimal
        .END
不用担心汇编代码的性质,只需看一下第3行,即在右边带有注释的那一行。我的解析器功能还不完整,但是这里是我拥有的功能
// Define three conditions to code
enum {DONE,OK,EMPTY_LINE};
// Tuple containing a condition and a string vector
typedef tuple<int,vector<string>> Code;

// Passed an alias to a string
// Parses the line passed to it
Code ReadAndParse(string& line)
{

    /***********************************************/
    /****************REMOVE COMMENTS****************/
    /***********************************************/
    // Sentinel to flag down position of first
    // semicolon and the index position itself
    bool found = false;
    size_t semicolonIndex = -1;

    // Convert the line to lowercase
    for(int i = 0; i < line.length(); i++)
    {
        line[i] = tolower(line[i]);

        // Find first semicolon
        if(line[i] == \';\' && !found)
        {
            semicolonIndex = i;
            // Throw the flag
            found = true;
        }
    }

    // Erase anything to and from semicolon to ignore comments
    if(found != false)
        line.erase(semicolonIndex);


    /***********************************************/
    /*****TEST AND SEE IF THERE\'S ANYTHING LEFT*****/
    /***********************************************/

    // To snatch and store words
    Code code;
    string token;
    stringstream ss(line);
    vector<string> words;

    // While the string stream is still of use
    while(ss.good())
    {
        // Send the next string to the token
        ss >> token;
        // Push it onto the words vector
        words.push_back(token);

        // If all we got was nothing,it\'s an empty line
        if(token == \"\")
        {
            code = make_tuple(EMPTY_LINE,words);
            return code;
        }
    }

    /***********************************************/
    /***********DETERmine OUR TYPE OF CODE**********/
    /***********************************************/


    // At this point it should be fine
    code = make_tuple(OK,words);
    return code;
}
如您所见,代码元组包含以枚举表示的条件和包含行中所有单词的向量。我想要的是将一行中的每个单词推入向量中然后返回。 该问题出现在函数的第三次调用(汇编代码的第三行)上。我使用ss.good()函数确定字符串流中是否有任何单词。由于某些原因,即使第三行中没有第四个单词,ss.good()函数也会返回true,而我最终将单词[lea] [r0,] [ten]和[ten]推入向量中。 ss.good()在第四个调用中为true,令牌没有收到任何东西,因此我[ten]被两次推入向量。 我注意到,如果删除分号和最后一个单词之间的空格,则不会发生此错误。我想知道如何将正确数量的单词推入向量中。 请不要推荐Boost库。我喜欢图书馆,但我想保持这个项目简单。这没什么大不了,该处理器只有十几条指令。另外,请记住,此功能只是半熟,我正在逐步测试和调试它。     

解决方法

        流的错误标志仅在条件(例如到达流的末尾)发生之后设置。 尝试将循环条件替换为:
while(ss >> token)
{
    // Push it onto the words vector
    words.push_back(token);

    // If all we got was nothing,it\'s an empty line
    if(token == \"\")
    {
        code = make_tuple(EMPTY_LINE,words);
        return code;
    }
}
有了这段代码,我得到了第3行的以下标记:
\"LEA\"
\"R0,\"
\"TEN\"
\";\"
\"This\"
\"instruction\"
\"will\"
\"be\"
\"loaded\"
\"into\"
\"memory\"
\"location\"
\"x3000\"
我知道您要解析的语言很简单。但是,如果您考虑使用一种专门的工具来工作,例如ѭ4you,那您就会帮自己一个忙。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...