저는 C++의 초보자이며 수표가 예상대로 작동하도록하려고했습니다. 그러나 거의 모든 테스트에서 사실로 돌아 왔습니다. 단지이 경우 true를 반환하는 가정 것 :태그 유효성을 검사하는 C++ 프로그램
<red> Red blank <dim> I'm now dim and red. </dim> </red>
그러나 지금 때이뿐만 아니라 true를 돌려 :이
<red> Blah I'm red.<dim> Im dim now </red> </dim>
나 :
<red> blah <im dim now
그래서 내가 궁금 해요 내 코드에서 간과 할 부분이 있습니다.
bool is_well_formed(ifstream& ifs, string& error_msg) {
string fname,line;
Token tok;
Lexer lexer;
tags.insert("blue");
tags.insert("red");
tags.insert("cyan");
tags.insert("white");
tags.insert("yellow");
tags.insert("magenta");
tags.insert("dim");
tags.insert("underline");
tags.insert("bold");
stack<string> tagstack;
while (getline(ifs, fname)) {
// tries to open the file whose name is in string fname
if (ifs.fail()) {
cerr << "ERROR: Failed to open file " << fname << endl;
ifs.clear();
} else {
while (getline(ifs, line)) {
lexer.set_input(line);
while (lexer.has_more_token()) {
tok = lexer.next_token();
string tmpTok = tok.value;
switch (tok.type) {
case TAG:
// If it has /, remove/from tmpTok
if (tok.value[0] == '/') {
// If it's a closing t
tmpTok = tmpTok.substr(1,tmpTok.length()-1);
}
if(tags.find(tmpTok) == tags.end()) {
// Check whether the encountered tag is valid
error_return("Tag " + tmpTok + " is invalid!");
return false;
} else {
// Valid Tag encountered
tagstack.push(tmpTok);
// Check if the tags are formed properly
if (tmpTok.find('/')) {
// Remove/from tmpTok
string closingTag = tmpTok;
string openingTag = tagstack.top();
tagstack.pop();
if(closingTag.compare(openingTag) != 0) {
error_return(closingTag+"doesn't match" +openingTag);
return false;
} //else
// return true; // if the file is well formed
}/**else{
tagstack.push(tmpTok);
}*/
}// else end
break;
case IDENT:
// cout << "IDENT: " << tok.value << endl;
break;
case ERRTOK:
error_return("Syntax error on this line\n");
return false;
//cout << "Syntax error on this line\n";
break;
case ENDTOK:
break;
}
}
}
}
}
return true; // if the file is well-formed
}
태그 스택이 너무 짧습니다. 하나 이상의 태그를 볼 수 있도록 함수의 맨 위에 선언해야합니다. 다른 문제가있을 수 있지만 프로그램의 완전한 편집 가능한 텍스트가 없으면 너무 분명하지 않은 것을 찾기가 실제로 불가능합니다. –