[英]Avoid grabbing nothing from string stream
我正在為一個非常基本的 ISA 開發一個匯編程序。 目前我正在實現解析器 function 並且我正在使用字符串 stream 從行中獲取單詞。 下面是一個匯編代碼示例:
; 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;
}
如您所見,代碼元組包含以枚舉 decleration 和包含行中所有單詞的向量表示的條件。 我想要的是將一行中的每個單詞推入向量然后返回。
該問題出現在 function(匯編代碼的第三行)的第三次調用中。 我使用 ss.good() function 來確定字符串 stream 中是否有任何單詞。 出於某種原因,即使第三行中沒有第四個單詞,ss.good() function 也會返回 true,並且我最終將單詞 [lea] [r0,] [ten] 和 [ten] 推入向量中。 ss.good() 在第四次調用時為真,並且令牌什么也沒收到,因此我將 [10] 推入向量兩次。
我注意到如果刪除分號和最后一個單詞之間的空格,則不會發生此錯誤。 我想知道如何將正確數量的單詞推入向量。
請不要推薦 Boost 庫。 我喜歡圖書館,但我想讓這個項目保持簡單。 這沒什么大不了的,這個處理器只有十幾個指令。 另外,請記住,這個 function 只是半生不熟,我正在逐步測試和調試它。
流的錯誤標志僅在條件(例如到達流的末尾)發生后設置。
嘗試將循環條件替換為:
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
,您會幫自己一個忙。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.