XML Validator
Time limit1sMemory limit128 MB
Decide for each input line whether it is valid XML: matching open and close tags, allowed plain text, and proper escape sequences.
- Level
Medium6 of 10
- Topics
- String, Stack, Implementation
- Solved
- No attempts yet
Problem
Professor Dasom Lee, who teaches Internet Programming, believes that XML is the revolutionary language that will change the world and constantly promotes its benefits to students. But because misusing it can cause serious trouble, a parser is needed to decide whether a document's syntax is correct. Since Professor Lee cannot write XML, you must implement the validator instead.
A document is judged valid by the following rules. A document is read as a sequence of the elements below; if at any position a part does not match one of these rules, the whole document is invalid.
- Plain text: characters whose ASCII code is between 32 and 127 inclusive. The three characters
<,>, and&may not appear as plain text. - Escape strings: the following strings encode
<,>, and&respectively and are valid.<→<>→>&→&
- Hex escape: a string of the form
&xHEX;, whereHEXis a hexadecimal string with a positive, even number of digits, made up of0~9or the lettersA~F(both uppercase and lowercase allowed). - Opening tag
<tag>:tagmust be a non-empty string of lowercase letters or digits. This tag name is pushed onto the context stack. - Self-closing tag
<tag/>: this tag is not pushed onto the context stack. - Closing tag
</tag>: pops the top value of the context stack. The tag name on top of the stack must match this tag's name.
After the entire document is parsed, the context stack must be empty. An empty string is also judged valid.
Input
Several lines are given as input. For each line, decide whether it is valid XML syntax. Each line consists only of characters with ASCII code between 32 and 127, and empty lines may appear. Input ends at end of file (EOF).
Output
For each line, print valid if the line is valid XML syntax, and invalid otherwise.