如何使用stringstream標記一個看起來像這樣的行。使用stringstream標記一個具有不同的分隔符的字符串
[標籤]操作碼[ARG1] [,ARG2]
的標籤可能並不總是存在,但如果不是,會有一個空白。操作碼總是存在,操作碼和arg1之間有空格或製表符。然後在arg1和arg2之間沒有空格,但是它被逗號分隔。
此外,一些空白行上將有空白,所以他們需要被丟棄。 「#」是註釋
因此,例如:
#Sample Input
TOP NoP
L 2,1
VAL INT 0
這僅僅是一個文本文件,我會從被閱讀的一個例子。所以在第一行的標籤中會是TOP,而opcode會= NOP,沒有參數被傳遞。
我一直在努力,但我需要一個更簡單的方式來標記和從我所見過的,stringstream似乎是我想要使用的,所以如果任何人都可以告訴我類的如何做這個,我真的很感激它。
我已經費盡瞭如何做到這一點我的大腦,只是告訴你,我不只是要求沒有工作,這是我當前的代碼:
int counter = 0;
int i = 0;
int j = 0;
int p = 0;
while (getline(myFile, line, '\n'))
{
if (line[0] == '#')
{
continue;
}
if (line.length() == 0)
{
continue;
}
if (line.empty())
{
continue;
}
// If the first letter isn't a tab or space then it's a label
if (line[0] != '\t' && line[0] != ' ')
{
string delimeters = "\t ";
int current;
int next = -1;
current = next + 1;
next = line.find_first_of(delimeters, current);
label = line.substr(current, next - current);
Symtablelab[i] = label;
Symtablepos[i] = counter;
if(next>0)
{
current = next + 1;
next = line.find_first_of(delimeters, current);
opcode = line.substr(current, next - current);
if (opcode != "WORDS" && opcode != "INT")
{
counter += 3;
}
if (opcode == "INT")
{
counter++;
}
if (next > 0)
{
delimeters = ", \n\t";
current = next + 1;
next = line.find_first_of(delimeters, current);
arg1 = line.substr(current, next-current);
if (opcode == "WORDS")
{
counter += atoi(arg1.c_str());
}
}
if (next > 0)
{
delimeters ="\n";
current = next +1;
next = line.find_first_of(delimeters,current);
arg2 = line.substr(current, next-current);
}
}
i++;
}
// If the first character is a tab or space then there is no label and we just need to get a counter
if (line[0] == '\t' || line[0] == ' ')
{
string delimeters = "\t \n";
int current;
int next = -1;
current = next + 1;
next = line.find_first_of(delimeters, current);
label = line.substr(current, next - current);
if(next>=0)
{
current = next + 1;
next = line.find_first_of(delimeters, current);
opcode = line.substr(current, next - current);
if (opcode == "\t" || opcode =="\n"|| opcode ==" ")
{
continue;
}
if (opcode != "WORDS" && opcode != "INT")
{
counter += 3;
}
if (opcode == "INT")
{
counter++;
}
if (next > 0)
{
delimeters = ", \n\t";
current = next + 1;
next = line.find_first_of(delimeters, current);
arg1 = line.substr(current, next-current);
if (opcode == "WORDS")
{
counter += atoi(arg1.c_str());
}
}
if (next > 0)
{
delimeters ="\n\t ";
current = next +1;
next = line.find_first_of(delimeters,current);
arg2 = line.substr(current, next-current);
}
}
}
}
myFile.clear();
myFile.seekg(0, ios::beg);
while(getline(myFile, line))
{
if (line.empty())
{
continue;
}
if (line[0] == '#')
{
continue;
}
if (line.length() == 0)
{
continue;
}
// If the first letter isn't a tab or space then it's a label
if (line[0] != '\t' && line[0] != ' ')
{
string delimeters = "\t ";
int current;
int next = -1;
current = next + 1;
next = line.find_first_of(delimeters, current);
label = line.substr(current, next - current);
if(next>0)
{
current = next + 1;
next = line.find_first_of(delimeters, current);
opcode = line.substr(current, next - current);
if (next > 0)
{
delimeters = ", \n\t";
current = next + 1;
next = line.find_first_of(delimeters, current);
arg1 = line.substr(current, next-current);
}
if (next > 0)
{
delimeters ="\n\t ";
current = next +1;
next = line.find_first_of(delimeters,current);
arg2 = line.substr(current, next-current);
}
}
if (opcode == "INT")
{
memory[p] = arg1;
p++;
continue;
}
if (opcode == "HALT" || opcode == "NOP" || opcode == "P_REGS")
{
memory[p] = opcode;
p+=3;
continue;
}
if(opcode == "J" || opcode =="JEQR" || opcode == "JNE" || opcode == "JNER" || opcode == "JLT" || opcode == "JLTR" || opcode == "JGT" || opcode == "JGTR" || opcode == "JLE" || opcode == "JLER" || opcode == "JGE" || opcode == "JGER" || opcode == "JR")
{
memory[p] = opcode;
memory[p+1] = arg1;
p+=3;
continue;
}
if (opcode == "WORDS")
{
int l = atoi(arg1.c_str());
for (int k = 0; k <= l; k++)
{
memory[p+k] = "0";
}
p+=l;
continue;
}
else
{
memory[p] = opcode;
memory[p+1] = arg1;
memory[p+2] = arg2;
p+=3;
}
}
// If the first character is a tab or space then there is no label and we just need to get a counter
if (line[0] == '\t' || line[0] == ' ')
{
string delimeters = "\t ";
int current;
int next = -1;
current = next + 1;
next = line.find_first_of(delimeters, current);
label = line.substr(current, next - current);
if(next>=0)
{
current = next + 1;
next = line.find_first_of(delimeters, current);
opcode = line.substr(current, next - current);
if (opcode == "\t" || opcode =="\n"|| opcode ==" "|| opcode == "")
{
continue;
}
if (next > 0)
{
delimeters = ", \n\t";
current = next + 1;
next = line.find_first_of(delimeters, current);
arg1 = line.substr(current, next-current);
}
if (next > 0)
{
delimeters ="\n\t ";
current = next +1;
next = line.find_first_of(delimeters,current);
arg2 = line.substr(current, next-current);
}
}
if (opcode == "INT")
{
memory[p] = arg1;
p++;
continue;
}
if (opcode == "HALT" || opcode == "NOP" || opcode == "P_REGS")
{
memory[p] = opcode;
p+=3;
continue;
}
if(opcode == "J" || opcode =="JEQR" || opcode == "JNE" || opcode == "JNER" || opcode == "JLT" || opcode == "JLTR" || opcode == "JGT" || opcode == "JGTR" || opcode == "JLE" || opcode == "JLER" || opcode == "JGE" || opcode == "JGER" || opcode == "JR")
{
memory[p] = opcode;
memory[p+1] = arg1;
p+=3;
continue;
}
if (opcode == "WORDS")
{
int l = atoi(arg1.c_str());
for (int k = 0; k <= l; k++)
{
memory[p+k] = "0";
}
p+=l;
continue;
}
else
{
memory[p] = opcode;
memory[p+1] = arg1;
memory[p+2] = arg2;
p+=3;
}
}
}
我顯然希望使這要好得多,所以任何幫助將不勝感激。
如果stringstream真的沒有執行,那麼我會建議你使用這個答案的參考 http://stackoverflow.com/a/53863/1410711 – Recker
鑑於這種複雜的輸入,你幾乎肯定想要開始思考就詞法分析器和可能的解析器而言。一些可能性包括Flex/byacc或Boost Spirit/Qi(儘管肯定會有更多)。 –
我可以使用字符串流來完成此任務嗎? Boost tokenizer是我現在無法使用的東西。 – cadavid4j