stringstream を使用して、このような行をトークン化するにはどうすればよいでしょうか。
[ラベル] オペコード [arg1] [,arg2]
ラベルは常にあるとは限りませんが、ない場合は空白になります。オペコードは常にそこにあり、オペコードと arg1 の間にはスペースまたはタブがあります。次に、arg1 と arg2 の間に空白はありませんが、コンマで区切られています。
また、一部の空白行には空白が含まれるため、破棄する必要があります。「#」はコメントです
たとえば、次のようになります。
#Sample Input
TOP NoP
L 2,1
VAL INT 0
これは、これから読み込むテキスト ファイルのほんの一例です。したがって、行 1 のラベルでは TOP になり、opcode は引数が渡されずに = NOP になります。
私はそれに取り組んできましたが、トークン化するためのより簡単な方法が必要です.本当に感謝しています。
私はこれを行う方法について頭を悩ませてきましたが、作業せずに尋ねるだけではないことを示すために、現在のコードは次のとおりです。
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;
}
}
}
私は明らかにこれをもっと良くしたいと思っているので、どんな助けも大歓迎です.