Read in the tags,
See if the tags balance.
int main() vector<tag> tags = read_tags(std::cin) string emsg = balance_tags(tags) if not emsg.empty() std::cout << emsg
tag
.
read_tags()
and balance_tags()
?
balance_tags(i, tags) if i >= tags.size() return i if not is_open_tag(tags[i]) error "unmatched close tag" j = balance_tags(i + 1, tags) if j > tags.size() error "missing close tag" if tag_match(tags[i], tags[j]) return j + 1 else error "unmatched tags"
is_open_tag()
and tag_match()
.
class tag bool is_open_tag() { ... } bool matches(const tag & t) { ... } tag(bool open, string name) { ... }
1 | y | : | the right answer. |
2 | + | : | almost right. |
3 | - | : | not completely wrong. |
4 | n | : | the wrong answer. |
5 | c | : | crash. |
c | n | - | + | y | |
1 | 0 | 0 | 0 | 0 | 7 |
2 | 0 | 1 | 0 | 3 | 3 |
3 | 0 | 2 | 0 | 0 | 5 |
4 | 1 | 1 | 1 | 1 | 3 |
5 | 1 | 1 | 1 | 1 | 3 |
6 | 0 | 1 | 1 | 1 | 4 |
7 | 0 | 2 | 0 | 1 | 4 |
8 | 0 | 2 | 0 | 2 | 3 |
9 | 1 | 3 | 0 | 1 | 2 |
10 | 0 | 2 | 0 | 0 | 5 |
11 | 0 | 2 | 0 | 0 | 5 |
"Error messages should be a single line of informative text starting with ! (that's bang followed by space)."
!INVALID DOCUMENT -- !Non-alphanumeric. OR ! tag without text couldn't find matching close tag -- !NON ALPHANUMERIC CHARACTER IN TAG NAME. --
if ctag.length() != 0 && !alphaNum(ctag) cout << "!Non-alphanumeric" exit(1) else cout<<"! unterminated tag" exit(1) if segment.size() == 0 cout << "matching close tag missing" exit(1)
*** *** <hello> test *** !!! Error message expected; actual output: --- matching close tag missing ---
static void oops(const std::string & emsg) cout << "! " << emsg << ".\n" exit(1) if ctag.length() != 0 && !alphaNum(ctag) oops("Non-alphanumeric in name") else oops("Unterminated tag") if segment.size() == 0 oops("Matching close tag missing")
oops()
clearly indicates an error state.
"Input comes entirely from std-in."
int main() data_info D(ReadFile("DATA.txt")) if (D.get_Vdata().empty()) return EXIT_SUCCESS // and so forth
/* bool Is_empty(string tag) { tag.erase(0,1) tag.erase(tag.length() - 1, 1) return tag.length() == 0 } */
while i < in.length() && !bracket(in[i]) chars++ i++ // cout << "i = " << i // cout << "in[" << i << "] = " << in[i] if (in[i] == '>') // cout << "in[" << i << "] = " << in[i] // cout << "j = " << j // cout << "chars = " << chars tag = in.substr(j, chars + 2) tag = remove_space(tag) ctag = trim(tag) // cout << "tag " << tag if ctag.length() !=0 && !alphaNum(ctag) // cout << "tag =" << tag
/* save all the stuff read in. */ string read_in int check_error(string str) int error_status = ALL_OK int length clear_tagStack() read_in += str pos = read_in.find(LEFT_ANGLE_BRACKET)
void readNextElement() string tag getline(cin, tag, '>') if !formatElement(tag) error("...") if(tag[0] == '/') if(elements.empty() == true) error("...") // and so forth
*** *** <> </> test *** !!! Error message expected; actual output: --- Accessing index 0 in a string of size 0. ../../test-code: line 10: Abort ./$ex <$ifile ---
assert(not tag.empty()) if(tag[0] == '/') if(elements.empty() == true) error("...") if(tag.at(0) == '/') if(elements.empty() == true) error("...")
txt = trim(txt) if txt.empty() error "tag contains no text" bool close_tag = txt[0] == '/' if close_tag txt[0] = ' ' txt = trim(txt)
This page last modified on 25 March 2004.