避免从字符串流中获取任何内容
Avoid grabbing nothing from string stream
我正在为一个非常基本的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;
}
如您所见,Code元组包含枚举分离中表示的条件和包含行中所有单词的向量。我想要的是把一行中的每个单词都推到向量中,然后返回。
该问题出现在函数的第三次调用(汇编代码的第三行)上。我使用ss.good()函数来确定字符串流中是否有单词。出于某种原因,ss.good()函数返回true,即使第三行中没有第四个单词,并且我最终将单词[lea][r0,][ten]和[ten]推入向量中。ss.good()在第四次调用中为true,token不接收任何内容,因此我已经两次将[10]推入向量中。
我注意到,如果我删除分号和最后一个单词之间的空格,就不会出现这个错误。我想知道如何将正确数量的单词推入向量中。
请不要推荐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"
我知道你试图解析的语言很简单。尽管如此,如果你考虑在工作中使用专门的工具,例如flex
,你会帮自己一个忙。
相关文章:
- 如何在C++中仅获取容器内类成员的迭代器
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- C++ 中是否有任何内置阶乘函数?
- 如何在C++中接收任何类型的函数参数并获取函数内传递变量的类型?
- 创建一个构造函数,即Capabe获取任何数量的参数
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 获取图形内存量
- 如何使用 Win32 API 获取任何线程的堆栈大小和堆栈限制
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- 使用CURL从HTTPS连接未获取任何内容
- 如何获取任何类型的默认值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 使用数学获取范围内的数字
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 获取范围内的蓝牙设备
- 获取类内"this"的上下文并分配给类指针 TheClass*
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数